@decaf-ts/for-nest 0.1.5 → 0.1.7

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 (61) hide show
  1. package/dist/for-nest.cjs +1 -1
  2. package/dist/for-nest.cjs.map +1 -1
  3. package/dist/for-nest.js +1 -1
  4. package/dist/for-nest.js.map +1 -1
  5. package/lib/constants.cjs +4 -1
  6. package/lib/constants.d.ts +3 -0
  7. package/lib/constants.js.map +1 -1
  8. package/lib/decaf-model/DecafModelModule.cjs +8 -0
  9. package/lib/decaf-model/DecafModelModule.d.ts +1 -0
  10. package/lib/decaf-model/DecafModelModule.js.map +1 -1
  11. package/lib/decaf-model/FromModelController.cjs +42 -30
  12. package/lib/decaf-model/FromModelController.d.ts +2 -10
  13. package/lib/decaf-model/FromModelController.js.map +1 -1
  14. package/lib/decaf-model/decorators/ApiParamsFromModel.d.ts +2 -2
  15. package/lib/decaf-model/decorators/ApiParamsFromModel.js.map +1 -1
  16. package/lib/decaf-model/decorators/DecafParams.d.ts +2 -2
  17. package/lib/decaf-model/decorators/DecafParams.js.map +1 -1
  18. package/lib/decaf-model/decorators/decorators.cjs +60 -0
  19. package/lib/decaf-model/decorators/decorators.d.ts +46 -0
  20. package/lib/decaf-model/decorators/decorators.js.map +1 -0
  21. package/lib/decaf-model/decorators/types.d.ts +7 -1
  22. package/lib/esm/constants.d.ts +3 -0
  23. package/lib/esm/constants.js +3 -0
  24. package/lib/esm/constants.js.map +1 -1
  25. package/lib/esm/decaf-model/DecafModelModule.d.ts +1 -0
  26. package/lib/esm/decaf-model/DecafModelModule.js +9 -1
  27. package/lib/esm/decaf-model/DecafModelModule.js.map +1 -1
  28. package/lib/esm/decaf-model/FromModelController.d.ts +2 -10
  29. package/lib/esm/decaf-model/FromModelController.js +43 -31
  30. package/lib/esm/decaf-model/FromModelController.js.map +1 -1
  31. package/lib/esm/decaf-model/decorators/ApiParamsFromModel.d.ts +2 -2
  32. package/lib/esm/decaf-model/decorators/ApiParamsFromModel.js.map +1 -1
  33. package/lib/esm/decaf-model/decorators/DecafParams.d.ts +2 -2
  34. package/lib/esm/decaf-model/decorators/DecafParams.js.map +1 -1
  35. package/lib/esm/decaf-model/decorators/decorators.d.ts +46 -0
  36. package/lib/esm/decaf-model/decorators/decorators.js +55 -0
  37. package/lib/esm/decaf-model/decorators/decorators.js.map +1 -0
  38. package/lib/esm/decaf-model/decorators/types.d.ts +7 -1
  39. package/lib/esm/index.d.ts +1 -1
  40. package/lib/esm/index.js +1 -1
  41. package/lib/esm/interceptors/AuthInterceptor.d.ts +10 -0
  42. package/lib/esm/interceptors/AuthInterceptor.js +37 -0
  43. package/lib/esm/interceptors/AuthInterceptor.js.map +1 -0
  44. package/lib/esm/interceptors/DecafRequestHandlerInterceptor.js +2 -1
  45. package/lib/esm/interceptors/DecafRequestHandlerInterceptor.js.map +1 -1
  46. package/lib/esm/request/DecafHandlerExecutor.d.ts +1 -1
  47. package/lib/esm/request/DecafHandlerExecutor.js +2 -2
  48. package/lib/esm/request/DecafHandlerExecutor.js.map +1 -1
  49. package/lib/esm/types.d.ts +46 -2
  50. package/lib/index.cjs +1 -1
  51. package/lib/index.d.ts +1 -1
  52. package/lib/interceptors/AuthInterceptor.cjs +40 -0
  53. package/lib/interceptors/AuthInterceptor.d.ts +10 -0
  54. package/lib/interceptors/AuthInterceptor.js.map +1 -0
  55. package/lib/interceptors/DecafRequestHandlerInterceptor.cjs +2 -1
  56. package/lib/interceptors/DecafRequestHandlerInterceptor.js.map +1 -1
  57. package/lib/request/DecafHandlerExecutor.cjs +2 -2
  58. package/lib/request/DecafHandlerExecutor.d.ts +1 -1
  59. package/lib/request/DecafHandlerExecutor.js.map +1 -1
  60. package/lib/types.d.ts +46 -2
  61. package/package.json +5 -2
