@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.
- package/lib/cjs/constants/index.js +34 -0
- package/lib/cjs/constants/index.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 +17 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/utils/getMiddlewaresForHook.js +26 -0
- package/lib/cjs/utils/getMiddlewaresForHook.js.map +1 -0
- package/lib/cjs/utils/mapUseOptions.js +26 -0
- package/lib/cjs/utils/mapUseOptions.js.map +1 -0
- package/lib/esm/constants/index.js +31 -0
- package/lib/esm/constants/index.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 +14 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/utils/getMiddlewaresForHook.js +22 -0
- package/lib/esm/utils/getMiddlewaresForHook.js.map +1 -0
- package/lib/esm/utils/mapUseOptions.js +22 -0
- package/lib/esm/utils/mapUseOptions.js.map +1 -0
- package/lib/{constants → types/constants}/index.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} +0 -0
- package/lib/{utils → types/utils}/getMiddlewaresForHook.d.ts +0 -0
- package/lib/{utils → types/utils}/mapUseOptions.d.ts +0 -0
- package/package.json +17 -13
- package/tsconfig.compile.esm.json +23 -0
- package/lib/index.js +0 -333
- package/lib/index.js.map +0 -1
- package/lib/index.modern.js +0 -326
- package/lib/index.modern.js.map +0 -1
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HTTP_METHODS = void 0;
|
|
4
|
+
exports.HTTP_METHODS = [
|
|
5
|
+
"all",
|
|
6
|
+
"checkout",
|
|
7
|
+
"connect",
|
|
8
|
+
"copy",
|
|
9
|
+
"delete",
|
|
10
|
+
"get",
|
|
11
|
+
"head",
|
|
12
|
+
"lock",
|
|
13
|
+
"merge",
|
|
14
|
+
"mkactivity",
|
|
15
|
+
"mkcol",
|
|
16
|
+
"move",
|
|
17
|
+
"m-search",
|
|
18
|
+
"notify",
|
|
19
|
+
"options",
|
|
20
|
+
"param",
|
|
21
|
+
"patch",
|
|
22
|
+
"post",
|
|
23
|
+
"propfind",
|
|
24
|
+
"propatch",
|
|
25
|
+
"purge",
|
|
26
|
+
"put",
|
|
27
|
+
"report",
|
|
28
|
+
"search",
|
|
29
|
+
"subscribe",
|
|
30
|
+
"trace",
|
|
31
|
+
"unlock",
|
|
32
|
+
"unsuscribe"
|
|
33
|
+
];
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/constants/index.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAG;IAC1B,KAAK;IACL,UAAU;IACV,SAAS;IACT,MAAM;IACN,QAAQ;IACR,KAAK;IACL,MAAM;IACN,MAAM;IACN,OAAO;IACP,YAAY;IACZ,OAAO;IACP,MAAM;IACN,UAAU;IACV,QAAQ;IACR,SAAS;IACT,OAAO;IACP,OAAO;IACP,MAAM;IACN,UAAU;IACV,UAAU;IACV,OAAO;IACP,KAAK;IACL,QAAQ;IACR,QAAQ;IACR,WAAW;IACX,OAAO;IACP,QAAQ;IACR,YAAY;CACb,CAAC","sourcesContent":["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"]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthOptions = void 0;
|
|
4
|
+
const core_1 = require("@tsed/core");
|
|
5
|
+
const schema_1 = require("@tsed/schema");
|
|
6
|
+
/**
|
|
7
|
+
* Change authentication options.
|
|
8
|
+
*
|
|
9
|
+
* ```typescript
|
|
10
|
+
* @Controller('/mypath')
|
|
11
|
+
* @UseAuth(MyAuthStrategy, {role: ''})
|
|
12
|
+
* class MyCtrl {
|
|
13
|
+
*
|
|
14
|
+
* @Get('/')
|
|
15
|
+
* @AuthOptions(MyAuthStrategy, {role: 'admin'})
|
|
16
|
+
* public getResource(){}
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @param guardAuth
|
|
21
|
+
* @param options {Object} Object passed to the customer auth strategy
|
|
22
|
+
* @returns {Function}
|
|
23
|
+
* @decorator
|
|
24
|
+
* @decorator
|
|
25
|
+
* @operation
|
|
26
|
+
*/
|
|
27
|
+
function AuthOptions(guardAuth, options = {}) {
|
|
28
|
+
return (...args) => {
|
|
29
|
+
switch ((0, core_1.decoratorTypeOf)(args)) {
|
|
30
|
+
case core_1.DecoratorTypes.METHOD:
|
|
31
|
+
return (0, schema_1.JsonEntityFn)((entity) => {
|
|
32
|
+
const store = entity.store;
|
|
33
|
+
store.merge(guardAuth, options, true);
|
|
34
|
+
})(...args);
|
|
35
|
+
case core_1.DecoratorTypes.CLASS:
|
|
36
|
+
(0, core_1.decorateMethodsOf)(args[0], AuthOptions(guardAuth, options));
|
|
37
|
+
break;
|
|
38
|
+
default:
|
|
39
|
+
throw new core_1.UnsupportedDecoratorType(AuthOptions, args);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
exports.AuthOptions = AuthOptions;
|
|
44
|
+
//# sourceMappingURL=authOptions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authOptions.js","sourceRoot":"","sources":["../../../src/decorators/authOptions.ts"],"names":[],"mappings":";;;AAAA,qCAQoB;AACpB,yCAA0C;AAE1C;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,SAAgB,WAAW,CAAC,SAAoB,EAAE,UAAmC,EAAE;IACrF,OAAO,CAAI,GAAG,IAAyB,EAAqC,EAAE;QAC5E,QAAQ,IAAA,sBAAe,EAAC,IAAI,CAAC,EAAE;YAC7B,KAAK,qBAAc,CAAC,MAAM;gBACxB,OAAO,IAAA,qBAAY,EAAC,CAAC,MAAM,EAAE,EAAE;oBAC7B,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;oBAE3B,KAAK,CAAC,KAAK,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;gBACxC,CAAC,CAAC,CAAC,GAAI,IAAkC,CAAC,CAAC;YAE7C,KAAK,qBAAc,CAAC,KAAK;gBACvB,IAAA,wBAAiB,EAAC,IAAI,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC;gBAC5D,MAAM;YAER;gBACE,MAAM,IAAI,+BAAwB,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;SACzD;IACH,CAAC,CAAC;AACJ,CAAC;AAlBD,kCAkBC","sourcesContent":["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"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Middleware = void 0;
|
|
4
|
+
const di_1 = require("@tsed/di");
|
|
5
|
+
/**
|
|
6
|
+
* Register a new Middleware class.
|
|
7
|
+
*
|
|
8
|
+
* @decorator
|
|
9
|
+
* @classDecorator
|
|
10
|
+
*/
|
|
11
|
+
function Middleware() {
|
|
12
|
+
return (0, di_1.Injectable)({
|
|
13
|
+
type: di_1.ProviderType.MIDDLEWARE
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
exports.Middleware = Middleware;
|
|
17
|
+
//# sourceMappingURL=middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../../src/decorators/middleware.ts"],"names":[],"mappings":";;;AAAA,iCAAkD;AAElD;;;;;GAKG;AACH,SAAgB,UAAU;IACxB,OAAO,IAAA,eAAU,EAAC;QAChB,IAAI,EAAE,iBAAY,CAAC,UAAU;KAC9B,CAAC,CAAC;AACL,CAAC;AAJD,gCAIC","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"]}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Use = void 0;
|
|
4
|
+
const core_1 = require("@tsed/core");
|
|
5
|
+
const schema_1 = require("@tsed/schema");
|
|
6
|
+
/**
|
|
7
|
+
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
8
|
+
* the base of the requested path matches `path.
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* @Controller('/')
|
|
12
|
+
* @Use(Middleware1)
|
|
13
|
+
* export class Ctrl {
|
|
14
|
+
*
|
|
15
|
+
* @Get('/')
|
|
16
|
+
* @Use(Middleware2)
|
|
17
|
+
* get() { }
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @returns {Function}
|
|
23
|
+
* @param args
|
|
24
|
+
* @decorator
|
|
25
|
+
* @operation
|
|
26
|
+
*/
|
|
27
|
+
function Use(...args) {
|
|
28
|
+
return (0, schema_1.JsonEntityFn)((entity, parameters) => {
|
|
29
|
+
switch (entity.decoratorType) {
|
|
30
|
+
case core_1.DecoratorTypes.METHOD:
|
|
31
|
+
return (0, schema_1.Route)(...args);
|
|
32
|
+
case core_1.DecoratorTypes.CLASS:
|
|
33
|
+
entity.store.merge("middlewares", {
|
|
34
|
+
use: args
|
|
35
|
+
});
|
|
36
|
+
break;
|
|
37
|
+
default:
|
|
38
|
+
throw new core_1.UnsupportedDecoratorType(Use, parameters);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
exports.Use = Use;
|
|
43
|
+
//# sourceMappingURL=use.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use.js","sourceRoot":"","sources":["../../../src/decorators/use.ts"],"names":[],"mappings":";;;AAAA,qCAAoE;AACpE,yCAAkF;AAGlF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,SAAgB,GAAG,CAAC,GAAG,IAAW;IAChC,OAAO,IAAA,qBAAY,EAAC,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE;QACzC,QAAQ,MAAM,CAAC,aAAa,EAAE;YAC5B,KAAK,qBAAc,CAAC,MAAM;gBACxB,OAAO,IAAA,cAAK,EAAC,GAAG,IAAI,CAAC,CAAC;YAExB,KAAK,qBAAc,CAAC,KAAK;gBACvB,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,aAAa,EAAE;oBAChC,GAAG,EAAE,IAAI;iBACV,CAAC,CAAC;gBACH,MAAM;YAER;gBACE,MAAM,IAAI,+BAAwB,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC;SACvD;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAhBD,kBAgBC","sourcesContent":["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"]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UseAfter = void 0;
|
|
4
|
+
const core_1 = require("@tsed/core");
|
|
5
|
+
const schema_1 = require("@tsed/schema");
|
|
6
|
+
/**
|
|
7
|
+
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
8
|
+
* the base of the requested path matches `path.
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* @Controller('/')
|
|
12
|
+
* @UseAfter(Middleware1)
|
|
13
|
+
* export class Ctrl {
|
|
14
|
+
*
|
|
15
|
+
* @Get('/')
|
|
16
|
+
* @UseAfter(Middleware2)
|
|
17
|
+
* get() { }
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @returns {function(any, any, any): *}
|
|
22
|
+
* @param args
|
|
23
|
+
* @decorator
|
|
24
|
+
* @operation
|
|
25
|
+
*/
|
|
26
|
+
function UseAfter(...args) {
|
|
27
|
+
return (0, schema_1.JsonEntityFn)((entity, parameters) => {
|
|
28
|
+
switch (entity.decoratorType) {
|
|
29
|
+
case core_1.DecoratorTypes.METHOD:
|
|
30
|
+
const endpoint = entity;
|
|
31
|
+
endpoint.afterMiddlewares = args.concat(endpoint.afterMiddlewares);
|
|
32
|
+
break;
|
|
33
|
+
case core_1.DecoratorTypes.CLASS:
|
|
34
|
+
const middlewares = entity.store.get("middlewares") || {};
|
|
35
|
+
entity.store.set("middlewares", {
|
|
36
|
+
...middlewares,
|
|
37
|
+
useAfter: [...args, ...(middlewares.useAfter || [])]
|
|
38
|
+
});
|
|
39
|
+
break;
|
|
40
|
+
default:
|
|
41
|
+
throw new core_1.UnsupportedDecoratorType(UseAfter, parameters);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
exports.UseAfter = UseAfter;
|
|
46
|
+
//# sourceMappingURL=useAfter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAfter.js","sourceRoot":"","sources":["../../../src/decorators/useAfter.ts"],"names":[],"mappings":";;;AAAA,qCAAoE;AACpE,yCAA0C;AAE1C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,QAAQ,CAAC,GAAG,IAAW;IACrC,OAAO,IAAA,qBAAY,EAAC,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE;QACzC,QAAQ,MAAM,CAAC,aAAa,EAAE;YAC5B,KAAK,qBAAc,CAAC,MAAM;gBACxB,MAAM,QAAQ,GAAG,MAAM,CAAC;gBACxB,QAAQ,CAAC,gBAAgB,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CAAC,CAAC;gBAEnE,MAAM;YAER,KAAK,qBAAc,CAAC,KAAK;gBACvB,MAAM,WAAW,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;gBAE1D,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,aAAa,EAAE;oBAC9B,GAAG,WAAW;oBACd,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,CAAC,WAAW,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC;iBACrD,CAAC,CAAC;gBACH,MAAM;YAER;gBACE,MAAM,IAAI,+BAAwB,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;SAC5D;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAtBD,4BAsBC","sourcesContent":["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"]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UseAuth = void 0;
|
|
4
|
+
const core_1 = require("@tsed/core");
|
|
5
|
+
const authOptions_1 = require("./authOptions");
|
|
6
|
+
const useBefore_1 = require("./useBefore");
|
|
7
|
+
/**
|
|
8
|
+
* Use custom authentication strategy on your endpoint.
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* @Controller('/mypath')
|
|
12
|
+
* @UseAuth(MyAuthStrategy)
|
|
13
|
+
* class MyCtrl {
|
|
14
|
+
*
|
|
15
|
+
* @Get('/')
|
|
16
|
+
* @UseAuth(MyAuthStrategy, {role: 'admin'})
|
|
17
|
+
* public getResource(){}
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @param guardAuth {Type<any>} A middleware which implement a custom auth strategy
|
|
22
|
+
* @param options {Object} Object passed to the customer auth strategy
|
|
23
|
+
* @returns {Function}
|
|
24
|
+
* @decorator
|
|
25
|
+
* @operation
|
|
26
|
+
*/
|
|
27
|
+
function UseAuth(guardAuth, options = {}) {
|
|
28
|
+
return (...args) => {
|
|
29
|
+
switch ((0, core_1.decoratorTypeOf)(args)) {
|
|
30
|
+
case core_1.DecoratorTypes.METHOD:
|
|
31
|
+
return (0, core_1.useDecorators)((0, core_1.StoreFn)((store) => {
|
|
32
|
+
if (!store.has(guardAuth)) {
|
|
33
|
+
return (0, useBefore_1.UseBefore)(guardAuth);
|
|
34
|
+
}
|
|
35
|
+
}), (0, authOptions_1.AuthOptions)(guardAuth, options))(...args);
|
|
36
|
+
case core_1.DecoratorTypes.CLASS:
|
|
37
|
+
(0, core_1.decorateMethodsOf)(args[0], UseAuth(guardAuth, options));
|
|
38
|
+
break;
|
|
39
|
+
default:
|
|
40
|
+
throw new core_1.UnsupportedDecoratorType(UseAuth, args);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
exports.UseAuth = UseAuth;
|
|
45
|
+
//# sourceMappingURL=useAuth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAuth.js","sourceRoot":"","sources":["../../../src/decorators/useAuth.ts"],"names":[],"mappings":";;;AAAA,qCAUoB;AACpB,+CAA0C;AAC1C,2CAAsC;AAEtC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,OAAO,CAAC,SAAoB,EAAE,UAAmC,EAAE;IACjF,OAAO,CAAI,GAAG,IAAyB,EAAqC,EAAE;QAC5E,QAAQ,IAAA,sBAAe,EAAC,IAAI,CAAC,EAAE;YAC7B,KAAK,qBAAc,CAAC,MAAM;gBACxB,OAAO,IAAA,oBAAa,EAClB,IAAA,cAAO,EAAC,CAAC,KAAY,EAAE,EAAE;oBACvB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE;wBACzB,OAAO,IAAA,qBAAS,EAAC,SAAS,CAAC,CAAC;qBAC7B;gBACH,CAAC,CAAC,EACF,IAAA,yBAAW,EAAC,SAAS,EAAE,OAAO,CAAC,CAChC,CAAC,GAAG,IAAI,CAAC,CAAC;YAEb,KAAK,qBAAc,CAAC,KAAK;gBACvB,IAAA,wBAAiB,EAAC,IAAI,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC;gBACxD,MAAM;YAER;gBACE,MAAM,IAAI,+BAAwB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;SACrD;IACH,CAAC,CAAC;AACJ,CAAC;AArBD,0BAqBC","sourcesContent":["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"]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UseBefore = void 0;
|
|
4
|
+
const core_1 = require("@tsed/core");
|
|
5
|
+
const schema_1 = require("@tsed/schema");
|
|
6
|
+
/**
|
|
7
|
+
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
8
|
+
* the base of the requested path matches `path.
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* @Controller('/')
|
|
12
|
+
* @UseBefore(Middleware1) // called only one time before all endpoint
|
|
13
|
+
* export class Ctrl {
|
|
14
|
+
*
|
|
15
|
+
* @Get('/')
|
|
16
|
+
* @UseBefore(Middleware2)
|
|
17
|
+
* get() { }
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @returns {function(any, any, any): *}
|
|
22
|
+
* @param args
|
|
23
|
+
* @decorator
|
|
24
|
+
* @operation
|
|
25
|
+
*/
|
|
26
|
+
function UseBefore(...args) {
|
|
27
|
+
return (0, schema_1.JsonEntityFn)((entity, parameters) => {
|
|
28
|
+
switch (entity.decoratorType) {
|
|
29
|
+
case core_1.DecoratorTypes.METHOD:
|
|
30
|
+
entity.beforeMiddlewares = args.concat(entity.beforeMiddlewares);
|
|
31
|
+
break;
|
|
32
|
+
case core_1.DecoratorTypes.CLASS:
|
|
33
|
+
const middlewares = entity.store.get("middlewares") || {};
|
|
34
|
+
entity.store.set("middlewares", {
|
|
35
|
+
...middlewares,
|
|
36
|
+
useBefore: [...args, ...(middlewares.useBefore || [])]
|
|
37
|
+
});
|
|
38
|
+
break;
|
|
39
|
+
default:
|
|
40
|
+
throw new core_1.UnsupportedDecoratorType(UseBefore, parameters);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
exports.UseBefore = UseBefore;
|
|
45
|
+
//# sourceMappingURL=useBefore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useBefore.js","sourceRoot":"","sources":["../../../src/decorators/useBefore.ts"],"names":[],"mappings":";;;AAAA,qCAAoE;AACpE,yCAA0C;AAE1C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,SAAS,CAAC,GAAG,IAAW;IACtC,OAAO,IAAA,qBAAY,EAAC,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE;QACzC,QAAQ,MAAM,CAAC,aAAa,EAAE;YAC5B,KAAK,qBAAc,CAAC,MAAM;gBACxB,MAAM,CAAC,iBAAiB,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC;gBAEjE,MAAM;YAER,KAAK,qBAAc,CAAC,KAAK;gBACvB,MAAM,WAAW,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;gBAE1D,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,aAAa,EAAE;oBAC9B,GAAG,WAAW;oBACd,SAAS,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,CAAC,WAAW,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC;iBACvD,CAAC,CAAC;gBACH,MAAM;YAER;gBACE,MAAM,IAAI,+BAAwB,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;SAC7D;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AArBD,8BAqBC","sourcesContent":["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"]}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UseBeforeEach = void 0;
|
|
4
|
+
const core_1 = require("@tsed/core");
|
|
5
|
+
const useBefore_1 = require("./useBefore");
|
|
6
|
+
/**
|
|
7
|
+
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
8
|
+
* the base of the requested path matches `path.
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* @Controller('/')
|
|
12
|
+
* @UseBeforeEach(Middleware1) // Called before each endpoint
|
|
13
|
+
* export class Ctrl {
|
|
14
|
+
*
|
|
15
|
+
* @Get('/')
|
|
16
|
+
* get() { }
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @returns {Function}
|
|
22
|
+
* @param args
|
|
23
|
+
* @decorator
|
|
24
|
+
* @operation
|
|
25
|
+
*/
|
|
26
|
+
function UseBeforeEach(...args) {
|
|
27
|
+
return (...decoratorArgs) => {
|
|
28
|
+
switch ((0, core_1.decoratorTypeOf)(decoratorArgs)) {
|
|
29
|
+
case core_1.DecoratorTypes.METHOD:
|
|
30
|
+
return (0, useBefore_1.UseBefore)(...args)(...decoratorArgs);
|
|
31
|
+
case core_1.DecoratorTypes.CLASS:
|
|
32
|
+
(0, core_1.decorateMethodsOf)(decoratorArgs[0], (0, useBefore_1.UseBefore)(...args));
|
|
33
|
+
break;
|
|
34
|
+
default:
|
|
35
|
+
throw new core_1.UnsupportedDecoratorType(UseBeforeEach, decoratorArgs);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
exports.UseBeforeEach = UseBeforeEach;
|
|
40
|
+
//# sourceMappingURL=useBeforeEach.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useBeforeEach.js","sourceRoot":"","sources":["../../../src/decorators/useBeforeEach.ts"],"names":[],"mappings":";;;AAAA,qCAA6H;AAC7H,2CAAsC;AAEtC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,aAAa,CAAC,GAAG,IAAW;IAC1C,OAAO,CAAI,GAAG,aAAkC,EAAqC,EAAE;QACrF,QAAQ,IAAA,sBAAe,EAAC,aAAa,CAAC,EAAE;YACtC,KAAK,qBAAc,CAAC,MAAM;gBACxB,OAAO,IAAA,qBAAS,EAAC,GAAG,IAAI,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC;YAE9C,KAAK,qBAAc,CAAC,KAAK;gBACvB,IAAA,wBAAiB,EAAC,aAAa,CAAC,CAAC,CAAC,EAAE,IAAA,qBAAS,EAAC,GAAG,IAAI,CAAC,CAAC,CAAC;gBACxD,MAAM;YAER;gBACE,MAAM,IAAI,+BAAwB,CAAC,aAAa,EAAE,aAAa,CAAC,CAAC;SACpE;IACH,CAAC,CAAC;AACJ,CAAC;AAdD,sCAcC","sourcesContent":["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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MiddlewareMethods.js","sourceRoot":"","sources":["../../../src/domain/MiddlewareMethods.ts"],"names":[],"mappings":"","sourcesContent":["export interface MiddlewareMethods {\n use(...args: any[]): void | any | Promise<any>;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlatformMiddlewareSettings.js","sourceRoot":"","sources":["../../../src/domain/PlatformMiddlewareSettings.ts"],"names":[],"mappings":"","sourcesContent":["import {Env, Type} from \"@tsed/core\";\n\nexport type PlatformMiddlewareLoadingOptions = {env?: Env; use: Function | Type<any>; hook?: string};\nexport type PlatformMiddlewareSettings = Function | Type<any> | PlatformMiddlewareLoadingOptions | any;\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n /**\n * Load middlewares on the $beforeRoutesInit hook (or on the specified hook event name).\n */\n middlewares: PlatformMiddlewareSettings[];\n }\n }\n}\n"]}
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
// decorators
|
|
5
|
+
(0, tslib_1.__exportStar)(require("./decorators/middleware"), exports);
|
|
6
|
+
(0, tslib_1.__exportStar)(require("./decorators/authOptions"), exports);
|
|
7
|
+
(0, tslib_1.__exportStar)(require("./decorators/use"), exports);
|
|
8
|
+
(0, tslib_1.__exportStar)(require("./decorators/useAfter"), exports);
|
|
9
|
+
(0, tslib_1.__exportStar)(require("./decorators/useAuth"), exports);
|
|
10
|
+
(0, tslib_1.__exportStar)(require("./decorators/useBefore"), exports);
|
|
11
|
+
(0, tslib_1.__exportStar)(require("./decorators/useBeforeEach"), exports);
|
|
12
|
+
// domain
|
|
13
|
+
(0, tslib_1.__exportStar)(require("./domain/MiddlewareMethods"), exports);
|
|
14
|
+
(0, tslib_1.__exportStar)(require("./domain/PlatformMiddlewareSettings"), exports);
|
|
15
|
+
// utils
|
|
16
|
+
(0, tslib_1.__exportStar)(require("./utils/getMiddlewaresForHook"), exports);
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,aAAa;AACb,uEAAwC;AACxC,wEAAyC;AACzC,gEAAiC;AACjC,qEAAsC;AACtC,oEAAqC;AACrC,sEAAuC;AACvC,0EAA2C;AAE3C,SAAS;AACT,0EAA2C;AAC3C,mFAAoD;AAEpD,QAAQ;AACR,6EAA8C","sourcesContent":["// decorators\nexport * from \"./decorators/middleware\";\nexport * from \"./decorators/authOptions\";\nexport * from \"./decorators/use\";\nexport * from \"./decorators/useAfter\";\nexport * from \"./decorators/useAuth\";\nexport * from \"./decorators/useBefore\";\nexport * from \"./decorators/useBeforeEach\";\n\n// domain\nexport * from \"./domain/MiddlewareMethods\";\nexport * from \"./domain/PlatformMiddlewareSettings\";\n\n// utils\nexport * from \"./utils/getMiddlewaresForHook\";\n"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMiddlewaresForHook = void 0;
|
|
4
|
+
const core_1 = require("@tsed/core");
|
|
5
|
+
function getMiddlewaresForHook(hook, settings, defaultHook = "") {
|
|
6
|
+
const { env, middlewares = [] } = settings;
|
|
7
|
+
return middlewares
|
|
8
|
+
.map((middleware) => {
|
|
9
|
+
return (0, core_1.isFunction)(middleware)
|
|
10
|
+
? {
|
|
11
|
+
env,
|
|
12
|
+
hook: defaultHook,
|
|
13
|
+
use: middleware
|
|
14
|
+
}
|
|
15
|
+
: {
|
|
16
|
+
env,
|
|
17
|
+
hook: defaultHook,
|
|
18
|
+
...middleware
|
|
19
|
+
};
|
|
20
|
+
})
|
|
21
|
+
.filter((options) => {
|
|
22
|
+
return options.use && options.env === env && options.hook === hook;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
exports.getMiddlewaresForHook = getMiddlewaresForHook;
|
|
26
|
+
//# sourceMappingURL=getMiddlewaresForHook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getMiddlewaresForHook.js","sourceRoot":"","sources":["../../../src/utils/getMiddlewaresForHook.ts"],"names":[],"mappings":";;;AAAA,qCAAsC;AAGtC,SAAgB,qBAAqB,CAAC,IAAY,EAAE,QAA4B,EAAE,WAAW,GAAG,EAAE;IAChG,MAAM,EAAC,GAAG,EAAE,WAAW,GAAG,EAAE,EAAC,GAAG,QAAQ,CAAC;IAEzC,OAAO,WAAW;SACf,GAAG,CAAmC,CAAC,UAAU,EAAE,EAAE;QACpD,OAAO,IAAA,iBAAU,EAAC,UAAU,CAAC;YAC3B,CAAC,CAAC;gBACE,GAAG;gBACH,IAAI,EAAE,WAAW;gBACjB,GAAG,EAAE,UAAU;aAChB;YACH,CAAC,CAAC;gBACE,GAAG;gBACH,IAAI,EAAE,WAAW;gBACjB,GAAG,UAAU;aACd,CAAC;IACR,CAAC,CAAC;SACD,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE;QAClB,OAAO,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,KAAK,GAAG,IAAI,OAAO,CAAC,IAAI,KAAK,IAAI,CAAC;IACrE,CAAC,CAAC,CAAC;AACP,CAAC;AApBD,sDAoBC","sourcesContent":["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"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mapUseOptions = void 0;
|
|
4
|
+
const constants_1 = require("../constants");
|
|
5
|
+
function mapUseOptions(args) {
|
|
6
|
+
let method = undefined;
|
|
7
|
+
let path = undefined;
|
|
8
|
+
const middlewares = args.filter((arg) => {
|
|
9
|
+
if (typeof arg === "string" && constants_1.HTTP_METHODS.includes(arg)) {
|
|
10
|
+
method = arg;
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
if (typeof arg === "string" || arg instanceof RegExp) {
|
|
14
|
+
path = arg ? arg : "/";
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
return !!arg;
|
|
18
|
+
});
|
|
19
|
+
return {
|
|
20
|
+
path,
|
|
21
|
+
method,
|
|
22
|
+
middlewares
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
exports.mapUseOptions = mapUseOptions;
|
|
26
|
+
//# sourceMappingURL=mapUseOptions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mapUseOptions.js","sourceRoot":"","sources":["../../../src/utils/mapUseOptions.ts"],"names":[],"mappings":";;;AAAA,4CAA0C;AAE1C,SAAgB,aAAa,CAAC,IAAW;IACvC,IAAI,MAAM,GAAuB,SAAS,CAAC;IAC3C,IAAI,IAAI,GAAgC,SAAS,CAAC;IAElD,MAAM,WAAW,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,GAAQ,EAAE,EAAE;QAC3C,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,wBAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;YACzD,MAAM,GAAG,GAAG,CAAC;YAEb,OAAO,KAAK,CAAC;SACd;QAED,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,YAAY,MAAM,EAAE;YACpD,IAAI,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;YAEvB,OAAO,KAAK,CAAC;SACd;QAED,OAAO,CAAC,CAAC,GAAG,CAAC;IACf,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,IAAI;QACJ,MAAM;QACN,WAAW;KACZ,CAAC;AACJ,CAAC;AAzBD,sCAyBC","sourcesContent":["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"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const HTTP_METHODS = [
|
|
2
|
+
"all",
|
|
3
|
+
"checkout",
|
|
4
|
+
"connect",
|
|
5
|
+
"copy",
|
|
6
|
+
"delete",
|
|
7
|
+
"get",
|
|
8
|
+
"head",
|
|
9
|
+
"lock",
|
|
10
|
+
"merge",
|
|
11
|
+
"mkactivity",
|
|
12
|
+
"mkcol",
|
|
13
|
+
"move",
|
|
14
|
+
"m-search",
|
|
15
|
+
"notify",
|
|
16
|
+
"options",
|
|
17
|
+
"param",
|
|
18
|
+
"patch",
|
|
19
|
+
"post",
|
|
20
|
+
"propfind",
|
|
21
|
+
"propatch",
|
|
22
|
+
"purge",
|
|
23
|
+
"put",
|
|
24
|
+
"report",
|
|
25
|
+
"search",
|
|
26
|
+
"subscribe",
|
|
27
|
+
"trace",
|
|
28
|
+
"unlock",
|
|
29
|
+
"unsuscribe"
|
|
30
|
+
];
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/constants/index.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,KAAK;IACL,UAAU;IACV,SAAS;IACT,MAAM;IACN,QAAQ;IACR,KAAK;IACL,MAAM;IACN,MAAM;IACN,OAAO;IACP,YAAY;IACZ,OAAO;IACP,MAAM;IACN,UAAU;IACV,QAAQ;IACR,SAAS;IACT,OAAO;IACP,OAAO;IACP,MAAM;IACN,UAAU;IACV,UAAU;IACV,OAAO;IACP,KAAK;IACL,QAAQ;IACR,QAAQ;IACR,WAAW;IACX,OAAO;IACP,QAAQ;IACR,YAAY;CACb,CAAC","sourcesContent":["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"]}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { decorateMethodsOf, decoratorTypeOf, DecoratorTypes, UnsupportedDecoratorType } from "@tsed/core";
|
|
2
|
+
import { JsonEntityFn } from "@tsed/schema";
|
|
3
|
+
/**
|
|
4
|
+
* Change authentication options.
|
|
5
|
+
*
|
|
6
|
+
* ```typescript
|
|
7
|
+
* @Controller('/mypath')
|
|
8
|
+
* @UseAuth(MyAuthStrategy, {role: ''})
|
|
9
|
+
* class MyCtrl {
|
|
10
|
+
*
|
|
11
|
+
* @Get('/')
|
|
12
|
+
* @AuthOptions(MyAuthStrategy, {role: 'admin'})
|
|
13
|
+
* public getResource(){}
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @param guardAuth
|
|
18
|
+
* @param options {Object} Object passed to the customer auth strategy
|
|
19
|
+
* @returns {Function}
|
|
20
|
+
* @decorator
|
|
21
|
+
* @decorator
|
|
22
|
+
* @operation
|
|
23
|
+
*/
|
|
24
|
+
export function AuthOptions(guardAuth, options = {}) {
|
|
25
|
+
return (...args) => {
|
|
26
|
+
switch (decoratorTypeOf(args)) {
|
|
27
|
+
case DecoratorTypes.METHOD:
|
|
28
|
+
return JsonEntityFn((entity) => {
|
|
29
|
+
const store = entity.store;
|
|
30
|
+
store.merge(guardAuth, options, true);
|
|
31
|
+
})(...args);
|
|
32
|
+
case DecoratorTypes.CLASS:
|
|
33
|
+
decorateMethodsOf(args[0], AuthOptions(guardAuth, options));
|
|
34
|
+
break;
|
|
35
|
+
default:
|
|
36
|
+
throw new UnsupportedDecoratorType(AuthOptions, args);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=authOptions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authOptions.js","sourceRoot":"","sources":["../../../src/decorators/authOptions.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAGjB,eAAe,EACf,cAAc,EAEd,wBAAwB,EACzB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAC,YAAY,EAAC,MAAM,cAAc,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,WAAW,CAAC,SAAoB,EAAE,UAAmC,EAAE;IACrF,OAAO,CAAI,GAAG,IAAyB,EAAqC,EAAE;QAC5E,QAAQ,eAAe,CAAC,IAAI,CAAC,EAAE;YAC7B,KAAK,cAAc,CAAC,MAAM;gBACxB,OAAO,YAAY,CAAC,CAAC,MAAM,EAAE,EAAE;oBAC7B,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;oBAE3B,KAAK,CAAC,KAAK,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;gBACxC,CAAC,CAAC,CAAC,GAAI,IAAkC,CAAC,CAAC;YAE7C,KAAK,cAAc,CAAC,KAAK;gBACvB,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC;gBAC5D,MAAM;YAER;gBACE,MAAM,IAAI,wBAAwB,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;SACzD;IACH,CAAC,CAAC;AACJ,CAAC","sourcesContent":["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"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Injectable, ProviderType } from "@tsed/di";
|
|
2
|
+
/**
|
|
3
|
+
* Register a new Middleware class.
|
|
4
|
+
*
|
|
5
|
+
* @decorator
|
|
6
|
+
* @classDecorator
|
|
7
|
+
*/
|
|
8
|
+
export function Middleware() {
|
|
9
|
+
return Injectable({
|
|
10
|
+
type: ProviderType.MIDDLEWARE
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../../src/decorators/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,YAAY,EAAC,MAAM,UAAU,CAAC;AAElD;;;;;GAKG;AACH,MAAM,UAAU,UAAU;IACxB,OAAO,UAAU,CAAC;QAChB,IAAI,EAAE,YAAY,CAAC,UAAU;KAC9B,CAAC,CAAC;AACL,CAAC","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"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { DecoratorTypes, UnsupportedDecoratorType } from "@tsed/core";
|
|
2
|
+
import { JsonEntityFn, Route } from "@tsed/schema";
|
|
3
|
+
/**
|
|
4
|
+
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
5
|
+
* the base of the requested path matches `path.
|
|
6
|
+
*
|
|
7
|
+
* ```typescript
|
|
8
|
+
* @Controller('/')
|
|
9
|
+
* @Use(Middleware1)
|
|
10
|
+
* export class Ctrl {
|
|
11
|
+
*
|
|
12
|
+
* @Get('/')
|
|
13
|
+
* @Use(Middleware2)
|
|
14
|
+
* get() { }
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @returns {Function}
|
|
20
|
+
* @param args
|
|
21
|
+
* @decorator
|
|
22
|
+
* @operation
|
|
23
|
+
*/
|
|
24
|
+
export function Use(...args) {
|
|
25
|
+
return JsonEntityFn((entity, parameters) => {
|
|
26
|
+
switch (entity.decoratorType) {
|
|
27
|
+
case DecoratorTypes.METHOD:
|
|
28
|
+
return Route(...args);
|
|
29
|
+
case DecoratorTypes.CLASS:
|
|
30
|
+
entity.store.merge("middlewares", {
|
|
31
|
+
use: args
|
|
32
|
+
});
|
|
33
|
+
break;
|
|
34
|
+
default:
|
|
35
|
+
throw new UnsupportedDecoratorType(Use, parameters);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=use.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use.js","sourceRoot":"","sources":["../../../src/decorators/use.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAE,wBAAwB,EAAC,MAAM,YAAY,CAAC;AACpE,OAAO,EAAC,YAAY,EAAmC,KAAK,EAAC,MAAM,cAAc,CAAC;AAGlF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,GAAG,CAAC,GAAG,IAAW;IAChC,OAAO,YAAY,CAAC,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE;QACzC,QAAQ,MAAM,CAAC,aAAa,EAAE;YAC5B,KAAK,cAAc,CAAC,MAAM;gBACxB,OAAO,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC;YAExB,KAAK,cAAc,CAAC,KAAK;gBACvB,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,aAAa,EAAE;oBAChC,GAAG,EAAE,IAAI;iBACV,CAAC,CAAC;gBACH,MAAM;YAER;gBACE,MAAM,IAAI,wBAAwB,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC;SACvD;IACH,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["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"]}
|