All files / src/modules grantee.ts

100% Statements 21/21
100% Branches 0/0
100% Functions 7/7
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 8033x   33x 33x 33x   33x   33x 2x           2x 4x     2x             33x         1x     3x         1x   1x               33x             1x       1x 2x                 1x   1x              
import { Types } from 'cafe-utility'
import { BeeRequestOptions, GetGranteesResult, GranteesResult } from '../types'
import { prepareRequestHeaders } from '../utils/headers'
import { http } from '../utils/http'
import { BatchId, PublicKey, Reference } from '../utils/typed-bytes'
 
const granteeEndpoint = 'grantee'
 
export async function getGrantees(reference: Reference, requestOptions: BeeRequestOptions): Promise<GetGranteesResult> {
  const response = await http<unknown>(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: BeeRequestOptions,
  postageBatchId: BatchId,
  grantees: PublicKey[],
): Promise<GranteesResult> {
  const response = await http<unknown>(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: BatchId,
  reference: Reference,
  historyRef: Reference,
  grantees: { add?: PublicKey[]; revoke?: PublicKey[] },
  requestOptions: BeeRequestOptions,
): Promise<GranteesResult> {
  const response = await http<unknown>(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' })),
  }
}