@ethersphere/bee-js
Version:
Javascript client for Bee
63 lines (62 loc) • 2.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.patchGrantees = exports.createGrantees = exports.getGrantees = void 0;
const cafe_utility_1 = require("cafe-utility");
const headers_1 = require("../utils/headers");
const http_1 = require("../utils/http");
const typed_bytes_1 = require("../utils/typed-bytes");
const granteeEndpoint = 'grantee';
async function getGrantees(reference, requestOptions) {
const response = await (0, http_1.http)(requestOptions, {
method: 'get',
url: `${granteeEndpoint}/${reference}`,
responseType: 'json',
});
const body = cafe_utility_1.Types.asArray(response.data, { name: 'response.data' }).map(x => new typed_bytes_1.PublicKey(cafe_utility_1.Types.asString(x, { name: 'grantee' })));
return {
status: response.status,
statusText: response.statusText,
grantees: body,
};
}
exports.getGrantees = getGrantees;
async function createGrantees(requestOptions, postageBatchId, grantees) {
const response = await (0, http_1.http)(requestOptions, {
method: 'post',
url: granteeEndpoint,
data: { grantees: grantees.map(x => x.toCompressedHex()) },
headers: (0, headers_1.prepareRequestHeaders)(postageBatchId),
responseType: 'json',
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return {
status: response.status,
statusText: response.statusText,
ref: new typed_bytes_1.Reference(cafe_utility_1.Types.asString(body.ref, { name: 'ref' })),
historyref: new typed_bytes_1.Reference(cafe_utility_1.Types.asString(body.historyref, { name: 'historyref' })),
};
}
exports.createGrantees = createGrantees;
async function patchGrantees(postageBatchId, reference, historyRef, grantees, requestOptions) {
const response = await (0, http_1.http)(requestOptions, {
method: 'patch',
url: `${granteeEndpoint}/${reference}`,
data: {
add: grantees.add?.map(x => x.toCompressedHex()),
revoke: grantees.revoke?.map(x => x.toCompressedHex()),
},
headers: {
...(0, headers_1.prepareRequestHeaders)(postageBatchId),
'swarm-act-history-address': historyRef.toHex(),
},
responseType: 'json',
});
const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
return {
status: response.status,
statusText: response.statusText,
ref: new typed_bytes_1.Reference(cafe_utility_1.Types.asString(body.ref, { name: 'ref' })),
historyref: new typed_bytes_1.Reference(cafe_utility_1.Types.asString(body.historyref, { name: 'historyref' })),
};
}
exports.patchGrantees = patchGrantees;