@tsed/platform-middlewares 7.0.0-alpha.1 → 7.0.0-beta.3

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.
Files changed (71) hide show
  1. package/lib/cjs/constants/index.js +34 -0
  2. package/lib/cjs/constants/index.js.map +1 -0
  3. package/lib/cjs/decorators/authOptions.js +44 -0
  4. package/lib/cjs/decorators/authOptions.js.map +1 -0
  5. package/lib/cjs/decorators/middleware.js +17 -0
  6. package/lib/cjs/decorators/middleware.js.map +1 -0
  7. package/lib/cjs/decorators/use.js +43 -0
  8. package/lib/cjs/decorators/use.js.map +1 -0
  9. package/lib/cjs/decorators/useAfter.js +46 -0
  10. package/lib/cjs/decorators/useAfter.js.map +1 -0
  11. package/lib/cjs/decorators/useAuth.js +45 -0
  12. package/lib/cjs/decorators/useAuth.js.map +1 -0
  13. package/lib/cjs/decorators/useBefore.js +45 -0
  14. package/lib/cjs/decorators/useBefore.js.map +1 -0
  15. package/lib/cjs/decorators/useBeforeEach.js +40 -0
  16. package/lib/cjs/decorators/useBeforeEach.js.map +1 -0
  17. package/lib/cjs/domain/MiddlewareMethods.js +3 -0
  18. package/lib/cjs/domain/MiddlewareMethods.js.map +1 -0
  19. package/lib/cjs/domain/PlatformMiddlewareSettings.js +3 -0
  20. package/lib/cjs/domain/PlatformMiddlewareSettings.js.map +1 -0
  21. package/lib/cjs/index.js +17 -0
  22. package/lib/cjs/index.js.map +1 -0
  23. package/lib/cjs/utils/getMiddlewaresForHook.js +26 -0
  24. package/lib/cjs/utils/getMiddlewaresForHook.js.map +1 -0
  25. package/lib/cjs/utils/mapUseOptions.js +26 -0
  26. package/lib/cjs/utils/mapUseOptions.js.map +1 -0
  27. package/lib/esm/constants/index.js +31 -0
  28. package/lib/esm/constants/index.js.map +1 -0
  29. package/lib/esm/decorators/authOptions.js +40 -0
  30. package/lib/esm/decorators/authOptions.js.map +1 -0
  31. package/lib/esm/decorators/middleware.js +13 -0
  32. package/lib/esm/decorators/middleware.js.map +1 -0
  33. package/lib/esm/decorators/use.js +39 -0
  34. package/lib/esm/decorators/use.js.map +1 -0
  35. package/lib/esm/decorators/useAfter.js +42 -0
  36. package/lib/esm/decorators/useAfter.js.map +1 -0
  37. package/lib/esm/decorators/useAuth.js +41 -0
  38. package/lib/esm/decorators/useAuth.js.map +1 -0
  39. package/lib/esm/decorators/useBefore.js +41 -0
  40. package/lib/esm/decorators/useBefore.js.map +1 -0
  41. package/lib/esm/decorators/useBeforeEach.js +36 -0
  42. package/lib/esm/decorators/useBeforeEach.js.map +1 -0
  43. package/lib/esm/domain/MiddlewareMethods.js +2 -0
  44. package/lib/esm/domain/MiddlewareMethods.js.map +1 -0
  45. package/lib/esm/domain/PlatformMiddlewareSettings.js +2 -0
  46. package/lib/esm/domain/PlatformMiddlewareSettings.js.map +1 -0
  47. package/lib/esm/index.js +14 -0
  48. package/lib/esm/index.js.map +1 -0
  49. package/lib/esm/utils/getMiddlewaresForHook.js +22 -0
  50. package/lib/esm/utils/getMiddlewaresForHook.js.map +1 -0
  51. package/lib/esm/utils/mapUseOptions.js +22 -0
  52. package/lib/esm/utils/mapUseOptions.js.map +1 -0
  53. package/lib/{constants → types/constants}/index.d.ts +0 -0
  54. package/lib/{decorators → types/decorators}/authOptions.d.ts +0 -0
  55. package/lib/{decorators → types/decorators}/middleware.d.ts +0 -0
  56. package/lib/{decorators → types/decorators}/use.d.ts +0 -0
  57. package/lib/{decorators → types/decorators}/useAfter.d.ts +0 -0
  58. package/lib/{decorators → types/decorators}/useAuth.d.ts +0 -0
  59. package/lib/{decorators → types/decorators}/useBefore.d.ts +0 -0
  60. package/lib/{decorators → types/decorators}/useBeforeEach.d.ts +0 -0
  61. package/lib/{domain → types/domain}/MiddlewareMethods.d.ts +0 -0
  62. package/lib/{domain → types/domain}/PlatformMiddlewareSettings.d.ts +0 -0
  63. package/lib/{index.d.ts → types/index.d.ts} +0 -0
  64. package/lib/{utils → types/utils}/getMiddlewaresForHook.d.ts +0 -0
  65. package/lib/{utils → types/utils}/mapUseOptions.d.ts +0 -0
  66. package/package.json +17 -13
  67. package/tsconfig.compile.esm.json +23 -0
  68. package/lib/index.js +0 -333
  69. package/lib/index.js.map +0 -1
  70. package/lib/index.modern.js +0 -326
  71. package/lib/index.modern.js.map +0 -1
