UNPKG

@senacor/azure-function-middleware

Version:

Middleware for azure functions to handle authentication, authorization, error handling and logging

20 lines (19 loc) 1.08 kB
import { FunctionHandler, InvocationContext } from '@azure/functions'; export type ExceptionalResult = { $failed: true; $error: Error; }; export type MiddlewareResult<T> = ExceptionalResult | { $failed: false; $result: Awaited<T | undefined>; }; export type BeforeExecutionFunction<T = FunctionHandler> = T extends (...a: infer U) => infer R ? (...a: [...U, MiddlewareResult<R>]) => unknown : never; export type PostExecutionFunction<T = FunctionHandler> = BeforeExecutionFunction<T>; export declare const isErrorResult: <T>(result: MiddlewareResult<T> | ExceptionalResult) => result is ExceptionalResult; export type Options = { errorResponseHandler?: (error: unknown, context: InvocationContext) => { [key: string]: unknown; }; disableErrorHandling?: boolean; }; export declare const middleware: <T extends FunctionHandler>(beforeExecution: (BeforeExecutionFunction<T> | false)[], handler: T, postExecution: (PostExecutionFunction<T> | false)[], opts?: Options) => (request: Parameters<T>[0], context: InvocationContext) => ReturnType<T>;