All files / src/modules pinning.ts

95.23% Statements 20/21
0% Branches 0/1
100% Functions 6/6
95% Lines 19/20

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 80 81 8233x   33x 33x   33x               33x 1x                         33x 2x                           33x 1x           1x   1x                   33x 4x           4x     4x       4x 110x     110x    
import { Types } from 'cafe-utility'
import type { BeeRequestOptions, Pin } from '../types'
import { http } from '../utils/http'
import { Reference } from '../utils/typed-bytes'
 
const PINNING_ENDPOINT = 'pins'
 
/**
 * Pin data with given reference
 *
 * @param requestOptions Options for making requests
 * @param reference Bee data reference
 */
export async function pin(requestOptions: BeeRequestOptions, reference: Reference): Promise<void> {
  await http<unknown>(requestOptions, {
    method: 'post',
    responseType: 'json',
    url: `${PINNING_ENDPOINT}/${reference}`,
  })
}
 
/**
 * Unpin data with given reference
 *
 * @param requestOptions Options for making requests
 * @param reference Bee data reference
 */
export async function unpin(requestOptions: BeeRequestOptions, reference: Reference): Promise<void> {
  await http<unknown>(requestOptions, {
    method: 'delete',
    responseType: 'json',
    url: `${PINNING_ENDPOINT}/${reference}`,
  })
}
 
/**
 * Get pin status for specific address.
 *
 * @param requestOptions Options for making requests
 * @param reference
 * @throws Error if given address is not pinned
 */
export async function getPin(requestOptions: BeeRequestOptions, reference: Reference): Promise<Pin> {
  const response = await http<unknown>(requestOptions, {
    method: 'get',
    responseType: 'json',
    url: `${PINNING_ENDPOINT}/${reference}`,
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
 
  return {
    reference: new Reference(Types.asString(body.reference, { name: 'reference' })),
  }
}
 
/**
 * Get list of all pins
 *
 * @param requestOptions Options for making requests
 */
export async function getAllPins(requestOptions: BeeRequestOptions): Promise<Reference[]> {
  const response = await http<unknown>(requestOptions, {
    method: 'get',
    responseType: 'json',
    url: `${PINNING_ENDPOINT}`,
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
 
  // TODO: https://github.com/ethersphere/bee/issues/4964
  Iif (body.references === null) {
    return []
  }
 
  const references = Types.asArray(body.references, { name: 'references' }).map(x =>
    Types.asString(x, { name: 'reference' }),
  )
 
  return references.map(x => new Reference(x))
}