UNPKG

4.68 kBTypeScriptView Raw
1import { ErrorObject } from '@fastify/ajv-compiler'
2import { FastifyRequestContext, FastifyContextConfig } from './context'
3import { FastifyInstance } from './instance'
4import { FastifyBaseLogger } from './logger'
5import { RouteGenericInterface, FastifyRouteConfig, RouteHandlerMethod } from './route'
6import { FastifySchema } from './schema'
7import { FastifyRequestType, FastifyTypeProvider, FastifyTypeProviderDefault, ResolveFastifyRequestType } from './type-provider'
8import { ContextConfigDefault, RawRequestDefaultExpression, RawServerBase, RawServerDefault, RequestBodyDefault, RequestHeadersDefault, RequestParamsDefault, RequestQuerystringDefault } from './utils'
9
10type HTTPRequestPart = 'body' | 'query' | 'querystring' | 'params' | 'headers'
11export interface RequestGenericInterface {
12 Body?: RequestBodyDefault;
13 Querystring?: RequestQuerystringDefault;
14 Params?: RequestParamsDefault;
15 Headers?: RequestHeadersDefault;
16}
17
18export interface ValidationFunction {
19 (input: any): boolean
20 errors?: null | ErrorObject[];
21}
22
23export interface RequestRouteOptions<ContextConfig = ContextConfigDefault, SchemaCompiler = FastifySchema> {
24 method: string;
25 // `url` can be `undefined` for instance when `request.is404` is true
26 url: string | undefined;
27 bodyLimit: number;
28 attachValidation: boolean;
29 logLevel: string;
30 version: string | undefined;
31 exposeHeadRoute: boolean;
32 prefixTrailingSlash: string;
33 config: FastifyContextConfig & FastifyRouteConfig & ContextConfig;
34 schema?: SchemaCompiler; // it is empty for 404 requests
35 handler: RouteHandlerMethod;
36}
37
38/**
39 * FastifyRequest is an instance of the standard http or http2 request objects.
40 * It defaults to http.IncomingMessage, and it also extends the relative request object.
41 */
42export interface FastifyRequest<RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
43 RawServer extends RawServerBase = RawServerDefault,
44 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
45 SchemaCompiler extends FastifySchema = FastifySchema,
46 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
47 ContextConfig = ContextConfigDefault,
48 Logger extends FastifyBaseLogger = FastifyBaseLogger,
49 RequestType extends FastifyRequestType = ResolveFastifyRequestType<TypeProvider, SchemaCompiler, RouteGeneric>
50 // ^ Temporary Note: RequestType has been re-ordered to be the last argument in
51 // generic list. This generic argument is now considered optional as it can be
52 // automatically inferred from the SchemaCompiler, RouteGeneric and TypeProvider
53 // arguments. Implementations that already pass this argument can either omit
54 // the RequestType (preferred) or swap Logger and RequestType arguments when
55 // creating custom types of FastifyRequest. Related issue #4123
56> {
57 id: string;
58 params: RequestType['params']; // deferred inference
59 raw: RawRequest;
60 query: RequestType['query'];
61 headers: RawRequest['headers'] & RequestType['headers']; // this enables the developer to extend the existing http(s|2) headers list
62 log: Logger;
63 server: FastifyInstance;
64 body: RequestType['body'];
65 context: FastifyRequestContext<ContextConfig>;
66 routeConfig: FastifyRequestContext<ContextConfig>['config'];
67 routeSchema?: FastifySchema; // it is empty for 404 requests
68
69 /** in order for this to be used the user should ensure they have set the attachValidation option. */
70 validationError?: Error & { validation: any; validationContext: string };
71
72 /**
73 * @deprecated Use `raw` property
74 */
75 readonly req: RawRequest & RouteGeneric['Headers']; // this enables the developer to extend the existing http(s|2) headers list
76 readonly ip: string;
77 readonly ips?: string[];
78 readonly hostname: string;
79 readonly url: string;
80 readonly originalUrl: string;
81 readonly protocol: 'http' | 'https';
82 readonly method: string;
83 readonly routerPath: string;
84 readonly routerMethod: string;
85 readonly routeOptions: Readonly<RequestRouteOptions<ContextConfig, SchemaCompiler>>
86 readonly is404: boolean;
87 readonly socket: RawRequest['socket'];
88
89 getValidationFunction(httpPart: HTTPRequestPart): ValidationFunction
90 getValidationFunction(schema: {[key: string]: any}): ValidationFunction
91 compileValidationSchema(schema: {[key: string]: any}, httpPart?: HTTPRequestPart): ValidationFunction
92 validateInput(input: any, schema: {[key: string]: any}, httpPart?: HTTPRequestPart): boolean
93 validateInput(input: any, httpPart?: HTTPRequestPart): boolean
94
95 // Prefer `socket` over deprecated `connection` property in node 13.0.0 or higher
96 // @deprecated
97 readonly connection: RawRequest['socket'];
98}