UNPKG

3.32 kBTypeScriptView Raw
1import * as http from 'http'
2import * as http2 from 'http2'
3import * as https from 'https'
4
5/**
6 * Standard HTTP method strings
7 */
8type _HTTPMethods = 'DELETE' | 'GET' | 'HEAD' | 'PATCH' | 'POST' | 'PUT' | 'OPTIONS' |
9'PROPFIND' | 'PROPPATCH' | 'MKCOL' | 'COPY' | 'MOVE' | 'LOCK' | 'UNLOCK' | 'TRACE' | 'SEARCH' | 'REPORT' | 'MKCALENDAR'
10
11export type HTTPMethods = Uppercase<_HTTPMethods> | Lowercase<_HTTPMethods>
12
13/**
14 * A union type of the Node.js server types from the http, https, and http2 modules.
15 */
16export type RawServerBase = http.Server | https.Server | http2.Http2Server | http2.Http2SecureServer
17
18/**
19 * The default server type
20 */
21export type RawServerDefault = http.Server
22
23/**
24 * The default request type based on the server type. Utilizes generic constraining.
25 */
26export type RawRequestDefaultExpression<
27 RawServer extends RawServerBase = RawServerDefault,
28> = RawServer extends http.Server | https.Server ? http.IncomingMessage
29 : RawServer extends http2.Http2Server | http2.Http2SecureServer ? http2.Http2ServerRequest
30 : never
31
32/**
33 * The default reply type based on the server type. Utilizes generic constraining.
34 */
35export type RawReplyDefaultExpression<
36 RawServer extends RawServerBase = RawServerDefault
37> = RawServer extends http.Server | https.Server ? http.ServerResponse
38 : RawServer extends http2.Http2Server | http2.Http2SecureServer ? http2.Http2ServerResponse
39 : never
40
41export type RequestBodyDefault = unknown
42export type RequestQuerystringDefault = unknown
43export type RequestParamsDefault = unknown
44export type RequestHeadersDefault = unknown
45
46export type ContextConfigDefault = unknown
47export type ReplyDefault = unknown
48
49/**
50 * Helpers for determining the type of the response payload based on the code
51 */
52
53type StringAsNumber<T extends string> = T extends `${infer N extends number}` ? N : never;
54type CodeClasses = 1 | 2 | 3 | 4 | 5;
55type Digit = 0 |1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
56type HttpCodes = StringAsNumber<`${CodeClasses}${Digit}${Digit}`>;
57type HttpKeys = HttpCodes | `${Digit}xx`;
58export type StatusCodeReply = {
59 // eslint-disable-next-line no-unused-vars
60 [Key in HttpKeys]?: unknown;
61};
62
63// weird TS quirk: https://stackoverflow.com/questions/58977876/generic-conditional-type-resolves-to-never-when-the-generic-type-is-set-to-never
64export type ReplyKeysToCodes<Key> = [Key] extends [never] ? number :
65 Key extends HttpCodes ? Key :
66 Key extends `${infer X extends CodeClasses}xx` ?
67 StringAsNumber<`${X}${Digit}${Digit}`> : number;
68
69export type CodeToReplyKey<Code extends number> = `${Code}` extends `${infer FirstDigit extends CodeClasses}${number}`
70 ? `${FirstDigit}xx`
71 : never;
72
73export type RecordKeysToLowercase<Input> = Input extends Record<string, unknown>
74 ? {
75 [Key in keyof Input as Key extends string
76 ? Lowercase<Key>
77 : Key
78 ]: Input[Key];
79 }
80 : Input;
81
82type OmitIndexSignature<T> = {
83 [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
84};
85
86/**
87 * HTTP header strings
88 * Use this type only for input values, not for output values.
89 */
90export type HttpHeader = keyof OmitIndexSignature<http.OutgoingHttpHeaders> | (string & Record<never, never>);
91
92// cheat for similar (same?) behavior as NoInfer but for TS <5.4
93export type NoInferCompat<SC> = {
94 [K in keyof SC]: SC[K]
95};