@tsed/platform-middlewares 6.88.1 → 6.92.0

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 (84) hide show
  1. package/lib/{constants → cjs/constants}/index.d.ts +0 -0
  2. package/lib/cjs/constants/index.js +34 -0
  3. package/lib/cjs/constants/index.js.map +1 -0
  4. package/lib/{decorators → cjs/decorators}/authOptions.d.ts +0 -0
  5. package/lib/cjs/decorators/authOptions.js +58 -0
  6. package/lib/cjs/decorators/authOptions.js.map +1 -0
  7. package/lib/{decorators → cjs/decorators}/middleware.d.ts +0 -0
  8. package/lib/cjs/decorators/middleware.js +17 -0
  9. package/lib/cjs/decorators/middleware.js.map +1 -0
  10. package/lib/{decorators → cjs/decorators}/use.d.ts +0 -0
  11. package/lib/cjs/decorators/use.js +43 -0
  12. package/lib/cjs/decorators/use.js.map +1 -0
  13. package/lib/{decorators → cjs/decorators}/useAfter.d.ts +0 -0
  14. package/lib/cjs/decorators/useAfter.js +46 -0
  15. package/lib/cjs/decorators/useAfter.js.map +1 -0
  16. package/lib/{decorators → cjs/decorators}/useAuth.d.ts +0 -0
  17. package/lib/cjs/decorators/useAuth.js +45 -0
  18. package/lib/cjs/decorators/useAuth.js.map +1 -0
  19. package/lib/{decorators → cjs/decorators}/useBefore.d.ts +0 -0
  20. package/lib/cjs/decorators/useBefore.js +45 -0
  21. package/lib/cjs/decorators/useBefore.js.map +1 -0
  22. package/lib/{decorators → cjs/decorators}/useBeforeEach.d.ts +0 -0
  23. package/lib/cjs/decorators/useBeforeEach.js +40 -0
  24. package/lib/cjs/decorators/useBeforeEach.js.map +1 -0
  25. package/lib/{domain → cjs/domain}/MiddlewareMethods.d.ts +0 -0
  26. package/lib/cjs/domain/MiddlewareMethods.js +3 -0
  27. package/lib/cjs/domain/MiddlewareMethods.js.map +1 -0
  28. package/lib/{domain → cjs/domain}/PlatformMiddlewareSettings.d.ts +0 -0
  29. package/lib/cjs/domain/PlatformMiddlewareSettings.js +3 -0
  30. package/lib/cjs/domain/PlatformMiddlewareSettings.js.map +1 -0
  31. package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
  32. package/lib/cjs/index.js +17 -0
  33. package/lib/cjs/index.js.map +1 -0
  34. package/lib/{utils → cjs/utils}/getMiddlewaresForHook.d.ts +0 -0
  35. package/lib/cjs/utils/getMiddlewaresForHook.js +26 -0
  36. package/lib/cjs/utils/getMiddlewaresForHook.js.map +1 -0
  37. package/lib/{utils → cjs/utils}/mapUseOptions.d.ts +0 -0
  38. package/lib/cjs/utils/mapUseOptions.js +26 -0
  39. package/lib/cjs/utils/mapUseOptions.js.map +1 -0
  40. package/lib/esm/constants/index.js +31 -0
  41. package/lib/esm/constants/index.js.map +1 -0
  42. package/lib/esm/decorators/authOptions.js +54 -0
  43. package/lib/esm/decorators/authOptions.js.map +1 -0
  44. package/lib/esm/decorators/middleware.js +13 -0
  45. package/lib/esm/decorators/middleware.js.map +1 -0
  46. package/lib/esm/decorators/use.js +39 -0
  47. package/lib/esm/decorators/use.js.map +1 -0
  48. package/lib/esm/decorators/useAfter.js +42 -0
  49. package/lib/esm/decorators/useAfter.js.map +1 -0
  50. package/lib/esm/decorators/useAuth.js +41 -0
  51. package/lib/esm/decorators/useAuth.js.map +1 -0
  52. package/lib/esm/decorators/useBefore.js +41 -0
  53. package/lib/esm/decorators/useBefore.js.map +1 -0
  54. package/lib/esm/decorators/useBeforeEach.js +36 -0
  55. package/lib/esm/decorators/useBeforeEach.js.map +1 -0
  56. package/lib/esm/domain/MiddlewareMethods.js +2 -0
  57. package/lib/esm/domain/MiddlewareMethods.js.map +1 -0
  58. package/lib/esm/domain/PlatformMiddlewareSettings.js +2 -0
  59. package/lib/esm/domain/PlatformMiddlewareSettings.js.map +1 -0
  60. package/lib/esm/index.js +14 -0
  61. package/lib/esm/index.js.map +1 -0
  62. package/lib/esm/utils/getMiddlewaresForHook.js +22 -0
  63. package/lib/esm/utils/getMiddlewaresForHook.js.map +1 -0
  64. package/lib/esm/utils/mapUseOptions.js +22 -0
  65. package/lib/esm/utils/mapUseOptions.js.map +1 -0
  66. package/lib/types/constants/index.d.ts +1 -0
  67. package/lib/types/decorators/authOptions.d.ts +42 -0
  68. package/lib/types/decorators/middleware.d.ts +7 -0
  69. package/lib/types/decorators/use.d.ts +22 -0
  70. package/lib/types/decorators/useAfter.d.ts +21 -0
  71. package/lib/types/decorators/useAuth.d.ts +23 -0
  72. package/lib/types/decorators/useBefore.d.ts +21 -0
  73. package/lib/types/decorators/useBeforeEach.d.ts +21 -0
  74. package/lib/types/domain/MiddlewareMethods.d.ts +8 -0
  75. package/lib/types/domain/PlatformMiddlewareSettings.d.ts +17 -0
  76. package/lib/types/index.d.ts +10 -0
  77. package/lib/types/utils/getMiddlewaresForHook.d.ts +2 -0
  78. package/lib/types/utils/mapUseOptions.d.ts +5 -0
  79. package/package.json +16 -12
  80. package/tsconfig.compile.esm.json +23 -0
  81. package/lib/index.js +0 -326
  82. package/lib/index.js.map +0 -1
  83. package/lib/index.modern.js +0 -319
  84. package/lib/index.modern.js.map +0 -1
