@sapphire/plugin-api
Version:
Plugin for @sapphire/framework to expose a REST API
1 lines • 2.38 kB
Source Map (JSON)
{"version":3,"sources":["../../../../src/lib/structures/Middleware.ts"],"names":[],"mappings":";;;AAQO,IAAe,WAAA,GAAf,MAAe,WAAA,SAA4E,KAA8B,CAAA;AAAA,EAaxH,WAAY,CAAA,OAAA,EAAmC,OAAmB,GAAA,EAAe,EAAA;AACvF,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA;AAHvB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,UAAA,CAAA;AAIf,IAAK,IAAA,CAAA,QAAA,GAAW,QAAQ,QAAY,IAAA,GAAA;AAAA;AAUtC,CAAA;AAzBgI,MAAA,CAAA,WAAA,EAAA,YAAA,CAAA;AAAzH,IAAe,UAAf,GAAA","file":"Middleware.mjs","sourcesContent":["import { Piece } from '@sapphire/pieces';\nimport type { Awaitable } from '@sapphire/utilities';\nimport type { ApiRequest } from './api/ApiRequest';\nimport type { ApiResponse } from './api/ApiResponse';\n\n/**\n * @since 1.0.0\n */\nexport abstract class Middleware<Options extends Middleware.Options = Middleware.Options> extends Piece<Options, 'middlewares'> {\n\t/**\n\t * The position the middleware has. The {@link MiddlewareStore} will run all middlewares with lower position than\n\t * this one.\n\t *\n\t * The built-in middlewares follow the following positions:\n\t * - headers: 10\n\t * - body: 20\n\t * - cookies: 30\n\t * - auth: 40\n\t */\n\tpublic readonly position: number;\n\n\tpublic constructor(context: Middleware.LoaderContext, options: Options = {} as Options) {\n\t\tsuper(context, options);\n\t\tthis.position = options.position ?? 1000;\n\t}\n\n\t/**\n\t * The method to be overridden by other middlewares.\n\t * @param request The client's request.\n\t * @param response The server's response.\n\t * @param route The route that matched this request, will be `null` if none matched.\n\t */\n\tpublic abstract run(request: Middleware.Request, response: Middleware.Response): Awaitable<unknown>;\n}\n\n/**\n * The options for all middlewares.\n */\nexport interface MiddlewareOptions extends Piece.Options {\n\t/**\n\t * The position to insert the middleware at.\n\t * @see Middleware#position\n\t * @default 1000\n\t */\n\tposition?: number;\n}\n\nexport namespace Middleware {\n\t/** @deprecated Use {@linkcode LoaderContext} instead. */\n\texport type Context = LoaderContext;\n\texport type LoaderContext = Piece.LoaderContext<'middlewares'>;\n\texport type Options = MiddlewareOptions;\n\texport type JSON = Piece.JSON;\n\texport type LocationJSON = Piece.LocationJSON;\n\n\texport type Request = ApiRequest;\n\texport type Response = ApiResponse;\n}\n"]}