@zdavison/nestjs-rpc-toolkit 0.0.11 → 0.0.13

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 (67) hide show
  1. package/dist/bin/bootstrap.d.ts +19 -0
  2. package/dist/bin/bootstrap.d.ts.map +1 -0
  3. package/dist/bin/bootstrap.js +275 -0
  4. package/dist/bin/bootstrap.js.map +1 -0
  5. package/dist/bin/init.js +15 -2
  6. package/dist/bin/init.js.map +1 -1
  7. package/dist/decorators/rpc-controller.decorator.d.ts +32 -0
  8. package/dist/decorators/rpc-controller.decorator.d.ts.map +1 -1
  9. package/dist/decorators/rpc-controller.decorator.js +56 -0
  10. package/dist/decorators/rpc-controller.decorator.js.map +1 -1
  11. package/dist/decorators/rpc-method.decorator.d.ts +22 -2
  12. package/dist/decorators/rpc-method.decorator.d.ts.map +1 -1
  13. package/dist/decorators/rpc-method.decorator.js +70 -20
  14. package/dist/decorators/rpc-method.decorator.js.map +1 -1
  15. package/dist/generators/rpc-types-generator.d.ts +3 -1
  16. package/dist/generators/rpc-types-generator.d.ts.map +1 -1
  17. package/dist/generators/rpc-types-generator.js +259 -25
  18. package/dist/generators/rpc-types-generator.js.map +1 -1
  19. package/dist/index.d.ts +1 -1
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +1 -1
  22. package/dist/index.js.map +1 -1
  23. package/dist/rpc/rpc-client.d.ts +3 -0
  24. package/dist/rpc/rpc-client.d.ts.map +1 -1
  25. package/dist/rpc/rpc-client.js +7 -2
  26. package/dist/rpc/rpc-client.js.map +1 -1
  27. package/dist/rpc/rpc-registry.d.ts.map +1 -1
  28. package/dist/rpc/rpc-registry.js +11 -1
  29. package/dist/rpc/rpc-registry.js.map +1 -1
  30. package/dist/rpc/typed-message-bus.d.ts +11 -0
  31. package/dist/rpc/typed-message-bus.d.ts.map +1 -1
  32. package/dist/rpc/typed-message-bus.js +2 -1
  33. package/dist/rpc/typed-message-bus.js.map +1 -1
  34. package/dist/transport/{in-memory.client.d.ts → in-process.client.d.ts} +4 -2
  35. package/dist/transport/in-process.client.d.ts.map +1 -0
  36. package/dist/transport/{in-memory.client.js → in-process.client.js} +13 -7
  37. package/dist/transport/in-process.client.js.map +1 -0
  38. package/dist/transport/{in-memory.transport.d.ts → in-process.transport.d.ts} +3 -3
  39. package/dist/transport/in-process.transport.d.ts.map +1 -0
  40. package/dist/transport/{in-memory.transport.js → in-process.transport.js} +18 -8
  41. package/dist/transport/in-process.transport.js.map +1 -0
  42. package/dist/transport/index.d.ts +2 -2
  43. package/dist/transport/index.d.ts.map +1 -1
  44. package/dist/transport/index.js +2 -2
  45. package/dist/transport/index.js.map +1 -1
  46. package/dist/types/serializable.d.ts +55 -4
  47. package/dist/types/serializable.d.ts.map +1 -1
  48. package/dist/types/serializable.js +4 -0
  49. package/dist/types/serializable.js.map +1 -1
  50. package/package.json +8 -6
  51. package/dist/module-base/index.d.ts +0 -15
  52. package/dist/module-base/index.d.ts.map +0 -1
  53. package/dist/module-base/index.js +0 -38
  54. package/dist/module-base/index.js.map +0 -1
  55. package/dist/test-date-serialization.d.ts +0 -2
  56. package/dist/test-date-serialization.d.ts.map +0 -1
  57. package/dist/test-date-serialization.js +0 -12
  58. package/dist/test-date-serialization.js.map +0 -1
  59. package/dist/test-type-validation.d.ts +0 -2
  60. package/dist/test-type-validation.d.ts.map +0 -1
  61. package/dist/test-type-validation.js +0 -87
  62. package/dist/test-type-validation.js.map +0 -1
  63. package/dist/transport/in-memory.client.d.ts.map +0 -1
  64. package/dist/transport/in-memory.client.js.map +0 -1
  65. package/dist/transport/in-memory.transport.d.ts.map +0 -1
  66. package/dist/transport/in-memory.transport.js.map +0 -1
  67. package/dist/tsconfig.tsbuildinfo +0 -1
