UNPKG

@ethersphere/bee-js

Version:
74 lines 2.21 kB
import { Types } from 'cafe-utility'; import { prepareRequestHeaders } from "../utils/headers.js"; import { http } from "../utils/http.js"; import { PublicKey, Reference } from "../utils/typed-bytes.js"; const granteeEndpoint = 'grantee'; export async function getGrantees(reference, requestOptions) { const response = await http(requestOptions, { method: 'get', url: `${granteeEndpoint}/${reference}`, responseType: 'json' }); const body = Types.asArray(response.data, { name: 'response.data' }).map(x => new PublicKey(Types.asString(x, { name: 'grantee' }))); return { status: response.status, statusText: response.statusText, grantees: body }; } export async function createGrantees(requestOptions, postageBatchId, grantees) { const response = await http(requestOptions, { method: 'post', url: granteeEndpoint, data: { grantees: grantees.map(x => x.toCompressedHex()) }, headers: prepareRequestHeaders(postageBatchId), responseType: 'json' }); const body = Types.asObject(response.data, { name: 'response.data' }); return { status: response.status, statusText: response.statusText, ref: new Reference(Types.asString(body.ref, { name: 'ref' })), historyref: new Reference(Types.asString(body.historyref, { name: 'historyref' })) }; } export async function patchGrantees(postageBatchId, reference, historyRef, grantees, requestOptions) { const response = await http(requestOptions, { method: 'patch', url: `${granteeEndpoint}/${reference}`, data: { add: grantees.add?.map(x => x.toCompressedHex()), revoke: grantees.revoke?.map(x => x.toCompressedHex()) }, headers: { ...prepareRequestHeaders(postageBatchId), 'swarm-act-history-address': historyRef.toHex() }, responseType: 'json' }); const body = Types.asObject(response.data, { name: 'response.data' }); return { status: response.status, statusText: response.statusText, ref: new Reference(Types.asString(body.ref, { name: 'ref' })), historyref: new Reference(Types.asString(body.historyref, { name: 'historyref' })) }; }