@nattyjs/core 0.0.1-beta.62 → 0.0.1-beta.64
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/dist/index.cjs +95 -12
- package/dist/index.d.ts +62 -62
- package/dist/index.mjs +93 -13
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -145,13 +145,28 @@ async function startWebSchedule(config) {
|
|
|
145
145
|
function init(config, appConfig) {
|
|
146
146
|
common.commonContainer.setupConfig(config);
|
|
147
147
|
common.commonContainer.setEnvTsDefinition(appConfig.envTsDefinition);
|
|
148
|
-
|
|
148
|
+
setupLegacyTypes(appConfig.types);
|
|
149
|
+
nattyContainer.setup(config, appConfig.routes, appConfig.resolver || defaultResolver);
|
|
149
150
|
callLifeCycleEvents(config, true);
|
|
150
151
|
const result = initializeModule(config);
|
|
151
152
|
callLifeCycleEvents(config);
|
|
152
153
|
startWebSchedules(config.webSchedules);
|
|
153
154
|
return result;
|
|
154
155
|
}
|
|
156
|
+
function setupLegacyTypes(types) {
|
|
157
|
+
if (types) {
|
|
158
|
+
common.commonContainer.types = {};
|
|
159
|
+
for (const [name, type] of Object.entries(types))
|
|
160
|
+
common.commonContainer.registerType({ name, ...type });
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function defaultResolver(path) {
|
|
164
|
+
if (typeof path === "function")
|
|
165
|
+
return path();
|
|
166
|
+
if (typeof path === "string")
|
|
167
|
+
return require(path);
|
|
168
|
+
return {};
|
|
169
|
+
}
|
|
155
170
|
function initializeModule(config) {
|
|
156
171
|
if (config.app) {
|
|
157
172
|
return config.app.init(config);
|
|
@@ -1016,11 +1031,12 @@ function getTypeName(typeName) {
|
|
|
1016
1031
|
}
|
|
1017
1032
|
|
|
1018
1033
|
class ModelBindingContext extends ParameterTypeConverter {
|
|
1019
|
-
constructor(type, typeInfo, data) {
|
|
1034
|
+
constructor(type, typeInfo, data, throwOnValidationError = false) {
|
|
1020
1035
|
super();
|
|
1021
1036
|
this.type = type;
|
|
1022
1037
|
this.typeInfo = typeInfo;
|
|
1023
1038
|
this.data = data;
|
|
1039
|
+
this.throwOnValidationError = throwOnValidationError;
|
|
1024
1040
|
this.serialize();
|
|
1025
1041
|
}
|
|
1026
1042
|
serialize() {
|
|
@@ -1030,7 +1046,7 @@ class ModelBindingContext extends ParameterTypeConverter {
|
|
|
1030
1046
|
else
|
|
1031
1047
|
this.data = body;
|
|
1032
1048
|
this.instance = this.convertToInstance(this.type, this.data);
|
|
1033
|
-
if (!this.isValid)
|
|
1049
|
+
if (this.throwOnValidationError && !this.isValid)
|
|
1034
1050
|
throw new HttpBadRequestException(CreateProblemDetail(this.type, this.errors));
|
|
1035
1051
|
}
|
|
1036
1052
|
get isValid() {
|
|
@@ -1091,8 +1107,11 @@ class RequestProcessor extends RouteParser {
|
|
|
1091
1107
|
const authentication = this.getAuthenticationClass();
|
|
1092
1108
|
const authenticationFilter = authentication ? this.resolveFilter(authentication) : void 0;
|
|
1093
1109
|
const anonymousInfo = decoratorStateContainer.getInfo(this.routeInfo.controller.name, this.routeInfo.methodInfo.name, DecoratorType.anonymous);
|
|
1094
|
-
if (!
|
|
1095
|
-
|
|
1110
|
+
if (!authenticationFilter) {
|
|
1111
|
+
if (common.commonContainer.nattyConfig?.secure?.denyByDefault && !anonymousInfo.controllerConfig && !anonymousInfo.methodConfig)
|
|
1112
|
+
throw new UnauthorizedAccessException(DENY_BY_DEFAULT);
|
|
1113
|
+
return;
|
|
1114
|
+
}
|
|
1096
1115
|
if (authenticationFilter) {
|
|
1097
1116
|
const result = await authenticationFilter.onAuthentication(this.httpContext);
|
|
1098
1117
|
this.httpContext.user = result;
|
|
@@ -1112,7 +1131,7 @@ class RequestProcessor extends RouteParser {
|
|
|
1112
1131
|
this.routeInfo,
|
|
1113
1132
|
authorizeConfig.methodConfig || authorizeConfig.controllerConfig
|
|
1114
1133
|
);
|
|
1115
|
-
const result = await authorizationFilter.onAuthorization(authorizationContext);
|
|
1134
|
+
const result = await authorizationFilter.onAuthorization(authorizationContext, authorizationContext.config);
|
|
1116
1135
|
if (!result)
|
|
1117
1136
|
throw new ForbiddenAccessException(authorizationFilter.onFailedAuthorization());
|
|
1118
1137
|
}
|
|
@@ -1294,6 +1313,7 @@ class Resolver extends RequestProcessor {
|
|
|
1294
1313
|
}
|
|
1295
1314
|
registerDependency() {
|
|
1296
1315
|
this.httpContext.services = nattyServiceResolver.createScope();
|
|
1316
|
+
common.commonContainer.setMetadata(HttpContext.name, HttpContext, "services");
|
|
1297
1317
|
this.httpContext.services.set(HttpContext, this.httpContext);
|
|
1298
1318
|
}
|
|
1299
1319
|
getControllerInstance() {
|
|
@@ -1302,18 +1322,27 @@ class Resolver extends RequestProcessor {
|
|
|
1302
1322
|
const instance = new controller(...parameters);
|
|
1303
1323
|
return instance;
|
|
1304
1324
|
}
|
|
1305
|
-
resolveClass(
|
|
1306
|
-
return this.httpContext.services.get(
|
|
1325
|
+
resolveClass(token) {
|
|
1326
|
+
return this.httpContext.services.get(token);
|
|
1307
1327
|
}
|
|
1308
1328
|
resolveConstructorParameters() {
|
|
1309
1329
|
let parameters = [];
|
|
1310
1330
|
for (const parameter of this.routeInfo.parameters) {
|
|
1311
|
-
const
|
|
1312
|
-
if (
|
|
1313
|
-
parameters.push(this.resolveClass(
|
|
1331
|
+
const token = this.getConstructorParameterToken(parameter);
|
|
1332
|
+
if (token)
|
|
1333
|
+
parameters.push(this.resolveClass(token));
|
|
1314
1334
|
}
|
|
1315
1335
|
return parameters;
|
|
1316
1336
|
}
|
|
1337
|
+
getConstructorParameterToken(parameter) {
|
|
1338
|
+
if (parameter.tokenKey) {
|
|
1339
|
+
return Symbol.for(parameter.tokenKey);
|
|
1340
|
+
}
|
|
1341
|
+
if (parameter.type) {
|
|
1342
|
+
return this.getClass(parameter.type);
|
|
1343
|
+
}
|
|
1344
|
+
return void 0;
|
|
1345
|
+
}
|
|
1317
1346
|
getMethodParameters() {
|
|
1318
1347
|
const parameters = new Array();
|
|
1319
1348
|
for (const parameter of this.routeInfo.methodInfo.parameters) {
|
|
@@ -1325,7 +1354,7 @@ class Resolver extends RequestProcessor {
|
|
|
1325
1354
|
else if (queryParams[parameter.name] !== void 0)
|
|
1326
1355
|
parameters.push(queryParams[parameter.name]);
|
|
1327
1356
|
else if (typeInfo && this.httpContext.request.body.json) {
|
|
1328
|
-
const context = new ModelBindingContext(parameter.type, typeInfo.props, this.httpContext.request.body.json);
|
|
1357
|
+
const context = new ModelBindingContext(parameter.type, typeInfo.props, this.httpContext.request.body.json, true);
|
|
1329
1358
|
parameters.push(context);
|
|
1330
1359
|
}
|
|
1331
1360
|
}
|
|
@@ -1789,6 +1818,57 @@ function singleton(token) {
|
|
|
1789
1818
|
return injectable({ lifetime: Lifetime.Singleton, token });
|
|
1790
1819
|
}
|
|
1791
1820
|
|
|
1821
|
+
function getTokenTypeName(token) {
|
|
1822
|
+
if (typeof token !== "symbol") {
|
|
1823
|
+
return void 0;
|
|
1824
|
+
}
|
|
1825
|
+
const key = Symbol.keyFor(token);
|
|
1826
|
+
if (!key) {
|
|
1827
|
+
return void 0;
|
|
1828
|
+
}
|
|
1829
|
+
const separatorIndex = key.lastIndexOf("#");
|
|
1830
|
+
if (separatorIndex < 0 || separatorIndex === key.length - 1) {
|
|
1831
|
+
return void 0;
|
|
1832
|
+
}
|
|
1833
|
+
return key.slice(separatorIndex + 1);
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
function registerLifetime(targetConstructor, lifetime) {
|
|
1837
|
+
if (lifetime === Lifetime.Singleton) {
|
|
1838
|
+
nattyServiceResolver.addSingleton(targetConstructor);
|
|
1839
|
+
return;
|
|
1840
|
+
}
|
|
1841
|
+
if (lifetime === Lifetime.Scoped) {
|
|
1842
|
+
nattyServiceResolver.addScoped(targetConstructor);
|
|
1843
|
+
return;
|
|
1844
|
+
}
|
|
1845
|
+
if (lifetime === Lifetime.Transient) {
|
|
1846
|
+
nattyServiceResolver.addTransient(targetConstructor);
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
function registerAliasTypeName(token) {
|
|
1850
|
+
const typeName = getTokenTypeName(token);
|
|
1851
|
+
if (typeName) {
|
|
1852
|
+
common.commonContainer.setMetadata(typeName, token, "services");
|
|
1853
|
+
}
|
|
1854
|
+
}
|
|
1855
|
+
function registerDiToken(targetConstructor, token, options = {}) {
|
|
1856
|
+
common.commonContainer.setMetadata(targetConstructor.name, targetConstructor, "services");
|
|
1857
|
+
registerAliasTypeName(token);
|
|
1858
|
+
registerLifetime(targetConstructor, options.lifetime);
|
|
1859
|
+
nattyServiceResolver.addTransient(token, void 0, (serviceProvider) => serviceProvider.get(targetConstructor));
|
|
1860
|
+
}
|
|
1861
|
+
|
|
1862
|
+
function di(token, options = {}) {
|
|
1863
|
+
return (targetConstructor) => {
|
|
1864
|
+
registerDiToken(targetConstructor, token, options);
|
|
1865
|
+
};
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1868
|
+
function createServiceScope() {
|
|
1869
|
+
return nattyServiceResolver.createScope();
|
|
1870
|
+
}
|
|
1871
|
+
|
|
1792
1872
|
exports.$request = $request;
|
|
1793
1873
|
exports.AbstractModelState = AbstractModelState;
|
|
1794
1874
|
exports.AcceptedException = AcceptedException;
|
|
@@ -1812,6 +1892,7 @@ exports.HttpNotFoundException = HttpNotFoundException;
|
|
|
1812
1892
|
exports.HttpResponse = HttpResponse;
|
|
1813
1893
|
exports.HttpStatusCode = HttpStatusCode;
|
|
1814
1894
|
exports.HttpUnprocessableEntityException = HttpUnprocessableEntityException;
|
|
1895
|
+
exports.Lifetime = Lifetime;
|
|
1815
1896
|
exports.ModelBindingContext = ModelBindingContext;
|
|
1816
1897
|
exports.NoContentResult = NoContentResult;
|
|
1817
1898
|
exports.NotFoundResult = NotFoundResult;
|
|
@@ -1835,10 +1916,12 @@ exports.authenticationOnly = authenticationOnly;
|
|
|
1835
1916
|
exports.authorize = authorize;
|
|
1836
1917
|
exports.badRequest = badRequest;
|
|
1837
1918
|
exports.conflict = conflict;
|
|
1919
|
+
exports.createServiceScope = createServiceScope;
|
|
1838
1920
|
exports.created = created;
|
|
1839
1921
|
exports.createdAt = createdAt;
|
|
1840
1922
|
exports.createdWith = createdWith;
|
|
1841
1923
|
exports.defineNattyConfig = defineNattyConfig;
|
|
1924
|
+
exports.di = di;
|
|
1842
1925
|
exports.entityContainer = entityContainer;
|
|
1843
1926
|
exports.file = file;
|
|
1844
1927
|
exports.filter = filter;
|
package/dist/index.d.ts
CHANGED
|
@@ -8,8 +8,9 @@ interface DecoratorInfo$1 {
|
|
|
8
8
|
|
|
9
9
|
interface TypeInfo$1 {
|
|
10
10
|
name: string;
|
|
11
|
-
documentation
|
|
12
|
-
type
|
|
11
|
+
documentation?: string;
|
|
12
|
+
type?: string;
|
|
13
|
+
tokenKey?: string;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
interface ParameterInfo {
|
|
@@ -59,64 +60,6 @@ declare function put(path?: string): (target: any, propertyKey?: string, descrip
|
|
|
59
60
|
|
|
60
61
|
declare function Delete(): (target: any, propertyKey?: string, descriptor?: any) => void;
|
|
61
62
|
|
|
62
|
-
interface RouteConfig {
|
|
63
|
-
controller: Function;
|
|
64
|
-
parameters: Array<{
|
|
65
|
-
name: string;
|
|
66
|
-
type: string;
|
|
67
|
-
}>;
|
|
68
|
-
get: {
|
|
69
|
-
[key: string]: {
|
|
70
|
-
name: string;
|
|
71
|
-
parameters: Array<{
|
|
72
|
-
name: string;
|
|
73
|
-
type: string;
|
|
74
|
-
}>;
|
|
75
|
-
returnType: string;
|
|
76
|
-
};
|
|
77
|
-
};
|
|
78
|
-
post: {
|
|
79
|
-
[key: string]: {
|
|
80
|
-
name: string;
|
|
81
|
-
parameters: Array<{
|
|
82
|
-
name: string;
|
|
83
|
-
type: string;
|
|
84
|
-
}>;
|
|
85
|
-
returnType: string;
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
|
-
put: {
|
|
89
|
-
[key: string]: {
|
|
90
|
-
name: string;
|
|
91
|
-
parameters: Array<{
|
|
92
|
-
name: string;
|
|
93
|
-
type: string;
|
|
94
|
-
}>;
|
|
95
|
-
returnType: string;
|
|
96
|
-
};
|
|
97
|
-
};
|
|
98
|
-
delete: {
|
|
99
|
-
[key: string]: {
|
|
100
|
-
name: string;
|
|
101
|
-
parameters: Array<{
|
|
102
|
-
name: string;
|
|
103
|
-
type: string;
|
|
104
|
-
}>;
|
|
105
|
-
returnType: string;
|
|
106
|
-
};
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
declare function init(config: NattyConfig, appConfig: {
|
|
111
|
-
routes: {
|
|
112
|
-
[key: string]: RouteConfig;
|
|
113
|
-
};
|
|
114
|
-
envTsDefinition: {
|
|
115
|
-
[key: string]: string;
|
|
116
|
-
};
|
|
117
|
-
resolver: (path: string) => {};
|
|
118
|
-
}): any;
|
|
119
|
-
|
|
120
63
|
interface DecoratorInfo {
|
|
121
64
|
httpMethod: string;
|
|
122
65
|
route: string;
|
|
@@ -126,6 +69,7 @@ interface TypeInfo {
|
|
|
126
69
|
name: string;
|
|
127
70
|
documentation?: string;
|
|
128
71
|
type?: string;
|
|
72
|
+
tokenKey?: string;
|
|
129
73
|
}
|
|
130
74
|
interface MethodInfo extends TypeInfo {
|
|
131
75
|
parameters?: TypeInfo[];
|
|
@@ -250,6 +194,50 @@ interface IHttpResult {
|
|
|
250
194
|
getResponse(): HttpResponseInit;
|
|
251
195
|
}
|
|
252
196
|
|
|
197
|
+
interface RouteConfig {
|
|
198
|
+
controller: Function;
|
|
199
|
+
parameters: TypeInfo$1[];
|
|
200
|
+
get: {
|
|
201
|
+
[key: string]: {
|
|
202
|
+
name: string;
|
|
203
|
+
parameters: TypeInfo$1[];
|
|
204
|
+
returnType: string;
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
post: {
|
|
208
|
+
[key: string]: {
|
|
209
|
+
name: string;
|
|
210
|
+
parameters: TypeInfo$1[];
|
|
211
|
+
returnType: string;
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
put: {
|
|
215
|
+
[key: string]: {
|
|
216
|
+
name: string;
|
|
217
|
+
parameters: TypeInfo$1[];
|
|
218
|
+
returnType: string;
|
|
219
|
+
};
|
|
220
|
+
};
|
|
221
|
+
delete: {
|
|
222
|
+
[key: string]: {
|
|
223
|
+
name: string;
|
|
224
|
+
parameters: TypeInfo$1[];
|
|
225
|
+
returnType: string;
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
declare function init(config: NattyConfig, appConfig: {
|
|
231
|
+
routes: {
|
|
232
|
+
[key: string]: RouteConfig;
|
|
233
|
+
};
|
|
234
|
+
types?: TypesInfo;
|
|
235
|
+
envTsDefinition: {
|
|
236
|
+
[key: string]: string;
|
|
237
|
+
};
|
|
238
|
+
resolver?: (path: any) => any;
|
|
239
|
+
}): any;
|
|
240
|
+
|
|
253
241
|
declare class HttpRequest {
|
|
254
242
|
private httpRequest;
|
|
255
243
|
constructor(http: HttpRequestInit);
|
|
@@ -443,10 +431,11 @@ declare class ModelBindingContext extends ParameterTypeConverter {
|
|
|
443
431
|
private type;
|
|
444
432
|
private typeInfo;
|
|
445
433
|
private data;
|
|
434
|
+
private throwOnValidationError;
|
|
446
435
|
root: any;
|
|
447
436
|
parent: any;
|
|
448
437
|
instance: any;
|
|
449
|
-
constructor(type: string, typeInfo: TypeInfo[], data: any);
|
|
438
|
+
constructor(type: string, typeInfo: TypeInfo[], data: any, throwOnValidationError?: boolean);
|
|
450
439
|
private serialize;
|
|
451
440
|
get isValid(): boolean;
|
|
452
441
|
get errors(): {};
|
|
@@ -690,4 +679,15 @@ declare function transient<T = unknown>(): ClassDecorator;
|
|
|
690
679
|
|
|
691
680
|
declare function singleton<T = unknown>(): ClassDecorator;
|
|
692
681
|
|
|
693
|
-
|
|
682
|
+
interface DiOptions {
|
|
683
|
+
lifetime?: Lifetime;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
declare function di<T = unknown>(token: Token<T> | unknown): ClassDecorator;
|
|
687
|
+
declare function di<T = unknown>(token: Token<T> | unknown, options: DiOptions): ClassDecorator;
|
|
688
|
+
|
|
689
|
+
declare function registerType<T>(): unknown;
|
|
690
|
+
|
|
691
|
+
declare function createServiceScope(): NattyScope;
|
|
692
|
+
|
|
693
|
+
export { $request, AbstractModelState, AcceptedException, AcceptedResult, BadRequestResult, BaseController, BaseResult, BuildOptions, ClassTypeInfo, ConflictResult, CreateProblemDetail, CreatedResult, Delete, DiOptions, FileResult, ForbiddenAccessException, ForbiddenAccessInfoResult, HttpBadRequestException, HttpConflictException, HttpContext, HttpException, HttpHandler, HttpModule, HttpNotFoundException, HttpResponse, HttpStatusCode, HttpUnprocessableEntityException, Lifetime, MethodInfo$1 as MethodInfo, ModelBindingContext, NoContentResult, NotFoundResult, OkResult, ParameterInfo, ProblemDetailsException, ProblemResult, RedirectException, RedirectPermanentException, RedirectResult, ResponseExtras, Results, RetryAfter, RunOn, TooManyRequestsException, TooManyRequestsResult, TypeInfo$1 as TypeInfo, UnAuthorizedResult, UnauthorizedAccessException, UnprocessableEntityResult, ValidationProblemDetailsException, accepted, anonymous, authenticationOnly, authorize, badRequest, conflict, createServiceScope, created, createdAt, createdWith, defineNattyConfig, di, entityContainer, file, filter, forbiddenAccess, forbiddenAccessInfo, get, init, injectable, noContent, notFound, notFoundWith, ok, post, problem, put, redirect, redirectPermanent, registerDecorator, registerType, route, scoped, setEnvInfo, singleton, tooManyRequests, transient, unAuthorized, unprocessableEntity, useFilter, validationProblem };
|
package/dist/index.mjs
CHANGED
|
@@ -143,13 +143,28 @@ async function startWebSchedule(config) {
|
|
|
143
143
|
function init(config, appConfig) {
|
|
144
144
|
commonContainer.setupConfig(config);
|
|
145
145
|
commonContainer.setEnvTsDefinition(appConfig.envTsDefinition);
|
|
146
|
-
|
|
146
|
+
setupLegacyTypes(appConfig.types);
|
|
147
|
+
nattyContainer.setup(config, appConfig.routes, appConfig.resolver || defaultResolver);
|
|
147
148
|
callLifeCycleEvents(config, true);
|
|
148
149
|
const result = initializeModule(config);
|
|
149
150
|
callLifeCycleEvents(config);
|
|
150
151
|
startWebSchedules(config.webSchedules);
|
|
151
152
|
return result;
|
|
152
153
|
}
|
|
154
|
+
function setupLegacyTypes(types) {
|
|
155
|
+
if (types) {
|
|
156
|
+
commonContainer.types = {};
|
|
157
|
+
for (const [name, type] of Object.entries(types))
|
|
158
|
+
commonContainer.registerType({ name, ...type });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function defaultResolver(path) {
|
|
162
|
+
if (typeof path === "function")
|
|
163
|
+
return path();
|
|
164
|
+
if (typeof path === "string")
|
|
165
|
+
return require(path);
|
|
166
|
+
return {};
|
|
167
|
+
}
|
|
153
168
|
function initializeModule(config) {
|
|
154
169
|
if (config.app) {
|
|
155
170
|
return config.app.init(config);
|
|
@@ -1014,11 +1029,12 @@ function getTypeName(typeName) {
|
|
|
1014
1029
|
}
|
|
1015
1030
|
|
|
1016
1031
|
class ModelBindingContext extends ParameterTypeConverter {
|
|
1017
|
-
constructor(type, typeInfo, data) {
|
|
1032
|
+
constructor(type, typeInfo, data, throwOnValidationError = false) {
|
|
1018
1033
|
super();
|
|
1019
1034
|
this.type = type;
|
|
1020
1035
|
this.typeInfo = typeInfo;
|
|
1021
1036
|
this.data = data;
|
|
1037
|
+
this.throwOnValidationError = throwOnValidationError;
|
|
1022
1038
|
this.serialize();
|
|
1023
1039
|
}
|
|
1024
1040
|
serialize() {
|
|
@@ -1028,7 +1044,7 @@ class ModelBindingContext extends ParameterTypeConverter {
|
|
|
1028
1044
|
else
|
|
1029
1045
|
this.data = body;
|
|
1030
1046
|
this.instance = this.convertToInstance(this.type, this.data);
|
|
1031
|
-
if (!this.isValid)
|
|
1047
|
+
if (this.throwOnValidationError && !this.isValid)
|
|
1032
1048
|
throw new HttpBadRequestException(CreateProblemDetail(this.type, this.errors));
|
|
1033
1049
|
}
|
|
1034
1050
|
get isValid() {
|
|
@@ -1089,8 +1105,11 @@ class RequestProcessor extends RouteParser {
|
|
|
1089
1105
|
const authentication = this.getAuthenticationClass();
|
|
1090
1106
|
const authenticationFilter = authentication ? this.resolveFilter(authentication) : void 0;
|
|
1091
1107
|
const anonymousInfo = decoratorStateContainer.getInfo(this.routeInfo.controller.name, this.routeInfo.methodInfo.name, DecoratorType.anonymous);
|
|
1092
|
-
if (!
|
|
1093
|
-
|
|
1108
|
+
if (!authenticationFilter) {
|
|
1109
|
+
if (commonContainer.nattyConfig?.secure?.denyByDefault && !anonymousInfo.controllerConfig && !anonymousInfo.methodConfig)
|
|
1110
|
+
throw new UnauthorizedAccessException(DENY_BY_DEFAULT);
|
|
1111
|
+
return;
|
|
1112
|
+
}
|
|
1094
1113
|
if (authenticationFilter) {
|
|
1095
1114
|
const result = await authenticationFilter.onAuthentication(this.httpContext);
|
|
1096
1115
|
this.httpContext.user = result;
|
|
@@ -1110,7 +1129,7 @@ class RequestProcessor extends RouteParser {
|
|
|
1110
1129
|
this.routeInfo,
|
|
1111
1130
|
authorizeConfig.methodConfig || authorizeConfig.controllerConfig
|
|
1112
1131
|
);
|
|
1113
|
-
const result = await authorizationFilter.onAuthorization(authorizationContext);
|
|
1132
|
+
const result = await authorizationFilter.onAuthorization(authorizationContext, authorizationContext.config);
|
|
1114
1133
|
if (!result)
|
|
1115
1134
|
throw new ForbiddenAccessException(authorizationFilter.onFailedAuthorization());
|
|
1116
1135
|
}
|
|
@@ -1292,6 +1311,7 @@ class Resolver extends RequestProcessor {
|
|
|
1292
1311
|
}
|
|
1293
1312
|
registerDependency() {
|
|
1294
1313
|
this.httpContext.services = nattyServiceResolver.createScope();
|
|
1314
|
+
commonContainer.setMetadata(HttpContext.name, HttpContext, "services");
|
|
1295
1315
|
this.httpContext.services.set(HttpContext, this.httpContext);
|
|
1296
1316
|
}
|
|
1297
1317
|
getControllerInstance() {
|
|
@@ -1300,18 +1320,27 @@ class Resolver extends RequestProcessor {
|
|
|
1300
1320
|
const instance = new controller(...parameters);
|
|
1301
1321
|
return instance;
|
|
1302
1322
|
}
|
|
1303
|
-
resolveClass(
|
|
1304
|
-
return this.httpContext.services.get(
|
|
1323
|
+
resolveClass(token) {
|
|
1324
|
+
return this.httpContext.services.get(token);
|
|
1305
1325
|
}
|
|
1306
1326
|
resolveConstructorParameters() {
|
|
1307
1327
|
let parameters = [];
|
|
1308
1328
|
for (const parameter of this.routeInfo.parameters) {
|
|
1309
|
-
const
|
|
1310
|
-
if (
|
|
1311
|
-
parameters.push(this.resolveClass(
|
|
1329
|
+
const token = this.getConstructorParameterToken(parameter);
|
|
1330
|
+
if (token)
|
|
1331
|
+
parameters.push(this.resolveClass(token));
|
|
1312
1332
|
}
|
|
1313
1333
|
return parameters;
|
|
1314
1334
|
}
|
|
1335
|
+
getConstructorParameterToken(parameter) {
|
|
1336
|
+
if (parameter.tokenKey) {
|
|
1337
|
+
return Symbol.for(parameter.tokenKey);
|
|
1338
|
+
}
|
|
1339
|
+
if (parameter.type) {
|
|
1340
|
+
return this.getClass(parameter.type);
|
|
1341
|
+
}
|
|
1342
|
+
return void 0;
|
|
1343
|
+
}
|
|
1315
1344
|
getMethodParameters() {
|
|
1316
1345
|
const parameters = new Array();
|
|
1317
1346
|
for (const parameter of this.routeInfo.methodInfo.parameters) {
|
|
@@ -1323,7 +1352,7 @@ class Resolver extends RequestProcessor {
|
|
|
1323
1352
|
else if (queryParams[parameter.name] !== void 0)
|
|
1324
1353
|
parameters.push(queryParams[parameter.name]);
|
|
1325
1354
|
else if (typeInfo && this.httpContext.request.body.json) {
|
|
1326
|
-
const context = new ModelBindingContext(parameter.type, typeInfo.props, this.httpContext.request.body.json);
|
|
1355
|
+
const context = new ModelBindingContext(parameter.type, typeInfo.props, this.httpContext.request.body.json, true);
|
|
1327
1356
|
parameters.push(context);
|
|
1328
1357
|
}
|
|
1329
1358
|
}
|
|
@@ -1787,4 +1816,55 @@ function singleton(token) {
|
|
|
1787
1816
|
return injectable({ lifetime: Lifetime.Singleton, token });
|
|
1788
1817
|
}
|
|
1789
1818
|
|
|
1790
|
-
|
|
1819
|
+
function getTokenTypeName(token) {
|
|
1820
|
+
if (typeof token !== "symbol") {
|
|
1821
|
+
return void 0;
|
|
1822
|
+
}
|
|
1823
|
+
const key = Symbol.keyFor(token);
|
|
1824
|
+
if (!key) {
|
|
1825
|
+
return void 0;
|
|
1826
|
+
}
|
|
1827
|
+
const separatorIndex = key.lastIndexOf("#");
|
|
1828
|
+
if (separatorIndex < 0 || separatorIndex === key.length - 1) {
|
|
1829
|
+
return void 0;
|
|
1830
|
+
}
|
|
1831
|
+
return key.slice(separatorIndex + 1);
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1834
|
+
function registerLifetime(targetConstructor, lifetime) {
|
|
1835
|
+
if (lifetime === Lifetime.Singleton) {
|
|
1836
|
+
nattyServiceResolver.addSingleton(targetConstructor);
|
|
1837
|
+
return;
|
|
1838
|
+
}
|
|
1839
|
+
if (lifetime === Lifetime.Scoped) {
|
|
1840
|
+
nattyServiceResolver.addScoped(targetConstructor);
|
|
1841
|
+
return;
|
|
1842
|
+
}
|
|
1843
|
+
if (lifetime === Lifetime.Transient) {
|
|
1844
|
+
nattyServiceResolver.addTransient(targetConstructor);
|
|
1845
|
+
}
|
|
1846
|
+
}
|
|
1847
|
+
function registerAliasTypeName(token) {
|
|
1848
|
+
const typeName = getTokenTypeName(token);
|
|
1849
|
+
if (typeName) {
|
|
1850
|
+
commonContainer.setMetadata(typeName, token, "services");
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
function registerDiToken(targetConstructor, token, options = {}) {
|
|
1854
|
+
commonContainer.setMetadata(targetConstructor.name, targetConstructor, "services");
|
|
1855
|
+
registerAliasTypeName(token);
|
|
1856
|
+
registerLifetime(targetConstructor, options.lifetime);
|
|
1857
|
+
nattyServiceResolver.addTransient(token, void 0, (serviceProvider) => serviceProvider.get(targetConstructor));
|
|
1858
|
+
}
|
|
1859
|
+
|
|
1860
|
+
function di(token, options = {}) {
|
|
1861
|
+
return (targetConstructor) => {
|
|
1862
|
+
registerDiToken(targetConstructor, token, options);
|
|
1863
|
+
};
|
|
1864
|
+
}
|
|
1865
|
+
|
|
1866
|
+
function createServiceScope() {
|
|
1867
|
+
return nattyServiceResolver.createScope();
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
export { $request, AbstractModelState, AcceptedException, AcceptedResult, BadRequestResult, BaseController, BaseResult, ConflictResult, CreateProblemDetail, CreatedResult, Delete, FileResult, ForbiddenAccessException, ForbiddenAccessInfoResult, HttpBadRequestException, HttpConflictException, HttpContext, HttpException, HttpHandler, HttpNotFoundException, HttpResponse, HttpStatusCode, HttpUnprocessableEntityException, Lifetime, ModelBindingContext, NoContentResult, NotFoundResult, OkResult, ProblemDetailsException, ProblemResult, RedirectException, RedirectPermanentException, RedirectResult, Results, RunOn, TooManyRequestsException, TooManyRequestsResult, UnAuthorizedResult, UnauthorizedAccessException, UnprocessableEntityResult, ValidationProblemDetailsException, accepted, anonymous, authenticationOnly, authorize, badRequest, conflict, createServiceScope, created, createdAt, createdWith, defineNattyConfig, di, entityContainer, file, filter, forbiddenAccess, forbiddenAccessInfo, get, init, injectable, noContent, notFound, notFoundWith, ok, post, problem, put, redirect, redirectPermanent, registerDecorator, route, scoped, setEnvInfo, singleton, tooManyRequests, transient, unAuthorized, unprocessableEntity, useFilter, validationProblem };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nattyjs/core",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.64",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"author": "ajayojha",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"reflect-metadata": "0.2.2",
|
|
19
19
|
"path-to-regexp": "6.2.1",
|
|
20
|
-
"@nattyjs/common": "0.0.1-beta.
|
|
20
|
+
"@nattyjs/common": "0.0.1-beta.64"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"unbuild": "1.2.1"
|