UNPKG

6.65 kBTypeScriptView Raw
1import { Buffer } from 'buffer'
2import { FastifyReplyContext } from './context'
3import { FastifyInstance } from './instance'
4import { FastifyBaseLogger } from './logger'
5import { FastifyRequest } from './request'
6import { RouteGenericInterface } from './route'
7import { FastifySchema } from './schema'
8import { FastifyReplyType, FastifyTypeProvider, FastifyTypeProviderDefault, ResolveFastifyReplyType, CallTypeProvider } from './type-provider'
9import { CodeToReplyKey, ContextConfigDefault, HttpKeys, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault, ReplyDefault, ReplyKeysToCodes, HttpHeader } from './utils'
10
11export interface ReplyGenericInterface {
12 Reply?: ReplyDefault;
13}
14
15type HttpCodesReplyType = Partial<Record<HttpKeys, unknown>>
16
17type ReplyTypeConstrainer<RouteGenericReply, Code extends ReplyKeysToCodes<keyof RouteGenericReply>> =
18 RouteGenericReply extends HttpCodesReplyType & Record<Exclude<keyof RouteGenericReply, keyof HttpCodesReplyType>, never> ?
19 Code extends keyof RouteGenericReply ? RouteGenericReply[Code] :
20 CodeToReplyKey<Code> extends keyof RouteGenericReply ? RouteGenericReply[CodeToReplyKey<Code>] : unknown :
21 RouteGenericReply;
22
23export type ResolveReplyTypeWithRouteGeneric<RouteGenericReply, Code extends ReplyKeysToCodes<keyof RouteGenericReply>,
24 SchemaCompiler extends FastifySchema = FastifySchema,
25 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault> =
26 Code extends keyof SchemaCompiler['response'] ?
27 CallTypeProvider<TypeProvider, SchemaCompiler['response'][Code]> :
28 ResolveFastifyReplyType<TypeProvider, SchemaCompiler, { Reply: ReplyTypeConstrainer<RouteGenericReply, Code> }>
29/**
30 * FastifyReply is an instance of the standard http or http2 reply types.
31 * It defaults to http.ServerResponse, and it also extends the relative reply object.
32 */
33export interface FastifyReply<
34 RawServer extends RawServerBase = RawServerDefault,
35 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
36 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
37 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
38 ContextConfig = ContextConfigDefault,
39 SchemaCompiler extends FastifySchema = FastifySchema,
40 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
41 ReplyType extends FastifyReplyType = ResolveFastifyReplyType<TypeProvider, SchemaCompiler, RouteGeneric>
42> {
43 raw: RawReply;
44 context: FastifyReplyContext<ContextConfig>;
45 elapsedTime: number;
46 log: FastifyBaseLogger;
47 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>;
48 server: FastifyInstance;
49 code<Code extends ReplyKeysToCodes<keyof RouteGeneric['Reply']>>(statusCode: Code): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, ResolveReplyTypeWithRouteGeneric<RouteGeneric['Reply'], Code, SchemaCompiler, TypeProvider>>;
50 status<Code extends ReplyKeysToCodes<keyof RouteGeneric['Reply']>>(statusCode: Code): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, ResolveReplyTypeWithRouteGeneric<RouteGeneric['Reply'], Code, SchemaCompiler, TypeProvider>>;
51 statusCode: number;
52 sent: boolean;
53 send(payload?: ReplyType): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
54 header(key: HttpHeader, value: any): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
55 headers(values: Partial<Record<HttpHeader, number | string | string[] | undefined>>): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
56 getHeader(key: HttpHeader): number | string | string[] | undefined;
57 getHeaders(): Record<HttpHeader, number | string | string[] | undefined>;
58 removeHeader(key: HttpHeader): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
59 hasHeader(key: HttpHeader): boolean;
60 /**
61 * @deprecated The `reply.redirect()` method has a new signature: `reply.reply.redirect(url: string, code?: number)`. It will be enforced in `fastify@v5`'.
62 */
63 redirect(statusCode: number, url: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
64 redirect(url: string, statusCode?: number): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
65 hijack(): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
66 callNotFound(): void;
67 /**
68 * @deprecated Use the Reply#elapsedTime property instead
69 */
70 getResponseTime(): number;
71 type(contentType: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
72 serializer(fn: (payload: any) => string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
73 serialize(payload: any): string | ArrayBuffer | Buffer;
74 // Serialization Methods
75 getSerializationFunction(httpStatus: string, contentType?: string): ((payload: {[key: string]: unknown}) => string) | undefined;
76 getSerializationFunction(schema: {[key: string]: unknown}): ((payload: {[key: string]: unknown}) => string) | undefined;
77 compileSerializationSchema(schema: {[key: string]: unknown}, httpStatus?: string, contentType?: string): (payload: {[key: string]: unknown}) => string;
78 serializeInput(input: {[key: string]: unknown}, schema: {[key: string]: unknown}, httpStatus?: string, contentType?: string): string;
79 serializeInput(input: {[key: string]: unknown}, httpStatus: string, contentType?: string): unknown;
80 then(fulfilled: () => void, rejected: (err: Error) => void): void;
81 trailer: (
82 key: string,
83 fn: ((reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>, payload: string | Buffer | null) => Promise<string>) | ((reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>, payload: string | Buffer | null, done: (err: Error | null, value?: string) => void) => void)
84 ) => FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
85 hasTrailer(key: string): boolean;
86 removeTrailer(key: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
87}