UNPKG

@dossierhq/core

Version:

The core Dossier library used by clients and server alike, used to interact with schema and entities directly, as well as remotely through a client.

59 lines 2.3 kB
/// <reference types="./SharedClient.d.ts" /> import { notOk } from '../ErrorResult.js'; import { assertIsDefined } from '../utils/Asserts.js'; export async function executeOperationPipeline(context, pipeline, operation) { if (pipeline.length === 0) { return notOk.Generic('Cannot execute an empty pipeline'); } return await executeOperationMiddleware(context, pipeline, 0, operation); } async function executeOperationMiddleware(context, pipeline, pipelineIndex, operation) { // Setup callbacks let result; const resolve = (res) => (result = res); const next = async () => { if (pipelineIndex >= pipeline.length - 1) { return notOk.Generic('The last middleware in the pipeline cannot call next()'); } const nextResult = await executeOperationMiddleware(context, pipeline, pipelineIndex + 1, operation); return nextResult; }; const operationWithCallbacks = { ...operation, resolve, next }; // Execute the middleware in pipelineIndex try { await pipeline[pipelineIndex](context, operationWithCallbacks); assertIsDefined(result); return result; } catch (error) { return notOk.GenericUnexpectedException(context, error); } } export async function LoggingClientMiddleware(context, operation) { const { logger } = context; const noArgs = Array.isArray(operation.args) && operation.args.length === 0; if (noArgs) { logger.info(`Executing ${operation.name}`); } else { logger.info(`Executing ${operation.name}: ${JSON.stringify(operation.args)}`); } const result = await operation.next(); if (result.isError()) { logger.warn(`Result ${operation.name} error: ${result.error}: ${result.message}`); } else { // Add effect to log, effect is just a convention, not a formal part of OkResult const effect = typeof result.value === 'object' && result.value && result.value.effect; if (typeof effect === 'string') { logger.info(`Result ${operation.name} ok, effect ${effect}`); } else { logger.info(`Result ${operation.name} ok`); } } operation.resolve(result); } //# sourceMappingURL=SharedClient.js.map