@ethersphere/bee-js
Version:
Javascript client for Bee
75 lines (74 loc) • 2.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAllPins = exports.getPin = exports.unpin = exports.pin = void 0;
const cafe_utility_1 = require("cafe-utility");
const http_1 = require("../utils/http");
const typed_bytes_1 = require("../utils/typed-bytes");
const PINNING_ENDPOINT = 'pins';
/**
* Pin data with given reference
*
* @param requestOptions Options for making requests
* @param reference Bee data reference
*/
async function pin(requestOptions, reference) {
await (0, http_1.http)(requestOptions, {
method: 'post',
responseType: 'json',
url: `${PINNING_ENDPOINT}/${reference}`,
});
}
exports.pin = pin;
/**
* Unpin data with given reference
*
* @param requestOptions Options for making requests
* @param reference Bee data reference
*/
async function unpin(requestOptions, reference) {
await (0, http_1.http)(requestOptions, {
method: 'delete',
responseType: 'json',
url: `${PINNING_ENDPOINT}/${reference}`,
});
}
exports.unpin = unpin;
/**
* Get pin status for specific address.
*
* @param requestOptions Options for making requests
* @param reference
* @throws Error if given address is not pinned
*/
async function getPin(requestOptions, reference) {
const response = await (0, http_1.http)(requestOptions, {
method: 'get',
responseType: 'json',
url: `${PINNING_ENDPOINT}/${reference}`,
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return {
reference: new typed_bytes_1.Reference(cafe_utility_1.Types.asString(body.reference, { name: 'reference' })),
};
}
exports.getPin = getPin;
/**
* Get list of all pins
*
* @param requestOptions Options for making requests
*/
async function getAllPins(requestOptions) {
const response = await (0, http_1.http)(requestOptions, {
method: 'get',
responseType: 'json',
url: `${PINNING_ENDPOINT}`,
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
// TODO: https://github.com/ethersphere/bee/issues/4964
if (body.references === null) {
return [];
}
const references = cafe_utility_1.Types.asArray(body.references, { name: 'references' }).map(x => cafe_utility_1.Types.asString(x, { name: 'reference' }));
return references.map(x => new typed_bytes_1.Reference(x));
}
exports.getAllPins = getAllPins;