All files / src/endpoints utils.ts

89.65% Statements 26/29
79.16% Branches 19/24
83.33% Functions 5/6
89.28% Lines 25/28

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 65608x                       1x       66x 66x 66x     1x   66x 66x 330x 38x   66x     1x 66x 66x 111x 38x   73x       66x                   2x 220x 2x   218x 218x 4x         214x    
export function buildQueryParams(
    params?: {[key: string]: any},
    extra?: {[key: string]: string}
): string {
    const queryParts = buildBodyParams(params, extra)
    const queryParams = Object.keys(queryParts).length
        ? '?' + new URLSearchParams(queryParts).toString()
        : ''
 
    return queryParams
}
 
export function buildBodyParams(
    params?: {[key: string]: any},
    extra?: {[key: string]: string}
): {[key: string]: any} {
    let options = serializeQueryParams(params)
    options = fixPostArguments(options)
    return Object.assign(extra ? extra : {}, options)
}
 
export function fixPostArguments(params) {
    // https://github.com/pinknetworkx/eosio-contract-api/issues/131
    const options = {...params}
    for (const key of ['page', 'limit', 'before', 'after', 'burned'])
        if (key in options) {
            options[key] = String(options[key])
        }
    return options
}
 
export function serializeQueryParams(params?: {[key: string]: any}): {[key: string]: any} {
    const result = {}
    for (const [key, value] of Object.entries(params || {})) {
        if (['boolean', 'number'].includes(typeof value)) {
            result[key] = value
        } else {
            result[key] = String(value)
        }
    }
 
    return result
}
 
/**
 * Encode a value for use as one path segment.
 *
 * An empty value or a dot segment would rewrite the request path, so both are rejected, as is a
 * missing value, which would otherwise travel as the literal segment 'undefined'. A value that
 * equals a sibling route literal such as '_count' is a valid segment and is not rejected here.
 */
export function pathSegment(value: unknown): string {
    if (value === null || value === undefined) {
        throw new Error('Invalid path segment: a value is required')
    }
    const segment = String(value)
    if (segment === '' || segment === '.' || segment === '..') {
        throw new Error(
            `Invalid path segment '${segment}': an empty or dot segment rewrites the request path`
        )
    }
 
    return encodeURIComponent(segment)
}