@deepkit/http 1.0.1-alpha.100
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +13 -0
- package/dist/.gitkeep +0 -0
- package/dist/cjs/index.d.ts +14 -0
- package/dist/cjs/index.js +31 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/src/controllers.d.ts +13 -0
- package/dist/cjs/src/controllers.js +17 -0
- package/dist/cjs/src/controllers.js.map +1 -0
- package/dist/cjs/src/decorator.d.ts +241 -0
- package/dist/cjs/src/decorator.js +422 -0
- package/dist/cjs/src/decorator.js.map +1 -0
- package/dist/cjs/src/filter.d.ts +49 -0
- package/dist/cjs/src/filter.js +215 -0
- package/dist/cjs/src/filter.js.map +1 -0
- package/dist/cjs/src/http-app.d.ts +8 -0
- package/dist/cjs/src/http-app.js +16 -0
- package/dist/cjs/src/http-app.js.map +1 -0
- package/dist/cjs/src/http.d.ts +278 -0
- package/dist/cjs/src/http.js +878 -0
- package/dist/cjs/src/http.js.map +1 -0
- package/dist/cjs/src/kernel.d.ts +16 -0
- package/dist/cjs/src/kernel.js +79 -0
- package/dist/cjs/src/kernel.js.map +1 -0
- package/dist/cjs/src/logger.d.ts +7 -0
- package/dist/cjs/src/logger.js +34 -0
- package/dist/cjs/src/logger.js.map +1 -0
- package/dist/cjs/src/middleware.d.ts +61 -0
- package/dist/cjs/src/middleware.js +95 -0
- package/dist/cjs/src/middleware.js.map +1 -0
- package/dist/cjs/src/model.d.ts +165 -0
- package/dist/cjs/src/model.js +398 -0
- package/dist/cjs/src/model.js.map +1 -0
- package/dist/cjs/src/module.config.d.ts +84 -0
- package/dist/cjs/src/module.config.js +14 -0
- package/dist/cjs/src/module.config.js.map +1 -0
- package/dist/cjs/src/module.d.ts +14 -0
- package/dist/cjs/src/module.js +124 -0
- package/dist/cjs/src/module.js.map +1 -0
- package/dist/cjs/src/request-parser.d.ts +36 -0
- package/dist/cjs/src/request-parser.js +305 -0
- package/dist/cjs/src/request-parser.js.map +1 -0
- package/dist/cjs/src/router.d.ts +230 -0
- package/dist/cjs/src/router.js +617 -0
- package/dist/cjs/src/router.js.map +1 -0
- package/dist/cjs/src/static-serving.d.ts +32 -0
- package/dist/cjs/src/static-serving.js +157 -0
- package/dist/cjs/src/static-serving.js.map +1 -0
- package/dist/cjs/src/utils.d.ts +1 -0
- package/dist/cjs/src/utils.js +13 -0
- package/dist/cjs/src/utils.js.map +1 -0
- package/dist/cjs/tsconfig.tsbuildinfo +1 -0
- package/dist/esm/index.d.ts +14 -0
- package/dist/esm/index.js +15 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/src/controllers.d.ts +13 -0
- package/dist/esm/src/controllers.js +13 -0
- package/dist/esm/src/controllers.js.map +1 -0
- package/dist/esm/src/decorator.d.ts +241 -0
- package/dist/esm/src/decorator.js +414 -0
- package/dist/esm/src/decorator.js.map +1 -0
- package/dist/esm/src/filter.d.ts +49 -0
- package/dist/esm/src/filter.js +209 -0
- package/dist/esm/src/filter.js.map +1 -0
- package/dist/esm/src/http-app.d.ts +8 -0
- package/dist/esm/src/http-app.js +12 -0
- package/dist/esm/src/http-app.js.map +1 -0
- package/dist/esm/src/http.d.ts +278 -0
- package/dist/esm/src/http.js +841 -0
- package/dist/esm/src/http.js.map +1 -0
- package/dist/esm/src/kernel.d.ts +16 -0
- package/dist/esm/src/kernel.js +75 -0
- package/dist/esm/src/kernel.js.map +1 -0
- package/dist/esm/src/logger.d.ts +7 -0
- package/dist/esm/src/logger.js +30 -0
- package/dist/esm/src/logger.js.map +1 -0
- package/dist/esm/src/middleware.d.ts +61 -0
- package/dist/esm/src/middleware.js +90 -0
- package/dist/esm/src/middleware.js.map +1 -0
- package/dist/esm/src/model.d.ts +165 -0
- package/dist/esm/src/model.js +363 -0
- package/dist/esm/src/model.js.map +1 -0
- package/dist/esm/src/module.config.d.ts +84 -0
- package/dist/esm/src/module.config.js +10 -0
- package/dist/esm/src/module.config.js.map +1 -0
- package/dist/esm/src/module.d.ts +14 -0
- package/dist/esm/src/module.js +120 -0
- package/dist/esm/src/module.js.map +1 -0
- package/dist/esm/src/request-parser.d.ts +36 -0
- package/dist/esm/src/request-parser.js +295 -0
- package/dist/esm/src/request-parser.js.map +1 -0
- package/dist/esm/src/router.d.ts +230 -0
- package/dist/esm/src/router.js +605 -0
- package/dist/esm/src/router.js.map +1 -0
- package/dist/esm/src/static-serving.d.ts +32 -0
- package/dist/esm/src/static-serving.js +149 -0
- package/dist/esm/src/static-serving.js.map +1 -0
- package/dist/esm/src/utils.d.ts +1 -0
- package/dist/esm/src/utils.js +9 -0
- package/dist/esm/src/utils.js.map +1 -0
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -0
- package/index.ts +14 -0
- package/package.json +67 -0
- package/src/controllers.ts +11 -0
- package/src/decorator.ts +411 -0
- package/src/filter.ts +224 -0
- package/src/http-app.ts +11 -0
- package/src/http.ts +828 -0
- package/src/kernel.ts +79 -0
- package/src/logger.ts +20 -0
- package/src/middleware.ts +116 -0
- package/src/model.ts +392 -0
- package/src/module.config.ts +95 -0
- package/src/module.ts +116 -0
- package/src/request-parser.ts +346 -0
- package/src/router.ts +747 -0
- package/src/static-serving.ts +153 -0
- package/src/utils.ts +5 -0
- package/tsconfig.esm.json +36 -0
- package/tsconfig.json +53 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ClassType, CompilerContext } from '@deepkit/core';
|
|
2
|
+
import { InjectorModule } from '@deepkit/injector';
|
|
3
|
+
import { ReflectionParameter, Type } from '@deepkit/type';
|
|
4
|
+
import { HttpRequest } from './model.js';
|
|
5
|
+
import { RouteConfig } from './router.js';
|
|
6
|
+
import { HttpParserOptions } from './module.config.js';
|
|
7
|
+
export declare class ParameterForRequestParser {
|
|
8
|
+
parameter: ReflectionParameter;
|
|
9
|
+
regexPosition?: number;
|
|
10
|
+
constructor(parameter: ReflectionParameter);
|
|
11
|
+
get body(): boolean;
|
|
12
|
+
get bodyValidation(): boolean;
|
|
13
|
+
getType(): Type;
|
|
14
|
+
get header(): boolean;
|
|
15
|
+
get query(): boolean;
|
|
16
|
+
get queries(): boolean;
|
|
17
|
+
get typePath(): string | undefined;
|
|
18
|
+
getName(): string;
|
|
19
|
+
isPartOfPath(): boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare function parseRoutePathToRegex(path: string, params: ReflectionParameter[]): {
|
|
22
|
+
regex: string;
|
|
23
|
+
parameterNames: {
|
|
24
|
+
[name: string]: number;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
export declare function buildRequestParser(parseOptions: HttpParserOptions, parameters: ReflectionParameter[], path?: string): (request: HttpRequest) => any[];
|
|
28
|
+
export declare function getRequestParserCodeForParameters(compiler: CompilerContext, parseOptions: HttpParserOptions, parameters: ParameterForRequestParser[], config: {
|
|
29
|
+
module?: InjectorModule<any>;
|
|
30
|
+
resolverForParameterName?: Map<string, ClassType>;
|
|
31
|
+
resolverForToken?: Map<any, ClassType>;
|
|
32
|
+
pathParameterNames?: {
|
|
33
|
+
[name: string]: number;
|
|
34
|
+
};
|
|
35
|
+
routeConfig?: RouteConfig;
|
|
36
|
+
}): string;
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
/*@ts-ignore*/
|
|
2
|
+
import { __ΩHttpParserOptions as __ΩHttpParserOptions } from './module.config.js';
|
|
3
|
+
/*@ts-ignore*/
|
|
4
|
+
import { __ΩClassType as __ΩClassType } from '@deepkit/core';
|
|
5
|
+
function __assignType(fn, args) {
|
|
6
|
+
fn.__type = args;
|
|
7
|
+
return fn;
|
|
8
|
+
}
|
|
9
|
+
import { asyncOperation, CompilerContext, getClassName, isObject } from '@deepkit/core';
|
|
10
|
+
import { InjectorModule } from '@deepkit/injector';
|
|
11
|
+
import { assertType, findMember, getSerializeFunction, getValidatorFunction, metaAnnotation, ReflectionKind, ReflectionParameter, resolveReceiveType, serializer, stringifyType, typeToObject, ValidationError } from '@deepkit/type';
|
|
12
|
+
import { BodyValidationError, createRequestWithCachedBody, getRegExp, HttpRequest, ValidatedBody } from './model.js';
|
|
13
|
+
import { RouteConfig, UploadedFile, UploadedFileSymbol } from './router.js';
|
|
14
|
+
//@ts-ignore
|
|
15
|
+
import qs from 'qs';
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
import formidable from 'formidable';
|
|
18
|
+
function parseBody(options, req, foundFiles) {
|
|
19
|
+
const form = formidable(Object.assign(options, {
|
|
20
|
+
multiples: true,
|
|
21
|
+
}));
|
|
22
|
+
return asyncOperation(__assignType((resolve, reject) => {
|
|
23
|
+
function parseData(err, fields, files) {
|
|
24
|
+
if (err) {
|
|
25
|
+
reject(err);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
for (const [name, file] of Object.entries(files)) {
|
|
29
|
+
if (file.size === 0)
|
|
30
|
+
continue;
|
|
31
|
+
foundFiles[name] = {
|
|
32
|
+
validator: UploadedFileSymbol,
|
|
33
|
+
size: file.size,
|
|
34
|
+
path: file.filepath,
|
|
35
|
+
name: file.originalFilename,
|
|
36
|
+
type: file.mimetype,
|
|
37
|
+
lastModifiedDate: file.lastModifiedDate,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
const body = { ...fields, ...foundFiles };
|
|
41
|
+
resolve(body);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
parseData.__type = ['err', 'fields', 'files', 'parseData', 'P"2!"2""2#"/$'];
|
|
45
|
+
if (req.body) {
|
|
46
|
+
form.parse(createRequestWithCachedBody(req, req.body), parseData);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const chunks = [];
|
|
50
|
+
function read(chunk) {
|
|
51
|
+
chunks.push(chunk);
|
|
52
|
+
}
|
|
53
|
+
read.__type = ['chunk', 'read', 'P!2!"/"'];
|
|
54
|
+
req.on('data', read);
|
|
55
|
+
req.once('end', () => {
|
|
56
|
+
req.body = Buffer.concat(chunks);
|
|
57
|
+
req.off('data', read);
|
|
58
|
+
});
|
|
59
|
+
req.once('error', () => req.off('data', read));
|
|
60
|
+
form.parse(req, parseData);
|
|
61
|
+
}, ['resolve', 'reject', '', 'P"2!"2""/#']));
|
|
62
|
+
}
|
|
63
|
+
parseBody.__type = [() => __ΩHttpParserOptions, 'options', () => HttpRequest, 'req', () => UploadedFile, 'foundFiles', 'parseBody', 'Pn!2"P7#2$P&P7%LM2&"/\''];
|
|
64
|
+
export class ParameterForRequestParser {
|
|
65
|
+
constructor(parameter) {
|
|
66
|
+
this.parameter = parameter;
|
|
67
|
+
}
|
|
68
|
+
get body() {
|
|
69
|
+
return metaAnnotation.getForName(this.parameter.type, 'httpBody') !== undefined;
|
|
70
|
+
}
|
|
71
|
+
get bodyValidation() {
|
|
72
|
+
return metaAnnotation.getForName(this.parameter.type, 'httpBodyValidation') !== undefined;
|
|
73
|
+
}
|
|
74
|
+
getType() {
|
|
75
|
+
if (this.bodyValidation) {
|
|
76
|
+
assertType(this.parameter.type, ReflectionKind.class);
|
|
77
|
+
const valueType = findMember('value', this.parameter.type.types);
|
|
78
|
+
if (!valueType || valueType.kind !== ReflectionKind.property)
|
|
79
|
+
throw new Error(`No property value found at ${stringifyType(this.parameter.type)}`);
|
|
80
|
+
return valueType.type;
|
|
81
|
+
}
|
|
82
|
+
return this.parameter.type;
|
|
83
|
+
}
|
|
84
|
+
get header() {
|
|
85
|
+
return metaAnnotation.getForName(this.parameter.type, 'httpHeader') !== undefined;
|
|
86
|
+
}
|
|
87
|
+
get query() {
|
|
88
|
+
return metaAnnotation.getForName(this.parameter.type, 'httpQuery') !== undefined;
|
|
89
|
+
}
|
|
90
|
+
get queries() {
|
|
91
|
+
return metaAnnotation.getForName(this.parameter.type, 'httpQueries') !== undefined;
|
|
92
|
+
}
|
|
93
|
+
get typePath() {
|
|
94
|
+
const typeOptions = metaAnnotation.getForName(this.parameter.type, 'httpQueries') || metaAnnotation.getForName(this.parameter.type, 'httpQuery')
|
|
95
|
+
|| metaAnnotation.getForName(this.parameter.type, 'httpPath') || metaAnnotation.getForName(this.parameter.type, 'httpHeader');
|
|
96
|
+
if (!typeOptions)
|
|
97
|
+
return;
|
|
98
|
+
const options = typeToObject(typeOptions[0]);
|
|
99
|
+
if (isObject(options))
|
|
100
|
+
return options.name;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
getName() {
|
|
104
|
+
return this.parameter.name;
|
|
105
|
+
}
|
|
106
|
+
isPartOfPath() {
|
|
107
|
+
return metaAnnotation.getForName(this.parameter.type, 'httpPath') !== undefined || this.regexPosition !== undefined;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
ParameterForRequestParser.__type = ['regexPosition', () => ReflectionParameter, 'parameter', 'constructor', 'getType', 'getName', 'isPartOfPath', '\'3!8PP7"2#:"0$!!P"0%!!!!P"0&P)0\'5'];
|
|
111
|
+
export function parseRoutePathToRegex(path, params) {
|
|
112
|
+
const parameterNames = {};
|
|
113
|
+
let argumentIndex = 0;
|
|
114
|
+
path = path.replace(/:(\w+)/g, __assignType((a, name) => {
|
|
115
|
+
parameterNames[name] = argumentIndex;
|
|
116
|
+
argumentIndex++;
|
|
117
|
+
const parameter = params.find(__assignType(v => v.name === name, ['v', '', 'P"2!"/"']));
|
|
118
|
+
if (parameter) {
|
|
119
|
+
const regExp = getRegExp(parameter.type);
|
|
120
|
+
if (regExp instanceof RegExp) {
|
|
121
|
+
return '(' + regExp.source + ')';
|
|
122
|
+
}
|
|
123
|
+
else if (regExp) {
|
|
124
|
+
return '(' + regExp + ')';
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return String.raw `([^/]+)`;
|
|
128
|
+
}, ['a', 'name', '', 'P"2!"2""/#']));
|
|
129
|
+
return { regex: path, parameterNames };
|
|
130
|
+
}
|
|
131
|
+
parseRoutePathToRegex.__type = ['path', () => ReflectionParameter, 'params', 'regex', 'parameterNames', 'parseRoutePathToRegex', 'P&2!P7"F2#P&4$P&\'LM4%M/&'];
|
|
132
|
+
export function buildRequestParser(parseOptions, parameters, path) {
|
|
133
|
+
const compiler = new CompilerContext();
|
|
134
|
+
const params = parameters.map(__assignType(v => new ParameterForRequestParser(v), ['v', '', 'P"2!"/"']));
|
|
135
|
+
//todo: parse path
|
|
136
|
+
let pathRegex = '';
|
|
137
|
+
if (path) {
|
|
138
|
+
const parsedPath = parseRoutePathToRegex(path, parameters);
|
|
139
|
+
pathRegex = parsedPath.regex;
|
|
140
|
+
for (const param of params) {
|
|
141
|
+
param.regexPosition = parsedPath.parameterNames[param.parameter.name];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const code = getRequestParserCodeForParameters(compiler, parseOptions, params, {});
|
|
145
|
+
compiler.context.set('ValidationError', ValidationError);
|
|
146
|
+
compiler.context.set('qs', qs);
|
|
147
|
+
let needsQueryString = !!params.find(__assignType(v => v.query || v.queries, ['v', '', 'P"2!"/"']));
|
|
148
|
+
const query = needsQueryString ? '_qPosition === -1 ? {} : qs.parse(_url.substr(_qPosition + 1))' : '{}';
|
|
149
|
+
const regexVar = compiler.reserveVariable('regex', new RegExp('^' + pathRegex + '$'));
|
|
150
|
+
return compiler.build(`
|
|
151
|
+
const _method = request.method || 'GET';
|
|
152
|
+
const _url = request.url || '/';
|
|
153
|
+
const _headers = request.headers || {};
|
|
154
|
+
const _qPosition = _url.indexOf('?');
|
|
155
|
+
let uploadedFiles = {};
|
|
156
|
+
const _path = _qPosition === -1 ? _url : _url.substr(0, _qPosition);
|
|
157
|
+
const _match = _path.match(${regexVar}) || [];
|
|
158
|
+
const _query = ${query};
|
|
159
|
+
return ${code}
|
|
160
|
+
`, 'request');
|
|
161
|
+
}
|
|
162
|
+
buildRequestParser.__type = [() => __ΩHttpParserOptions, 'parseOptions', () => ReflectionParameter, 'parameters', 'path', () => HttpRequest, 'request', '', 'buildRequestParser', 'Pn!2"P7#F2$&2%8PP7&2\'"F/(/)'];
|
|
163
|
+
export function getRequestParserCodeForParameters(compiler, parseOptions, parameters, config) {
|
|
164
|
+
var _a, _b;
|
|
165
|
+
let enableParseBody = false;
|
|
166
|
+
let requiresAsyncParameters = false;
|
|
167
|
+
const setParameters = [];
|
|
168
|
+
const parameterNames = [];
|
|
169
|
+
const parameterValidator = [];
|
|
170
|
+
let setParametersFromPath = '';
|
|
171
|
+
let bodyValidationErrorHandling = `if (bodyErrors.length) throw ValidationError.from(bodyErrors);`;
|
|
172
|
+
for (const parameter of parameters) {
|
|
173
|
+
if (parameter.body || parameter.bodyValidation) {
|
|
174
|
+
const type = parameter.getType();
|
|
175
|
+
const validatorVar = compiler.reserveVariable('argumentValidator', getValidatorFunction(undefined, type));
|
|
176
|
+
const converterVar = compiler.reserveVariable('argumentConverter', getSerializeFunction(type, serializer.deserializeRegistry));
|
|
177
|
+
enableParseBody = true;
|
|
178
|
+
setParameters.push(`parameters.${parameter.parameter.name} = ${converterVar}(bodyFields, {loosely: true});`);
|
|
179
|
+
parameterValidator.push(`${validatorVar}(parameters.${parameter.parameter.name}, {errors: bodyErrors});`);
|
|
180
|
+
if (parameter.bodyValidation) {
|
|
181
|
+
compiler.context.set('BodyValidation', ValidatedBody);
|
|
182
|
+
compiler.context.set('BodyValidationError', BodyValidationError);
|
|
183
|
+
parameterNames.push(`new BodyValidation(new BodyValidationError(bodyErrors), bodyErrors.length === 0 ? parameters.${parameter.parameter.name} : undefined)`);
|
|
184
|
+
bodyValidationErrorHandling = '';
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
parameterNames.push(`parameters.${parameter.parameter.name}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
else if (parameter.query || parameter.queries || parameter.header) {
|
|
191
|
+
const converted = getSerializeFunction(parameter.parameter.parameter, serializer.deserializeRegistry, undefined, parameter.getName());
|
|
192
|
+
const validator = getValidatorFunction(undefined, parameter.parameter.parameter);
|
|
193
|
+
const converterVar = compiler.reserveVariable('argumentConverter', converted);
|
|
194
|
+
const validatorVar = compiler.reserveVariable('argumentValidator', validator);
|
|
195
|
+
const queryPath = parameter.typePath === undefined && !parameter.queries ? parameter.parameter.name : parameter.typePath;
|
|
196
|
+
const accessor = queryPath ? `['` + (queryPath.replace(/\./g, `']['`)) + `']` : '';
|
|
197
|
+
const queryAccessor = parameter.header ? `_headers${accessor}` : queryPath ? `_query${accessor}` : '_query';
|
|
198
|
+
setParameters.push(`parameters.${parameter.parameter.name} = ${converterVar}(${queryAccessor}, {loosely: true});`);
|
|
199
|
+
parameterNames.push(`parameters.${parameter.parameter.name}`);
|
|
200
|
+
parameterValidator.push(`${validatorVar}(parameters.${parameter.parameter.name}, {errors: validationErrors}, ${JSON.stringify(parameter.typePath || parameter.getName())});`);
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
parameterNames.push(`parameters.${parameter.parameter.name}`);
|
|
204
|
+
if (parameter.isPartOfPath()) {
|
|
205
|
+
if (parameter.parameter.type.kind !== ReflectionKind.class) {
|
|
206
|
+
const converted = getSerializeFunction(parameter.parameter.parameter, serializer.deserializeRegistry, undefined, parameter.getName());
|
|
207
|
+
const converterVar = compiler.reserveVariable('argumentConverter', converted);
|
|
208
|
+
setParameters.push(`parameters.${parameter.parameter.name} = ${converterVar}(_match[${1 + (parameter.regexPosition || 0)}], {loosely: true});`);
|
|
209
|
+
const validator = getValidatorFunction(undefined, parameter.parameter.parameter);
|
|
210
|
+
const validatorVar = compiler.reserveVariable('argumentValidator', validator);
|
|
211
|
+
parameterValidator.push(`${validatorVar}(parameters.${parameter.parameter.name}, {errors: validationErrors}, ${JSON.stringify(parameter.getName())});`);
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
setParameters.push(`parameters.${parameter.parameter.name} = _match[${1 + (parameter.regexPosition || 0)}];`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const injectorToken = parameter.parameter.type.kind === ReflectionKind.class ? parameter.parameter.type.classType : undefined;
|
|
218
|
+
const injectorTokenVar = compiler.reserveVariable('classType', injectorToken);
|
|
219
|
+
const parameterResolverFoundVar = compiler.reserveVariable('parameterResolverFound', false);
|
|
220
|
+
setParameters.push(`${parameterResolverFoundVar} = false;`);
|
|
221
|
+
const resolverType = ((_a = config.resolverForParameterName) === null || _a === void 0 ? void 0 : _a.get(parameter.getName())) || ((_b = config.resolverForToken) === null || _b === void 0 ? void 0 : _b.get(injectorToken));
|
|
222
|
+
//make sure all parameter values from the path are available, important for parameter resolver
|
|
223
|
+
if (resolverType && !setParametersFromPath && config.pathParameterNames) {
|
|
224
|
+
for (const i in config.pathParameterNames) {
|
|
225
|
+
setParametersFromPath += `parameters.${i} = _match[${1 + config.pathParameterNames[i]}];`;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
let injector = '_injector';
|
|
229
|
+
const moduleVar = config.module ? ', ' + compiler.reserveConst(config.module, 'module') : '';
|
|
230
|
+
if (resolverType) {
|
|
231
|
+
requiresAsyncParameters = true;
|
|
232
|
+
let instanceFetcher = '';
|
|
233
|
+
if (config.module && config.module.injector) {
|
|
234
|
+
const resolverResolverVar = compiler.reserveVariable('resolverProvideToken', config.module.injector.createResolver(resolveReceiveType(resolverType)));
|
|
235
|
+
instanceFetcher = `${resolverResolverVar}(${injector}.scope)`;
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
const resolverProvideTokenVar = compiler.reserveVariable('resolverProvideToken', resolverType);
|
|
239
|
+
instanceFetcher = `${injector}.get(${resolverProvideTokenVar}${moduleVar})`;
|
|
240
|
+
}
|
|
241
|
+
const instance = compiler.reserveVariable('resolverInstance');
|
|
242
|
+
const routeConfigVar = compiler.reserveVariable('routeConfigVar', config.routeConfig);
|
|
243
|
+
setParameters.push(`
|
|
244
|
+
//resolver ${getClassName(resolverType)} for ${parameter.getName()}
|
|
245
|
+
${instance} = ${instanceFetcher};
|
|
246
|
+
if (!${parameterResolverFoundVar}) {
|
|
247
|
+
${parameterResolverFoundVar} = true;
|
|
248
|
+
parameters.${parameter.parameter.name} = await ${instance}.resolve({
|
|
249
|
+
token: ${injectorTokenVar},
|
|
250
|
+
route: ${routeConfigVar},
|
|
251
|
+
request: request,
|
|
252
|
+
name: ${JSON.stringify(parameter.parameter.name)},
|
|
253
|
+
value: parameters.${parameter.parameter.name},
|
|
254
|
+
query: _query,
|
|
255
|
+
parameters: parameters,
|
|
256
|
+
type: ${compiler.reserveVariable('parameterType', parameter.parameter)}
|
|
257
|
+
});
|
|
258
|
+
}`);
|
|
259
|
+
}
|
|
260
|
+
if (!parameter.isPartOfPath()) {
|
|
261
|
+
let injectorGet = `parameters.${parameter.parameter.name} = ${injector}.get(${injectorTokenVar});`;
|
|
262
|
+
if (parameter.parameter.isOptional()) {
|
|
263
|
+
injectorGet = `try {parameters.${parameter.parameter.name} = ${injector}.get(${injectorTokenVar}); } catch (e) {}`;
|
|
264
|
+
}
|
|
265
|
+
setParameters.push(`if (!${parameterResolverFoundVar}) ${injectorGet}`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
let parseBodyLoading = '';
|
|
270
|
+
if (enableParseBody) {
|
|
271
|
+
const parseOptionsVar = compiler.reserveVariable('parseOptions', parseOptions);
|
|
272
|
+
const parseBodyVar = compiler.reserveVariable('parseBody', parseBody);
|
|
273
|
+
parseBodyLoading = `
|
|
274
|
+
const bodyFields = (await ${parseBodyVar}(${parseOptionsVar}, request, uploadedFiles));`;
|
|
275
|
+
requiresAsyncParameters = true;
|
|
276
|
+
}
|
|
277
|
+
let parametersLoader = '() => {}';
|
|
278
|
+
if (setParameters.length) {
|
|
279
|
+
parametersLoader = `${requiresAsyncParameters ? 'async' : ''} function parse(_injector){
|
|
280
|
+
const validationErrors = [];
|
|
281
|
+
const bodyErrors = [];
|
|
282
|
+
const parameters = {};
|
|
283
|
+
${setParametersFromPath}
|
|
284
|
+
${parseBodyLoading}
|
|
285
|
+
${setParameters.join('\n')}
|
|
286
|
+
${parameterValidator.join('\n')}
|
|
287
|
+
${bodyValidationErrorHandling}
|
|
288
|
+
if (validationErrors.length) throw new ValidationError(validationErrors);
|
|
289
|
+
return {arguments: [${parameterNames.join(',')}], parameters: parameters};
|
|
290
|
+
}`;
|
|
291
|
+
}
|
|
292
|
+
return parametersLoader;
|
|
293
|
+
}
|
|
294
|
+
getRequestParserCodeForParameters.__type = [() => CompilerContext, 'compiler', () => __ΩHttpParserOptions, 'parseOptions', () => ParameterForRequestParser, 'parameters', () => InjectorModule, 'module', () => __ΩClassType, 'resolverForParameterName', () => __ΩClassType, 'resolverForToken', 'pathParameterNames', () => RouteConfig, 'routeConfig', 'config', 'getRequestParserCodeForParameters', 'PP7!2"n#2$P7%F2&PP"7\'4(8&n)E4*8"n+E4,8P&\'LM4-8P7.4/8M20"/1'];
|
|
295
|
+
//# sourceMappingURL=request-parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-parser.js","sourceRoot":"","sources":["../../../src/request-parser.ts"],"names":[],"mappings":";6DAyBkC,oBAAoB;;6CAzB6B,eAAe;;;;;AAAlG,OAAO,EAAE,cAAc,EAAa,eAAe,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACnG,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EACH,UAAU,EACV,UAAU,EACV,oBAAoB,EACpB,oBAAoB,EACpB,cAAc,EACd,cAAc,EACd,mBAAmB,EACnB,kBAAkB,EAClB,UAAU,EACV,aAAa,EAEb,YAAY,EACZ,eAAe,EAClB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,mBAAmB,EAAE,2BAA2B,EAAE,SAAS,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACrH,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAE5E,YAAY;AACZ,OAAO,EAAE,MAAM,IAAI,CAAC;AAEpB,aAAa;AACb,OAAO,UAAU,MAAM,YAAY,CAAC;AAIpC,SAAS,SAAS,CACd,OAA0B,EAC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,mBAAmB,CAAC;iBACtH;gBACD,aAAa,CAAC,IAAI,CAAC,QAAQ,yBAAyB,KAAK,WAAW,EAAE,CAAC,CAAC;aAC3E;SACJ;KACJ;IAGD,IAAI,gBAAgB,GAAG,EAAE,CAAC;IAC1B,IAAI,eAAe,EAAE;QACjB,MAAM,eAAe,GAAG,QAAQ,CAAC,eAAe,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;QAC/E,MAAM,YAAY,GAAG,QAAQ,CAAC,eAAe,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;QACtE,gBAAgB,GAAG;wCACa,YAAY,IAAI,eAAe,6BAA6B,CAAC;QAC7F,uBAAuB,GAAG,IAAI,CAAC;KAClC;IAED,IAAI,gBAAgB,GAAG,UAAU,CAAC;IAElC,IAAI,aAAa,CAAC,MAAM,EAAE;QACtB,gBAAgB,GAAG,GAAG,uBAAuB,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;;;;kBAIlD,qBAAqB;kBACrB,gBAAgB;kBAChB,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC;kBACxB,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC;kBAC7B,2BAA2B;;sCAEP,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC;cAChD,CAAC;KACV;IACD,OAAO,gBAAgB,CAAC;AAC5B,CAAC"}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { ClassType, CompilerContext } from '@deepkit/core';
|
|
2
|
+
import { ReflectionFunction, ReflectionParameter, SerializationOptions, Serializer, Type } from '@deepkit/type';
|
|
3
|
+
import { HttpDecorator } from './decorator.js';
|
|
4
|
+
import { HttpRequest, HttpRequestPositionedParameters, HttpRequestQuery, HttpRequestResolvedParameters } from './model.js';
|
|
5
|
+
import { InjectorContext, InjectorModule } from '@deepkit/injector';
|
|
6
|
+
import { LoggerInterface } from '@deepkit/logger';
|
|
7
|
+
import { HttpControllers } from './controllers.js';
|
|
8
|
+
import { MiddlewareRegistry } from '@deepkit/app';
|
|
9
|
+
import { HttpMiddlewareConfig, HttpMiddlewareFn } from './middleware.js';
|
|
10
|
+
import { ParameterForRequestParser } from './request-parser.js';
|
|
11
|
+
import { HttpConfig } from './module.config.js';
|
|
12
|
+
export declare type RouteParameterResolverForInjector = ((injector: InjectorContext) => HttpRequestPositionedParameters | Promise<HttpRequestPositionedParameters>);
|
|
13
|
+
interface ResolvedController {
|
|
14
|
+
parameters: RouteParameterResolverForInjector;
|
|
15
|
+
routeConfig: RouteConfig;
|
|
16
|
+
uploadedFiles: {
|
|
17
|
+
[name: string]: UploadedFile;
|
|
18
|
+
};
|
|
19
|
+
middlewares?: (injector: InjectorContext) => {
|
|
20
|
+
fn: HttpMiddlewareFn;
|
|
21
|
+
timeout?: number;
|
|
22
|
+
}[];
|
|
23
|
+
}
|
|
24
|
+
export declare const UploadedFileSymbol: unique symbol;
|
|
25
|
+
export declare class UploadedFile {
|
|
26
|
+
/**
|
|
27
|
+
* Validator to ensure the file was provided by the framework, and not the user spoofing it.
|
|
28
|
+
*/
|
|
29
|
+
validator: typeof UploadedFileSymbol | null;
|
|
30
|
+
/**
|
|
31
|
+
* The size of the uploaded file in bytes.
|
|
32
|
+
*/
|
|
33
|
+
size: number;
|
|
34
|
+
/**
|
|
35
|
+
* The local path this file is being written to. Will be deleted when request is handled.
|
|
36
|
+
*/
|
|
37
|
+
path: string;
|
|
38
|
+
/**
|
|
39
|
+
* The name this file had according to the uploading client.
|
|
40
|
+
*/
|
|
41
|
+
name: string | null;
|
|
42
|
+
/**
|
|
43
|
+
* The mime type of this file, according to the uploading client.
|
|
44
|
+
*/
|
|
45
|
+
type: string | null;
|
|
46
|
+
/**
|
|
47
|
+
* A Date object (or `null`) containing the time this file was last written to.
|
|
48
|
+
* Mostly here for compatibility with the [W3C File API Draft](http://dev.w3.org/2006/webapi/FileAPI/).
|
|
49
|
+
*/
|
|
50
|
+
lastModifiedDate: Date | null;
|
|
51
|
+
}
|
|
52
|
+
export interface RouteFunctionControllerAction {
|
|
53
|
+
type: 'function';
|
|
54
|
+
module?: InjectorModule<any>;
|
|
55
|
+
fn: (...args: any[]) => any;
|
|
56
|
+
}
|
|
57
|
+
export interface RouteClassControllerAction {
|
|
58
|
+
type: 'controller';
|
|
59
|
+
module?: InjectorModule<any>;
|
|
60
|
+
controller: ClassType;
|
|
61
|
+
methodName: string;
|
|
62
|
+
}
|
|
63
|
+
export declare class RouteConfig {
|
|
64
|
+
readonly name: string;
|
|
65
|
+
readonly httpMethods: string[];
|
|
66
|
+
readonly path: string;
|
|
67
|
+
readonly action: RouteClassControllerAction | RouteFunctionControllerAction;
|
|
68
|
+
internal: boolean;
|
|
69
|
+
baseUrl: string;
|
|
70
|
+
responses: {
|
|
71
|
+
statusCode: number;
|
|
72
|
+
description: string;
|
|
73
|
+
type?: Type;
|
|
74
|
+
}[];
|
|
75
|
+
description: string;
|
|
76
|
+
groups: string[];
|
|
77
|
+
category: string;
|
|
78
|
+
returnType?: Type;
|
|
79
|
+
serializationOptions?: SerializationOptions;
|
|
80
|
+
serializer?: Serializer;
|
|
81
|
+
/**
|
|
82
|
+
* When assigned defines where this route came from.
|
|
83
|
+
*/
|
|
84
|
+
module?: InjectorModule<any>;
|
|
85
|
+
resolverForToken: Map<any, ClassType>;
|
|
86
|
+
middlewares: {
|
|
87
|
+
config: HttpMiddlewareConfig;
|
|
88
|
+
module?: InjectorModule<any>;
|
|
89
|
+
}[];
|
|
90
|
+
resolverForParameterName: Map<string, ClassType>;
|
|
91
|
+
/**
|
|
92
|
+
* An arbitrary data container the user can use to store app specific settings/values.
|
|
93
|
+
*/
|
|
94
|
+
data: Map<any, any>;
|
|
95
|
+
constructor(name: string, httpMethods: string[], path: string, action: RouteClassControllerAction | RouteFunctionControllerAction, internal?: boolean);
|
|
96
|
+
getReflectionFunction(): ReflectionFunction;
|
|
97
|
+
getSchemaForResponse(statusCode: number): Type | undefined;
|
|
98
|
+
getFullPath(): string;
|
|
99
|
+
}
|
|
100
|
+
declare class ParsedRoute {
|
|
101
|
+
routeConfig: RouteConfig;
|
|
102
|
+
regex?: string;
|
|
103
|
+
pathParameterNames: {
|
|
104
|
+
[name: string]: number;
|
|
105
|
+
};
|
|
106
|
+
protected parameters: ParameterForRequestParser[];
|
|
107
|
+
constructor(routeConfig: RouteConfig);
|
|
108
|
+
addParameter(property: ReflectionParameter): ParameterForRequestParser;
|
|
109
|
+
getParameters(): ParameterForRequestParser[];
|
|
110
|
+
getParameter(name: string): ParameterForRequestParser;
|
|
111
|
+
}
|
|
112
|
+
export declare function parseRouteControllerAction(routeConfig: RouteConfig): ParsedRoute;
|
|
113
|
+
export declare function dotToUrlPath(dotPath: string): string;
|
|
114
|
+
export interface RouteParameterResolver {
|
|
115
|
+
resolve(context: RouteParameterResolverContext): any | Promise<any>;
|
|
116
|
+
}
|
|
117
|
+
export interface RouteParameterResolverContext {
|
|
118
|
+
token: ClassType | string | symbol | any;
|
|
119
|
+
route: RouteConfig;
|
|
120
|
+
request: HttpRequest;
|
|
121
|
+
/**
|
|
122
|
+
* The parameter name (variable name).
|
|
123
|
+
*/
|
|
124
|
+
name: any;
|
|
125
|
+
/**
|
|
126
|
+
* The raw parameter value from the path, if the parameter is defined in the path (e.g. /user/:name).
|
|
127
|
+
* If not in the path, you have to use `parameters.<name>` instead.
|
|
128
|
+
*/
|
|
129
|
+
value: any;
|
|
130
|
+
query: HttpRequestQuery;
|
|
131
|
+
parameters: HttpRequestResolvedParameters;
|
|
132
|
+
type: ReflectionParameter;
|
|
133
|
+
}
|
|
134
|
+
export interface HttpRouterFunctionOptions {
|
|
135
|
+
path: string;
|
|
136
|
+
name?: string;
|
|
137
|
+
methods?: string[];
|
|
138
|
+
description?: string;
|
|
139
|
+
category?: string;
|
|
140
|
+
groups?: string[];
|
|
141
|
+
/**
|
|
142
|
+
* An arbitrary data container the user can use to store app specific settings/values.
|
|
143
|
+
*/
|
|
144
|
+
data?: Record<any, any>;
|
|
145
|
+
baseUrl?: string;
|
|
146
|
+
middlewares?: (() => HttpMiddlewareConfig)[];
|
|
147
|
+
serializer?: Serializer;
|
|
148
|
+
serializationOptions?: SerializationOptions;
|
|
149
|
+
resolverForToken?: Map<any, ClassType>;
|
|
150
|
+
resolverForParameterName?: Map<string, ClassType>;
|
|
151
|
+
responses?: {
|
|
152
|
+
statusCode: number;
|
|
153
|
+
description: string;
|
|
154
|
+
type?: Type;
|
|
155
|
+
}[];
|
|
156
|
+
}
|
|
157
|
+
export declare abstract class HttpRouterRegistryFunctionRegistrar {
|
|
158
|
+
protected defaultOptions: Partial<HttpRouterFunctionOptions>;
|
|
159
|
+
abstract addRoute(routeConfig: RouteConfig): void;
|
|
160
|
+
/**
|
|
161
|
+
* Returns a new registrar object with default options that apply to each registered route through this registrar.
|
|
162
|
+
*
|
|
163
|
+
* ```typescript
|
|
164
|
+
* const registry: HttpRouterRegistry = ...;
|
|
165
|
+
*
|
|
166
|
+
* const secretRegistry = registry.forOptions({groups: ['secret']});
|
|
167
|
+
*
|
|
168
|
+
* secretRegistry.get('/admin/groups', () => {
|
|
169
|
+
* });
|
|
170
|
+
*
|
|
171
|
+
* secretRegistry.get('/admin/users', () => {
|
|
172
|
+
* });
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
forOptions(options: Partial<HttpRouterFunctionOptions>): HttpRouterRegistryFunctionRegistrar;
|
|
176
|
+
any(pathOrOptions: string | HttpRouterFunctionOptions, callback: (...args: any[]) => any): void;
|
|
177
|
+
add(decorator: HttpDecorator, callback: (...args: any[]) => any): void;
|
|
178
|
+
get(pathOrOptions: string | HttpRouterFunctionOptions, callback: (...args: any[]) => any): void;
|
|
179
|
+
post(pathOrOptions: string | HttpRouterFunctionOptions, callback: (...args: any[]) => any): void;
|
|
180
|
+
put(pathOrOptions: string | HttpRouterFunctionOptions, callback: (...args: any[]) => any): void;
|
|
181
|
+
patch(pathOrOptions: string | HttpRouterFunctionOptions, callback: (...args: any[]) => any): void;
|
|
182
|
+
delete(pathOrOptions: string | HttpRouterFunctionOptions, callback: (...args: any[]) => any): void;
|
|
183
|
+
options(pathOrOptions: string | HttpRouterFunctionOptions, callback: (...args: any[]) => any): void;
|
|
184
|
+
trace(pathOrOptions: string | HttpRouterFunctionOptions, callback: (...args: any[]) => any): void;
|
|
185
|
+
head(pathOrOptions: string | HttpRouterFunctionOptions, callback: (...args: any[]) => any): void;
|
|
186
|
+
private register;
|
|
187
|
+
}
|
|
188
|
+
export declare class HttpRouterRegistry extends HttpRouterRegistryFunctionRegistrar {
|
|
189
|
+
protected routes: RouteConfig[];
|
|
190
|
+
private buildId;
|
|
191
|
+
getBuildId(): number;
|
|
192
|
+
getRoutes(): RouteConfig[];
|
|
193
|
+
addRouteForController(controller: ClassType, module: InjectorModule<any>): void;
|
|
194
|
+
addRoute(routeConfig: RouteConfig): void;
|
|
195
|
+
}
|
|
196
|
+
export declare class HttpRouter {
|
|
197
|
+
private logger;
|
|
198
|
+
private config;
|
|
199
|
+
private middlewareRegistry;
|
|
200
|
+
private registry;
|
|
201
|
+
protected fn?: (request: HttpRequest) => ResolvedController | undefined;
|
|
202
|
+
protected buildId: number;
|
|
203
|
+
protected resolveFn?: (name: string, parameters: {
|
|
204
|
+
[name: string]: any;
|
|
205
|
+
}) => string;
|
|
206
|
+
constructor(controllers: HttpControllers, logger: LoggerInterface, config: HttpConfig, middlewareRegistry?: MiddlewareRegistry, registry?: HttpRouterRegistry);
|
|
207
|
+
getRoutes(): RouteConfig[];
|
|
208
|
+
static forControllers(controllers: (ClassType | {
|
|
209
|
+
module: InjectorModule<any>;
|
|
210
|
+
controller: ClassType;
|
|
211
|
+
})[], middlewareRegistry?: MiddlewareRegistry, module?: InjectorModule<any>, config?: HttpConfig): HttpRouter;
|
|
212
|
+
protected getRouteCode(compiler: CompilerContext, routeConfig: RouteConfig): string;
|
|
213
|
+
protected getRouteUrlResolveCode(compiler: CompilerContext, routeConfig: RouteConfig): string;
|
|
214
|
+
addRoute(routeConfig: RouteConfig): void;
|
|
215
|
+
addRouteForController(controller: ClassType, module: InjectorModule<any>): void;
|
|
216
|
+
protected build(): (request: HttpRequest) => ResolvedController | undefined;
|
|
217
|
+
protected buildUrlResolver(): any;
|
|
218
|
+
resolveUrl(routeName: string, parameters?: {
|
|
219
|
+
[name: string]: any;
|
|
220
|
+
}): string;
|
|
221
|
+
resolveRequest(request: HttpRequest): ResolvedController | undefined;
|
|
222
|
+
resolve(method: string, url: string): ResolvedController | undefined;
|
|
223
|
+
}
|
|
224
|
+
export {};
|
|
225
|
+
export declare type __ΩRouteParameterResolverForInjector = any[];
|
|
226
|
+
export declare type __ΩRouteFunctionControllerAction = any[];
|
|
227
|
+
export declare type __ΩRouteClassControllerAction = any[];
|
|
228
|
+
export declare type __ΩRouteParameterResolver = any[];
|
|
229
|
+
export declare type __ΩRouteParameterResolverContext = any[];
|
|
230
|
+
export declare type __ΩHttpRouterFunctionOptions = any[];
|