@@ -0,0 +1,10 @@
1
+ import { NestInterceptor, ExecutionContext, CallHandler } from "@nestjs/common";
2
+ import { Reflector } from "@nestjs/core";
3
+ import { Observable } from "rxjs";
4
+ import type { AuthHandler } from "../types";
5
+ export declare class AuthInterceptor implements NestInterceptor {
6
+ private readonly reflector;
7
+ private readonly authHandler?;
8
+ constructor(reflector: Reflector, authHandler?: AuthHandler | undefined);
9
+ intercept(ctx: ExecutionContext, next: CallHandler): Promise<Observable<any>>;
10
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthInterceptor.js","sourceRoot":"","sources":["../../src/interceptors/AuthInterceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAOwB;AACxB,uCAAyC;AAIzC,kDAA2D;AAIpD,IAAM,eAAe,GAArB,MAAM,eAAe;IAC1B,YACmB,SAAoB,EACc,WAAyB;QAD3D,cAAS,GAAT,SAAS,CAAW;QACc,gBAAW,GAAX,WAAW,CAAc;IAC3E,CAAC;IAEJ,KAAK,CAAC,SAAS,CACb,GAAqB,EACrB,IAAiB;QAEjB,MAAM,SAAS,GACb,IAAI,CAAC,SAAS,CAAC,GAAG,CAChB,yBAAa,EACb,GAAG,CAAC,UAAU,EAAE,CACjB;YACD,IAAI,CAAC,SAAS,CAAC,GAAG,CAAuB,yBAAa,EAAE,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;QAE1E,IAAI,SAAS,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YAClC,MAAM,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QACnD,CAAC;QAED,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;CACF,CAAA;AAvBY,0CAAe;0BAAf,eAAe;IAD3B,IAAA,mBAAU,GAAE;IAIR,WAAA,IAAA,iBAAQ,GAAE,CAAA;IAAE,WAAA,IAAA,eAAM,EAAC,wBAAY,CAAC,CAAA;qCADL,gBAAS;GAF5B,eAAe,CAuB3B"}
@@ -64,7 +64,8 @@ let DecafRequestHandlerInterceptor = class DecafRequestHandlerInterceptor {
64
64
  }
65
65
  async intercept(ctx, next) {
66
66
  const req = ctx.switchToHttp().getRequest();
67
- await this.executor.exec(req);
67
+ const res = ctx.switchToHttp().getResponse();
68
+ await this.executor.exec(req, res);
68
69
  return next.handle();
69
70
  }
70
71
  };
