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

46.15% Statements 30/65
31.57% Branches 6/19
100% Functions 2/2
46.15% Lines 30/65

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 661x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x                             14x 14x 14x 14x                 14x 14x 14x 14x                   10x 10x     10x 10x     14x 1x  
import {handler, type IMeta, type ITypedError} from '@feasibleone/blong/types';
import {type Response} from 'got';
 
export default handler(({errors}) => ({
    async receive(
        response: Response<{
            jsonrpc?: string;
            error?: unknown;
            validation?: unknown;
            debug?: unknown;
            checkpoints?: unknown[];
        }>,
        $meta?: IMeta,
    ) {
        const {body} = super.receive ? await super.receive(response) : response;
        if (body?.error !== undefined) {
            const error: ITypedError = body.jsonrpc
                ? Object.assign(new Error(), body.error)
                : typeof body.error === 'string'
                  ? new Error(body.error)
                  : Object.assign(new Error(), body.error);
            if (error.type)
                Object.defineProperty(error, 'name', {
                    value: error.type,
                    configurable: true,
                    enumerable: false,
                });
            error.req = response.request && {
                httpVersion: response.httpVersion,
                url: response.request.requestUrl as URL,
                method: response.request.options.method,
                // ...config.debug && this.sanitize(params, $meta)
            };
            error.res = {
                httpVersion: response.httpVersion,
                statusCode: response.statusCode,
            };
            throw error;
        } else if (response.statusCode < 200 || response.statusCode >= 300) {
            throw errors.jsonrpcHttp({
                statusCode: response.statusCode,
                // statusText: response.statusText,
                statusMessage: response.statusMessage,
                httpVersion: response.httpVersion,
                validation: response.body?.validation,
                debug: response.body?.debug,
                body: response.body,
                params: {
                    code: response.statusCode,
                },
                ...(response.request && {
                    url: response.request.requestUrl,
                    method: response.request.options.method,
                }),
            });
        } else if (typeof body === 'object' && 'result' in body && !('error' in body)) {
            if ($meta && body.checkpoints?.length) {
                ($meta.checkpoints ??= []).push(...body.checkpoints);
            }
            return body.result;
        } else {
            throw errors.jsonrpcEmpty();
        }
    },
}));