All files utils.ts

94.44% Statements 34/36
75% Branches 6/8
100% Functions 2/2
94.44% Lines 34/36

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 82 83 84 85 86 87 88 89 90 91 92 93 941x                     1x                                       1x 3x 3x 3x   1x 1x 1x     1x 1x 1x 1x     1x 1x 1x 1x           3x                   1x 15x 15x 15x   11x 11x 11x     2x 2x 2x 2x     2x 2x 2x 2x           15x    
import {Action, AnyAction, FetchProviderOptions, Transaction} from '@wharfkit/antelope'
import type {Fetch} from '@wharfkit/common'
import {SigningRequest} from '@wharfkit/signing-request'
 
/**
 * Return an instance of fetch.
 *
 * @param options FetchProviderOptions
 * @returns Fetch
 */
/* istanbul ignore next */
export function getFetch(options?: FetchProviderOptions): Fetch {
    if (options && options.fetch) {
        return options.fetch
    }
    if (typeof window !== 'undefined' && window.fetch) {
        return window.fetch.bind(window)
    }
    if (typeof global !== 'undefined' && global.fetch) {
        return global.fetch.bind(global)
    }
    throw new Error('Missing fetch')
}
 
/**
 * Append an action to the end of the array of actions in a SigningRequest.
 *
 * @param request SigningRequest
 * @param action AnyAction
 * @returns SigningRequest
 */
export function appendAction(request: SigningRequest, action: AnyAction): SigningRequest {
    const newAction = Action.from(action)
    const cloned = request.clone()
    switch (cloned.data.req.variantName) {
        case 'action': {
            cloned.data.req.value = [cloned.data.req.value as Action, newAction]
            cloned.data.req.variantIdx = 1
            break
        }
        case 'action[]': {
            const array = cloned.data.req.value as Action[]
            array.push(newAction)
            cloned.data.req.value = array
            break
        }
        case 'transaction': {
            const tx = cloned.data.req.value as Transaction
            tx.actions.push(newAction)
            cloned.data.req.value = tx
            break
        }
        default: {
            throw new Error('unknown data req type')
        }
    }
    return cloned
}
 
/**
 * Prepend an action to the end of the array of actions in a SigningRequest.
 *
 * @param request SigningRequest
 * @param action AnyAction
 * @returns SigningRequest
 */
export function prependAction(request: SigningRequest, action: AnyAction): SigningRequest {
    const newAction = Action.from(action)
    const cloned = request.clone()
    switch (cloned.data.req.variantName) {
        case 'action': {
            cloned.data.req.value = [newAction, cloned.data.req.value as Action]
            cloned.data.req.variantIdx = 1
            break
        }
        case 'action[]': {
            const array = cloned.data.req.value as Action[]
            array.unshift(newAction)
            cloned.data.req.value = array
            break
        }
        case 'transaction': {
            const tx = cloned.data.req.value as Transaction
            tx.actions.unshift(newAction)
            cloned.data.req.value = tx
            break
        }
        default: {
            throw new Error('unknown data req type')
        }
    }
    return cloned
}