@ethersphere/bee-js
Version:
Javascript client for Bee
76 lines • 2.69 kB
JavaScript
import { Types } from 'cafe-utility';
import { Bytes } from "../utils/bytes.js";
import { BeeError } from "../utils/error.js";
import { prepareRequestHeaders } from "../utils/headers.js";
import { http } from "../utils/http.js";
import { FeedIndex, Reference } from "../utils/typed-bytes.js";
const feedEndpoint = 'feeds';
/**
* Create an initial feed root manifest
*
* @param requestOptions Options for making requests
* @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')
*/
export async function createFeedManifest(requestOptions, owner, topic, stamp, options) {
const response = await http(requestOptions, {
method: 'post',
responseType: 'json',
url: `${feedEndpoint}/${owner}/${topic}`,
headers: prepareRequestHeaders(stamp, options)
});
const body = Types.asObject(response.data, {
name: 'response.data'
});
return new Reference(Types.asHexString(body.reference));
}
function readFeedUpdateHeaders(headers) {
const feedIndex = headers['swarm-feed-index'];
const feedIndexNext = headers['swarm-feed-index-next'];
if (!feedIndex) {
throw new BeeError('Response did not contain expected swarm-feed-index!');
}
if (!feedIndexNext) {
throw new BeeError('Response did not contain expected swarm-feed-index-next!');
}
return {
feedIndex: new FeedIndex(feedIndex),
feedIndexNext: new 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 requestOptions Options for making requests
* @param owner Owner's ethereum address
* @param topic Topic
* @param options Additional options, like index, at, type
*/
export async function fetchLatestFeedUpdate(requestOptions, owner, topic, options) {
const response = await http(requestOptions, {
responseType: 'arraybuffer',
url: `${feedEndpoint}/${owner}/${topic}`,
params: options
});
return {
payload: new Bytes(response.data),
...readFeedUpdateHeaders(response.headers)
};
}
export async function probeFeed(requestOptions, owner, topic) {
const response = await http(requestOptions, {
responseType: 'arraybuffer',
url: `${feedEndpoint}/${owner}/${topic}`,
params: {
'Swarm-Only-Root-Chunk': true
}
});
return readFeedUpdateHeaders(response.headers);
}