@@ -1 +1 @@
1
- {"version":3,"file":"DecafRequestHandlerInterceptor.js","sourceRoot":"","sources":["../../src/interceptors/DecafRequestHandlerInterceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAMwB;AACxB,oDAAkD;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AAEI,IAAM,8BAA8B,GAApC,MAAM,8BAA8B;IACzC,YAA6B,QAA8B;QAA9B,aAAQ,GAAR,QAAQ,CAAsB;IAAG,CAAC;IAE/D,KAAK,CAAC,SAAS,CAAC,GAAqB,EAAE,IAAiB;QACtD,MAAM,GAAG,GAAG,GAAG,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;QAC5C,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC9B,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;CACF,CAAA;AARY,wEAA8B;yCAA9B,8BAA8B;IAD1C,IAAA,mBAAU,EAAC,EAAE,KAAK,EAAE,cAAK,CAAC,OAAO,EAAE,CAAC;qCAEI,8BAAoB;GADhD,8BAA8B,CAQ1C"}
1
+ {"version":3,"file":"DecafRequestHandlerInterceptor.js","sourceRoot":"","sources":["../../src/interceptors/DecafRequestHandlerInterceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAMwB;AACxB,oDAAkD;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AAEI,IAAM,8BAA8B,GAApC,MAAM,8BAA8B;IACzC,YAA6B,QAA8B;QAA9B,aAAQ,GAAR,QAAQ,CAAsB;IAAG,CAAC;IAE/D,KAAK,CAAC,SAAS,CAAC,GAAqB,EAAE,IAAiB;QACtD,MAAM,GAAG,GAAG,GAAG,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;QAC5C,MAAM,GAAG,GAAG,GAAG,CAAC,YAAY,EAAE,CAAC,WAAW,EAAE,CAAC;QAC7C,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACnC,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;CACF,CAAA;AATY,wEAA8B;yCAA9B,8BAA8B;IAD1C,IAAA,mBAAU,EAAC,EAAE,KAAK,EAAE,cAAK,CAAC,OAAO,EAAE,CAAC;qCAEI,8BAAoB;GADhD,8BAA8B,CAS1C"}
@@ -65,9 +65,9 @@ let DecafHandlerExecutor = class DecafHandlerExecutor {
65
65
  this.handlers = handlers;
66
66
  this.context = context;
67
67
  }
68
- async exec(req) {
68
+ async exec(req, res) {
69
69
  for (const handler of this.handlers) {
70
- await handler.handle(this.context, req);
70
+ await handler.handle(this.context, req, res);
71
71
  }
72
72
  }
73
73
  };
@@ -48,5 +48,5 @@ export declare class DecafHandlerExecutor {
48
48
  private readonly handlers;
49
49
  private readonly context;
50
50
  constructor(handlers: DecafRequestHandler[], context: DecafRequestContext);
51
- exec(req: Request): Promise<void>;
51
+ exec(req: Request, res: Response): Promise<void>;
52
52
  }
@@ -1 +1 @@
1
- {"version":3,"file":"DecafHandlerExecutor.js","sourceRoot":"","sources":["../../src/request/DecafHandlerExecutor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA2D;AAC3D,mEAA4D;AAE5D,kDAA8C;AAE9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AAEI,IAAM,oBAAoB,GAA1B,MAAM,oBAAoB;IAC/B,YAC2C,QAA+B,EACvD,OAA4B;QADJ,aAAQ,GAAR,QAAQ,CAAuB;QACvD,YAAO,GAAP,OAAO,CAAqB;IAC5C,CAAC;IAEJ,KAAK,CAAC,IAAI,CAAC,GAAY;QACrB,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YACpC,MAAM,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QAC1C,CAAC;IACH,CAAC;CACF,CAAA;AAXY,oDAAoB;+BAApB,oBAAoB;IADhC,IAAA,mBAAU,EAAC,EAAE,KAAK,EAAE,cAAK,CAAC,OAAO,EAAE,CAAC;IAGhC,WAAA,IAAA,eAAM,EAAC,0BAAc,CAAC,CAAA;4CACG,yCAAmB;GAHpC,oBAAoB,CAWhC"}
1
+ {"version":3,"file":"DecafHandlerExecutor.js","sourceRoot":"","sources":["../../src/request/DecafHandlerExecutor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA2D;AAC3D,mEAA4D;AAE5D,kDAA8C;AAE9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AAEI,IAAM,oBAAoB,GAA1B,MAAM,oBAAoB;IAC/B,YAC2C,QAA+B,EACvD,OAA4B;QADJ,aAAQ,GAAR,QAAQ,CAAuB;QACvD,YAAO,GAAP,OAAO,CAAqB;IAC5C,CAAC;IAEJ,KAAK,CAAC,IAAI,CAAC,GAAY,EAAE,GAAa;QACpC,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YACpC,MAAM,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;CACF,CAAA;AAXY,oDAAoB;+BAApB,oBAAoB;IADhC,IAAA,mBAAU,EAAC,EAAE,KAAK,EAAE,cAAK,CAAC,OAAO,EAAE,CAAC;IAGhC,WAAA,IAAA,eAAM,EAAC,0BAAc,CAAC,CAAA;4CACG,yCAAmB;GAHpC,oBAAoB,CAWhC"}
package/lib/types.d.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  import { Adapter } from "@decaf-ts/core";
2
2
  import { Constructor } from "@decaf-ts/decoration";
3
- import { Type } from "@nestjs/common";
3
+ import { ExecutionContext, Type } from "@nestjs/common";
4
4
  export interface RequestContextAccessor {
5
5
  set(key: string | symbol, value: any): void;
6
6
  get<T = any>(key: string | symbol): T | undefined;
7
7
  }
8
8
  export interface DecafRequestHandler {
9
- handle(context: RequestContextAccessor, req: Request): Promise<void>;
9
+ handle(context: RequestContextAccessor, req: Request, res: Response): Promise<void>;
10
10
  }
11
11
  /**
12
12
  * @publicApi
@@ -18,3 +18,47 @@ export type DecafModuleOptions<CONF = any, A extends Adapter<CONF, any, any, any
18
18
  autoControllers: boolean;
19
19
  handlers?: Type<DecafRequestHandler>[];
20
20
  };
21
+ /**
22
+ * Abstraction used by the {@link AuthInterceptor} to authorize decaf models.
23
+ *
24
+ * // costumauthHandler.ts
25
+ *
26
+ * @example
27
+ * export class CustomAuthHandler implements AuthHandler {
28
+ * async authorize(ctx: ExecutionContext, resource: string) {
29
+ * const req = ctx.switchToHttp().getRequest();
30
+ * const userRole = req.headers.authorization?.split(" ")[1] as string;
31
+ * if (!userRole) throw new AuthorizationError("Unauthenticated");
32
+ * const roles = Metadata.get(Model.get(resource)!, AuthRole);
33
+ * if (!roles.includes(userRole)) {
34
+ * throw new AuthorizationError("Unauthorized");
35
+ * }
36
+ * }
37
+ * }
38
+ *
39
+ * // auth.module.ts
40
+ *
41
+ * @Global()
42
+ * @Module({
43
+ * providers: [
44
+ * AuthInterceptor,
45
+ * CustomAuthHandler,
46
+ * {
47
+ * provide: AUTH_HANDLER,
48
+ * useClass: CustomAuthHandler, //swap this to use another provider
49
+ * },
50
+ * ],
51
+ * exports: [AUTH_HANDLER, AuthInterceptor],
52
+ * })
53
+ * export class AuthModule {}
54
+ */
55
+ export interface AuthHandler {
56
+ /**
57
+ * Inspect the request context and ensure the caller can access the model.
58
+ * Implementations should throw an {@link AuthorizationError} on denial.
59
+ *
60
+ * @param ctx - Nest execution context that exposes the request/response.
61
+ * @param model - Model name or constructor being accessed.
62
+ */
63
+ authorize(ctx: ExecutionContext, model: string | Constructor): Promise<void> | void;
64
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decaf-ts/for-nest",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "NestJS decaf integration",
5
5
  "type": "module",
6
6
  "exports": {
@@ -75,13 +75,16 @@
75
75
  },
76
76
  "homepage": "https://github.com/decaf-ts/for-nest#readme",
77
77
  "devDependencies": {
78
+ "@decaf-ts/for-fabric": "^0.1.16",
78
79
  "@decaf-ts/utils": "latest",
79
80
  "@eslint/js": "^9.25.1",
80
81
  "@nestjs/cli": "^11.0.0",
82
+ "@nestjs/platform-express": "^11.1.9",
81
83
  "@nestjs/schematics": "^11.0.9",
82
84
  "@nestjs/swagger": "^11.2.0",
83
85
  "@nestjs/testing": "^11.0.1",
84
- "@types/lodash": "^4.17.20"
86
+ "@types/lodash": "^4.17.20",
87
+ "supertest": "^7.1.4"
85
88
  },
86
89
  "dependencies": {
87
90
  "@decaf-ts/core": "latest",