All files / blong-gogo/src/codec/adapter/openapi ready.ts

60.97% Statements 25/41
80% Branches 4/5
33.33% Functions 1/3
60.97% Lines 25/41

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 421x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x                 1x     1x 1x  
import {handler, type IMeta} from '@feasibleone/blong/types';
 
import {methodId} from '../../../lib.ts';
 
export default handler<{
    namespace: {[namespace: string]: unknown};
}>(({config, lib: {load}, errors}) => {
    let handlers: {[name: string]: unknown};
    const assets: {[namespace: string]: unknown} = {};
    return {
        async ready() {
            Object.keys(config.namespace)
                .filter(Boolean)
                .forEach(namespace => {
                    if (
                        ![]
                            .concat(this.config.namespace as unknown as never[])
                            .find((n: string) => namespace.startsWith(n))
                    ) {
                        throw errors.openapiNamespaceNotDefined({
                            params: {namespace: namespace.split('.')[0]},
                        });
                    }
                });
            for (const [key, value] of Object.entries(config.namespace))
                assets[key] = await this.attach?.(`${value}.asset`);
            handlers = await load(assets, /./, this.configBase as string);
        },
        requestSend(params: unknown, $meta: IMeta) {
            const handler = $meta.method
                ? (handlers?.[methodId($meta.method)] as
                      | ((params: unknown, $meta: unknown) => unknown)
                      | undefined)
                : undefined;
            return handler ? handler.call(this, params, $meta) : params;
        },
        responseReceive(response: {body: unknown}) {
            return response.body;
        },
    };
});