All files / core/blong-realm/adapter/semlog responseReceive.ts

100% Statements 32/32
80% Branches 4/5
100% Functions 1/1
100% Lines 32/32

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 331x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 5x 5x 1x  
import {handler} from '@feasibleone/blong';
 
/**
 * The answer every read comes back through.
 *
 * `response.receive` is the framework's other conversion seam: the port loop runs it on
 * what the adapter's `exec` returned, with the method and the response's own meta, so one
 * handler here unwraps every one of the six reads. What it does is exactly what the reads
 * would otherwise each do by hand — hand the body on, and refuse to flatten a refusal into
 * an empty answer.
 *
 * The status travels with the failure because the service's refusals are answers of its
 * own: `404` says the execution was never retained, which a page has to be able to tell
 * apart from "nothing has been observed yet". The URL is read off the response rather than
 * from a parameter, because this handler serves six paths and knows none of them.
 */
export default handler(
    () =>
        function responseReceive(response: {
            statusCode: number;
            body: unknown;
            request?: {requestUrl?: string | URL};
        }) {
            if (response.statusCode < 200 || response.statusCode >= 300) {
                throw new Error(
                    `the semantic log service answered ${String(response.statusCode)} for ` +
                        `${String(response.request?.requestUrl ?? '')}`,
                );
            }
            return response.body;
        },
);