-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterfaces.ts
172 lines (154 loc) Β· 3.69 KB
/
interfaces.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import * as express from 'express'
import * as core from 'express-serve-static-core'
/**
* @public
*/
export type ClassOrMethodDecorator = <TFunction extends Function>(
target: TFunction | Object,
propertyKey?: string | symbol,
descriptor?: TypedPropertyDescriptor<any>
) => any
/**
* @public
*/
export type PropertyOrMethodDecorator = (
target: Object,
propertyKey: string | symbol,
descriptor?: TypedPropertyDescriptor<any>
) => any
/**
* @public
*/
export type ClassOrTypedMethodDecorator<T> = <TFunction extends Function>(
target: Object | TFunction,
propertyKey?: string | symbol,
descriptor?: MethodDescriptorReturn<T>
) => TFunction extends Function ? any : MethodDescriptorReturn<T> | void
/**
* {@linkcode TypedPropertyDescriptor}
* @public
*/
interface MethodDescriptorReturn<T> {
value?: (...args: any[]) => T
}
/**
* Defines a class type. Does the opposite of built-in `InstanceType`.
* @public
*/
export type ClassType = new (...args: any[]) => any
/**
* @example
* ```ts
* const routers: Registration[] = [
* ['/foo', Foo],
* ['/bar', Bar],
* new Baz(),
* ]
* register(app, routers)
* ```
* ------
* @public
*/
export type Registration = Registration.Class | Registration.Instance | Registration.Tuple
export namespace Registration {
/**
* @public
*/
export type Class = new () => any
/**
* @public
*/
export type Instance = object & NotFunction & NotArray
/**
* @public
*/
export type Tuple = [path: string | RegExp, router: Registration.Class | Registration.Instance | express.IRouter]
}
/**
* @public
*/
type NotFunction = { bind?(): never } | { call?(): never } | { apply?(): never }
/**
* @public
*/
type NotArray = { push?(): never } | { pop?(): never } | { shift?(): never } | { unshift?(): never }
/**
* @public
*/
export type IsAny<T> = 0 extends 1 & T ? true : false
/**
* @public
*/
export interface Handler<Req extends {} = {}> {
(
req: keyof Req extends undefined
? express.Request
: express.Request<
Req extends { params: infer P } ? P : core.ParamsDictionary,
any,
Req extends { body: infer B } ? B : any,
Req extends { query: infer Q } ? Q : core.Query
> &
Req,
res: express.Response,
next: express.NextFunction
): any
}
/**
* Only readonly properties and methods from response.
* @public
*/
export type ResponseReadonly = Pick<
express.Response,
| 'statusCode'
| 'statusMessage'
| 'locals'
| 'charset'
| 'headersSent'
| 'getHeader'
| 'getHeaders'
| 'getHeaderNames'
| 'hasHeader'
| 'finished'
| 'writableEnded'
| 'writableFinished'
>
declare module 'express' {
export interface Application {
_router: _Router
}
export type _Router = {
params: { [key: string]: any }
_params: any[]
caseSensitive: boolean
mergeParams: boolean | undefined
strict: boolean | undefined
stack: Layer[]
}
type Layer = {
// prevent ts4033 or ts2717 errors on build
handle:
| (import('express').RequestHandler & _Router)
| import('express').RequestHandler
| import('express').ErrorRequestHandler
name: string // '<anonymous>' | 'query' | 'expressInit' | 'bound dispatch' | 'router' | 'serveStatic' | 'jsonParser' | 'urlencodedParser'
params: { [key: string]: any } | undefined
path: string | undefined
keys: { name: string; optional: boolean; offset: number }[]
regexp: RegExp & { fast_star: boolean; fast_slash: boolean }
route: LayerRoute | undefined
}
type LayerRoute = {
path: string
methods: { [method: string]: true }
stack: {
handle: import('express').RequestHandler
name: string
params: { [key: string]: any } | undefined
path: string | undefined
keys: { name: string; optional: boolean; offset: number }[]
regexp: RegExp
method: string
}[]
}
}