@@ -1,33 +1,83 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RpcMethod = RpcMethod;
4
- const microservices_1 = require("@nestjs/microservices");
5
- const rpc_registry_1 = require("../rpc/rpc-registry");
6
4
  require("reflect-metadata");
5
+ /**
6
+ * RPC Method decorator with compile-time serialization validation.
7
+ * Ensures all parameters and return types are JSON-serializable for TCP transport.
8
+ * Pattern is automatically generated from the class name or @Controller decorator.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * @RpcMethod()
13
+ * async findUser(id: string): Promise<User> {
14
+ * // ✅ Valid: string param, User return type are serializable
15
+ * // Pattern: auto-generated as 'user.findUser' or 'controllerName.findUser'
16
+ * return this.userRepo.findById(id);
17
+ * }
18
+ *
19
+ * @RpcMethod()
20
+ * async invalidMethod(callback: (data: string) => void): Promise<HTMLElement> {
21
+ * // ❌ Error: callback and HTMLElement are not serializable
22
+ * }
23
+ * ```
24
+ */
7
25
  function RpcMethod() {
8
26
  return function (target, propertyKey, descriptor) {
9
27
  const originalMethod = descriptor.value;
10
- const module = Reflect.getMetadata('rpc:module', target.constructor);
11
- if (!module) {
12
- throw new Error(`@RpcMethod can only be used in classes decorated with @RpcController. Class: ${target.constructor.name}`);
13
- }
14
- const methodName = propertyKey;
15
- const actualPattern = `${module}.${methodName}`;
16
- const metadata = {
17
- pattern: actualPattern,
18
- module,
19
- methodName,
20
- target: target.constructor,
21
- propertyKey,
28
+ // Defer the module lookup and pattern registration until runtime
29
+ // This is necessary because method decorators execute before class decorators
30
+ const getModuleAndPattern = () => {
31
+ const module = Reflect.getMetadata('rpc:module', target.constructor);
32
+ if (!module) {
33
+ throw new Error(`@RpcMethod can only be used in classes decorated with @RpcController. Class: ${target.constructor.name}`);
34
+ }
35
+ return {
36
+ module,
37
+ pattern: `${module}.${propertyKey}`
38
+ };
22
39
  };
23
- rpc_registry_1.rpcRegistry.registerMethod(metadata);
24
- descriptor.value = function (args) {
25
- if (Array.isArray(args)) {
26
- return originalMethod.apply(this, args);
40
+ // Create wrapper method that handles RPC parameter unwrapping
41
+ descriptor.value = function (...args) {
42
+ // Defer the module check until the method is actually called
43
+ // This allows the class decorator to run first
44
+ getModuleAndPattern();
45
+ // NestJS microservices sends parameters as a single object: { paramName: value }
46
+ // We need to unwrap this and pass the actual values to the method
47
+ let unwrappedArgs = args;
48
+ if (args.length === 1 && typeof args[0] === 'object' && args[0] !== null) {
49
+ // Extract parameter names from the original method
50
+ const methodStr = originalMethod.toString();
51
+ const paramMatch = methodStr.match(/^(?:async\s+)?(?:function\s*)?\w*\s*\(([^)]*)\)/);
52
+ if (paramMatch) {
53
+ const paramNames = paramMatch[1]
54
+ .split(',')
55
+ .map(p => p.trim().split(/[:\s=]/)[0])
56
+ .filter(p => p.length > 0);
57
+ // If the incoming object has exactly the parameter names as keys, unwrap them
58
+ const incomingKeys = Object.keys(args[0]);
59
+ if (paramNames.length > 0 && incomingKeys.length === paramNames.length) {
60
+ const allMatch = paramNames.every(name => incomingKeys.includes(name));
61
+ if (allMatch) {
62
+ unwrappedArgs = paramNames.map(name => args[0][name]);
63
+ }
64
+ }
65
+ }
27
66
  }
28
- return originalMethod.call(this, args);
67
+ // Call the original method with the unwrapped arguments
68
+ return originalMethod.apply(this, unwrappedArgs);
29
69
  };
30
- (0, microservices_1.MessagePattern)(actualPattern)(target, propertyKey, descriptor);
70
+ // Store the method for lazy pattern registration
71
+ // We'll register it when @RpcController is applied
72
+ if (!Reflect.hasMetadata('rpc:pending-methods', target.constructor)) {
73
+ Reflect.defineMetadata('rpc:pending-methods', [], target.constructor);
74
+ }
75
+ const pendingMethods = Reflect.getMetadata('rpc:pending-methods', target.constructor);
76
+ pendingMethods.push({
77
+ propertyKey,
78
+ originalMethod,
79
+ descriptor,
80
+ });
31
81
  };
32
82
  }
33
83
  //# sourceMappingURL=rpc-method.decorator.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"rpc-method.decorator.js","sourceRoot":"","sources":["../../src/decorators/rpc-method.decorator.ts"],"names":[],"mappings":";;AAyBA,8BA6CC;AAtED,yDAA6E;AAC7E,sDAAqE;AAErE,4BAA0B;AAsB1B,SAAgB,SAAS;IAKvB,OAAO,UACL,MAAW,EACX,WA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M,YAAY,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;oBAC1C,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,IAAI,YAAY,CAAC,MAAM,KAAK,UAAU,CAAC,MAAM,EAAE,CAAC;wBACvE,MAAM,QAAQ,GAAG,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;wBACvE,IAAI,QAAQ,EAAE,CAAC;4BACb,aAAa,GAAG,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;wBACxD,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC;YAED,wDAAwD;YACxD,OAAO,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,aAAoB,CAAC,CAAC;QAC1D,CAAQ,CAAC;QAET,iDAAiD;QACjD,mDAAmD;QACnD,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,qBAAqB,EAAE,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC;YACpE,OAAO,CAAC,cAAc,CAAC,qBAAqB,EAAE,EAAE,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;QACxE,CAAC;QACD,MAAM,cAAc,GAAG,OAAO,CAAC,WAAW,CAAC,qBAAqB,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;QACtF,cAAc,CAAC,IAAI,CAAC;YAClB,WAAW;YACX,cAAc;YACd,UAAU;SACX,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
@@ -1,4 +1,5 @@
1
1
  export interface RpcGenerationConfig {
2
+ /** Package paths to scan for RPC methods. Supports glob patterns like 'packages/modules/*' */
2
3
  packages: string[];
3
4
  outputDir: string;
4
5
  }
@@ -25,11 +26,12 @@ export declare class RpcTypesGenerator {
25
26
  private extractTypesFromFile;
26
27
  private extractInterface;
27
28
  private extractClassAsInterface;
29
+ private extractTypeAlias;
28
30
  private isRelevantInterface;
29
31
  private getModuleForFile;
30
32
  private isInternalType;
31
33
  private processMethod;
32
- private extractPattern;
34
+ private extractJsDoc;
33
35
  private generateTypesFile;
34
36
  private generateModuleTypesFile;
35
37
  private generateMainTypesFile;
@@ -1 +1 @@
1
- {"version":3,"file":"rpc-types-generator.d.ts","sourceRoot":"","sources":["../../src/generators/rpc-types-generator.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,mBAAmB;IAElC,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;CACpB;AAiBD,qBAAa,iBAAiB;IAShB,OAAO,CAAC,OAAO;IAR3B,OAAO,CAAC,QAAQ,CAAmC;IACnD,OAAO,CAAC,UAAU,CAAuB;IACzC,OAAO,CAAC,UAAU,CAA+C;IACjE,OAAO,CAAC,MAAM,CAAsB;IACpC,OAAO,CAAC,YAAY,CAAoC;IACxD,OAAO,CAAC,gBAAgB,CAAgB;IACxC,OAAO,CAAC,eAAe,CAAkC;gBAErC,OAAO,EAAE,gBAAgB;IAa7C,OAAO,CAAC,kBAAkB;IA+B1B,OAAO,CAAC,wBAAwB;IA2BhC,OAAO,CAAC,sBAAsB;IA6B9B,OAAO,CAAC,UAAU;IAOlB,QAAQ,IAAI,IAAI;IAqChB,OAAO,CAAC,iBAAiB;IAsBzB,OAAO,CAAC,oBAAoB;IAU5B,OAAO,CAAC,gBAAgB;IAcxB,OAAO,CAAC,uBAAuB;IAmC/B,OAAO,CAAC,mBAAmB;IAI3B,OAAO,CAAC,gBAAgB;IAqBxB,OAAO,CAAC,cAAc;IAQtB,OAAO,CAAC,aAAa;IA8ErB,OAAO,CAAC,cAAc;IAOtB,OAAO,CAAC,iBAAiB;IA4BzB,OAAO,CAAC,uBAAuB;IA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