All files / test/ctp/adapter/payshield init.ts

100% Statements 75/75
75% Branches 6/8
100% Functions 0/0
100% Lines 75/75

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 761x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 100x 100x 100x 100x 2x 2x 2x 2x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 2x 2x 2x 2x  
import {createRequire} from 'node:module';
import bitsyntax from 'ut-bitsyntax';
 
import {library, type Errors} from '@feasibleone/blong';
 
export default library(({config: {messageFormat}, lib: {merge}, lib}) => {
    const messages = Object.entries<{
        warnings?: string[];
        requestPattern?: string;
        requestCode: string;
        responsePattern?: string;
        responseCode: string;
        errorPattern?: string;
    }>(merge({}, createRequire(import.meta.url)('./messages.json'), messageFormat));
    const errors = lib.errors as unknown as Errors<{
        'payshield.parser.request': unknown;
        'payshield.parser.parserResponse': unknown;
    }>;
    return {
        commandNames: messages.reduce(
            (prev, [name, {requestPattern, responsePattern, requestCode, responseCode}]) => {
                if (requestPattern) prev[requestCode] = name + ':request';
                if (responsePattern) prev[responseCode] = name + ':response';
                return prev;
            },
            {} as Record<string, string>,
        ),
        commands: messages.reduce(
            (
                prev,
                [
                    name,
                    {
                        requestPattern,
                        requestCode,
                        warnings,
                        responsePattern,
                        responseCode,
                        errorPattern,
                    },
                ],
            ) => {
                if (requestPattern) {
                    const pattern = bitsyntax.parse(requestPattern);
                    if (!pattern)
                        throw errors['payshield.parser.request']({params: {command: name}});
                    prev[name + ':request'] = {
                        pattern,
                        matcher: bitsyntax.matcher(requestPattern),
                        code: requestCode,
                        warnings,
                        method: name,
                        mtid: 'request',
                    };
                }
                if (responsePattern) {
                    const pattern = bitsyntax.parse(responsePattern);
                    if (!pattern)
                        throw errors['payshield.parser.parserResponse']({params: {command: name}});
                    prev[name + ':response'] = {
                        pattern,
                        matcher: bitsyntax.matcher(responsePattern),
                        errorMatcher: errorPattern && bitsyntax.matcher(errorPattern),
                        code: responseCode,
                        warnings,
                        method: name,
                        mtid: 'response',
                    };
                }
                return prev;
            },
            {} as Record<string, unknown>,
        ),
    };
});