All files / core/blong-gogo/src/codec/adapter/jsonrpc send.ts

100% Statements 36/36
55.55% Branches 5/9
100% Functions 1/1
100% Lines 36/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 371x 1x 1x 2x 2x 2x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 2x 2x  
import {handler, type IMeta} from '@feasibleone/blong/types';
 
export default handler(({config, lib: {timing}}) => {
    let id = 1;
    return {
        send(
            msg: {$http?: {method?: string; headers?: unknown; path?: unknown}},
            $meta: IMeta,
            context: unknown,
        ) {
            const params = (msg && !(msg instanceof Array) && Object.assign({}, msg)) || msg;
            const $http = params?.$http;
            delete params?.$http;
            const result = {
                method: $http?.method,
                headers: $http?.headers,
                path:
                    $http?.path ??
                    `/rpc/${$meta.method!.replace(/\//gi, '%2F').replace(/\./g, '/')}`,
                responseType: 'json',
                // body,
                // form,
                json: {
                    jsonrpc: '2.0',
                    ...($meta.mtid === 'request' && {id: id++}),
                    method: $meta.method,
                    params,
                    expect: $meta.expect,
                    ...($meta.timeout &&
                        $meta.timeout[0] && {timeout: timing.spare($meta.timeout, config.latency as number | undefined)}),
                },
            };
            return super.send ? super.send(result, $meta, context) : result;
        },
    };
});