mappersmith
Version:
It is a lightweight rest client for node.js and the browser
1 lines • 3.17 kB
Source Map (JSON)
{"version":3,"sources":["../../../src/middleware/index.ts"],"sourcesContent":["import type { Request } from '../request'\nimport type { Response } from '../response'\n\nexport type Context = object\n\nexport type RequestGetter = () => Promise<Request>\n\nexport type ResponseGetter = () => Promise<Response>\n\nexport type AbortFn = (error: Error) => void\n\nexport type RenewFn = () => Promise<Response>\n\nexport interface MiddlewareDescriptor {\n __name?: string\n /**\n * @deprecated: Use prepareRequest\n */\n request?(request: Request): Promise<Request> | Request\n /**\n * Allows a middleware to tap into the prepare request phase\n */\n prepareRequest(\n /**\n * This function must return a `Promise` resolving the `Request`.\n *\n * The method `enhance` can be used to generate a new request based on the previous one.\n */\n next: RequestGetter,\n /**\n * Function that can be used to abort the middleware execution early on and throw a custom error to the user.\n */\n abort: AbortFn\n ): Promise<Request | void>\n /**\n * Allows a middleware to tap into the response phase\n */\n response(\n /**\n * This function must return a `Promise` resolving the `Response`.\n *\n * The method `enhance` can be used to generate a new response based on the previous one.\n */\n next: ResponseGetter,\n /**\n * Function that is used to rerun the middleware stack.\n *\n * Useful for example when automatically refreshing an expired access token.\n */\n renew: RenewFn,\n /**\n * The final request object (after the whole middleware chain has prepared and all `prepareRequest` been executed).\n *\n * Useful for example when you want to get access to the request without invoking `next`\n */\n request: Request\n ): Promise<Response>\n}\n\nexport interface MiddlewareParams {\n readonly clientId: string | null\n readonly context: Context\n readonly resourceMethod: string\n readonly resourceName: string\n readonly mockRequest?: boolean\n}\n\n// eslint-disable-next-line @typescript-eslint/ban-types\ntype DefaultPrivateProps = {}\n\n/**\n * Mappersmith middleware, used to describe a factory function that given MiddlewareParams\n * returns a middleware object (partial of MiddlewareDescriptor).\n *\n * If the middleware needs to save local state you can use PrivateProps to allow it.\n */\nexport type Middleware<PrivateProps extends Record<string, unknown> = DefaultPrivateProps> = (\n params: MiddlewareParams\n) => Partial<MiddlewareDescriptor & PrivateProps>\n\nexport { BasicAuthMiddleware } from './basic-auth'\nexport { RetryMiddleware } from './retry/v2/index'\nexport { CsrfMiddleware } from './csrf'\nexport { DurationMiddleware } from './duration'\nexport { EncodeJsonMiddleware } from './encode-json'\nexport { GlobalErrorHandlerMiddleware } from './global-error-handler'\nexport { LogMiddleware } from './log'\nexport { TimeoutMiddleware } from './timeout'\n"],"mappings":";AAgFA,SAAS,2BAA2B;AACpC,SAAS,uBAAuB;AAChC,SAAS,sBAAsB;AAC/B,SAAS,0BAA0B;AACnC,SAAS,4BAA4B;AACrC,SAAS,oCAAoC;AAC7C,SAAS,qBAAqB;AAC9B,SAAS,yBAAyB;","names":[]}