package/lib/index.js DELETED
@@ -1,326 +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
-
47
- if (options.responses) {
48
- const {
49
- responses
50
- } = options;
51
- store.merge("responses", responses, true);
52
- delete options.responses;
53
- }
54
-
55
- if (options.security) {
56
- const {
57
- security
58
- } = options;
59
- [].concat(security).forEach(security => {
60
- Object.entries(security).forEach(([name, scopes]) => {
61
- entity.operation.addSecurityScopes(name, scopes);
62
- });
63
- });
64
- delete options.security;
65
- }
66
-
67
- store.merge(guardAuth, options, true);
68
- })(...args);
69
-
70
- case core.DecoratorTypes.CLASS:
71
- core.decorateMethodsOf(args[0], AuthOptions(guardAuth, options));
72
- break;
73
-
74
- default:
75
- throw new core.UnsupportedDecoratorType(AuthOptions, args);
76
- }
77
- };
78
- }
79
-
80
- /**
81
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
82
- * the base of the requested path matches `path.
83
- *
84
- * ```typescript
85
- * @Controller('/')
86
- * @Use(Middleware1)
87
- * export class Ctrl {
88
- *
89
- * @Get('/')
90
- * @Use(Middleware2)
91
- * get() { }
92
- * }
93
- *
94
- * ```
95
- *
96
- * @returns {Function}
97
- * @param args
98
- * @decorator
99
- * @operation
100
- */
101
-
102
- function Use(...args) {
103
- return schema.JsonEntityFn((entity, parameters) => {
104
- switch (entity.decoratorType) {
105
- case core.DecoratorTypes.METHOD:
106
- return schema.Route(...args);
107
-
108
- case core.DecoratorTypes.CLASS:
109
- entity.store.merge("middlewares", {
110
- use: args
111
- });
112
- break;
113
-
114
- default:
115
- throw new core.UnsupportedDecoratorType(Use, parameters);
116
- }
117
- });
118
- }
119
-
120
- function _extends() {
121
- _extends = Object.assign || function (target) {
122
- for (var i = 1; i < arguments.length; i++) {
123
- var source = arguments[i];
124
-
125
- for (var key in source) {
126
- if (Object.prototype.hasOwnProperty.call(source, key)) {
127
- target[key] = source[key];
128
- }
129
- }
130
- }
131
-
132
- return target;
133
- };
134
-
135
- return _extends.apply(this, arguments);
136
- }
137
-
138
- /**
139
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
140
- * the base of the requested path matches `path.
141
- *
142
- * ```typescript
143
- * @Controller('/')
144
- * @UseAfter(Middleware1)
145
- * export class Ctrl {
146
- *
147
- * @Get('/')
148
- * @UseAfter(Middleware2)
149
- * get() { }
150
- * }
151
- * ```
152
- *
153
- * @returns {function(any, any, any): *}
154
- * @param args
155
- * @decorator
156
- * @operation
157
- */
158
-
159
- function UseAfter(...args) {
160
- return schema.JsonEntityFn((entity, parameters) => {
161
- switch (entity.decoratorType) {
162
- case core.DecoratorTypes.METHOD:
163
- const endpoint = entity;
164
- endpoint.afterMiddlewares = args.concat(endpoint.afterMiddlewares);
165
- break;
166
-
167
- case core.DecoratorTypes.CLASS:
168
- const middlewares = entity.store.get("middlewares") || {};
169
- entity.store.set("middlewares", _extends({}, middlewares, {
170
- useAfter: [...args, ...(middlewares.useAfter || [])]
171
- }));
172
- break;
173
-
174
- default:
175
- throw new core.UnsupportedDecoratorType(UseAfter, parameters);
176
- }
177
- });
178
- }
179
-
180
- /**
181
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
182
- * the base of the requested path matches `path.
183
- *
184
- * ```typescript
185
- * @Controller('/')
186
- * @UseBefore(Middleware1) // called only one time before all endpoint
187
- * export class Ctrl {
188
- *
189
- * @Get('/')
190
- * @UseBefore(Middleware2)
191
- * get() { }
192
- * }
193
- * ```
194
- *
195
- * @returns {function(any, any, any): *}
196
- * @param args
197
- * @decorator
198
- * @operation
199
- */
200
-
201
- function UseBefore(...args) {
202
- return schema.JsonEntityFn((entity, parameters) => {
203
- switch (entity.decoratorType) {
204
- case core.DecoratorTypes.METHOD:
205
- entity.beforeMiddlewares = args.concat(entity.beforeMiddlewares);
206
- break;
207
-
208
- case core.DecoratorTypes.CLASS:
209
- const middlewares = entity.store.get("middlewares") || {};
210
- entity.store.set("middlewares", _extends({}, middlewares, {
211
- useBefore: [...args, ...(middlewares.useBefore || [])]
212
- }));
213
- break;
214
-
215
- default:
216
- throw new core.UnsupportedDecoratorType(UseBefore, parameters);
217
- }
218
- });
219
- }
220
-
221
- /**
222
- * Use custom authentication strategy on your endpoint.
223
- *
224
- * ```typescript
225
- * @Controller('/mypath')
226
- * @UseAuth(MyAuthStrategy)
227
- * class MyCtrl {
228
- *
229
- * @Get('/')
230
- * @UseAuth(MyAuthStrategy, {role: 'admin'})
231
- * public getResource(){}
232
- * }
233
- * ```
234
- *
235
- * @param guardAuth {Type<any>} A middleware which implement a custom auth strategy
236
- * @param options {Object} Object passed to the customer auth strategy
237
- * @returns {Function}
238
- * @decorator
239
- * @operation
240
- */
241
-
242
- function UseAuth(guardAuth, options = {}) {
243
- return (...args) => {
244
- switch (core.decoratorTypeOf(args)) {
245
- case core.DecoratorTypes.METHOD:
246
- return core.useDecorators(core.StoreFn(store => {
247
- if (!store.has(guardAuth)) {
248
- return UseBefore(guardAuth);
249
- }
250
- }), AuthOptions(guardAuth, options))(...args);
251
-
252
- case core.DecoratorTypes.CLASS:
253
- core.decorateMethodsOf(args[0], UseAuth(guardAuth, options));
254
- break;
255
-
256
- default:
257
- throw new core.UnsupportedDecoratorType(UseAuth, args);
258
- }
259
- };
260
- }
261
-
262
- /**
263
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
264
- * the base of the requested path matches `path.
265
- *
266
- * ```typescript
267
- * @Controller('/')
268
- * @UseBeforeEach(Middleware1) // Called before each endpoint
269
- * export class Ctrl {
270
- *
271
- * @Get('/')
272
- * get() { }
273
- * }
274
- *
275
- * ```
276
- *
277
- * @returns {Function}
278
- * @param args
279
- * @decorator
280
- * @operation
281
- */
282
-
283
- function UseBeforeEach(...args) {
284
- return (...decoratorArgs) => {
285
- switch (core.decoratorTypeOf(decoratorArgs)) {
286
- case core.DecoratorTypes.METHOD:
287
- return UseBefore(...args)(...decoratorArgs);
288
-
289
- case core.DecoratorTypes.CLASS:
290
- core.decorateMethodsOf(decoratorArgs[0], UseBefore(...args));
291
- break;
292
-
293
- default:
294
- throw new core.UnsupportedDecoratorType(UseBeforeEach, decoratorArgs);
295
- }
296
- };
297
- }
298
-
299
- function getMiddlewaresForHook(hook, settings, defaultHook = "") {
300
- const {
301
- env,
302
- middlewares = []
303
- } = settings;
304
- return middlewares.map(middleware => {
305
- return core.isFunction(middleware) ? {
306
- env,
307
- hook: defaultHook,
308
- use: middleware
309
- } : _extends({
310
- env,
311
- hook: defaultHook
312
- }, middleware);
313
- }).filter(options => {
314
- return options.use && options.env === env && options.hook === hook;
315
- });
316
- }
317
-
318
- exports.AuthOptions = AuthOptions;
319
- exports.Middleware = Middleware;
320
- exports.Use = Use;
321
- exports.UseAfter = UseAfter;
322
- exports.UseAuth = UseAuth;
323
- exports.UseBefore = UseBefore;
324
- exports.UseBeforeEach = UseBeforeEach;
325
- exports.getMiddlewaresForHook = getMiddlewaresForHook;
326
- //# 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\nexport interface IAuthOptions {\n /**\n * @deprecated Since v6. Use @Returns from @tsed/schema\n */\n responses?: {\n [statusCode: string]: {\n description: string;\n };\n };\n /**\n * @deprecated Since v6. Use @Security from @tsed/schema\n */\n security?:\n | {\n [securityName: string]: string[];\n }[]\n | {\n [securityName: string]: string[];\n };\n\n [key: string]: any;\n}\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: IAuthOptions = {}): 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 if (options.responses) {\n const {responses} = options;\n store.merge(\"responses\", responses, true);\n delete options.responses;\n }\n\n if (options.security) {\n const {security} = options;\n [].concat(security as any).forEach((security) => {\n Object.entries(security).forEach(([name, scopes]: [string, string[]]) => {\n entity.operation!.addSecurityScopes(name, scopes);\n });\n });\n\n delete options.security;\n }\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, IAuthOptions} 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: IAuthOptions = {}): 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","responses","merge","security","concat","forEach","Object","entries","name","scopes","operation","addSecurityScopes","CLASS","decorateMethodsOf","UnsupportedDecoratorType","Use","parameters","decoratorType","Route","use","UseAfter","endpoint","afterMiddlewares","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,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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
-
47
- if (options.responses) {
48
- const {
49
- responses
50
- } = options;
51
- store.merge("responses", responses, true);
52
- delete options.responses;
53
- }
54
-
55
- if (options.security) {
56
- const {
57
- security
58
- } = options;
59
- [].concat(security).forEach(security => {
60
- Object.entries(security).forEach(([name, scopes]) => {
61
- entity.operation.addSecurityScopes(name, scopes);
62
- });
63
- });
64
- delete options.security;
65
- }
66
-
67
- store.merge(guardAuth, options, true);
68
- })(...args);
69
-
70
- case DecoratorTypes.CLASS:
71
- decorateMethodsOf(args[0], AuthOptions(guardAuth, options));
72
- break;
73
-
74
- default:
75
- throw new UnsupportedDecoratorType(AuthOptions, args);
76
- }
77
- };
78
- }
79
-
80
- /**
81
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
82
- * the base of the requested path matches `path.
83
- *
84
- * ```typescript
85
- * @Controller('/')
86
- * @Use(Middleware1)
87
- * export class Ctrl {
88
- *
89
- * @Get('/')
90
- * @Use(Middleware2)
91
- * get() { }
92
- * }
93
- *
94
- * ```
95
- *
96
- * @returns {Function}
97
- * @param args
98
- * @decorator
99
- * @operation
100
- */
101
-
102
- function Use(...args) {
103
- return JsonEntityFn((entity, parameters) => {
104
- switch (entity.decoratorType) {
105
- case DecoratorTypes.METHOD:
106
- return Route(...args);
107
-
108
- case DecoratorTypes.CLASS:
109
- entity.store.merge("middlewares", {
110
- use: args
111
- });
112
- break;
113
-
114
- default:
115
- throw new UnsupportedDecoratorType(Use, parameters);
116
- }
117
- });
118
- }
119
-
120
- function _extends() {
121
- _extends = Object.assign || function (target) {
122
- for (var i = 1; i < arguments.length; i++) {
123
- var source = arguments[i];
124
-
125
- for (var key in source) {
126
- if (Object.prototype.hasOwnProperty.call(source, key)) {
127
- target[key] = source[key];
128
- }
129
- }
130
- }
131
-
132
- return target;
133
- };
134
-
135
- return _extends.apply(this, arguments);
136
- }
137
-
138
- /**
139
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
140
- * the base of the requested path matches `path.
141
- *
142
- * ```typescript
143
- * @Controller('/')
144
- * @UseAfter(Middleware1)
145
- * export class Ctrl {
146
- *
147
- * @Get('/')
148
- * @UseAfter(Middleware2)
149
- * get() { }
150
- * }
151
- * ```
152
- *
153
- * @returns {function(any, any, any): *}
154
- * @param args
155
- * @decorator
156
- * @operation
157
- */
158
-
159
- function UseAfter(...args) {
160
- return JsonEntityFn((entity, parameters) => {
161
- switch (entity.decoratorType) {
162
- case DecoratorTypes.METHOD:
163
- const endpoint = entity;
164
- endpoint.afterMiddlewares = args.concat(endpoint.afterMiddlewares);
165
- break;
166
-
167
- case DecoratorTypes.CLASS:
168
- const middlewares = entity.store.get("middlewares") || {};
169
- entity.store.set("middlewares", _extends({}, middlewares, {
170
- useAfter: [...args, ...(middlewares.useAfter || [])]
171
- }));
172
- break;
173
-
174
- default:
175
- throw new UnsupportedDecoratorType(UseAfter, parameters);
176
- }
177
- });
178
- }
179
-
180
- /**
181
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
182
- * the base of the requested path matches `path.
183
- *
184
- * ```typescript
185
- * @Controller('/')
186
- * @UseBefore(Middleware1) // called only one time before all endpoint
187
- * export class Ctrl {
188
- *
189
- * @Get('/')
190
- * @UseBefore(Middleware2)
191
- * get() { }
192
- * }
193
- * ```
194
- *
195
- * @returns {function(any, any, any): *}
196
- * @param args
197
- * @decorator
198
- * @operation
199
- */
200
-
201
- function UseBefore(...args) {
202
- return JsonEntityFn((entity, parameters) => {
203
- switch (entity.decoratorType) {
204
- case DecoratorTypes.METHOD:
205
- entity.beforeMiddlewares = args.concat(entity.beforeMiddlewares);
206
- break;
207
-
208
- case DecoratorTypes.CLASS:
209
- const middlewares = entity.store.get("middlewares") || {};
210
- entity.store.set("middlewares", _extends({}, middlewares, {
211
- useBefore: [...args, ...(middlewares.useBefore || [])]
212
- }));
213
- break;
214
-
215
- default:
216
- throw new UnsupportedDecoratorType(UseBefore, parameters);
217
- }
218
- });
219
- }
220
-
221
- /**
222
- * Use custom authentication strategy on your endpoint.
223
- *
224
- * ```typescript
225
- * @Controller('/mypath')
226
- * @UseAuth(MyAuthStrategy)
227
- * class MyCtrl {
228
- *
229
- * @Get('/')
230
- * @UseAuth(MyAuthStrategy, {role: 'admin'})
231
- * public getResource(){}
232
- * }
233
- * ```
234
- *
235
- * @param guardAuth {Type<any>} A middleware which implement a custom auth strategy
236
- * @param options {Object} Object passed to the customer auth strategy
237
- * @returns {Function}
238
- * @decorator
239
- * @operation
240
- */
241
-
242
- function UseAuth(guardAuth, options = {}) {
243
- return (...args) => {
244
- switch (decoratorTypeOf(args)) {
245
- case DecoratorTypes.METHOD:
246
- return useDecorators(StoreFn(store => {
247
- if (!store.has(guardAuth)) {
248
- return UseBefore(guardAuth);
249
- }
250
- }), AuthOptions(guardAuth, options))(...args);
251
-
252
- case DecoratorTypes.CLASS:
253
- decorateMethodsOf(args[0], UseAuth(guardAuth, options));
254
- break;
255
-
256
- default:
257
- throw new UnsupportedDecoratorType(UseAuth, args);
258
- }
259
- };
260
- }
261
-
262
- /**
263
- * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
264
- * the base of the requested path matches `path.
265
- *
266
- * ```typescript
267
- * @Controller('/')
268
- * @UseBeforeEach(Middleware1) // Called before each endpoint
269
- * export class Ctrl {
270
- *
271
- * @Get('/')
272
- * get() { }
273
- * }
274
- *
275
- * ```
276
- *
277
- * @returns {Function}
278
- * @param args
279
- * @decorator
280
- * @operation
281
- */
282
-
283
- function UseBeforeEach(...args) {
284
- return (...decoratorArgs) => {
285
- switch (decoratorTypeOf(decoratorArgs)) {
286
- case DecoratorTypes.METHOD:
287
- return UseBefore(...args)(...decoratorArgs);
288
-
289
- case DecoratorTypes.CLASS:
290
- decorateMethodsOf(decoratorArgs[0], UseBefore(...args));
291
- break;
292
-
293
- default:
294
- throw new UnsupportedDecoratorType(UseBeforeEach, decoratorArgs);
295
- }
296
- };
297
- }
298
-
299
- function getMiddlewaresForHook(hook, settings, defaultHook = "") {
300
- const {
301
- env,
302
- middlewares = []
303
- } = settings;
304
- return middlewares.map(middleware => {
305
- return isFunction(middleware) ? {
306
- env,
307
- hook: defaultHook,
308
- use: middleware
309
- } : _extends({
310
- env,
311
- hook: defaultHook
312
- }, middleware);
313
- }).filter(options => {
314
- return options.use && options.env === env && options.hook === hook;
315
- });
316
- }
317
-
318
- export { AuthOptions, Middleware, Use, UseAfter, UseAuth, UseBefore, UseBeforeEach, getMiddlewaresForHook };
319
- //# sourceMappingURL=index.modern.js.map