UNPKG

6.96 kBTypeScriptView Raw
1import * as http from 'http'
2import * as http2 from 'http2'
3import * as https from 'https'
4import * as LightMyRequest from 'light-my-request'
5
6import { FastifyRequest, RequestGenericInterface } from './types/request'
7import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression } from './types/utils'
8import { FastifyLoggerInstance, FastifyLoggerOptions } from './types/logger'
9import { FastifyInstance } from './types/instance'
10import { FastifyServerFactory } from './types/serverFactory'
11import * as ajv from 'ajv'
12import { FastifyError } from 'fastify-error'
13import { FastifyReply } from './types/reply'
14import { FastifySchemaValidationError } from './types/schema'
15
16/**
17 * Fastify factor function for the standard fastify http, https, or http2 server instance.
18 *
19 * The default function utilizes http
20 *
21 * @param opts Fastify server options
22 * @returns Fastify server instance
23 */
24declare function fastify<
25 Server extends http2.Http2SecureServer,
26 Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
27 Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
28 Logger extends FastifyLoggerInstance = FastifyLoggerInstance
29>(opts: FastifyHttp2SecureOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger>>
30declare function fastify<
31 Server extends http2.Http2Server,
32 Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
33 Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
34 Logger extends FastifyLoggerInstance = FastifyLoggerInstance
35>(opts: FastifyHttp2Options<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger>>
36declare function fastify<
37 Server extends https.Server,
38 Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
39 Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
40 Logger extends FastifyLoggerInstance = FastifyLoggerInstance
41>(opts: FastifyHttpsOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger>>
42declare function fastify<
43 Server extends http.Server,
44 Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
45 Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
46 Logger extends FastifyLoggerInstance = FastifyLoggerInstance
47>(opts?: FastifyServerOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger>>
48export default fastify
49
50export type FastifyHttp2SecureOptions<
51 Server extends http2.Http2SecureServer,
52 Logger extends FastifyLoggerInstance = FastifyLoggerInstance
53> = FastifyServerOptions<Server, Logger> & {
54 http2: true,
55 https: http2.SecureServerOptions
56}
57
58export type FastifyHttp2Options<
59 Server extends http2.Http2Server,
60 Logger extends FastifyLoggerInstance = FastifyLoggerInstance
61> = FastifyServerOptions<Server, Logger> & {
62 http2: true,
63 http2SessionTimeout?: number,
64}
65
66export type FastifyHttpsOptions<
67 Server extends https.Server,
68 Logger extends FastifyLoggerInstance = FastifyLoggerInstance
69> = FastifyServerOptions<Server, Logger> & {
70 https: https.ServerOptions
71}
72/**
73 * Options for a fastify server instance. Utilizes conditional logic on the generic server parameter to enforce certain https and http2
74 */
75export type FastifyServerOptions<
76 RawServer extends RawServerBase = RawServerDefault,
77 Logger extends FastifyLoggerInstance = FastifyLoggerInstance
78> = {
79 ignoreTrailingSlash?: boolean,
80 connectionTimeout?: number,
81 keepAliveTimeout?: number,
82 pluginTimeout?: number,
83 bodyLimit?: number,
84 maxParamLength?: number,
85 disableRequestLogging?: boolean,
86 onProtoPoisoning?: 'error' | 'remove' | 'ignore',
87 onConstructorPoisoning?: 'error' | 'remove' | 'ignore',
88 logger?: boolean | FastifyLoggerOptions<RawServer> | Logger,
89 serverFactory?: FastifyServerFactory<RawServer>,
90 caseSensitive?: boolean,
91 requestIdHeader?: string,
92 requestIdLogLabel?: string;
93 genReqId?: <RequestGeneric extends RequestGenericInterface = RequestGenericInterface>(req: FastifyRequest<RequestGeneric, RawServer, RawRequestDefaultExpression<RawServer>>) => string,
94 trustProxy?: boolean | string | string[] | number | TrustProxyFunction,
95 querystringParser?: (str: string) => { [key: string]: string | string[] },
96 versioning?: {
97 storage(): {
98 get(version: string): string | null,
99 set(version: string, store: Function): void
100 del(version: string): void,
101 empty(): void
102 },
103 deriveVersion<Context>(req: Object, ctx?: Context): string // not a fan of using Object here. Also what is Context? Can either of these be better defined?
104 },
105 return503OnClosing?: boolean,
106 ajv?: {
107 customOptions?: ajv.Options,
108 plugins?: Function[]
109 },
110 frameworkErrors?: <RequestGeneric extends RequestGenericInterface = RequestGenericInterface>(
111 error: FastifyError,
112 req: FastifyRequest<RequestGeneric, RawServer, RawRequestDefaultExpression<RawServer>>,
113 res: FastifyReply<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>>
114 ) => void,
115 rewriteUrl?: (req: RawRequestDefaultExpression<RawServer>) => string,
116 schemaErrorFormatter?: (errors: FastifySchemaValidationError[], dataVar: string) => Error
117}
118
119type TrustProxyFunction = (address: string, hop: number) => boolean
120
121/* Export all additional types */
122export { FastifyRequest, RequestGenericInterface } from './types/request'
123export { FastifyReply } from './types/reply'
124export { FastifyPluginCallback, FastifyPluginAsync, FastifyPluginOptions, FastifyPlugin } from './types/plugin'
125export { FastifyInstance } from './types/instance'
126export { FastifyLoggerOptions, FastifyLoggerInstance, FastifyLogFn, LogLevel } from './types/logger'
127export { FastifyContext } from './types/context'
128export { RouteHandler, RouteHandlerMethod, RouteOptions, RouteShorthandMethod, RouteShorthandOptions, RouteShorthandOptionsWithHandler } from './types/route'
129export * from './types/register'
130export { FastifyBodyParser, FastifyContentTypeParser, AddContentTypeParser, hasContentTypeParser } from './types/content-type-parser'
131export { FastifyError, ValidationResult } from 'fastify-error'
132export { FastifySchema, FastifySchemaCompiler } from './types/schema'
133export { HTTPMethods, RawServerBase, RawRequestDefaultExpression, RawReplyDefaultExpression, RawServerDefault, ContextConfigDefault, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault } from './types/utils'
134export * from './types/hooks'
135export { FastifyServerFactory, FastifyServerFactoryHandler } from './types/serverFactory'
136export { fastify }