Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: added http header types to reply #5046

Merged
merged 7 commits into from
Dec 8, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions types/reply.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface ReplyGenericInterface {

type HttpCodesReplyType = Partial<Record<HttpKeys, unknown>>

type HttpHeader = 'accept' | 'accept-charset' | 'accept-encoding' | 'accept-language' | 'accept-ranges' | 'access-control-allow-credentials' | 'access-control-allow-headers' | 'access-control-allow-methods' | 'access-control-allow-origin' | 'access-control-expose-headers' | 'access-control-max-age' | 'access-control-request-headers' | 'access-control-request-method' | 'age' | 'allow' | 'authorization' | 'cache-control' | 'cdn-cache-control' | 'connection' | 'content-disposition' | 'content-encoding' | 'content-language' | 'content-length' | 'content-location' | 'content-range' | 'content-security-policy' | 'content-security-policy-report-only' | 'cookie' | 'dnt' | 'date' | 'etag' | 'expect' | 'expires' | 'forwarded' | 'from' | 'host' | 'if-match' | 'if-modified-since' | 'if-none-match' | 'if-range' | 'if-unmodified-since' | 'last-modified' | 'link' | 'location' | 'max-forwards' | 'origin' | 'prgama' | 'proxy-authenticate' | 'proxy-authorization' | 'public-key-pins' | 'public-key-pins-report-only' | 'range' | 'referer' | 'referrer-policy' | 'refresh' | 'retry-after' | 'sec-websocket-accept' | 'sec-websocket-extensions' | 'sec-websocket-key' | 'sec-websocket-protocol' | 'sec-websocket-version' | 'server' | 'set-cookie' | 'strict-transport-security' | 'te' | 'trailer' | 'transfer-encoding' | 'user-agent' | 'upgrade' | 'upgrade-insecure-requests' | 'vary' | 'via' | 'warning' | 'www-authenticate' | 'x-content-type-options' | 'x-dns-prefetch-control' | 'x-frame-options' | 'x-xss-protection' | string;
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved

type ReplyTypeConstrainer<RouteGenericReply, Code extends ReplyKeysToCodes<keyof RouteGenericReply>> =
RouteGenericReply extends HttpCodesReplyType & Record<Exclude<keyof RouteGenericReply, keyof HttpCodesReplyType>, never> ?
Code extends keyof RouteGenericReply ? RouteGenericReply[Code] :
Expand Down Expand Up @@ -48,15 +50,15 @@ export interface FastifyReply<
statusCode: number;
sent: boolean;
send(payload?: ReplyType): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
header(key: string, value: any): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
headers(values: {[key: string]: any}): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
getHeader(key: string): number | string | string[] | undefined;
header(key: HttpHeader, value: any): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
headers(values: {[key: HttpHeader]: any}): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
getHeader(key: HttpHeader): number | string | string[] | undefined;
getHeaders(): {
// Node's `getHeaders()` can return numbers and arrays, so they're included here as possible types.
[key: string]: number | string | string[] | undefined;
[key: HttpHeader]: number | string | string[] | undefined;
};
removeHeader(key: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
hasHeader(key: string): boolean;
removeHeader(key: HttpHeader): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
hasHeader(key: HttpHeader): boolean;
// Note: should consider refactoring the argument order for redirect. statusCode is optional so it should be after the required url param
redirect(statusCode: number, url: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
redirect(url: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
Expand Down