-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathdefinition.ts
More file actions
290 lines (262 loc) · 9.15 KB
/
definition.ts
File metadata and controls
290 lines (262 loc) · 9.15 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import type { IUser } from '@rocket.chat/core-typings';
import type { Logger } from '@rocket.chat/logger';
import type { Method, MethodOf, OperationParams, OperationResult, PathPattern, UrlParams } from '@rocket.chat/rest-typings';
import type { ValidateFunction } from 'ajv';
import type { Request, Response } from 'express';
import type { ITwoFactorOptions } from '../../2fa/server/code';
export type SuccessResult<T> = {
statusCode: 200;
body: T extends object ? { success: true } & T : T;
};
export type FailureResult<T, TStack = undefined, TErrorType = undefined, TErrorDetails = undefined> = {
statusCode: 400;
body: T extends object
? { success: false } & T
: {
success: false;
error?: T;
stack?: TStack;
errorType?: TErrorType;
details?: TErrorDetails;
message?: string;
} & (undefined extends TErrorType ? object : { errorType: TErrorType }) &
(undefined extends TErrorDetails ? object : { details: TErrorDetails extends string ? unknown : TErrorDetails });
};
export type UnauthorizedResult<T> = {
statusCode: 401;
body: {
success: false;
error: T | 'unauthorized';
};
};
export type ForbiddenResult<T> = {
statusCode: 403;
body: {
success: false;
// TODO: MAJOR remove 'unauthorized'
error: T | 'forbidden' | 'unauthorized';
};
};
export type InternalError<T> = {
statusCode: 500;
body: {
error: T | 'Internal server error';
success: false;
};
};
export type NotFoundResult<T = string> = {
statusCode: 404;
body: {
success: false;
error: T;
};
};
export type TOperation = 'hasAll' | 'hasAny';
export type NonEnterpriseTwoFactorOptions = {
authRequired: true;
forceTwoFactorAuthenticationForNonEnterprise: true;
twoFactorRequired: true;
permissionsRequired?: string[] | { [key in Method]: string[] } | { [key in Method]: { operation: TOperation; permissions: string[] } };
twoFactorOptions: ITwoFactorOptions;
};
export type Options = (
| {
permissionsRequired?:
| string[]
| ({ [key in Method]?: string[] } & { '*'?: string[] })
| ({ [key in Method]?: { operation: TOperation; permissions: string[] } } & {
'*'?: { operation: TOperation; permissions: string[] };
});
authRequired?: boolean;
forceTwoFactorAuthenticationForNonEnterprise?: boolean;
rateLimiterOptions?:
| {
numRequestsAllowed?: number;
intervalTimeInMS?: number;
}
| boolean;
queryOperations?: string[];
queryFields?: string[];
}
| {
permissionsRequired?:
| string[]
| ({ [key in Method]?: string[] } & { '*'?: string[] })
| ({ [key in Method]?: { operation: TOperation; permissions: string[] } } & {
'*'?: { operation: TOperation; permissions: string[] };
});
authRequired: true;
twoFactorRequired: true;
twoFactorOptions?: ITwoFactorOptions;
rateLimiterOptions?:
| {
numRequestsAllowed?: number;
intervalTimeInMS?: number;
}
| boolean;
queryOperations?: string[];
queryFields?: string[];
}
) & {
validateParams?: ValidateFunction | { [key in Method]?: ValidateFunction };
authOrAnonRequired?: true;
deprecation?: {
version: string;
alternatives?: string[];
};
};
export type PartialThis = {
readonly request: Request & { query: Record<string, string> };
readonly response: Response;
readonly userId: string;
readonly bodyParams: Record<string, unknown>;
readonly queryParams: Record<string, string>;
readonly queryOperations?: string[];
readonly queryFields?: string[];
readonly logger: Logger;
};
type ActionThis<TMethod extends Method, TPathPattern extends PathPattern, TOptions> = {
readonly requestIp: string;
urlParams: UrlParams<TPathPattern>;
readonly response: Response;
// TODO make it unsafe
readonly queryParams: TMethod extends 'GET'
? TOptions extends { validateParams: ValidateFunction<infer T> }
? T
: TOptions extends { validateParams: { GET: ValidateFunction<infer T> } }
? T
: Partial<OperationParams<TMethod, TPathPattern>> & { offset?: number; count?: number }
: Record<string, string>;
// TODO make it unsafe
readonly bodyParams: TMethod extends 'GET'
? Record<string, unknown>
: TOptions extends { validateParams: ValidateFunction<infer T> }
? T
: TOptions extends { validateParams: infer V }
? V extends { [key in TMethod]: ValidateFunction<infer T> }
? T
: Partial<OperationParams<TMethod, TPathPattern>>
: // TODO remove the extra (optionals) params when all the endpoints that use these are typed correctly
Partial<OperationParams<TMethod, TPathPattern>>;
readonly request: Request;
readonly queryOperations: TOptions extends { queryOperations: infer T } ? T : never;
parseJsonQuery(): Promise<{
sort: Record<string, 1 | -1>;
/**
* @deprecated To access "fields" parameter, use ALLOW_UNSAFE_QUERY_AND_FIELDS_API_PARAMS environment variable.
*/
fields: Record<string, 0 | 1>;
/**
* @deprecated To access "query" parameter, use ALLOW_UNSAFE_QUERY_AND_FIELDS_API_PARAMS environment variable.
*/
query: Record<string, unknown>;
}>;
} & (TOptions extends { authRequired: true }
? {
user: IUser;
userId: string;
readonly token: string;
}
: TOptions extends { authOrAnonRequired: true }
? {
user?: IUser;
userId?: string;
readonly token?: string;
}
: {
user?: IUser | null;
userId?: string | undefined;
readonly token?: string;
});
export type ResultFor<TMethod extends Method, TPathPattern extends PathPattern> =
| SuccessResult<OperationResult<TMethod, TPathPattern>>
| FailureResult<unknown, unknown, unknown, unknown>
| UnauthorizedResult<unknown>
| NotFoundResult
| {
statusCode: number;
body: unknown;
};
export type Action<TMethod extends Method, TPathPattern extends PathPattern, TOptions> =
| ((this: ActionThis<TMethod, TPathPattern, TOptions>) => Promise<ResultFor<TMethod, TPathPattern>>)
| ((this: ActionThis<TMethod, TPathPattern, TOptions>) => ResultFor<TMethod, TPathPattern>);
export type InnerAction<TMethod extends Method, TPathPattern extends PathPattern, TOptions> =
Action<TMethod, TPathPattern, TOptions> extends (this: infer This) => infer Result
? This extends ActionThis<TMethod, TPathPattern, TOptions>
? (this: Mutable<This>) => Result
: never
: never;
type Mutable<Immutable> = {
-readonly [key in keyof Immutable]: Immutable[key];
};
type Operation<TMethod extends Method, TPathPattern extends PathPattern, TEndpointOptions> =
| Action<TMethod, TPathPattern, TEndpointOptions>
| ({
action: Action<TMethod, TPathPattern, TEndpointOptions>;
} & { twoFactorRequired: boolean });
type ActionOperation<TMethod extends Method, TPathPattern extends PathPattern, TEndpointOptions extends Options = object> = {
action: Action<TMethod, TPathPattern, TEndpointOptions>;
} & TEndpointOptions;
export type Operations<TPathPattern extends PathPattern, TOptions extends Options = object> = {
[M in MethodOf<TPathPattern> as Lowercase<M>]: Operation<Uppercase<M>, TPathPattern, TOptions>;
};
export type ActionOperations<TPathPattern extends PathPattern, TOptions extends Options = object> = {
[M in MethodOf<TPathPattern> as Lowercase<M>]: ActionOperation<Uppercase<M>, TPathPattern, TOptions>;
};
export type TypedOptions = {
response: {
200: ValidateFunction;
300?: ValidateFunction;
400?: ValidateFunction;
401?: ValidateFunction;
403?: ValidateFunction;
404?: ValidateFunction;
500?: ValidateFunction;
};
query?: ValidateFunction;
body?: ValidateFunction;
tags?: string[];
typed?: boolean;
} & Options;
export type TypedThis<TOptions extends TypedOptions, TPath extends string = ''> = {
userId: TOptions['authRequired'] extends true ? string : string | undefined;
token: TOptions['authRequired'] extends true ? string : string | undefined;
queryParams: TOptions['query'] extends ValidateFunction<infer Query> ? Query : never;
urlParams: UrlParams<TPath> extends Record<any, any> ? UrlParams<TPath> : never;
parseJsonQuery(): Promise<{
sort: Record<string, 1 | -1>;
/**
* @deprecated To access "fields" parameter, use ALLOW_UNSAFE_QUERY_AND_FIELDS_API_PARAMS environment variable.
*/
fields: Record<string, 0 | 1>;
/**
* @deprecated To access "query" parameter, use ALLOW_UNSAFE_QUERY_AND_FIELDS_API_PARAMS environment variable.
*/
query: Record<string, unknown>;
}>;
bodyParams: TOptions['body'] extends ValidateFunction<infer Body> ? Body : never;
requestIp?: string;
};
type PromiseOrValue<T> = T | Promise<T>;
type InferResult<TResult> = TResult extends ValidateFunction<infer T> ? T : TResult;
type Results<TResponse extends TypedOptions['response']> = {
[K in keyof TResponse]: K extends 200
? SuccessResult<InferResult<TResponse[200]>>
: K extends 400
? FailureResult<InferResult<TResponse[400]>>
: K extends 401
? UnauthorizedResult<InferResult<TResponse[401]>>
: K extends 403
? ForbiddenResult<InferResult<TResponse[403]>>
: K extends 404
? NotFoundResult<InferResult<TResponse[404]>>
: K extends 500
? InternalError<InferResult<TResponse[500]>>
: never;
}[keyof TResponse] & {
headers?: Record<string, string>;
};
export type TypedAction<TOptions extends TypedOptions, TPath extends string = ''> = (
this: TypedThis<TOptions, TPath>,
request: Request,
) => PromiseOrValue<Results<TOptions['response']>>;