package/lib/index.js DELETED
@@ -1,333 +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
- const HTTP_METHODS = ["all", "checkout", "connect", "copy", "delete", "get", "head", "lock", "merge", "mkactivity", "mkcol", "move", "m-search", "notify", "options", "param", "patch", "post", "propfind", "propatch", "purge", "put", "report", "search", "subscribe", "trace", "unlock", "unsuscribe"];
60
-
61
- function mapUseOptions(args) {
62
- let method = undefined;
63
- let path = undefined;
64
- const middlewares = args.filter(arg => {
65
- if (typeof arg === "string" && HTTP_METHODS.includes(arg)) {
66
- method = arg;
67
- return false;
68
- }
69
-
70
- if (typeof arg === "string" || arg instanceof RegExp) {
71
- path = arg ? arg : "/";
72
- return false;
73
- }
74
-
75
- return !!arg;
76
- });
77
- return {
78
- path,
79
- method,
80
- middlewares
81
- };
82
- }
83
-
84
- /**
85
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
86
- * the base of the requested path matches `path.
87
- *
88
- * ```typescript
89
- * @Controller('/')
90
- * @Use(Middleware1)
91
- * export class Ctrl {
92
- *
93
- * @Get('/')
94
- * @Use(Middleware2)
95
- * get() { }
96
- * }
97
- *
98
- * ```
99
- *
100
- * @returns {Function}
101
- * @param args
102
- * @decorator
103
- * @operation
104
- */
105
-
106
- function Use(...args) {
107
- return schema.JsonEntityFn((entity, parameters) => {
108
- switch (entity.decoratorType) {
109
- case core.DecoratorTypes.METHOD:
110
- const options = mapUseOptions(args);
111
- options.path && schema.OperationPath(options.method || schema.OperationMethods.CUSTOM, options.path)(...parameters);
112
- entity.use(args);
113
- break;
114
-
115
- case core.DecoratorTypes.CLASS:
116
- entity.store.merge("middlewares", {
117
- use: args
118
- });
119
- break;
120
-
121
- default:
122
- throw new core.UnsupportedDecoratorType(Use, parameters);
123
- }
124
- });
125
- }
126
-
127
- function _extends() {
128
- _extends = Object.assign || function (target) {
129
- for (var i = 1; i < arguments.length; i++) {
130
- var source = arguments[i];
131
-
132
- for (var key in source) {
133
- if (Object.prototype.hasOwnProperty.call(source, key)) {
134
- target[key] = source[key];
135
- }
136
- }
137
- }
138
-
139
- return target;
140
- };
141
-
142
- return _extends.apply(this, arguments);
143
- }
144
-
145
- /**
146
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
147
- * the base of the requested path matches `path.
148
- *
149
- * ```typescript
150
- * @Controller('/')
151
- * @UseAfter(Middleware1)
152
- * export class Ctrl {
153
- *
154
- * @Get('/')
155
- * @UseAfter(Middleware2)
156
- * get() { }
157
- * }
158
- * ```
159
- *
160
- * @returns {function(any, any, any): *}
161
- * @param args
162
- * @decorator
163
- * @operation
164
- */
165
-
166
- function UseAfter(...args) {
167
- return schema.JsonEntityFn((entity, parameters) => {
168
- switch (entity.decoratorType) {
169
- case core.DecoratorTypes.METHOD:
170
- const endpoint = entity;
171
- endpoint.afterMiddlewares = args.concat(endpoint.afterMiddlewares);
172
- break;
173
-
174
- case core.DecoratorTypes.CLASS:
175
- const middlewares = entity.store.get("middlewares") || {};
176
- entity.store.set("middlewares", _extends({}, middlewares, {
177
- useAfter: [...args, ...(middlewares.useAfter || [])]
178
- }));
179
- break;
180
-
181
- default:
182
- throw new core.UnsupportedDecoratorType(UseAfter, parameters);
183
- }
184
- });
185
- }
186
-
187
- /**
188
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
189
- * the base of the requested path matches `path.
190
- *
191
- * ```typescript
192
- * @Controller('/')
193
- * @UseBefore(Middleware1) // called only one time before all endpoint
194
- * export class Ctrl {
195
- *
196
- * @Get('/')
197
- * @UseBefore(Middleware2)
198
- * get() { }
199
- * }
200
- * ```
201
- *
202
- * @returns {function(any, any, any): *}
203
- * @param args
204
- * @decorator
205
- * @operation
206
- */
207
-
208
- function UseBefore(...args) {
209
- return schema.JsonEntityFn((entity, parameters) => {
210
- switch (entity.decoratorType) {
211
- case core.DecoratorTypes.METHOD:
212
- entity.beforeMiddlewares = args.concat(entity.beforeMiddlewares);
213
- break;
214
-
215
- case core.DecoratorTypes.CLASS:
216
- const middlewares = entity.store.get("middlewares") || {};
217
- entity.store.set("middlewares", _extends({}, middlewares, {
218
- useBefore: [...args, ...(middlewares.useBefore || [])]
219
- }));
220
- break;
221
-
222
- default:
223
- throw new core.UnsupportedDecoratorType(UseBefore, parameters);
224
- }
225
- });
226
- }
227
-
228
- /**
229
- * Use custom authentication strategy on your endpoint.
230
- *
231
- * ```typescript
232
- * @Controller('/mypath')
233
- * @UseAuth(MyAuthStrategy)
234
- * class MyCtrl {
235
- *
236
- * @Get('/')
237
- * @UseAuth(MyAuthStrategy, {role: 'admin'})
238
- * public getResource(){}
239
- * }
240
- * ```
241
- *
242
- * @param guardAuth {Type<any>} A middleware which implement a custom auth strategy
243
- * @param options {Object} Object passed to the customer auth strategy
244
- * @returns {Function}
245
- * @decorator
246
- * @operation
247
- */
248
-
249
- function UseAuth(guardAuth, options = {}) {
250
- return (...args) => {
251
- switch (core.decoratorTypeOf(args)) {
252
- case core.DecoratorTypes.METHOD:
253
- return core.useDecorators(core.StoreFn(store => {
254
- if (!store.has(guardAuth)) {
255
- return UseBefore(guardAuth);
256
- }
257
- }), AuthOptions(guardAuth, options))(...args);
258
-
259
- case core.DecoratorTypes.CLASS:
260
- core.decorateMethodsOf(args[0], UseAuth(guardAuth, options));
261
- break;
262
-
263
- default:
264
- throw new core.UnsupportedDecoratorType(UseAuth, args);
265
- }
266
- };
267
- }
268
-
269
- /**
270
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
271
- * the base of the requested path matches `path.
272
- *
273
- * ```typescript
274
- * @Controller('/')
275
- * @UseBeforeEach(Middleware1) // Called before each endpoint
276
- * export class Ctrl {
277
- *
278
- * @Get('/')
279
- * get() { }
280
- * }
281
- *
282
- * ```
283
- *
284
- * @returns {Function}
285
- * @param args
286
- * @decorator
287
- * @operation
288
- */
289
-
290
- function UseBeforeEach(...args) {
291
- return (...decoratorArgs) => {
292
- switch (core.decoratorTypeOf(decoratorArgs)) {
293
- case core.DecoratorTypes.METHOD:
294
- return UseBefore(...args)(...decoratorArgs);
295
-
296
- case core.DecoratorTypes.CLASS:
297
- core.decorateMethodsOf(decoratorArgs[0], UseBefore(...args));
298
- break;
299
-
300
- default:
301
- throw new core.UnsupportedDecoratorType(UseBeforeEach, decoratorArgs);
302
- }
303
- };
304
- }
305
-
306
- function getMiddlewaresForHook(hook, settings, defaultHook = "") {
307
- const {
308
- env,
309
- middlewares = []
310
- } = settings;
311
- return middlewares.map(middleware => {
312
- return core.isFunction(middleware) ? {
313
- env,
314
- hook: defaultHook,
315
- use: middleware
316
- } : _extends({
317
- env,
318
- hook: defaultHook
319
- }, middleware);
320
- }).filter(options => {
321
- return options.use && options.env === env && options.hook === hook;
322
- });
323
- }
324
-
325
- exports.AuthOptions = AuthOptions;
326
- exports.Middleware = Middleware;
327
- exports.Use = Use;
328
- exports.UseAfter = UseAfter;
329
- exports.UseAuth = UseAuth;
330
- exports.UseBefore = UseBefore;
331
- exports.UseBeforeEach = UseBeforeEach;
332
- exports.getMiddlewaresForHook = getMiddlewaresForHook;
333
- //# 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/constants/index.ts","../src/utils/mapUseOptions.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","export const HTTP_METHODS = [\n \"all\",\n \"checkout\",\n \"connect\",\n \"copy\",\n \"delete\",\n \"get\",\n \"head\",\n \"lock\",\n \"merge\",\n \"mkactivity\",\n \"mkcol\",\n \"move\",\n \"m-search\",\n \"notify\",\n \"options\",\n \"param\",\n \"patch\",\n \"post\",\n \"propfind\",\n \"propatch\",\n \"purge\",\n \"put\",\n \"report\",\n \"search\",\n \"subscribe\",\n \"trace\",\n \"unlock\",\n \"unsuscribe\"\n];\n","import {HTTP_METHODS} from \"../constants\";\n\nexport function mapUseOptions(args: any[]) {\n let method: string | undefined = undefined;\n let path: string | RegExp | undefined = undefined;\n\n const middlewares = args.filter((arg: any) => {\n if (typeof arg === \"string\" && HTTP_METHODS.includes(arg)) {\n method = arg;\n\n return false;\n }\n\n if (typeof arg === \"string\" || arg instanceof RegExp) {\n path = arg ? arg : \"/\";\n\n return false;\n }\n\n return !!arg;\n });\n\n return {\n path,\n method,\n middlewares\n };\n}\n","import {DecoratorTypes, UnsupportedDecoratorType} from \"@tsed/core\";\nimport {JsonEntityFn, OperationMethods, OperationPath} 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 const options = mapUseOptions(args);\n\n options.path && OperationPath(options.method || OperationMethods.CUSTOM, options.path)(...parameters);\n\n entity.use(args);\n break;\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","HTTP_METHODS","mapUseOptions","method","undefined","path","middlewares","filter","arg","includes","RegExp","Use","parameters","decoratorType","OperationPath","OperationMethods","CUSTOM","use","UseAfter","endpoint","afterMiddlewares","concat","get","set","useAfter","UseBefore","beforeMiddlewares","useBefore","UseAuth","useDecorators","StoreFn","has","UseBeforeEach","decoratorArgs","getMiddlewaresForHook","hook","settings","defaultHook","env","map","middle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AACEC,QAAAA,sBAAiB,CAACkC,aAAa,CAAC,CAAD,CAAd,EAAmBR,SAAS,CAAC,GAAGnC,IAAJ,CAA5B,CAAjB;AACA;;AAEF;AACE,cAAM,IAAIU,6BAAJ,CAA6BgC,aAA7B,EAA4CC,aAA5C,CAAN;AATJ;AAWD,GAZD;AAaD;;SClCeC,sBAAsBC,MAAcC,UAA8BC,WAAW,GAAG;AAC9F,QAAM;AAACC,IAAAA,GAAD;AAAMhC,IAAAA,WAAW,GAAG;AAApB,MAA0B8B,QAAhC;AAEA,SAAO9B,WAAW,CACfiC,GADI,CACmCC,UAAD;AACrC,WAAOC,eAAU,CAACD,UAAD,CAAV,GACH;AACEF,MAAAA,GADF;AAEEH,MAAAA,IAAI,EAAEE,WAFR;AAGEpB,MAAAA,GAAG,EAAEuB;AAHP,KADG;AAODF,MAAAA,GAPC;AAQDH,MAAAA,IAAI,EAAEE;AARL,OASEG,UATF,CAAP;AAWD,GAbI,EAcJjC,MAdI,CAcIlB,OAAD;AACN,WAAOA,OAAO,CAAC4B,GAAR,IAAe5B,OAAO,CAACiD,GAAR,KAAgBA,GAA/B,IAAsCjD,OAAO,CAAC8C,IAAR,KAAiBA,IAA9D;AACD,GAhBI,CAAP;AAiBD;;;;;;;;;;;"}
@@ -1,326 +0,0 @@
1
- import { Injectable, ProviderType } from '@tsed/di';
2
- import { decoratorTypeOf, UnsupportedDecoratorType, DecoratorTypes, decorateMethodsOf, useDecorators, StoreFn, isFunction } from '@tsed/core';
3
- import { JsonEntityFn, OperationPath, OperationMethods } 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
- const HTTP_METHODS = ["all", "checkout", "connect", "copy", "delete", "get", "head", "lock", "merge", "mkactivity", "mkcol", "move", "m-search", "notify", "options", "param", "patch", "post", "propfind", "propatch", "purge", "put", "report", "search", "subscribe", "trace", "unlock", "unsuscribe"];
60
-
61
- function mapUseOptions(args) {
62
- let method = undefined;
63
- let path = undefined;
64
- const middlewares = args.filter(arg => {
65
- if (typeof arg === "string" && HTTP_METHODS.includes(arg)) {
66
- method = arg;
67
- return false;
68
- }
69
-
70
- if (typeof arg === "string" || arg instanceof RegExp) {
71
- path = arg ? arg : "/";
72
- return false;
73
- }
74
-
75
- return !!arg;
76
- });
77
- return {
78
- path,
79
- method,
80
- middlewares
81
- };
82
- }
83
-
84
- /**
85
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
86
- * the base of the requested path matches `path.
87
- *
88
- * ```typescript
89
- * @Controller('/')
90
- * @Use(Middleware1)
91
- * export class Ctrl {
92
- *
93
- * @Get('/')
94
- * @Use(Middleware2)
95
- * get() { }
96
- * }
97
- *
98
- * ```
99
- *
100
- * @returns {Function}
101
- * @param args
102
- * @decorator
103
- * @operation
104
- */
105
-
106
- function Use(...args) {
107
- return JsonEntityFn((entity, parameters) => {
108
- switch (entity.decoratorType) {
109
- case DecoratorTypes.METHOD:
110
- const options = mapUseOptions(args);
111
- options.path && OperationPath(options.method || OperationMethods.CUSTOM, options.path)(...parameters);
112
- entity.use(args);
113
- break;
114
-
115
- case DecoratorTypes.CLASS:
116
- entity.store.merge("middlewares", {
117
- use: args
118
- });
119
- break;
120
-
121
- default:
122
- throw new UnsupportedDecoratorType(Use, parameters);
123
- }
124
- });
125
- }
126
-
127
- function _extends() {
128
- _extends = Object.assign || function (target) {
129
- for (var i = 1; i < arguments.length; i++) {
130
- var source = arguments[i];
131
-
132
- for (var key in source) {
133
- if (Object.prototype.hasOwnProperty.call(source, key)) {
134
- target[key] = source[key];
135
- }
136
- }
137
- }
138
-
139
- return target;
140
- };
141
-
142
- return _extends.apply(this, arguments);
143
- }
144
-
145
- /**
146
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
147
- * the base of the requested path matches `path.
148
- *
149
- * ```typescript
150
- * @Controller('/')
151
- * @UseAfter(Middleware1)
152
- * export class Ctrl {
153
- *
154
- * @Get('/')
155
- * @UseAfter(Middleware2)
156
- * get() { }
157
- * }
158
- * ```
159
- *
160
- * @returns {function(any, any, any): *}
161
- * @param args
162
- * @decorator
163
- * @operation
164
- */
165
-
166
- function UseAfter(...args) {
167
- return JsonEntityFn((entity, parameters) => {
168
- switch (entity.decoratorType) {
169
- case DecoratorTypes.METHOD:
170
- const endpoint = entity;
171
- endpoint.afterMiddlewares = args.concat(endpoint.afterMiddlewares);
172
- break;
173
-
174
- case DecoratorTypes.CLASS:
175
- const middlewares = entity.store.get("middlewares") || {};
176
- entity.store.set("middlewares", _extends({}, middlewares, {
177
- useAfter: [...args, ...(middlewares.useAfter || [])]
178
- }));
179
- break;
180
-
181
- default:
182
- throw new UnsupportedDecoratorType(UseAfter, parameters);
183
- }
184
- });
185
- }
186
-
187
- /**
188
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
189
- * the base of the requested path matches `path.
190
- *
191
- * ```typescript
192
- * @Controller('/')
193
- * @UseBefore(Middleware1) // called only one time before all endpoint
194
- * export class Ctrl {
195
- *
196
- * @Get('/')
197
- * @UseBefore(Middleware2)
198
- * get() { }
199
- * }
200
- * ```
201
- *
202
- * @returns {function(any, any, any): *}
203
- * @param args
204
- * @decorator
205
- * @operation
206
- */
207
-
208
- function UseBefore(...args) {
209
- return JsonEntityFn((entity, parameters) => {
210
- switch (entity.decoratorType) {
211
- case DecoratorTypes.METHOD:
212
- entity.beforeMiddlewares = args.concat(entity.beforeMiddlewares);
213
- break;
214
-
215
- case DecoratorTypes.CLASS:
216
- const middlewares = entity.store.get("middlewares") || {};
217
- entity.store.set("middlewares", _extends({}, middlewares, {
218
- useBefore: [...args, ...(middlewares.useBefore || [])]
219
- }));
220
- break;
221
-
222
- default:
223
- throw new UnsupportedDecoratorType(UseBefore, parameters);
224
- }
225
- });
226
- }
227
-
228
- /**
229
- * Use custom authentication strategy on your endpoint.
230
- *
231
- * ```typescript
232
- * @Controller('/mypath')
233
- * @UseAuth(MyAuthStrategy)
234
- * class MyCtrl {
235
- *
236
- * @Get('/')
237
- * @UseAuth(MyAuthStrategy, {role: 'admin'})
238
- * public getResource(){}
239
- * }
240
- * ```
241
- *
242
- * @param guardAuth {Type<any>} A middleware which implement a custom auth strategy
243
- * @param options {Object} Object passed to the customer auth strategy
244
- * @returns {Function}
245
- * @decorator
246
- * @operation
247
- */
248
-
249
- function UseAuth(guardAuth, options = {}) {
250
- return (...args) => {
251
- switch (decoratorTypeOf(args)) {
252
- case DecoratorTypes.METHOD:
253
- return useDecorators(StoreFn(store => {
254
- if (!store.has(guardAuth)) {
255
- return UseBefore(guardAuth);
256
- }
257
- }), AuthOptions(guardAuth, options))(...args);
258
-
259
- case DecoratorTypes.CLASS:
260
- decorateMethodsOf(args[0], UseAuth(guardAuth, options));
261
- break;
262
-
263
- default:
264
- throw new UnsupportedDecoratorType(UseAuth, args);
265
- }
266
- };
267
- }
268
-
269
- /**
270
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
271
- * the base of the requested path matches `path.
272
- *
273
- * ```typescript
274
- * @Controller('/')
275
- * @UseBeforeEach(Middleware1) // Called before each endpoint
276
- * export class Ctrl {
277
- *
278
- * @Get('/')
279
- * get() { }
280
- * }
281
- *
282
- * ```
283
- *
284
- * @returns {Function}
285
- * @param args
286
- * @decorator
287
- * @operation
288
- */
289
-
290
- function UseBeforeEach(...args) {
291
- return (...decoratorArgs) => {
292
- switch (decoratorTypeOf(decoratorArgs)) {
293
- case DecoratorTypes.METHOD:
294
- return UseBefore(...args)(...decoratorArgs);
295
-
296
- case DecoratorTypes.CLASS:
297
- decorateMethodsOf(decoratorArgs[0], UseBefore(...args));
298
- break;
299
-
300
- default:
301
- throw new UnsupportedDecoratorType(UseBeforeEach, decoratorArgs);
302
- }
303
- };
304
- }
305
-
306
- function getMiddlewaresForHook(hook, settings, defaultHook = "") {
307
- const {
308
- env,
309
- middlewares = []
310
- } = settings;
311
- return middlewares.map(middleware => {
312
- return isFunction(middleware) ? {
313
- env,
314
- hook: defaultHook,
315
- use: middleware
316
- } : _extends({
317
- env,
318
- hook: defaultHook
319
- }, middleware);
320
- }).filter(options => {
321
- return options.use && options.env === env && options.hook === hook;
322
- });
323
- }
324
-
325
- export { AuthOptions, Middleware, Use, UseAfter, UseAuth, UseBefore, UseBeforeEach, getMiddlewaresForHook };
326
- //# sourceMappingURL=index.modern.js.map