UNPKG

6.17 kBTypeScriptView Raw
1/// <reference types="express" />
2import { HandlerContext, Request, Response } from '@loopback/express';
3import { ReferenceObject, SchemaObject } from '@loopback/openapi-v3';
4import Ajv, { ErrorObject, FormatDefinition, KeywordDefinition, Options as AjvOptions, ValidateFunction } from 'ajv';
5import { ErrorMessageOptions } from 'ajv-errors';
6import { Options, OptionsJson, OptionsText, OptionsUrlencoded } from 'body-parser';
7import { ResolvedRoute, RouteEntry } from './router';
8/**
9 * Re-export types from `./middleware`
10 */
11export * from '@loopback/express';
12/**
13 * Find a route matching the incoming request.
14 * Throw an error when no route was found.
15 */
16export type FindRoute = (request: Request) => ResolvedRoute;
17/**
18 * A function to parse OpenAPI operation parameters for a given route
19 */
20export type ParseParams = (request: Request, route: ResolvedRoute) => Promise<OperationArgs>;
21/**
22 * Invokes a method defined in the Application Controller
23 *
24 * @param controller - Name of end-user's application controller
25 * class which defines the methods.
26 * @param method - Method name in application controller class
27 * @param args - Operation arguments for the method
28 * @returns OperationRetval Result from method invocation
29 */
30export type InvokeMethod = (route: RouteEntry, args: OperationArgs) => Promise<OperationRetval>;
31/**
32 * Send the operation response back to the client.
33 *
34 * @param response - The response the response to send to.
35 * @param result - The operation result to send.
36 */
37export type Send = (response: Response, result: OperationRetval) => void;
38/**
39 * Reject the request with an error.
40 *
41 * @param handlerContext - The context object holding HTTP request, response
42 * and other data needed to handle an incoming HTTP request.
43 * @param err - The error.
44 */
45export type Reject = (handlerContext: HandlerContext, err: Error) => void;
46/**
47 * Log information about a failed request.
48 *
49 * @param err - The error reported by request handling code.
50 * @param statusCode - Status code of the HTTP response
51 * @param request - The request that failed.
52 */
53export type LogError = (err: Error, statusCode: number, request: Request) => void;
54/**
55 * Cache for AJV schema validators
56 */
57export type SchemaValidatorCache = WeakMap<SchemaObject | ReferenceObject, // First keyed by schema object
58Map<string, ValidateFunction>>;
59/**
60 * Options for AJV errors
61 */
62export type AjvErrorOptions = ErrorMessageOptions;
63/**
64 * Factory function for Ajv instances
65 */
66export type AjvFactory = (options?: AjvOptions) => Ajv;
67/**
68 * Ajv keyword definition with a name
69 */
70export type AjvKeyword = KeywordDefinition;
71/**
72 * Ajv format definition with a name
73 */
74export type AjvFormat<T extends string | number = string> = FormatDefinition<T> & {
75 name: string;
76};
77/**
78 * Options for any value validation using AJV
79 */
80export interface ValueValidationOptions extends ValidationOptions {
81 /**
82 * Where the data comes from. It can be 'body', 'path', 'header',
83 * 'query', 'cookie', etc...
84 */
85 source?: string;
86 /**
87 * Parameter name, as provided in `ParameterObject#name` property.
88 */
89 name?: string;
90}
91/**
92 * Options for request body validation using AJV
93 */
94export interface ValidationOptions extends AjvOptions {
95 /**
96 * Custom cache for compiled schemas by AJV. This setting makes it possible
97 * to skip the default cache.
98 */
99 compiledSchemaCache?: SchemaValidatorCache;
100 /**
101 * Enable additional AJV keywords from https://github.com/epoberezkin/ajv-keywords
102 * - `string[]`: Add an array of keywords from `ajv-keywords`
103 */
104 ajvKeywords?: string[];
105 /**
106 * Enable custom error messages in JSON-Schema for AJV validator
107 * from https://github.com/epoberezkin/ajv-errors
108 * - `true`: Enable `ajv-errors`
109 * - `AjvErrorOptions`: Enable `ajv-errors` with options
110 */
111 ajvErrors?: AjvErrorOptions;
112 /**
113 * A function that transform the `ErrorObject`s reported by AJV.
114 * This could be used for error messages customization, localization, etc.
115 */
116 ajvErrorTransformer?: (errors: ErrorObject[]) => ErrorObject[];
117 /**
118 * A factory to create Ajv instance
119 */
120 ajvFactory?: (options: AjvOptions) => Ajv;
121 /**
122 * An array of keys to be rejected, such as `__proto__`.
123 */
124 prohibitedKeys?: string[];
125}
126/**
127 * Options for request body parsing
128 * See https://github.com/expressjs/body-parser/#options
129 *
130 * Built-in parsers retrieve their own options from the request body parser
131 * options. The parser specific properties override common ones.
132 */
133export interface RequestBodyParserOptions extends Options {
134 /**
135 * Options for json parser
136 */
137 json?: OptionsJson;
138 /**
139 * Options for urlencoded parser
140 */
141 urlencoded?: OptionsUrlencoded;
142 /**
143 * Options for text parser
144 */
145 text?: OptionsText;
146 /**
147 * Options for raw parser
148 */
149 raw?: Options;
150 /**
151 * Validation options for AJV, see https://github.com/epoberezkin/ajv#options
152 * This setting is global for all request body parsers and it cannot be
153 * overridden inside parser specific properties such as `json` or `text`.
154 */
155 validation?: ValidationOptions;
156 /**
157 * Common options for all parsers
158 */
159 [name: string]: unknown;
160}
161export type PathParameterValues = {
162 [key: string]: any;
163};
164export type OperationArgs = any[];
165/**
166 * Return value of a controller method (a function implementing an operation).
167 * This is a type alias for "any", used to distinguish
168 * operation results from other "any" typed values.
169 */
170export type OperationRetval = any;
171/**
172 * user profile to add in session
173 */
174export interface SessionUserProfile {
175 provider: string;
176 token: string;
177 email: string;
178 [attribute: string]: any;
179}
180/**
181 * interface to set variables in user session
182 */
183export interface Session {
184 profile: SessionUserProfile;
185 [key: string]: any;
186}
187/**
188 * extending express request type with a session field
189 */
190export interface RequestWithSession extends Request {
191 session: Session;
192}
193export type RequestBodyValidationOptions = ValidationOptions;