77 lines
3.2 KiB
JavaScript
77 lines
3.2 KiB
JavaScript
"use strict";
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.fetchLatestFeedUpdate = exports.createFeedManifest = void 0;
|
|
const http_1 = require("../utils/http");
|
|
const headers_1 = require("../utils/headers");
|
|
const error_1 = require("../utils/error");
|
|
const feedEndpoint = 'feeds';
|
|
/**
|
|
* Create an initial feed root manifest
|
|
*
|
|
* @param ky Ky instance
|
|
* @param owner Owner's ethereum address in hex
|
|
* @param topic Topic in hex
|
|
* @param postageBatchId Postage BatchId to be used to create the Feed Manifest
|
|
* @param options Additional options, like type (default: 'sequence')
|
|
*/
|
|
function createFeedManifest(ky, owner, topic, postageBatchId, options) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
const response = yield (0, http_1.http)(ky, {
|
|
method: 'post',
|
|
responseType: 'json',
|
|
path: `${feedEndpoint}/${owner}/${topic}`,
|
|
searchParams: (0, http_1.filterHeaders)(options),
|
|
headers: (0, headers_1.extractUploadHeaders)(postageBatchId),
|
|
});
|
|
return response.data.reference;
|
|
});
|
|
}
|
|
exports.createFeedManifest = createFeedManifest;
|
|
function readFeedUpdateHeaders(headers) {
|
|
const feedIndex = headers.get('swarm-feed-index');
|
|
const feedIndexNext = headers.get('swarm-feed-index-next');
|
|
if (!feedIndex) {
|
|
throw new error_1.BeeError('Response did not contain expected swarm-feed-index!');
|
|
}
|
|
if (!feedIndexNext) {
|
|
throw new error_1.BeeError('Response did not contain expected swarm-feed-index-next!');
|
|
}
|
|
return {
|
|
feedIndex,
|
|
feedIndexNext,
|
|
};
|
|
}
|
|
/**
|
|
* Find and retrieve feed update
|
|
*
|
|
* The feed consists of updates. This endpoint looks up an
|
|
* update that matches the provided parameters and returns
|
|
* the reference it contains along with its index and the
|
|
* index of the subsequent update.
|
|
*
|
|
* @param ky Ky instance
|
|
* @param owner Owner's ethereum address in hex
|
|
* @param topic Topic in hex
|
|
* @param options Additional options, like index, at, type
|
|
*/
|
|
function fetchLatestFeedUpdate(ky, owner, topic, options) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
const response = yield (0, http_1.http)(ky, {
|
|
responseType: 'json',
|
|
path: `${feedEndpoint}/${owner}/${topic}`,
|
|
searchParams: (0, http_1.filterHeaders)(options),
|
|
});
|
|
return Object.assign(Object.assign({}, response.data), readFeedUpdateHeaders(response.headers));
|
|
});
|
|
}
|
|
exports.fetchLatestFeedUpdate = fetchLatestFeedUpdate;
|