@tsed/platform-middlewares 7.0.0-alpha.2 → 7.0.0-beta.11
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/lib/cjs/constants/constants.js +34 -0
- package/lib/cjs/constants/constants.js.map +1 -0
- package/lib/cjs/decorators/authOptions.js +44 -0
- package/lib/cjs/decorators/authOptions.js.map +1 -0
- package/lib/cjs/decorators/middleware.js +17 -0
- package/lib/cjs/decorators/middleware.js.map +1 -0
- package/lib/cjs/decorators/use.js +43 -0
- package/lib/cjs/decorators/use.js.map +1 -0
- package/lib/cjs/decorators/useAfter.js +46 -0
- package/lib/cjs/decorators/useAfter.js.map +1 -0
- package/lib/cjs/decorators/useAuth.js +45 -0
- package/lib/cjs/decorators/useAuth.js.map +1 -0
- package/lib/cjs/decorators/useBefore.js +45 -0
- package/lib/cjs/decorators/useBefore.js.map +1 -0
- package/lib/cjs/decorators/useBeforeEach.js +40 -0
- package/lib/cjs/decorators/useBeforeEach.js.map +1 -0
- package/lib/cjs/domain/MiddlewareMethods.js +3 -0
- package/lib/cjs/domain/MiddlewareMethods.js.map +1 -0
- package/lib/cjs/domain/PlatformMiddlewareSettings.js +3 -0
- package/lib/cjs/domain/PlatformMiddlewareSettings.js.map +1 -0
- package/lib/cjs/index.js +18 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/utils/getMiddlewaresForHook.js +26 -0
- package/lib/cjs/utils/getMiddlewaresForHook.js.map +1 -0
- package/lib/esm/constants/constants.js +31 -0
- package/lib/esm/constants/constants.js.map +1 -0
- package/lib/esm/decorators/authOptions.js +40 -0
- package/lib/esm/decorators/authOptions.js.map +1 -0
- package/lib/esm/decorators/middleware.js +13 -0
- package/lib/esm/decorators/middleware.js.map +1 -0
- package/lib/esm/decorators/use.js +39 -0
- package/lib/esm/decorators/use.js.map +1 -0
- package/lib/esm/decorators/useAfter.js +42 -0
- package/lib/esm/decorators/useAfter.js.map +1 -0
- package/lib/esm/decorators/useAuth.js +41 -0
- package/lib/esm/decorators/useAuth.js.map +1 -0
- package/lib/esm/decorators/useBefore.js +41 -0
- package/lib/esm/decorators/useBefore.js.map +1 -0
- package/lib/esm/decorators/useBeforeEach.js +36 -0
- package/lib/esm/decorators/useBeforeEach.js.map +1 -0
- package/lib/esm/domain/MiddlewareMethods.js +2 -0
- package/lib/esm/domain/MiddlewareMethods.js.map +1 -0
- package/lib/esm/domain/PlatformMiddlewareSettings.js +2 -0
- package/lib/esm/domain/PlatformMiddlewareSettings.js.map +1 -0
- package/lib/esm/index.js +15 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/utils/getMiddlewaresForHook.js +22 -0
- package/lib/esm/utils/getMiddlewaresForHook.js.map +1 -0
- package/lib/{constants/index.d.ts → types/constants/constants.d.ts} +0 -0
- package/lib/{decorators → types/decorators}/authOptions.d.ts +0 -0
- package/lib/{decorators → types/decorators}/middleware.d.ts +0 -0
- package/lib/{decorators → types/decorators}/use.d.ts +0 -0
- package/lib/{decorators → types/decorators}/useAfter.d.ts +0 -0
- package/lib/{decorators → types/decorators}/useAuth.d.ts +0 -0
- package/lib/{decorators → types/decorators}/useBefore.d.ts +0 -0
- package/lib/{decorators → types/decorators}/useBeforeEach.d.ts +0 -0
- package/lib/{domain → types/domain}/MiddlewareMethods.d.ts +0 -0
- package/lib/{domain → types/domain}/PlatformMiddlewareSettings.d.ts +0 -0
- package/lib/{index.d.ts → types/index.d.ts} +5 -1
- package/lib/{utils → types/utils}/getMiddlewaresForHook.d.ts +0 -0
- package/package.json +32 -18
- package/readme.md +1 -2
- package/tsconfig.compile.esm.json +12 -0
- package/lib/index.js +0 -305
- package/lib/index.js.map +0 -1
- package/lib/index.modern.js +0 -298
- package/lib/index.modern.js.map +0 -1
- package/lib/utils/mapUseOptions.d.ts +0 -5
package/lib/index.js
DELETED
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
var di = require('@tsed/di');
|
|
2
|
-
var core = require('@tsed/core');
|
|
3
|
-
var schema = require('@tsed/schema');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Register a new Middleware class.
|
|
7
|
-
*
|
|
8
|
-
* @decorator
|
|
9
|
-
* @classDecorator
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
function Middleware() {
|
|
13
|
-
return di.Injectable({
|
|
14
|
-
type: di.ProviderType.MIDDLEWARE
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Change authentication options.
|
|
20
|
-
*
|
|
21
|
-
* ```typescript
|
|
22
|
-
* @Controller('/mypath')
|
|
23
|
-
* @UseAuth(MyAuthStrategy, {role: ''})
|
|
24
|
-
* class MyCtrl {
|
|
25
|
-
*
|
|
26
|
-
* @Get('/')
|
|
27
|
-
* @AuthOptions(MyAuthStrategy, {role: 'admin'})
|
|
28
|
-
* public getResource(){}
|
|
29
|
-
* }
|
|
30
|
-
* ```
|
|
31
|
-
*
|
|
32
|
-
* @param guardAuth
|
|
33
|
-
* @param options {Object} Object passed to the customer auth strategy
|
|
34
|
-
* @returns {Function}
|
|
35
|
-
* @decorator
|
|
36
|
-
* @decorator
|
|
37
|
-
* @operation
|
|
38
|
-
*/
|
|
39
|
-
|
|
40
|
-
function AuthOptions(guardAuth, options = {}) {
|
|
41
|
-
return (...args) => {
|
|
42
|
-
switch (core.decoratorTypeOf(args)) {
|
|
43
|
-
case core.DecoratorTypes.METHOD:
|
|
44
|
-
return schema.JsonEntityFn(entity => {
|
|
45
|
-
const store = entity.store;
|
|
46
|
-
store.merge(guardAuth, options, true);
|
|
47
|
-
})(...args);
|
|
48
|
-
|
|
49
|
-
case core.DecoratorTypes.CLASS:
|
|
50
|
-
core.decorateMethodsOf(args[0], AuthOptions(guardAuth, options));
|
|
51
|
-
break;
|
|
52
|
-
|
|
53
|
-
default:
|
|
54
|
-
throw new core.UnsupportedDecoratorType(AuthOptions, args);
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
61
|
-
* the base of the requested path matches `path.
|
|
62
|
-
*
|
|
63
|
-
* ```typescript
|
|
64
|
-
* @Controller('/')
|
|
65
|
-
* @Use(Middleware1)
|
|
66
|
-
* export class Ctrl {
|
|
67
|
-
*
|
|
68
|
-
* @Get('/')
|
|
69
|
-
* @Use(Middleware2)
|
|
70
|
-
* get() { }
|
|
71
|
-
* }
|
|
72
|
-
*
|
|
73
|
-
* ```
|
|
74
|
-
*
|
|
75
|
-
* @returns {Function}
|
|
76
|
-
* @param args
|
|
77
|
-
* @decorator
|
|
78
|
-
* @operation
|
|
79
|
-
*/
|
|
80
|
-
|
|
81
|
-
function Use(...args) {
|
|
82
|
-
return schema.JsonEntityFn((entity, parameters) => {
|
|
83
|
-
switch (entity.decoratorType) {
|
|
84
|
-
case core.DecoratorTypes.METHOD:
|
|
85
|
-
return schema.Route(...args);
|
|
86
|
-
|
|
87
|
-
case core.DecoratorTypes.CLASS:
|
|
88
|
-
entity.store.merge("middlewares", {
|
|
89
|
-
use: args
|
|
90
|
-
});
|
|
91
|
-
break;
|
|
92
|
-
|
|
93
|
-
default:
|
|
94
|
-
throw new core.UnsupportedDecoratorType(Use, parameters);
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function _extends() {
|
|
100
|
-
_extends = Object.assign || function (target) {
|
|
101
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
102
|
-
var source = arguments[i];
|
|
103
|
-
|
|
104
|
-
for (var key in source) {
|
|
105
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
106
|
-
target[key] = source[key];
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return target;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
return _extends.apply(this, arguments);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
119
|
-
* the base of the requested path matches `path.
|
|
120
|
-
*
|
|
121
|
-
* ```typescript
|
|
122
|
-
* @Controller('/')
|
|
123
|
-
* @UseAfter(Middleware1)
|
|
124
|
-
* export class Ctrl {
|
|
125
|
-
*
|
|
126
|
-
* @Get('/')
|
|
127
|
-
* @UseAfter(Middleware2)
|
|
128
|
-
* get() { }
|
|
129
|
-
* }
|
|
130
|
-
* ```
|
|
131
|
-
*
|
|
132
|
-
* @returns {function(any, any, any): *}
|
|
133
|
-
* @param args
|
|
134
|
-
* @decorator
|
|
135
|
-
* @operation
|
|
136
|
-
*/
|
|
137
|
-
|
|
138
|
-
function UseAfter(...args) {
|
|
139
|
-
return schema.JsonEntityFn((entity, parameters) => {
|
|
140
|
-
switch (entity.decoratorType) {
|
|
141
|
-
case core.DecoratorTypes.METHOD:
|
|
142
|
-
const endpoint = entity;
|
|
143
|
-
endpoint.afterMiddlewares = args.concat(endpoint.afterMiddlewares);
|
|
144
|
-
break;
|
|
145
|
-
|
|
146
|
-
case core.DecoratorTypes.CLASS:
|
|
147
|
-
const middlewares = entity.store.get("middlewares") || {};
|
|
148
|
-
entity.store.set("middlewares", _extends({}, middlewares, {
|
|
149
|
-
useAfter: [...args, ...(middlewares.useAfter || [])]
|
|
150
|
-
}));
|
|
151
|
-
break;
|
|
152
|
-
|
|
153
|
-
default:
|
|
154
|
-
throw new core.UnsupportedDecoratorType(UseAfter, parameters);
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
161
|
-
* the base of the requested path matches `path.
|
|
162
|
-
*
|
|
163
|
-
* ```typescript
|
|
164
|
-
* @Controller('/')
|
|
165
|
-
* @UseBefore(Middleware1) // called only one time before all endpoint
|
|
166
|
-
* export class Ctrl {
|
|
167
|
-
*
|
|
168
|
-
* @Get('/')
|
|
169
|
-
* @UseBefore(Middleware2)
|
|
170
|
-
* get() { }
|
|
171
|
-
* }
|
|
172
|
-
* ```
|
|
173
|
-
*
|
|
174
|
-
* @returns {function(any, any, any): *}
|
|
175
|
-
* @param args
|
|
176
|
-
* @decorator
|
|
177
|
-
* @operation
|
|
178
|
-
*/
|
|
179
|
-
|
|
180
|
-
function UseBefore(...args) {
|
|
181
|
-
return schema.JsonEntityFn((entity, parameters) => {
|
|
182
|
-
switch (entity.decoratorType) {
|
|
183
|
-
case core.DecoratorTypes.METHOD:
|
|
184
|
-
entity.beforeMiddlewares = args.concat(entity.beforeMiddlewares);
|
|
185
|
-
break;
|
|
186
|
-
|
|
187
|
-
case core.DecoratorTypes.CLASS:
|
|
188
|
-
const middlewares = entity.store.get("middlewares") || {};
|
|
189
|
-
entity.store.set("middlewares", _extends({}, middlewares, {
|
|
190
|
-
useBefore: [...args, ...(middlewares.useBefore || [])]
|
|
191
|
-
}));
|
|
192
|
-
break;
|
|
193
|
-
|
|
194
|
-
default:
|
|
195
|
-
throw new core.UnsupportedDecoratorType(UseBefore, parameters);
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Use custom authentication strategy on your endpoint.
|
|
202
|
-
*
|
|
203
|
-
* ```typescript
|
|
204
|
-
* @Controller('/mypath')
|
|
205
|
-
* @UseAuth(MyAuthStrategy)
|
|
206
|
-
* class MyCtrl {
|
|
207
|
-
*
|
|
208
|
-
* @Get('/')
|
|
209
|
-
* @UseAuth(MyAuthStrategy, {role: 'admin'})
|
|
210
|
-
* public getResource(){}
|
|
211
|
-
* }
|
|
212
|
-
* ```
|
|
213
|
-
*
|
|
214
|
-
* @param guardAuth {Type<any>} A middleware which implement a custom auth strategy
|
|
215
|
-
* @param options {Object} Object passed to the customer auth strategy
|
|
216
|
-
* @returns {Function}
|
|
217
|
-
* @decorator
|
|
218
|
-
* @operation
|
|
219
|
-
*/
|
|
220
|
-
|
|
221
|
-
function UseAuth(guardAuth, options = {}) {
|
|
222
|
-
return (...args) => {
|
|
223
|
-
switch (core.decoratorTypeOf(args)) {
|
|
224
|
-
case core.DecoratorTypes.METHOD:
|
|
225
|
-
return core.useDecorators(core.StoreFn(store => {
|
|
226
|
-
if (!store.has(guardAuth)) {
|
|
227
|
-
return UseBefore(guardAuth);
|
|
228
|
-
}
|
|
229
|
-
}), AuthOptions(guardAuth, options))(...args);
|
|
230
|
-
|
|
231
|
-
case core.DecoratorTypes.CLASS:
|
|
232
|
-
core.decorateMethodsOf(args[0], UseAuth(guardAuth, options));
|
|
233
|
-
break;
|
|
234
|
-
|
|
235
|
-
default:
|
|
236
|
-
throw new core.UnsupportedDecoratorType(UseAuth, args);
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
243
|
-
* the base of the requested path matches `path.
|
|
244
|
-
*
|
|
245
|
-
* ```typescript
|
|
246
|
-
* @Controller('/')
|
|
247
|
-
* @UseBeforeEach(Middleware1) // Called before each endpoint
|
|
248
|
-
* export class Ctrl {
|
|
249
|
-
*
|
|
250
|
-
* @Get('/')
|
|
251
|
-
* get() { }
|
|
252
|
-
* }
|
|
253
|
-
*
|
|
254
|
-
* ```
|
|
255
|
-
*
|
|
256
|
-
* @returns {Function}
|
|
257
|
-
* @param args
|
|
258
|
-
* @decorator
|
|
259
|
-
* @operation
|
|
260
|
-
*/
|
|
261
|
-
|
|
262
|
-
function UseBeforeEach(...args) {
|
|
263
|
-
return (...decoratorArgs) => {
|
|
264
|
-
switch (core.decoratorTypeOf(decoratorArgs)) {
|
|
265
|
-
case core.DecoratorTypes.METHOD:
|
|
266
|
-
return UseBefore(...args)(...decoratorArgs);
|
|
267
|
-
|
|
268
|
-
case core.DecoratorTypes.CLASS:
|
|
269
|
-
core.decorateMethodsOf(decoratorArgs[0], UseBefore(...args));
|
|
270
|
-
break;
|
|
271
|
-
|
|
272
|
-
default:
|
|
273
|
-
throw new core.UnsupportedDecoratorType(UseBeforeEach, decoratorArgs);
|
|
274
|
-
}
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
function getMiddlewaresForHook(hook, settings, defaultHook = "") {
|
|
279
|
-
const {
|
|
280
|
-
env,
|
|
281
|
-
middlewares = []
|
|
282
|
-
} = settings;
|
|
283
|
-
return middlewares.map(middleware => {
|
|
284
|
-
return core.isFunction(middleware) ? {
|
|
285
|
-
env,
|
|
286
|
-
hook: defaultHook,
|
|
287
|
-
use: middleware
|
|
288
|
-
} : _extends({
|
|
289
|
-
env,
|
|
290
|
-
hook: defaultHook
|
|
291
|
-
}, middleware);
|
|
292
|
-
}).filter(options => {
|
|
293
|
-
return options.use && options.env === env && options.hook === hook;
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
exports.AuthOptions = AuthOptions;
|
|
298
|
-
exports.Middleware = Middleware;
|
|
299
|
-
exports.Use = Use;
|
|
300
|
-
exports.UseAfter = UseAfter;
|
|
301
|
-
exports.UseAuth = UseAuth;
|
|
302
|
-
exports.UseBefore = UseBefore;
|
|
303
|
-
exports.UseBeforeEach = UseBeforeEach;
|
|
304
|
-
exports.getMiddlewaresForHook = getMiddlewaresForHook;
|
|
305
|
-
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/decorators/middleware.ts","../src/decorators/authOptions.ts","../src/decorators/use.ts","../src/decorators/useAfter.ts","../src/decorators/useBefore.ts","../src/decorators/useAuth.ts","../src/decorators/useBeforeEach.ts","../src/utils/getMiddlewaresForHook.ts"],"sourcesContent":["import {Injectable, ProviderType} from \"@tsed/di\";\n\n/**\n * Register a new Middleware class.\n *\n * @decorator\n * @classDecorator\n */\nexport function Middleware(): ClassDecorator {\n return Injectable({\n type: ProviderType.MIDDLEWARE\n });\n}\n","import {\n decorateMethodsOf,\n DecoratorMethodParameters,\n DecoratorParameters,\n decoratorTypeOf,\n DecoratorTypes,\n Type,\n UnsupportedDecoratorType\n} from \"@tsed/core\";\nimport {JsonEntityFn} from \"@tsed/schema\";\n\n/**\n * Change authentication options.\n *\n * ```typescript\n * @Controller('/mypath')\n * @UseAuth(MyAuthStrategy, {role: ''})\n * class MyCtrl {\n *\n * @Get('/')\n * @AuthOptions(MyAuthStrategy, {role: 'admin'})\n * public getResource(){}\n * }\n * ```\n *\n * @param guardAuth\n * @param options {Object} Object passed to the customer auth strategy\n * @returns {Function}\n * @decorator\n * @decorator\n * @operation\n */\nexport function AuthOptions(guardAuth: Type<any>, options: Record<string, unknown> = {}): Function {\n return <T>(...args: DecoratorParameters): TypedPropertyDescriptor<T> | void => {\n switch (decoratorTypeOf(args)) {\n case DecoratorTypes.METHOD:\n return JsonEntityFn((entity) => {\n const store = entity.store;\n\n store.merge(guardAuth, options, true);\n })(...(args as DecoratorMethodParameters));\n\n case DecoratorTypes.CLASS:\n decorateMethodsOf(args[0], AuthOptions(guardAuth, options));\n break;\n\n default:\n throw new UnsupportedDecoratorType(AuthOptions, args);\n }\n };\n}\n","import {DecoratorTypes, UnsupportedDecoratorType} from \"@tsed/core\";\nimport {JsonEntityFn, OperationMethods, OperationPath, Route} from \"@tsed/schema\";\nimport {mapUseOptions} from \"../utils/mapUseOptions\";\n\n/**\n * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when\n * the base of the requested path matches `path.\n *\n * ```typescript\n * @Controller('/')\n * @Use(Middleware1)\n * export class Ctrl {\n *\n * @Get('/')\n * @Use(Middleware2)\n * get() { }\n * }\n *\n * ```\n *\n * @returns {Function}\n * @param args\n * @decorator\n * @operation\n */\nexport function Use(...args: any[]): Function {\n return JsonEntityFn((entity, parameters) => {\n switch (entity.decoratorType) {\n case DecoratorTypes.METHOD:\n return Route(...args);\n\n case DecoratorTypes.CLASS:\n entity.store.merge(\"middlewares\", {\n use: args\n });\n break;\n\n default:\n throw new UnsupportedDecoratorType(Use, parameters);\n }\n });\n}\n","import {DecoratorTypes, UnsupportedDecoratorType} from \"@tsed/core\";\nimport {JsonEntityFn} from \"@tsed/schema\";\n\n/**\n * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when\n * the base of the requested path matches `path.\n *\n * ```typescript\n * @Controller('/')\n * @UseAfter(Middleware1)\n * export class Ctrl {\n *\n * @Get('/')\n * @UseAfter(Middleware2)\n * get() { }\n * }\n * ```\n *\n * @returns {function(any, any, any): *}\n * @param args\n * @decorator\n * @operation\n */\nexport function UseAfter(...args: any[]): Function {\n return JsonEntityFn((entity, parameters) => {\n switch (entity.decoratorType) {\n case DecoratorTypes.METHOD:\n const endpoint = entity;\n endpoint.afterMiddlewares = args.concat(endpoint.afterMiddlewares);\n\n break;\n\n case DecoratorTypes.CLASS:\n const middlewares = entity.store.get(\"middlewares\") || {};\n\n entity.store.set(\"middlewares\", {\n ...middlewares,\n useAfter: [...args, ...(middlewares.useAfter || [])]\n });\n break;\n\n default:\n throw new UnsupportedDecoratorType(UseAfter, parameters);\n }\n });\n}\n","import {DecoratorTypes, UnsupportedDecoratorType} from \"@tsed/core\";\nimport {JsonEntityFn} from \"@tsed/schema\";\n\n/**\n * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when\n * the base of the requested path matches `path.\n *\n * ```typescript\n * @Controller('/')\n * @UseBefore(Middleware1) // called only one time before all endpoint\n * export class Ctrl {\n *\n * @Get('/')\n * @UseBefore(Middleware2)\n * get() { }\n * }\n * ```\n *\n * @returns {function(any, any, any): *}\n * @param args\n * @decorator\n * @operation\n */\nexport function UseBefore(...args: any[]): Function {\n return JsonEntityFn((entity, parameters) => {\n switch (entity.decoratorType) {\n case DecoratorTypes.METHOD:\n entity.beforeMiddlewares = args.concat(entity.beforeMiddlewares);\n\n break;\n\n case DecoratorTypes.CLASS:\n const middlewares = entity.store.get(\"middlewares\") || {};\n\n entity.store.set(\"middlewares\", {\n ...middlewares,\n useBefore: [...args, ...(middlewares.useBefore || [])]\n });\n break;\n\n default:\n throw new UnsupportedDecoratorType(UseBefore, parameters);\n }\n });\n}\n","import {\n decorateMethodsOf,\n DecoratorParameters,\n decoratorTypeOf,\n DecoratorTypes,\n Store,\n StoreFn,\n Type,\n UnsupportedDecoratorType,\n useDecorators\n} from \"@tsed/core\";\nimport {AuthOptions} from \"./authOptions\";\nimport {UseBefore} from \"./useBefore\";\n\n/**\n * Use custom authentication strategy on your endpoint.\n *\n * ```typescript\n * @Controller('/mypath')\n * @UseAuth(MyAuthStrategy)\n * class MyCtrl {\n *\n * @Get('/')\n * @UseAuth(MyAuthStrategy, {role: 'admin'})\n * public getResource(){}\n * }\n * ```\n *\n * @param guardAuth {Type<any>} A middleware which implement a custom auth strategy\n * @param options {Object} Object passed to the customer auth strategy\n * @returns {Function}\n * @decorator\n * @operation\n */\nexport function UseAuth(guardAuth: Type<any>, options: Record<string, unknown> = {}): Function {\n return <T>(...args: DecoratorParameters): TypedPropertyDescriptor<T> | void => {\n switch (decoratorTypeOf(args)) {\n case DecoratorTypes.METHOD:\n return useDecorators(\n StoreFn((store: Store) => {\n if (!store.has(guardAuth)) {\n return UseBefore(guardAuth);\n }\n }),\n AuthOptions(guardAuth, options)\n )(...args);\n\n case DecoratorTypes.CLASS:\n decorateMethodsOf(args[0], UseAuth(guardAuth, options));\n break;\n\n default:\n throw new UnsupportedDecoratorType(UseAuth, args);\n }\n };\n}\n","import {decorateMethodsOf, DecoratorParameters, decoratorTypeOf, DecoratorTypes, UnsupportedDecoratorType} from \"@tsed/core\";\nimport {UseBefore} from \"./useBefore\";\n\n/**\n * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when\n * the base of the requested path matches `path.\n *\n * ```typescript\n * @Controller('/')\n * @UseBeforeEach(Middleware1) // Called before each endpoint\n * export class Ctrl {\n *\n * @Get('/')\n * get() { }\n * }\n *\n * ```\n *\n * @returns {Function}\n * @param args\n * @decorator\n * @operation\n */\nexport function UseBeforeEach(...args: any[]): Function {\n return <T>(...decoratorArgs: DecoratorParameters): TypedPropertyDescriptor<T> | void => {\n switch (decoratorTypeOf(decoratorArgs)) {\n case DecoratorTypes.METHOD:\n return UseBefore(...args)(...decoratorArgs);\n\n case DecoratorTypes.CLASS:\n decorateMethodsOf(decoratorArgs[0], UseBefore(...args));\n break;\n\n default:\n throw new UnsupportedDecoratorType(UseBeforeEach, decoratorArgs);\n }\n };\n}\n","import {isFunction} from \"@tsed/core\";\nimport {PlatformMiddlewareLoadingOptions} from \"../domain/PlatformMiddlewareSettings\";\n\nexport function getMiddlewaresForHook(hook: string, settings: TsED.Configuration, defaultHook = \"\") {\n const {env, middlewares = []} = settings;\n\n return middlewares\n .map<PlatformMiddlewareLoadingOptions>((middleware) => {\n return isFunction(middleware)\n ? {\n env,\n hook: defaultHook,\n use: middleware\n }\n : {\n env,\n hook: defaultHook,\n ...middleware\n };\n })\n .filter((options) => {\n return options.use && options.env === env && options.hook === hook;\n });\n}\n"],"names":["Middleware","Injectable","type","ProviderType","MIDDLEWARE","AuthOptions","guardAuth","options","args","decoratorTypeOf","DecoratorTypes","METHOD","JsonEntityFn","entity","store","merge","CLASS","decorateMethodsOf","UnsupportedDecoratorType","Use","parameters","decoratorType","Route","use","UseAfter","endpoint","afterMiddlewares","concat","middlewares","get","set","useAfter","UseBefore","beforeMiddlewares","useBefore","UseAuth","useDecorators","StoreFn","has","UseBeforeEach","decoratorArgs","getMiddlewaresForHook","hook","settings","defaultHook","env","map","middleware","isFunction","filter"],"mappings":";;;;AAEA;;;;;;;SAMgBA;AACd,SAAOC,aAAU,CAAC;AAChBC,IAAAA,IAAI,EAAEC,eAAY,CAACC;AADH,GAAD,CAAjB;AAGD;;ACDD;;;;;;;;;;;;;;;;;;;;;;SAqBgBC,YAAYC,WAAsBC,UAAmC;AACnF,SAAO,CAAI,GAAGC,I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AApB,MAA0Be,QAAhC;AAEA,SAAOf,WAAW,CACfkB,GADI,CACmCC,UAAD;AACrC,WAAOC,eAAU,CAACD,UAAD,CAAV,GACH;AACEF,MAAAA,GADF;AAEEH,MAAAA,IAAI,EAAEE,WAFR;AAGErB,MAAAA,GAAG,EAAEwB;AAHP,KADG;AAODF,MAAAA,GAPC;AAQDH,MAAAA,IAAI,EAAEE;AARL,OASEG,UATF,CAAP;AAWD,GAbI,EAcJE,MAdI,CAcI1C,OAAD;AACN,WAAOA,OAAO,CAACgB,GAAR,IAAehB,OAAO,CAACsC,GAAR,KAAgBA,GAA/B,IAAsCtC,OAAO,CAACmC,IAAR,KAAiBA,IAA9D;AACD,GAhBI,CAAP;AAiBD;;;;;;;;;;;"}
|
package/lib/index.modern.js
DELETED
|
@@ -1,298 +0,0 @@
|
|
|
1
|
-
import { Injectable, ProviderType } from '@tsed/di';
|
|
2
|
-
import { decoratorTypeOf, UnsupportedDecoratorType, DecoratorTypes, decorateMethodsOf, useDecorators, StoreFn, isFunction } from '@tsed/core';
|
|
3
|
-
import { JsonEntityFn, Route } from '@tsed/schema';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Register a new Middleware class.
|
|
7
|
-
*
|
|
8
|
-
* @decorator
|
|
9
|
-
* @classDecorator
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
function Middleware() {
|
|
13
|
-
return Injectable({
|
|
14
|
-
type: ProviderType.MIDDLEWARE
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Change authentication options.
|
|
20
|
-
*
|
|
21
|
-
* ```typescript
|
|
22
|
-
* @Controller('/mypath')
|
|
23
|
-
* @UseAuth(MyAuthStrategy, {role: ''})
|
|
24
|
-
* class MyCtrl {
|
|
25
|
-
*
|
|
26
|
-
* @Get('/')
|
|
27
|
-
* @AuthOptions(MyAuthStrategy, {role: 'admin'})
|
|
28
|
-
* public getResource(){}
|
|
29
|
-
* }
|
|
30
|
-
* ```
|
|
31
|
-
*
|
|
32
|
-
* @param guardAuth
|
|
33
|
-
* @param options {Object} Object passed to the customer auth strategy
|
|
34
|
-
* @returns {Function}
|
|
35
|
-
* @decorator
|
|
36
|
-
* @decorator
|
|
37
|
-
* @operation
|
|
38
|
-
*/
|
|
39
|
-
|
|
40
|
-
function AuthOptions(guardAuth, options = {}) {
|
|
41
|
-
return (...args) => {
|
|
42
|
-
switch (decoratorTypeOf(args)) {
|
|
43
|
-
case DecoratorTypes.METHOD:
|
|
44
|
-
return JsonEntityFn(entity => {
|
|
45
|
-
const store = entity.store;
|
|
46
|
-
store.merge(guardAuth, options, true);
|
|
47
|
-
})(...args);
|
|
48
|
-
|
|
49
|
-
case DecoratorTypes.CLASS:
|
|
50
|
-
decorateMethodsOf(args[0], AuthOptions(guardAuth, options));
|
|
51
|
-
break;
|
|
52
|
-
|
|
53
|
-
default:
|
|
54
|
-
throw new UnsupportedDecoratorType(AuthOptions, args);
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
61
|
-
* the base of the requested path matches `path.
|
|
62
|
-
*
|
|
63
|
-
* ```typescript
|
|
64
|
-
* @Controller('/')
|
|
65
|
-
* @Use(Middleware1)
|
|
66
|
-
* export class Ctrl {
|
|
67
|
-
*
|
|
68
|
-
* @Get('/')
|
|
69
|
-
* @Use(Middleware2)
|
|
70
|
-
* get() { }
|
|
71
|
-
* }
|
|
72
|
-
*
|
|
73
|
-
* ```
|
|
74
|
-
*
|
|
75
|
-
* @returns {Function}
|
|
76
|
-
* @param args
|
|
77
|
-
* @decorator
|
|
78
|
-
* @operation
|
|
79
|
-
*/
|
|
80
|
-
|
|
81
|
-
function Use(...args) {
|
|
82
|
-
return JsonEntityFn((entity, parameters) => {
|
|
83
|
-
switch (entity.decoratorType) {
|
|
84
|
-
case DecoratorTypes.METHOD:
|
|
85
|
-
return Route(...args);
|
|
86
|
-
|
|
87
|
-
case DecoratorTypes.CLASS:
|
|
88
|
-
entity.store.merge("middlewares", {
|
|
89
|
-
use: args
|
|
90
|
-
});
|
|
91
|
-
break;
|
|
92
|
-
|
|
93
|
-
default:
|
|
94
|
-
throw new UnsupportedDecoratorType(Use, parameters);
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function _extends() {
|
|
100
|
-
_extends = Object.assign || function (target) {
|
|
101
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
102
|
-
var source = arguments[i];
|
|
103
|
-
|
|
104
|
-
for (var key in source) {
|
|
105
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
106
|
-
target[key] = source[key];
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return target;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
return _extends.apply(this, arguments);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
119
|
-
* the base of the requested path matches `path.
|
|
120
|
-
*
|
|
121
|
-
* ```typescript
|
|
122
|
-
* @Controller('/')
|
|
123
|
-
* @UseAfter(Middleware1)
|
|
124
|
-
* export class Ctrl {
|
|
125
|
-
*
|
|
126
|
-
* @Get('/')
|
|
127
|
-
* @UseAfter(Middleware2)
|
|
128
|
-
* get() { }
|
|
129
|
-
* }
|
|
130
|
-
* ```
|
|
131
|
-
*
|
|
132
|
-
* @returns {function(any, any, any): *}
|
|
133
|
-
* @param args
|
|
134
|
-
* @decorator
|
|
135
|
-
* @operation
|
|
136
|
-
*/
|
|
137
|
-
|
|
138
|
-
function UseAfter(...args) {
|
|
139
|
-
return JsonEntityFn((entity, parameters) => {
|
|
140
|
-
switch (entity.decoratorType) {
|
|
141
|
-
case DecoratorTypes.METHOD:
|
|
142
|
-
const endpoint = entity;
|
|
143
|
-
endpoint.afterMiddlewares = args.concat(endpoint.afterMiddlewares);
|
|
144
|
-
break;
|
|
145
|
-
|
|
146
|
-
case DecoratorTypes.CLASS:
|
|
147
|
-
const middlewares = entity.store.get("middlewares") || {};
|
|
148
|
-
entity.store.set("middlewares", _extends({}, middlewares, {
|
|
149
|
-
useAfter: [...args, ...(middlewares.useAfter || [])]
|
|
150
|
-
}));
|
|
151
|
-
break;
|
|
152
|
-
|
|
153
|
-
default:
|
|
154
|
-
throw new UnsupportedDecoratorType(UseAfter, parameters);
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
161
|
-
* the base of the requested path matches `path.
|
|
162
|
-
*
|
|
163
|
-
* ```typescript
|
|
164
|
-
* @Controller('/')
|
|
165
|
-
* @UseBefore(Middleware1) // called only one time before all endpoint
|
|
166
|
-
* export class Ctrl {
|
|
167
|
-
*
|
|
168
|
-
* @Get('/')
|
|
169
|
-
* @UseBefore(Middleware2)
|
|
170
|
-
* get() { }
|
|
171
|
-
* }
|
|
172
|
-
* ```
|
|
173
|
-
*
|
|
174
|
-
* @returns {function(any, any, any): *}
|
|
175
|
-
* @param args
|
|
176
|
-
* @decorator
|
|
177
|
-
* @operation
|
|
178
|
-
*/
|
|
179
|
-
|
|
180
|
-
function UseBefore(...args) {
|
|
181
|
-
return JsonEntityFn((entity, parameters) => {
|
|
182
|
-
switch (entity.decoratorType) {
|
|
183
|
-
case DecoratorTypes.METHOD:
|
|
184
|
-
entity.beforeMiddlewares = args.concat(entity.beforeMiddlewares);
|
|
185
|
-
break;
|
|
186
|
-
|
|
187
|
-
case DecoratorTypes.CLASS:
|
|
188
|
-
const middlewares = entity.store.get("middlewares") || {};
|
|
189
|
-
entity.store.set("middlewares", _extends({}, middlewares, {
|
|
190
|
-
useBefore: [...args, ...(middlewares.useBefore || [])]
|
|
191
|
-
}));
|
|
192
|
-
break;
|
|
193
|
-
|
|
194
|
-
default:
|
|
195
|
-
throw new UnsupportedDecoratorType(UseBefore, parameters);
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Use custom authentication strategy on your endpoint.
|
|
202
|
-
*
|
|
203
|
-
* ```typescript
|
|
204
|
-
* @Controller('/mypath')
|
|
205
|
-
* @UseAuth(MyAuthStrategy)
|
|
206
|
-
* class MyCtrl {
|
|
207
|
-
*
|
|
208
|
-
* @Get('/')
|
|
209
|
-
* @UseAuth(MyAuthStrategy, {role: 'admin'})
|
|
210
|
-
* public getResource(){}
|
|
211
|
-
* }
|
|
212
|
-
* ```
|
|
213
|
-
*
|
|
214
|
-
* @param guardAuth {Type<any>} A middleware which implement a custom auth strategy
|
|
215
|
-
* @param options {Object} Object passed to the customer auth strategy
|
|
216
|
-
* @returns {Function}
|
|
217
|
-
* @decorator
|
|
218
|
-
* @operation
|
|
219
|
-
*/
|
|
220
|
-
|
|
221
|
-
function UseAuth(guardAuth, options = {}) {
|
|
222
|
-
return (...args) => {
|
|
223
|
-
switch (decoratorTypeOf(args)) {
|
|
224
|
-
case DecoratorTypes.METHOD:
|
|
225
|
-
return useDecorators(StoreFn(store => {
|
|
226
|
-
if (!store.has(guardAuth)) {
|
|
227
|
-
return UseBefore(guardAuth);
|
|
228
|
-
}
|
|
229
|
-
}), AuthOptions(guardAuth, options))(...args);
|
|
230
|
-
|
|
231
|
-
case DecoratorTypes.CLASS:
|
|
232
|
-
decorateMethodsOf(args[0], UseAuth(guardAuth, options));
|
|
233
|
-
break;
|
|
234
|
-
|
|
235
|
-
default:
|
|
236
|
-
throw new UnsupportedDecoratorType(UseAuth, args);
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
243
|
-
* the base of the requested path matches `path.
|
|
244
|
-
*
|
|
245
|
-
* ```typescript
|
|
246
|
-
* @Controller('/')
|
|
247
|
-
* @UseBeforeEach(Middleware1) // Called before each endpoint
|
|
248
|
-
* export class Ctrl {
|
|
249
|
-
*
|
|
250
|
-
* @Get('/')
|
|
251
|
-
* get() { }
|
|
252
|
-
* }
|
|
253
|
-
*
|
|
254
|
-
* ```
|
|
255
|
-
*
|
|
256
|
-
* @returns {Function}
|
|
257
|
-
* @param args
|
|
258
|
-
* @decorator
|
|
259
|
-
* @operation
|
|
260
|
-
*/
|
|
261
|
-
|
|
262
|
-
function UseBeforeEach(...args) {
|
|
263
|
-
return (...decoratorArgs) => {
|
|
264
|
-
switch (decoratorTypeOf(decoratorArgs)) {
|
|
265
|
-
case DecoratorTypes.METHOD:
|
|
266
|
-
return UseBefore(...args)(...decoratorArgs);
|
|
267
|
-
|
|
268
|
-
case DecoratorTypes.CLASS:
|
|
269
|
-
decorateMethodsOf(decoratorArgs[0], UseBefore(...args));
|
|
270
|
-
break;
|
|
271
|
-
|
|
272
|
-
default:
|
|
273
|
-
throw new UnsupportedDecoratorType(UseBeforeEach, decoratorArgs);
|
|
274
|
-
}
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
function getMiddlewaresForHook(hook, settings, defaultHook = "") {
|
|
279
|
-
const {
|
|
280
|
-
env,
|
|
281
|
-
middlewares = []
|
|
282
|
-
} = settings;
|
|
283
|
-
return middlewares.map(middleware => {
|
|
284
|
-
return isFunction(middleware) ? {
|
|
285
|
-
env,
|
|
286
|
-
hook: defaultHook,
|
|
287
|
-
use: middleware
|
|
288
|
-
} : _extends({
|
|
289
|
-
env,
|
|
290
|
-
hook: defaultHook
|
|
291
|
-
}, middleware);
|
|
292
|
-
}).filter(options => {
|
|
293
|
-
return options.use && options.env === env && options.hook === hook;
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
export { AuthOptions, Middleware, Use, UseAfter, UseAuth, UseBefore, UseBeforeEach, getMiddlewaresForHook };
|
|
298
|
-
//# sourceMappingURL=index.modern.js.map
|
package/lib/index.modern.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.modern.js","sources":["../src/decorators/middleware.ts","../src/decorators/authOptions.ts","../src/decorators/use.ts","../src/decorators/useAfter.ts","../src/decorators/useBefore.ts","../src/decorators/useAuth.ts","../src/decorators/useBeforeEach.ts","../src/utils/getMiddlewaresForHook.ts"],"sourcesContent":["import {Injectable, ProviderType} from \"@tsed/di\";\n\n/**\n * Register a new Middleware class.\n *\n * @decorator\n * @classDecorator\n */\nexport function Middleware(): ClassDecorator {\n return Injectable({\n type: ProviderType.MIDDLEWARE\n });\n}\n","import {\n decorateMethodsOf,\n DecoratorMethodParameters,\n DecoratorParameters,\n decoratorTypeOf,\n DecoratorTypes,\n Type,\n UnsupportedDecoratorType\n} from \"@tsed/core\";\nimport {JsonEntityFn} from \"@tsed/schema\";\n\n/**\n * Change authentication options.\n *\n * ```typescript\n * @Controller('/mypath')\n * @UseAuth(MyAuthStrategy, {role: ''})\n * class MyCtrl {\n *\n * @Get('/')\n * @AuthOptions(MyAuthStrategy, {role: 'admin'})\n * public getResource(){}\n * }\n * ```\n *\n * @param guardAuth\n * @param options {Object} Object passed to the customer auth strategy\n * @returns {Function}\n * @decorator\n * @decorator\n * @operation\n */\nexport function AuthOptions(guardAuth: Type<any>, options: Record<string, unknown> = {}): Function {\n return <T>(...args: DecoratorParameters): TypedPropertyDescriptor<T> | void => {\n switch (decoratorTypeOf(args)) {\n case DecoratorTypes.METHOD:\n return JsonEntityFn((entity) => {\n const store = entity.store;\n\n store.merge(guardAuth, options, true);\n })(...(args as DecoratorMethodParameters));\n\n case DecoratorTypes.CLASS:\n decorateMethodsOf(args[0], AuthOptions(guardAuth, options));\n break;\n\n default:\n throw new UnsupportedDecoratorType(AuthOptions, args);\n }\n };\n}\n","import {DecoratorTypes, UnsupportedDecoratorType} from \"@tsed/core\";\nimport {JsonEntityFn, OperationMethods, OperationPath, Route} from \"@tsed/schema\";\nimport {mapUseOptions} from \"../utils/mapUseOptions\";\n\n/**\n * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when\n * the base of the requested path matches `path.\n *\n * ```typescript\n * @Controller('/')\n * @Use(Middleware1)\n * export class Ctrl {\n *\n * @Get('/')\n * @Use(Middleware2)\n * get() { }\n * }\n *\n * ```\n *\n * @returns {Function}\n * @param args\n * @decorator\n * @operation\n */\nexport function Use(...args: any[]): Function {\n return JsonEntityFn((entity, parameters) => {\n switch (entity.decoratorType) {\n case DecoratorTypes.METHOD:\n return Route(...args);\n\n case DecoratorTypes.CLASS:\n entity.store.merge(\"middlewares\", {\n use: args\n });\n break;\n\n default:\n throw new UnsupportedDecoratorType(Use, parameters);\n }\n });\n}\n","import {DecoratorTypes, UnsupportedDecoratorType} from \"@tsed/core\";\nimport {JsonEntityFn} from \"@tsed/schema\";\n\n/**\n * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when\n * the base of the requested path matches `path.\n *\n * ```typescript\n * @Controller('/')\n * @UseAfter(Middleware1)\n * export class Ctrl {\n *\n * @Get('/')\n * @UseAfter(Middleware2)\n * get() { }\n * }\n * ```\n *\n * @returns {function(any, any, any): *}\n * @param args\n * @decorator\n * @operation\n */\nexport function UseAfter(...args: any[]): Function {\n return JsonEntityFn((entity, parameters) => {\n switch (entity.decoratorType) {\n case DecoratorTypes.METHOD:\n const endpoint = entity;\n endpoint.afterMiddlewares = args.concat(endpoint.afterMiddlewares);\n\n break;\n\n case DecoratorTypes.CLASS:\n const middlewares = entity.store.get(\"middlewares\") || {};\n\n entity.store.set(\"middlewares\", {\n ...middlewares,\n useAfter: [...args, ...(middlewares.useAfter || [])]\n });\n break;\n\n default:\n throw new UnsupportedDecoratorType(UseAfter, parameters);\n }\n });\n}\n","import {DecoratorTypes, UnsupportedDecoratorType} from \"@tsed/core\";\nimport {JsonEntityFn} from \"@tsed/schema\";\n\n/**\n * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when\n * the base of the requested path matches `path.\n *\n * ```typescript\n * @Controller('/')\n * @UseBefore(Middleware1) // called only one time before all endpoint\n * export class Ctrl {\n *\n * @Get('/')\n * @UseBefore(Middleware2)\n * get() { }\n * }\n * ```\n *\n * @returns {function(any, any, any): *}\n * @param args\n * @decorator\n * @operation\n */\nexport function UseBefore(...args: any[]): Function {\n return JsonEntityFn((entity, parameters) => {\n switch (entity.decoratorType) {\n case DecoratorTypes.METHOD:\n entity.beforeMiddlewares = args.concat(entity.beforeMiddlewares);\n\n break;\n\n case DecoratorTypes.CLASS:\n const middlewares = entity.store.get(\"middlewares\") || {};\n\n entity.store.set(\"middlewares\", {\n ...middlewares,\n useBefore: [...args, ...(middlewares.useBefore || [])]\n });\n break;\n\n default:\n throw new UnsupportedDecoratorType(UseBefore, parameters);\n }\n });\n}\n","import {\n decorateMethodsOf,\n DecoratorParameters,\n decoratorTypeOf,\n DecoratorTypes,\n Store,\n StoreFn,\n Type,\n UnsupportedDecoratorType,\n useDecorators\n} from \"@tsed/core\";\nimport {AuthOptions} from \"./authOptions\";\nimport {UseBefore} from \"./useBefore\";\n\n/**\n * Use custom authentication strategy on your endpoint.\n *\n * ```typescript\n * @Controller('/mypath')\n * @UseAuth(MyAuthStrategy)\n * class MyCtrl {\n *\n * @Get('/')\n * @UseAuth(MyAuthStrategy, {role: 'admin'})\n * public getResource(){}\n * }\n * ```\n *\n * @param guardAuth {Type<any>} A middleware which implement a custom auth strategy\n * @param options {Object} Object passed to the customer auth strategy\n * @returns {Function}\n * @decorator\n * @operation\n */\nexport function UseAuth(guardAuth: Type<any>, options: Record<string, unknown> = {}): Function {\n return <T>(...args: DecoratorParameters): TypedPropertyDescriptor<T> | void => {\n switch (decoratorTypeOf(args)) {\n case DecoratorTypes.METHOD:\n return useDecorators(\n StoreFn((store: Store) => {\n if (!store.has(guardAuth)) {\n return UseBefore(guardAuth);\n }\n }),\n AuthOptions(guardAuth, options)\n )(...args);\n\n case DecoratorTypes.CLASS:\n decorateMethodsOf(args[0], UseAuth(guardAuth, options));\n break;\n\n default:\n throw new UnsupportedDecoratorType(UseAuth, args);\n }\n };\n}\n","import {decorateMethodsOf, DecoratorParameters, decoratorTypeOf, DecoratorTypes, UnsupportedDecoratorType} from \"@tsed/core\";\nimport {UseBefore} from \"./useBefore\";\n\n/**\n * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when\n * the base of the requested path matches `path.\n *\n * ```typescript\n * @Controller('/')\n * @UseBeforeEach(Middleware1) // Called before each endpoint\n * export class Ctrl {\n *\n * @Get('/')\n * get() { }\n * }\n *\n * ```\n *\n * @returns {Function}\n * @param args\n * @decorator\n * @operation\n */\nexport function UseBeforeEach(...args: any[]): Function {\n return <T>(...decoratorArgs: DecoratorParameters): TypedPropertyDescriptor<T> | void => {\n switch (decoratorTypeOf(decoratorArgs)) {\n case DecoratorTypes.METHOD:\n return UseBefore(...args)(...decoratorArgs);\n\n case DecoratorTypes.CLASS:\n decorateMethodsOf(decoratorArgs[0], UseBefore(...args));\n break;\n\n default:\n throw new UnsupportedDecoratorType(UseBeforeEach, decoratorArgs);\n }\n };\n}\n","import {isFunction} from \"@tsed/core\";\nimport {PlatformMiddlewareLoadingOptions} from \"../domain/PlatformMiddlewareSettings\";\n\nexport function getMiddlewaresForHook(hook: string, settings: TsED.Configuration, defaultHook = \"\") {\n const {env, middlewares = []} = settings;\n\n return middlewares\n .map<PlatformMiddlewareLoadingOptions>((middleware) => {\n return isFunction(middleware)\n ? {\n env,\n hook: defaultHook,\n use: middleware\n }\n : {\n env,\n hook: defaultHook,\n ...middleware\n };\n })\n .filter((options) => {\n return options.use && options.env === env && options.hook === hook;\n });\n}\n"],"names":["Middleware","Injectable","type","ProviderType","MIDDLEWARE","AuthOptions","guardAuth","options","args","decoratorTypeOf","DecoratorTypes","METHOD","JsonEntityFn","entity","store","merge","CLASS","decorateMethodsOf","UnsupportedDecoratorType","Use","parameters","decoratorType","Route","use","UseAfter","endpoint","afterMiddlewares","concat","middlewares","get","set","useAfter","UseBefore","beforeMiddlewares","useBefore","UseAuth","useDecorators","StoreFn","has","UseBeforeEach","decoratorArgs","getMiddlewaresForHook","hook","settings","defaultHook","env","map","middleware","isFunction","filter"],"mappings":";;;;AAEA;;;;;;;SAMgBA;AACd,SAAOC,UAAU,CAAC;AAChBC,IAAAA,IAAI,EAAEC,YAAY,CAACC;AADH,GAAD,CAAjB;AAGD;;ACDD;;;;;;;;;;;;;;;;;;;;;;SAqBgBC,YAAYC,WAAsBC,UAAmC;AACnF,SAAO,CAAI,GAAGC,IAAP;AACL,YAAQC,eAAe,CAACD,IAAD,CAAvB;AACE,WAAKE,cAAc,CAACC,MAApB;AACE,eAAOC,YAAY,CAAEC,MAAD;AAClB,gBAAMC,KAAK,GAAGD,MAAM,CAACC,KAArB;AAEAA,UAAAA,KAAK,CAACC,KAAN,CAAYT,SAAZ,EAAuBC,OAAvB,EAAgC,IAAhC;AACD,SAJkB,CAAZ,CAIJ,GAAIC,IAJA,CAAP;;AAMF,WAAKE,cAAc,CAACM,KAApB;AACEC,QAAAA,iBAAiB,CAACT,IAAI,CAAC,CAAD,CAAL,EAAUH,WAAW,CAACC,SAAD,EAAYC,OAAZ,CAArB,CAAjB;AACA;;AAEF;AACE,cAAM,IAAIW,wBAAJ,CAA6Bb,WAA7B,EAA0CG,IAA1C,CAAN;AAbJ;AAeD,GAhBD;AAiBD;;AC9CD;;;;;;;;;;;;;;;;;;;;;;SAqBgBW,IAAI,GAAGX;AACrB,SAAOI,YAAY,CAAC,CAACC,MAAD,EAASO,UAAT;AAClB,YAAQP,MAAM,CAACQ,aAAf;AACE,WAAKX,cAAc,CAACC,MAApB;AACE,eAAOW,KAAK,CAAC,GAAGd,IAAJ,CAAZ;;AAEF,WAAKE,cAAc,CAACM,KAApB;AACEH,QAAAA,MAAM,CAACC,KAAP,CAAaC,KAAb,CAAmB,aAAnB,EAAkC;AAChCQ,UAAAA,GAAG,EAAEf;AAD2B,SAA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