@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,617 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.HttpRouter = exports.HttpRouterRegistry = exports.HttpRouterRegistryFunctionRegistrar = exports.__ΩHttpRouterFunctionOptions = exports.__ΩRouteParameterResolverContext = exports.__ΩRouteParameterResolver = exports.dotToUrlPath = exports.parseRouteControllerAction = exports.RouteConfig = exports.__ΩRouteClassControllerAction = exports.__ΩRouteFunctionControllerAction = exports.UploadedFile = exports.UploadedFileSymbol = exports.__ΩRouteParameterResolverForInjector = void 0;
|
|
13
|
+
/*@ts-ignore*/
|
|
14
|
+
const { __ΩSerializationOptions } = require('@deepkit/type');
|
|
15
|
+
/*@ts-ignore*/
|
|
16
|
+
const { __ΩClassType } = require('@deepkit/core');
|
|
17
|
+
/*@ts-ignore*/
|
|
18
|
+
const { __ΩMiddlewareRegistryEntry } = require('@deepkit/app');
|
|
19
|
+
/*@ts-ignore*/
|
|
20
|
+
const { __ΩHttpDecorator } = require('./decorator.js');
|
|
21
|
+
/*@ts-ignore*/
|
|
22
|
+
const { __ΩLoggerInterface } = require('@deepkit/logger');
|
|
23
|
+
/*@ts-ignore*/
|
|
24
|
+
const { __ΩHttpRequestPositionedParameters } = require('./model.js');
|
|
25
|
+
/*@ts-ignore*/
|
|
26
|
+
const { __ΩHttpMiddlewareFn } = require('./middleware.js');
|
|
27
|
+
/*@ts-ignore*/
|
|
28
|
+
const { __ΩHttpRequestQuery } = require('./model.js');
|
|
29
|
+
/*@ts-ignore*/
|
|
30
|
+
const { __ΩHttpRequestResolvedParameters } = require('./model.js');
|
|
31
|
+
function __assignType(fn, args) {
|
|
32
|
+
fn.__type = args;
|
|
33
|
+
return fn;
|
|
34
|
+
}
|
|
35
|
+
const __ΩPartial = ['T', 'l+e#!e"!fRb!Pde"!gN#"'];
|
|
36
|
+
const __ΩRecord = ['K', 'T', 'l\'e#"Rb!b"Pde"!N#!'];
|
|
37
|
+
/*
|
|
38
|
+
* Deepkit Framework
|
|
39
|
+
* Copyright (C) 2021 Deepkit UG, Marc J. Schmidt
|
|
40
|
+
*
|
|
41
|
+
* This program is free software: you can redistribute it and/or modify
|
|
42
|
+
* it under the terms of the MIT License.
|
|
43
|
+
*
|
|
44
|
+
* You should have received a copy of the MIT License along with this program.
|
|
45
|
+
*/
|
|
46
|
+
const core_1 = require("@deepkit/core");
|
|
47
|
+
const type_1 = require("@deepkit/type");
|
|
48
|
+
const decorator_js_1 = require("./decorator.js");
|
|
49
|
+
const model_js_1 = require("./model.js");
|
|
50
|
+
const injector_1 = require("@deepkit/injector");
|
|
51
|
+
const logger_1 = require("@deepkit/logger");
|
|
52
|
+
const controllers_js_1 = require("./controllers.js");
|
|
53
|
+
const app_1 = require("@deepkit/app");
|
|
54
|
+
const middleware_js_1 = require("./middleware.js");
|
|
55
|
+
//@ts-ignore
|
|
56
|
+
const qs_1 = __importDefault(require("qs"));
|
|
57
|
+
const http_js_1 = require("./http.js");
|
|
58
|
+
const request_parser_js_1 = require("./request-parser.js");
|
|
59
|
+
const module_config_js_1 = require("./module.config.js");
|
|
60
|
+
const __ΩRouteParameterResolverForInjector = [() => injector_1.InjectorContext, 'injector', () => __ΩHttpRequestPositionedParameters, () => __ΩHttpRequestPositionedParameters, '', 'PP7!2"Pn#n$`J/%'];
|
|
61
|
+
exports.__ΩRouteParameterResolverForInjector = __ΩRouteParameterResolverForInjector;
|
|
62
|
+
const __ΩResolvedController = [() => __ΩRouteParameterResolverForInjector, 'parameters', () => RouteConfig, 'routeConfig', () => UploadedFile, 'uploadedFiles', () => injector_1.InjectorContext, 'injector', () => __ΩHttpMiddlewareFn, 'fn', 'timeout', '', 'middlewares', 'Pn!4"P7#4$P&P7%LM4&PP7\'2(Pn)4*\'4+8MF/,4-8M'];
|
|
63
|
+
exports.UploadedFileSymbol = Symbol('UploadedFile');
|
|
64
|
+
let UploadedFile = class UploadedFile {
|
|
65
|
+
};
|
|
66
|
+
UploadedFile.__type = [() => exports.UploadedFileSymbol, 'validator', 'size', 'path', 'name', 'type', 'lastModifiedDate', 'Pi!,J3"\'3#&3$P&,J3%P&,J3&PT,J3\'5'];
|
|
67
|
+
UploadedFile = __decorate([
|
|
68
|
+
type_1.entity.name('@deepkit/UploadedFile')
|
|
69
|
+
], UploadedFile);
|
|
70
|
+
exports.UploadedFile = UploadedFile;
|
|
71
|
+
type_1.serializer.typeGuards.getRegistry(1).registerClass(UploadedFile, __assignType((type, state) => {
|
|
72
|
+
state.setContext({ UploadedFileSymbol: exports.UploadedFileSymbol });
|
|
73
|
+
state.addSetterAndReportErrorIfInvalid('uploadSecurity', 'Not an uploaded file', `typeof ${state.accessor} === 'object' && ${state.accessor} !== null && ${state.accessor}.validator === UploadedFileSymbol`);
|
|
74
|
+
}, ['type', 'state', '', 'P"2!"2""/#']));
|
|
75
|
+
const __ΩRouteFunctionControllerAction = ["function", 'type', () => injector_1.InjectorModule, 'module', 'args', '', 'fn', 'P.!4"P"7#4$8P"@2%"/&4\'M'];
|
|
76
|
+
exports.__ΩRouteFunctionControllerAction = __ΩRouteFunctionControllerAction;
|
|
77
|
+
const __ΩRouteClassControllerAction = ["controller", 'type', () => injector_1.InjectorModule, 'module', () => __ΩClassType, 'controller', 'methodName', 'P.!4"P"7#4$8n%4&&4\'M'];
|
|
78
|
+
exports.__ΩRouteClassControllerAction = __ΩRouteClassControllerAction;
|
|
79
|
+
class RouteConfig {
|
|
80
|
+
constructor(name, httpMethods, path, action, internal = false) {
|
|
81
|
+
this.name = name;
|
|
82
|
+
this.httpMethods = httpMethods;
|
|
83
|
+
this.path = path;
|
|
84
|
+
this.action = action;
|
|
85
|
+
this.internal = internal;
|
|
86
|
+
this.baseUrl = '';
|
|
87
|
+
this.responses = [];
|
|
88
|
+
this.description = '';
|
|
89
|
+
this.groups = [];
|
|
90
|
+
this.category = '';
|
|
91
|
+
this.resolverForToken = new Map();
|
|
92
|
+
this.middlewares = [];
|
|
93
|
+
this.resolverForParameterName = new Map();
|
|
94
|
+
/**
|
|
95
|
+
* An arbitrary data container the user can use to store app specific settings/values.
|
|
96
|
+
*/
|
|
97
|
+
this.data = (Map.Ω = [['"'], ['"']], new Map());
|
|
98
|
+
}
|
|
99
|
+
getReflectionFunction() {
|
|
100
|
+
return this.action.type === 'controller' ?
|
|
101
|
+
type_1.ReflectionClass.from(this.action.controller).getMethod(this.action.methodName)
|
|
102
|
+
: type_1.ReflectionFunction.from(this.action.fn);
|
|
103
|
+
}
|
|
104
|
+
getSchemaForResponse(statusCode) {
|
|
105
|
+
if (!this.responses.length)
|
|
106
|
+
return;
|
|
107
|
+
for (const response of this.responses) {
|
|
108
|
+
if (response.statusCode === statusCode)
|
|
109
|
+
return response.type;
|
|
110
|
+
}
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
getFullPath() {
|
|
114
|
+
let path = this.baseUrl ? (0, core_1.urlJoin)(this.baseUrl, this.path) : this.path;
|
|
115
|
+
if (!path.startsWith('/'))
|
|
116
|
+
path = '/' + path;
|
|
117
|
+
return path;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
exports.RouteConfig = RouteConfig;
|
|
121
|
+
RouteConfig.__type = ['baseUrl', function () { return ''; }, 'statusCode', 'description', 'type', 'responses', function () { return []; }, function () { return ''; }, 'groups', function () { return []; }, 'category', function () { return ''; }, 'returnType', () => __ΩSerializationOptions, 'serializationOptions', () => type_1.Serializer, 'serializer', () => injector_1.InjectorModule, 'module', () => __ΩClassType, 'resolverForToken', function () { return new Map(); }, () => middleware_js_1.HttpMiddlewareConfig, 'config', () => injector_1.InjectorModule, 'middlewares', function () { return []; }, () => __ΩClassType, 'resolverForParameterName', function () { return new Map(); }, 'name', 'httpMethods', 'path', () => __ΩRouteClassControllerAction, () => __ΩRouteFunctionControllerAction, 'action', 'internal', () => false, 'constructor', () => type_1.ReflectionFunction, 'getReflectionFunction', 'getSchemaForResponse', 'getFullPath', '&3!>"P\'4#&4$"4%8MF3&>\'&3$>(&F3)>*&3+>,"3-8n.3/8P70318P"72338"n4E35>6PP7748P"79438MF3:>;&n<E3=>>P&2?:9&F2@:9&2A:9PnBnCJ2D:9)2E:>F"0GPP7H0IP\'2#P"-J0JP&0K5'];
|
|
122
|
+
class ParsedRoute {
|
|
123
|
+
constructor(routeConfig) {
|
|
124
|
+
this.routeConfig = routeConfig;
|
|
125
|
+
this.pathParameterNames = {};
|
|
126
|
+
this.parameters = [];
|
|
127
|
+
}
|
|
128
|
+
addParameter(property) {
|
|
129
|
+
const parameter = new request_parser_js_1.ParameterForRequestParser(property);
|
|
130
|
+
this.parameters.push(parameter);
|
|
131
|
+
return parameter;
|
|
132
|
+
}
|
|
133
|
+
getParameters() {
|
|
134
|
+
return this.parameters;
|
|
135
|
+
}
|
|
136
|
+
getParameter(name) {
|
|
137
|
+
for (const parameter of this.parameters) {
|
|
138
|
+
if (parameter.getName() === name)
|
|
139
|
+
return parameter;
|
|
140
|
+
}
|
|
141
|
+
throw new Error(`No route parameter with name ${name} defined.`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
ParsedRoute.__type = ['regex', 'pathParameterNames', function () { return {}; }, () => request_parser_js_1.ParameterForRequestParser, 'parameters', function () { return []; }, () => RouteConfig, 'routeConfig', 'constructor', () => type_1.ReflectionParameter, 'property', () => request_parser_js_1.ParameterForRequestParser, 'addParameter', () => request_parser_js_1.ParameterForRequestParser, 'getParameters', 'name', () => request_parser_js_1.ParameterForRequestParser, 'getParameter', '&3!8P&\'LM3">#P7$F3%<>&PP7\'2(:"0)PP7*2+P7,0-PP7.F0/P&20P71025'];
|
|
145
|
+
function parseRouteControllerAction(routeConfig) {
|
|
146
|
+
const parsedRoute = new ParsedRoute(routeConfig);
|
|
147
|
+
const parameters = routeConfig.getReflectionFunction().getParameters();
|
|
148
|
+
const parsedPath = (0, request_parser_js_1.parseRoutePathToRegex)(routeConfig.getFullPath(), parameters);
|
|
149
|
+
parsedRoute.regex = parsedPath.regex;
|
|
150
|
+
parsedRoute.pathParameterNames = parsedPath.parameterNames;
|
|
151
|
+
for (const property of parameters) {
|
|
152
|
+
const parsedParameter = parsedRoute.addParameter(property);
|
|
153
|
+
parsedParameter.regexPosition = parsedPath.parameterNames[property.name];
|
|
154
|
+
}
|
|
155
|
+
return parsedRoute;
|
|
156
|
+
}
|
|
157
|
+
exports.parseRouteControllerAction = parseRouteControllerAction;
|
|
158
|
+
parseRouteControllerAction.__type = [() => RouteConfig, 'routeConfig', () => ParsedRoute, 'parseRouteControllerAction', 'PP7!2"P7#/$'];
|
|
159
|
+
function dotToUrlPath(dotPath) {
|
|
160
|
+
if (-1 === dotPath.indexOf('.'))
|
|
161
|
+
return dotPath;
|
|
162
|
+
return dotPath.replace(/\./g, '][').replace('][', '[') + ']';
|
|
163
|
+
}
|
|
164
|
+
exports.dotToUrlPath = dotToUrlPath;
|
|
165
|
+
dotToUrlPath.__type = ['dotPath', 'dotToUrlPath', 'P&2!&/"'];
|
|
166
|
+
const __ΩRouteParameterResolver = [() => __ΩRouteParameterResolverContext, 'context', 'resolve', 'PPn!2"P""`J1#M'];
|
|
167
|
+
exports.__ΩRouteParameterResolver = __ΩRouteParameterResolver;
|
|
168
|
+
const __ΩRouteParameterResolverContext = [() => __ΩClassType, 'token', () => RouteConfig, 'route', () => model_js_1.HttpRequest, 'request', 'name', 'value', () => __ΩHttpRequestQuery, 'query', () => __ΩHttpRequestResolvedParameters, 'parameters', () => type_1.ReflectionParameter, 'type', 'PPn!&+"J4"P7#4$P7%4&"4\'"4(n)4*n+4,P7-4.M'];
|
|
169
|
+
exports.__ΩRouteParameterResolverContext = __ΩRouteParameterResolverContext;
|
|
170
|
+
function filterMiddlewaresForRoute(middlewareRawConfigs, routeConfig, fullPath) {
|
|
171
|
+
const middlewares = middlewareRawConfigs.slice(0);
|
|
172
|
+
middlewares.push(...routeConfig.middlewares);
|
|
173
|
+
const middlewareConfigs = middlewares.filter(__assignType((v) => {
|
|
174
|
+
var _a;
|
|
175
|
+
if (!(v.config instanceof middleware_js_1.HttpMiddlewareConfig))
|
|
176
|
+
return false;
|
|
177
|
+
if (v.config.controllers.length && routeConfig.action.type === 'controller' && !v.config.controllers.includes(routeConfig.action.controller)) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
if (v.config.excludeControllers.length && routeConfig.action.type === 'controller' && v.config.excludeControllers.includes(routeConfig.action.controller)) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
if (v.config.modules.length && (!routeConfig.module || !v.config.modules.includes(routeConfig.module))) {
|
|
184
|
+
if (!routeConfig.module)
|
|
185
|
+
return false;
|
|
186
|
+
for (const module of v.config.modules) {
|
|
187
|
+
if (routeConfig.module.id !== module.id)
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (v.config.selfModule && v.module.id !== ((_a = routeConfig.module) === null || _a === void 0 ? void 0 : _a.id))
|
|
192
|
+
return false;
|
|
193
|
+
if (v.config.routeNames.length) {
|
|
194
|
+
let found = false;
|
|
195
|
+
for (const name of v.config.routeNames) {
|
|
196
|
+
if (name.includes('*')) {
|
|
197
|
+
const regex = new RegExp('^' + name.replace(/\*/g, '.*') + '$');
|
|
198
|
+
if (regex.test(routeConfig.name)) {
|
|
199
|
+
found = true;
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
else if (name === routeConfig.name) {
|
|
204
|
+
found = true;
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (!found)
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
if (v.config.excludeRouteNames.length) {
|
|
212
|
+
for (const name of v.config.excludeRouteNames) {
|
|
213
|
+
if (name.includes('*')) {
|
|
214
|
+
const regex = new RegExp('^' + name.replace(/\*/g, '.*') + '$');
|
|
215
|
+
if (regex.test(routeConfig.name))
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
else if (name == routeConfig.name) {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
for (const route of v.config.routes) {
|
|
224
|
+
if (route.httpMethod && !routeConfig.httpMethods.includes(route.httpMethod))
|
|
225
|
+
return false;
|
|
226
|
+
if (route.category && route.category !== routeConfig.category)
|
|
227
|
+
return false;
|
|
228
|
+
if (route.excludeCategory && route.excludeCategory === routeConfig.category)
|
|
229
|
+
return false;
|
|
230
|
+
if (route.group && !routeConfig.groups.includes(route.group))
|
|
231
|
+
return false;
|
|
232
|
+
if (route.excludeGroup && routeConfig.groups.includes(route.excludeGroup))
|
|
233
|
+
return false;
|
|
234
|
+
if (route.path || route.pathRegExp) {
|
|
235
|
+
if (!route.pathRegExp && route.path)
|
|
236
|
+
route.pathRegExp = new RegExp('^' + route.path.replace(/\*/g, '.*') + '$');
|
|
237
|
+
if (route.pathRegExp && !route.pathRegExp.test(fullPath))
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return true;
|
|
242
|
+
}, ['v', '', 'P"2!"/"']));
|
|
243
|
+
middlewareConfigs.sort(__assignType((a, b) => {
|
|
244
|
+
return a.config.order - b.config.order;
|
|
245
|
+
}, ['a', 'b', '', 'P"2!"2""/#']));
|
|
246
|
+
return middlewareConfigs;
|
|
247
|
+
}
|
|
248
|
+
filterMiddlewaresForRoute.__type = [() => __ΩMiddlewareRegistryEntry, 'middlewareRawConfigs', () => RouteConfig, 'routeConfig', 'fullPath', () => middleware_js_1.HttpMiddlewareConfig, 'config', () => injector_1.InjectorModule, 'module', 'filterMiddlewaresForRoute', 'Pn!F2"P7#2$&2%PP7&4\'P"7(4)MF/*'];
|
|
249
|
+
const __ΩHttpRouterFunctionOptions = ['path', 'name', 'methods', 'description', 'category', 'groups', () => __ΩRecord, 'data', 'baseUrl', () => middleware_js_1.HttpMiddlewareConfig, '', 'middlewares', () => type_1.Serializer, 'serializer', () => __ΩSerializationOptions, 'serializationOptions', () => __ΩClassType, 'resolverForToken', () => __ΩClassType, 'resolverForParameterName', 'statusCode', 'type', 'responses', 'P&4!&4"8&F4#8&4$8&4%8&F4&8""o\'#4(8&4)8PP7*/+F4,8P7-4.8n/408"n1E428&n3E448P\'45&4$"468MF478M'];
|
|
250
|
+
exports.__ΩHttpRouterFunctionOptions = __ΩHttpRouterFunctionOptions;
|
|
251
|
+
function convertOptions(methods, pathOrOptions, defaultOptions) {
|
|
252
|
+
const options = 'string' === typeof pathOrOptions ? { path: pathOrOptions } : pathOrOptions;
|
|
253
|
+
if (options.methods)
|
|
254
|
+
return options;
|
|
255
|
+
return { ...options, methods };
|
|
256
|
+
}
|
|
257
|
+
convertOptions.__type = ['methods', () => __ΩHttpRouterFunctionOptions, 'pathOrOptions', () => __ΩPartial, () => __ΩHttpRouterFunctionOptions, 'defaultOptions', () => __ΩHttpRouterFunctionOptions, 'convertOptions', 'P&F2!P&n"J2#n%o$"2&n\'/('];
|
|
258
|
+
/**
|
|
259
|
+
* Annotated types like HTMLResponse/JSONResponse are not used for serialization.
|
|
260
|
+
*/
|
|
261
|
+
function filterValidReturnType(type) {
|
|
262
|
+
if (type.kind === type_1.ReflectionKind.class && (type.classType === http_js_1.HtmlResponse || type.classType === http_js_1.JSONResponse || type.classType === http_js_1.Response))
|
|
263
|
+
return;
|
|
264
|
+
return type;
|
|
265
|
+
}
|
|
266
|
+
filterValidReturnType.__type = ['type', 'filterValidReturnType', 'P"2!P"-J/"'];
|
|
267
|
+
class HttpRouterRegistryFunctionRegistrar {
|
|
268
|
+
constructor() {
|
|
269
|
+
this.defaultOptions = {};
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Returns a new registrar object with default options that apply to each registered route through this registrar.
|
|
273
|
+
*
|
|
274
|
+
* ```typescript
|
|
275
|
+
* const registry: HttpRouterRegistry = ...;
|
|
276
|
+
*
|
|
277
|
+
* const secretRegistry = registry.forOptions({groups: ['secret']});
|
|
278
|
+
*
|
|
279
|
+
* secretRegistry.get('/admin/groups', () => {
|
|
280
|
+
* });
|
|
281
|
+
*
|
|
282
|
+
* secretRegistry.get('/admin/users', () => {
|
|
283
|
+
* });
|
|
284
|
+
* ```
|
|
285
|
+
*/
|
|
286
|
+
forOptions(options) {
|
|
287
|
+
var _a;
|
|
288
|
+
const that = this;
|
|
289
|
+
return new (_a = class extends HttpRouterRegistryFunctionRegistrar {
|
|
290
|
+
constructor() {
|
|
291
|
+
super(...arguments);
|
|
292
|
+
this.defaultOptions = options;
|
|
293
|
+
}
|
|
294
|
+
addRoute(routeConfig) {
|
|
295
|
+
that.addRoute(routeConfig);
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
_a.__type = [() => HttpRouterRegistryFunctionRegistrar, () => RouteConfig, 'routeConfig', 'addRoute', 'P7!PP7"2#"0$5'],
|
|
299
|
+
_a);
|
|
300
|
+
}
|
|
301
|
+
any(pathOrOptions, callback) {
|
|
302
|
+
this.register(convertOptions([], pathOrOptions, this.defaultOptions), callback);
|
|
303
|
+
}
|
|
304
|
+
add(decorator, callback) {
|
|
305
|
+
const data = decorator(Object, '_');
|
|
306
|
+
const action = (0, core_1.isArray)(data) ? data.find(__assignType(v => v instanceof decorator_js_1.HttpAction, ['v', '', 'P"2!"/"'])) : undefined;
|
|
307
|
+
if (!action)
|
|
308
|
+
throw new Error('No HttpAction available');
|
|
309
|
+
const fn = type_1.ReflectionFunction.from(callback);
|
|
310
|
+
const routeConfig = createRouteConfigFromHttpAction({
|
|
311
|
+
type: 'function',
|
|
312
|
+
fn: callback,
|
|
313
|
+
}, action);
|
|
314
|
+
routeConfig.returnType = filterValidReturnType(fn.getReturnType());
|
|
315
|
+
this.addRoute(routeConfig);
|
|
316
|
+
}
|
|
317
|
+
get(pathOrOptions, callback) {
|
|
318
|
+
this.register(convertOptions(['GET'], pathOrOptions, this.defaultOptions), callback);
|
|
319
|
+
}
|
|
320
|
+
post(pathOrOptions, callback) {
|
|
321
|
+
this.register(convertOptions(['POST'], pathOrOptions, this.defaultOptions), callback);
|
|
322
|
+
}
|
|
323
|
+
put(pathOrOptions, callback) {
|
|
324
|
+
this.register(convertOptions(['PUT'], pathOrOptions, this.defaultOptions), callback);
|
|
325
|
+
}
|
|
326
|
+
patch(pathOrOptions, callback) {
|
|
327
|
+
this.register(convertOptions(['PATCH'], pathOrOptions, this.defaultOptions), callback);
|
|
328
|
+
}
|
|
329
|
+
delete(pathOrOptions, callback) {
|
|
330
|
+
this.register(convertOptions(['DELETE'], pathOrOptions, this.defaultOptions), callback);
|
|
331
|
+
}
|
|
332
|
+
options(pathOrOptions, callback) {
|
|
333
|
+
this.register(convertOptions(['OPTIONS'], pathOrOptions, this.defaultOptions), callback);
|
|
334
|
+
}
|
|
335
|
+
trace(pathOrOptions, callback) {
|
|
336
|
+
this.register(convertOptions(['TRACE'], pathOrOptions, this.defaultOptions), callback);
|
|
337
|
+
}
|
|
338
|
+
head(pathOrOptions, callback) {
|
|
339
|
+
this.register(convertOptions(['HEAD'], pathOrOptions, this.defaultOptions), callback);
|
|
340
|
+
}
|
|
341
|
+
register(options, callback, module) {
|
|
342
|
+
const fn = type_1.ReflectionFunction.from(callback);
|
|
343
|
+
const routeConfig = new RouteConfig(options.name || '', options.methods || [], options.path, {
|
|
344
|
+
type: 'function',
|
|
345
|
+
fn: callback,
|
|
346
|
+
});
|
|
347
|
+
routeConfig.module = module;
|
|
348
|
+
if (options.responses)
|
|
349
|
+
routeConfig.responses = options.responses;
|
|
350
|
+
if (options.description)
|
|
351
|
+
routeConfig.description = options.description;
|
|
352
|
+
if (options.category)
|
|
353
|
+
routeConfig.category = options.category;
|
|
354
|
+
if (options.groups)
|
|
355
|
+
routeConfig.groups = options.groups;
|
|
356
|
+
if (options.data)
|
|
357
|
+
routeConfig.data = new Map(Object.entries(options.data));
|
|
358
|
+
if (options.baseUrl)
|
|
359
|
+
routeConfig.baseUrl = options.baseUrl;
|
|
360
|
+
if (options.middlewares) {
|
|
361
|
+
routeConfig.middlewares = options.middlewares.map(__assignType(v => {
|
|
362
|
+
return { config: v(), module };
|
|
363
|
+
}, ['v', '', 'P"2!"/"']));
|
|
364
|
+
}
|
|
365
|
+
if (options.resolverForToken) {
|
|
366
|
+
for (const item of options.resolverForToken)
|
|
367
|
+
routeConfig.resolverForToken.set(...item);
|
|
368
|
+
}
|
|
369
|
+
if (options.resolverForToken) {
|
|
370
|
+
for (const item of options.resolverForToken)
|
|
371
|
+
routeConfig.resolverForToken.set(...item);
|
|
372
|
+
}
|
|
373
|
+
if (options.resolverForParameterName) {
|
|
374
|
+
for (const item of options.resolverForParameterName)
|
|
375
|
+
routeConfig.resolverForParameterName.set(...item);
|
|
376
|
+
}
|
|
377
|
+
routeConfig.serializer = options.serializer;
|
|
378
|
+
routeConfig.serializationOptions = options.serializationOptions;
|
|
379
|
+
routeConfig.returnType = filterValidReturnType(fn.getReturnType());
|
|
380
|
+
this.addRoute(routeConfig);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
exports.HttpRouterRegistryFunctionRegistrar = HttpRouterRegistryFunctionRegistrar;
|
|
384
|
+
HttpRouterRegistryFunctionRegistrar.__type = [() => __ΩPartial, () => __ΩHttpRouterFunctionOptions, 'defaultOptions', function () { return {}; }, () => RouteConfig, 'routeConfig', 'addRoute', () => __ΩPartial, () => __ΩHttpRouterFunctionOptions, 'options', () => HttpRouterRegistryFunctionRegistrar, 'forOptions', () => __ΩHttpRouterFunctionOptions, 'pathOrOptions', 'args', '', 'callback', 'any', () => __ΩHttpDecorator, 'decorator', 'add', () => __ΩHttpRouterFunctionOptions, 'get', () => __ΩHttpRouterFunctionOptions, 'post', () => __ΩHttpRouterFunctionOptions, 'put', () => __ΩHttpRouterFunctionOptions, 'patch', () => __ΩHttpRouterFunctionOptions, 'delete', () => __ΩHttpRouterFunctionOptions, () => __ΩHttpRouterFunctionOptions, 'trace', () => __ΩHttpRouterFunctionOptions, 'head', () => __ΩHttpRouterFunctionOptions, () => injector_1.InjectorModule, 'module', 'register', 'n"o!"3#<>$PP7%2&$0\'=Pn)o("2*P7+0,PP&n-J2.P"@2/"/021"02Pn324P"@2/"/021"05PP&n6J2.P"@2/"/021"07PP&n8J2.P"@2/"/021"09PP&n:J2.P"@2/"/021"0;PP&n<J2.P"@2/"/021"0=PP&n>J2.P"@2/"/021"0?PP&n@J2.P"@2/"/021"0*PP&nAJ2.P"@2/"/021"0BPP&nCJ2.P"@2/"/021"0DPnE2*P"@2/"/021P"7F2G8"0H;5'];
|
|
385
|
+
function createRouteConfigFromHttpAction(routeAction, action, module, controller) {
|
|
386
|
+
const routeConfig = new RouteConfig(action.name, action.httpMethods, action.path, routeAction);
|
|
387
|
+
routeConfig.responses = action.responses;
|
|
388
|
+
routeConfig.description = action.description;
|
|
389
|
+
routeConfig.category = action.category;
|
|
390
|
+
routeConfig.groups = action.groups;
|
|
391
|
+
routeConfig.data = new Map(action.data);
|
|
392
|
+
if (controller) {
|
|
393
|
+
routeConfig.baseUrl = controller.baseUrl;
|
|
394
|
+
routeConfig.middlewares = controller.middlewares.map(__assignType(v => {
|
|
395
|
+
return { config: v(), module };
|
|
396
|
+
}, ['v', '', 'P"2!"/"']));
|
|
397
|
+
routeConfig.resolverForToken = new Map(controller.resolverForToken);
|
|
398
|
+
routeConfig.resolverForParameterName = new Map(controller.resolverForParameterName);
|
|
399
|
+
}
|
|
400
|
+
routeConfig.middlewares.push(...action.middlewares.map(__assignType(v => {
|
|
401
|
+
return { config: v(), module };
|
|
402
|
+
}, ['v', '', 'P"2!"/"'])));
|
|
403
|
+
for (const item of action.resolverForToken)
|
|
404
|
+
routeConfig.resolverForToken.set(...item);
|
|
405
|
+
for (const item of action.resolverForParameterName)
|
|
406
|
+
routeConfig.resolverForParameterName.set(...item);
|
|
407
|
+
routeConfig.serializer = action.serializer;
|
|
408
|
+
routeConfig.serializationOptions = action.serializationOptions;
|
|
409
|
+
return routeConfig;
|
|
410
|
+
}
|
|
411
|
+
createRouteConfigFromHttpAction.__type = [() => __ΩRouteClassControllerAction, () => __ΩRouteFunctionControllerAction, 'routeAction', () => decorator_js_1.HttpAction, 'action', () => injector_1.InjectorModule, 'module', () => decorator_js_1.HttpController, 'controller', 'createRouteConfigFromHttpAction', 'PPn!n"J2#P7$2%P"7&2\'8P7(2)8"/*'];
|
|
412
|
+
class HttpRouterRegistry extends HttpRouterRegistryFunctionRegistrar {
|
|
413
|
+
constructor() {
|
|
414
|
+
super(...arguments);
|
|
415
|
+
this.routes = [];
|
|
416
|
+
this.buildId = 1;
|
|
417
|
+
}
|
|
418
|
+
getBuildId() {
|
|
419
|
+
return this.buildId;
|
|
420
|
+
}
|
|
421
|
+
getRoutes() {
|
|
422
|
+
return this.routes;
|
|
423
|
+
}
|
|
424
|
+
addRouteForController(controller, module) {
|
|
425
|
+
const controllerData = decorator_js_1.httpClass._fetch(controller);
|
|
426
|
+
if (!controllerData)
|
|
427
|
+
throw new Error(`Http controller class ${(0, core_1.getClassName)(controller)} has no @http.controller decorator.`);
|
|
428
|
+
const schema = type_1.ReflectionClass.from(controller);
|
|
429
|
+
for (const action of controllerData.getActions()) {
|
|
430
|
+
const routeAction = {
|
|
431
|
+
type: 'controller',
|
|
432
|
+
controller,
|
|
433
|
+
module,
|
|
434
|
+
methodName: action.methodName
|
|
435
|
+
};
|
|
436
|
+
const routeConfig = createRouteConfigFromHttpAction(routeAction, action, module, controllerData);
|
|
437
|
+
routeConfig.module = module;
|
|
438
|
+
if (schema.hasMethod(action.methodName))
|
|
439
|
+
routeConfig.returnType = filterValidReturnType(schema.getMethod(action.methodName).getReturnType());
|
|
440
|
+
this.addRoute(routeConfig);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
addRoute(routeConfig) {
|
|
444
|
+
this.routes.push(routeConfig);
|
|
445
|
+
this.buildId++;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
exports.HttpRouterRegistry = HttpRouterRegistry;
|
|
449
|
+
HttpRouterRegistry.__type = [() => HttpRouterRegistryFunctionRegistrar, () => RouteConfig, 'routes', function () { return []; }, 'buildId', function () { return 1; }, 'getBuildId', () => RouteConfig, 'getRoutes', () => __ΩClassType, 'controller', () => injector_1.InjectorModule, 'module', 'addRouteForController', () => RouteConfig, 'routeConfig', 'addRoute', 'P7!P7"F3#<>$\'3%;>&P\'0\'PP7(F0)Pn*2+P"7,2-"0.PP7/20"015'];
|
|
450
|
+
class HttpRouter {
|
|
451
|
+
constructor(controllers, logger, config, middlewareRegistry = new app_1.MiddlewareRegistry, registry = new HttpRouterRegistry) {
|
|
452
|
+
this.logger = logger;
|
|
453
|
+
this.config = config;
|
|
454
|
+
this.middlewareRegistry = middlewareRegistry;
|
|
455
|
+
this.registry = registry;
|
|
456
|
+
this.buildId = 0;
|
|
457
|
+
for (const controller of controllers.controllers) {
|
|
458
|
+
this.addRouteForController(controller.controller, controller.module);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
getRoutes() {
|
|
462
|
+
return this.registry.getRoutes();
|
|
463
|
+
}
|
|
464
|
+
static forControllers(controllers, middlewareRegistry = new app_1.MiddlewareRegistry(), module = new injector_1.InjectorModule(), config = new module_config_js_1.HttpConfig()) {
|
|
465
|
+
return new this(new controllers_js_1.HttpControllers(controllers.map(__assignType(v => {
|
|
466
|
+
return (0, core_1.isClass)(v) ? { controller: v, module } : v;
|
|
467
|
+
}, ['v', '', 'P"2!"/"']))), new logger_1.Logger([], []), config, middlewareRegistry);
|
|
468
|
+
}
|
|
469
|
+
getRouteCode(compiler, routeConfig) {
|
|
470
|
+
const parsedRoute = parseRouteControllerAction(routeConfig);
|
|
471
|
+
const path = routeConfig.getFullPath();
|
|
472
|
+
const pathParamStarter = path.indexOf(':');
|
|
473
|
+
const prefix = path.substr(0, pathParamStarter);
|
|
474
|
+
const regexVar = compiler.reserveVariable('regex', new RegExp('^' + parsedRoute.regex + '$'));
|
|
475
|
+
const hasParameters = pathParamStarter !== -1 || parsedRoute.getParameters().length > 0;
|
|
476
|
+
const fullPath = routeConfig.getFullPath();
|
|
477
|
+
const middlewareConfigs = filterMiddlewaresForRoute(this.middlewareRegistry.configs, routeConfig, fullPath);
|
|
478
|
+
const parameters = [...parsedRoute.getParameters()];
|
|
479
|
+
//look through all http listeners and check if they need any http parameters.
|
|
480
|
+
//the router then parses
|
|
481
|
+
let matcher = `_path.startsWith(${JSON.stringify(prefix)}) && (_match = _path.match(${regexVar}))`;
|
|
482
|
+
if (!hasParameters) {
|
|
483
|
+
matcher = `_path === ${JSON.stringify(path)}`;
|
|
484
|
+
}
|
|
485
|
+
let middlewares = 'undefined';
|
|
486
|
+
if (middlewareConfigs.length) {
|
|
487
|
+
const middlewareItems = [];
|
|
488
|
+
for (const middlewareConfig of middlewareConfigs) {
|
|
489
|
+
const moduleVar = middlewareConfig.module ? ', ' + compiler.reserveVariable('module', middlewareConfig.module) : '';
|
|
490
|
+
for (const middleware of middlewareConfig.config.middlewares) {
|
|
491
|
+
if ((0, core_1.isClass)(middleware)) {
|
|
492
|
+
const classVar = compiler.reserveVariable('middlewareClassType', middleware);
|
|
493
|
+
middlewareItems.push(`{fn: function() {return _injector.get(${classVar}${moduleVar}).execute(...arguments) }, timeout: ${middlewareConfig.config.timeout}}`);
|
|
494
|
+
}
|
|
495
|
+
else {
|
|
496
|
+
middlewareItems.push(`{fn: ${compiler.reserveVariable('middlewareFn', middleware)}, timeout: ${middlewareConfig.config.timeout}}`);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
middlewares = `
|
|
501
|
+
function(_injector) {
|
|
502
|
+
return [${middlewareItems.join(', ')}];
|
|
503
|
+
}
|
|
504
|
+
`;
|
|
505
|
+
}
|
|
506
|
+
const parametersLoader = (0, request_parser_js_1.getRequestParserCodeForParameters)(compiler, this.config.parser, parameters, {
|
|
507
|
+
module: routeConfig.module,
|
|
508
|
+
resolverForToken: routeConfig.resolverForToken,
|
|
509
|
+
resolverForParameterName: routeConfig.resolverForParameterName,
|
|
510
|
+
pathParameterNames: parsedRoute.pathParameterNames,
|
|
511
|
+
routeConfig
|
|
512
|
+
});
|
|
513
|
+
let methodCheck = '';
|
|
514
|
+
if (routeConfig.httpMethods.length) {
|
|
515
|
+
methodCheck = '(' + routeConfig.httpMethods.map(__assignType(v => {
|
|
516
|
+
return `_method === '${v.toUpperCase()}'`;
|
|
517
|
+
}, ['v', '', 'P"2!"/"'])).join(' || ') + ') && ';
|
|
518
|
+
}
|
|
519
|
+
const routeConfigVar = compiler.reserveVariable('routeConfigVar', routeConfig);
|
|
520
|
+
return `
|
|
521
|
+
//=> ${routeConfig.httpMethods.join(',')} ${path}
|
|
522
|
+
if (${methodCheck}${matcher}) {
|
|
523
|
+
return {routeConfig: ${routeConfigVar}, parameters: ${parametersLoader}, uploadedFiles: uploadedFiles, middlewares: ${middlewares}};
|
|
524
|
+
}
|
|
525
|
+
`;
|
|
526
|
+
}
|
|
527
|
+
getRouteUrlResolveCode(compiler, routeConfig) {
|
|
528
|
+
const parsedRoute = parseRouteControllerAction(routeConfig);
|
|
529
|
+
let url = routeConfig.getFullPath();
|
|
530
|
+
url = url.replace(/:(\w+)/g, __assignType((a, name) => {
|
|
531
|
+
return `\${parameters.${name}}`;
|
|
532
|
+
}, ['a', 'name', '', 'P"2!"2""/#']));
|
|
533
|
+
const modify = [];
|
|
534
|
+
for (const parameter of parsedRoute.getParameters()) {
|
|
535
|
+
if (parameter.query || parameter.queries) {
|
|
536
|
+
const queryPath = parameter.typePath === undefined && parameter.query ? parameter.parameter.name : parameter.typePath || '';
|
|
537
|
+
if (parameter.parameter.type.kind === type_1.ReflectionKind.class || parameter.parameter.type.kind === type_1.ReflectionKind.objectLiteral) {
|
|
538
|
+
for (const property of type_1.ReflectionClass.from(parameter.parameter.type).getProperties()) {
|
|
539
|
+
const accessor = `parameters.${parameter.getName()}?.${property.name}`;
|
|
540
|
+
const thisPath = queryPath ? queryPath + '.' + property.name : property.name;
|
|
541
|
+
modify.push(`${accessor} !== undefined && query.push(${JSON.stringify(dotToUrlPath(thisPath))} + '=' + encodeURIComponent(${accessor}))`);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
else {
|
|
545
|
+
modify.push(`parameters.${parameter.getName()} !== undefined && query.push(${JSON.stringify(dotToUrlPath(queryPath))} + '=' + encodeURIComponent(parameters.${parameter.getName()}))`);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
return `
|
|
550
|
+
case ${JSON.stringify(routeConfig.name)}: {
|
|
551
|
+
let url = \`${url}\`;
|
|
552
|
+
let query = [];
|
|
553
|
+
${modify.join('\n')}
|
|
554
|
+
return url + (query.length ? '?'+query.join('&') : '');
|
|
555
|
+
}
|
|
556
|
+
`;
|
|
557
|
+
}
|
|
558
|
+
addRoute(routeConfig) {
|
|
559
|
+
this.registry.addRoute(routeConfig);
|
|
560
|
+
}
|
|
561
|
+
addRouteForController(controller, module) {
|
|
562
|
+
this.registry.addRouteForController(controller, module);
|
|
563
|
+
}
|
|
564
|
+
build() {
|
|
565
|
+
this.buildId = this.registry.getBuildId();
|
|
566
|
+
const compiler = new core_1.CompilerContext;
|
|
567
|
+
compiler.context.set('ValidationError', type_1.ValidationError);
|
|
568
|
+
compiler.context.set('qs', qs_1.default);
|
|
569
|
+
const code = [];
|
|
570
|
+
for (const route of this.getRoutes()) {
|
|
571
|
+
code.push(this.getRouteCode(compiler, route));
|
|
572
|
+
}
|
|
573
|
+
return compiler.build(`
|
|
574
|
+
let _match;
|
|
575
|
+
const _method = request.method || 'GET';
|
|
576
|
+
const _url = request.url || '/';
|
|
577
|
+
const _headers = request.headers || {};
|
|
578
|
+
const _qPosition = _url.indexOf('?');
|
|
579
|
+
let uploadedFiles = {};
|
|
580
|
+
const _path = _qPosition === -1 ? _url : _url.substr(0, _qPosition);
|
|
581
|
+
const _query = _qPosition === -1 ? {} : qs.parse(_url.substr(_qPosition + 1));
|
|
582
|
+
${code.join('\n')}
|
|
583
|
+
`, 'request');
|
|
584
|
+
}
|
|
585
|
+
buildUrlResolver() {
|
|
586
|
+
const compiler = new core_1.CompilerContext;
|
|
587
|
+
const code = [];
|
|
588
|
+
for (const route of this.getRoutes()) {
|
|
589
|
+
code.push(this.getRouteUrlResolveCode(compiler, route));
|
|
590
|
+
}
|
|
591
|
+
return compiler.build(`
|
|
592
|
+
switch (name) {
|
|
593
|
+
${code.join('\n')}
|
|
594
|
+
}
|
|
595
|
+
throw new Error('No route for name ' + name + ' found');
|
|
596
|
+
`, 'name', 'parameters');
|
|
597
|
+
}
|
|
598
|
+
resolveUrl(routeName, parameters = {}) {
|
|
599
|
+
if (!this.resolveFn) {
|
|
600
|
+
this.resolveFn = this.buildUrlResolver();
|
|
601
|
+
}
|
|
602
|
+
return this.resolveFn(routeName, parameters);
|
|
603
|
+
}
|
|
604
|
+
resolveRequest(request) {
|
|
605
|
+
if (!this.fn || this.buildId !== this.registry.getBuildId()) {
|
|
606
|
+
this.fn = this.build();
|
|
607
|
+
}
|
|
608
|
+
return this.fn(request);
|
|
609
|
+
}
|
|
610
|
+
resolve(method, url) {
|
|
611
|
+
method = method.toUpperCase();
|
|
612
|
+
return this.resolveRequest({ url, method });
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
exports.HttpRouter = HttpRouter;
|
|
616
|
+
HttpRouter.__type = [() => model_js_1.HttpRequest, 'request', () => __ΩResolvedController, '', 'fn', 'buildId', function () { return 0; }, 'name', 'parameters', 'resolveFn', () => controllers_js_1.HttpControllers, 'controllers', () => __ΩLoggerInterface, 'logger', () => module_config_js_1.HttpConfig, 'config', () => app_1.MiddlewareRegistry, 'middlewareRegistry', () => new app_1.MiddlewareRegistry, () => HttpRouterRegistry, 'registry', () => new HttpRouterRegistry, 'constructor', () => RouteConfig, 'getRoutes', () => __ΩClassType, () => injector_1.InjectorModule, 'module', () => __ΩClassType, 'controller', () => app_1.MiddlewareRegistry, () => new app_1.MiddlewareRegistry(), () => injector_1.InjectorModule, () => new injector_1.InjectorModule(), () => module_config_js_1.HttpConfig, () => new module_config_js_1.HttpConfig(), () => HttpRouter, 'forControllers', () => core_1.CompilerContext, 'compiler', () => RouteConfig, 'routeConfig', 'getRouteCode', () => core_1.CompilerContext, () => RouteConfig, 'getRouteUrlResolveCode', () => RouteConfig, 'addRoute', () => __ΩClassType, () => injector_1.InjectorModule, 'addRouteForController', () => model_js_1.HttpRequest, () => __ΩResolvedController, 'build', 'buildUrlResolver', 'routeName', () => ({}), 'resolveUrl', () => model_js_1.HttpRequest, () => __ΩResolvedController, 'resolveRequest', 'method', 'url', () => __ΩResolvedController, 'resolve', 'PP7!2"Pn#-J/$3%8<\'3&<>\'P&2(P&"LM2)&/$3*8<PP7+2,n-2.;P7/20;P7122;>3P7425;>6"07PP78F09PPn:PP"7;4<n=4>MJF2,P7?22>@P"7A2<>BP7C20>DP7E0FsPP7G2HP7I2J&0K<PP7L2HP7M2J&0N<PP7O2J"0PPnQ2>P"7R2<"0SPPP7T2"PnU-J/$0V<P"0W<P&2XP&"LM2)>Y&0ZPP7[2"Pn\\-J0]P&2^&2_Pn`-J0a5'];
|
|
617
|
+
//# sourceMappingURL=router.js.map
|