@funduck/connectrpc-fastify 1.0.17 → 1.0.19

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/README.md CHANGED
@@ -1,4 +1,8 @@
1
1
  # Connectrpc Fastify Wrapper
2
+ <img src="./coverage/badge.svg" alt="Coverage Badge" />
3
+
4
+ [github](https://github.com/funduck/connectrpc-fastify)
5
+
2
6
  !BETA
3
7
 
4
8
  Code is not production ready.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funduck/connectrpc-fastify",
3
- "version": "1.0.17",
3
+ "version": "1.0.19",
4
4
  "author": "Oleg Milekhin <qlfunduck@gmail.com>",
5
5
  "description": "Wrapper for official @connectrpc/connect and fastify. Simplifies configuration, type safe binding to controller, simplifies use of middlewares and guards.",
6
6
  "main": "dist/index.js",
@@ -10,9 +10,9 @@
10
10
  "compile-proto": "cd test/demo && npx buf dep update; npx buf lint; npx buf generate",
11
11
  "test:demo": "npm run compile-proto && cd test/demo && DEBUG=true npx tsx e2e-demo.ts",
12
12
  "test": "npm run compile-proto && jest --verbose",
13
- "test:coverage": "npm run compile-proto && jest --coverage --runInBand",
14
- "publish:check": "npm test && npm run build && npm publish --tag latest --access public --dry-run",
15
- "publish:latest": "npm test && npm run build && npm publish --tag latest --access public"
13
+ "test:coverage": "npm run compile-proto && jest --coverage --coverageReporters=json-summary --runInBand ; npx make-coverage-badge",
14
+ "publish:check": "npm run test && npm run test:demo && npm run build && npm publish --tag latest --access public --dry-run",
15
+ "publish:latest": "npm run test:coverage && npm run test:demo && npm run build && npm publish --tag latest --access public"
16
16
  },
17
17
  "license": "MIT",
18
18
  "peerDependencies": {
@@ -1,34 +0,0 @@
1
- import { GenService, GenServiceMethods } from '@bufbuild/protobuf/codegenv2';
2
- import { FastifyInstance } from 'fastify';
3
- import { Interceptor, InterceptorConfigUnion, Logger, Middleware, MiddlewareConfigUnion, Service } from './interfaces';
4
- declare class ConnectRPCClass {
5
- setLogger(customLogger: Logger | boolean): void;
6
- /** Should be called in middleware constructor */
7
- registerMiddleware(self: Middleware, options?: {
8
- allowMultipleInstances?: boolean;
9
- }): void;
10
- /** Should be called in interceptor constructor */
11
- registerInterceptor(self: Interceptor, options?: {
12
- allowMultipleInstances?: boolean;
13
- }): void;
14
- /** Should be called in controller constructor */
15
- registerController<T extends GenServiceMethods>(self: Service<GenService<T>>, service: GenService<T>, options?: {
16
- allowMultipleInstances?: boolean;
17
- }): void;
18
- /** Initialize ConnectRPC with interceptors, Fastify plugin, and middlewares */
19
- init(server: FastifyInstance, options?: {
20
- logger?: Logger | false;
21
- interceptors?: InterceptorConfigUnion[];
22
- middlewares?: MiddlewareConfigUnion[];
23
- }): Promise<void>;
24
- /** Clear all registered controllers, routes, middlewares, and interceptors. Useful for testing */
25
- clear(): void;
26
- initInterceptors(configs: InterceptorConfigUnion[]): void;
27
- registerFastifyPlugin(server: FastifyInstance): Promise<void>;
28
- initMiddlewares(server: FastifyInstance, middlewareConfigs: MiddlewareConfigUnion[]): Promise<void>;
29
- }
30
- /**
31
- * Main ConnectRPC class to manage registration of controllers and middlewares
32
- */
33
- export declare const ConnectRPC: ConnectRPCClass;
34
- export {};
@@ -1,59 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ConnectRPC = void 0;
4
- const fastify_plugin_1 = require("./fastify-plugin");
5
- const helpers_1 = require("./helpers");
6
- const interceptors_1 = require("./interceptors");
7
- const middlewares_1 = require("./middlewares");
8
- const stores_1 = require("./stores");
9
- class ConnectRPCClass {
10
- setLogger(customLogger) {
11
- (0, helpers_1.setLogger)(customLogger);
12
- }
13
- /** Should be called in middleware constructor */
14
- registerMiddleware(self, options) {
15
- stores_1.MiddlewareStore.registerInstance(self, options);
16
- }
17
- /** Should be called in interceptor constructor */
18
- registerInterceptor(self, options) {
19
- stores_1.InterceptorStore.registerInstance(self, options);
20
- }
21
- /** Should be called in controller constructor */
22
- registerController(self, service, options) {
23
- stores_1.ControllersStore.registerInstance(self, service, options);
24
- }
25
- /** Initialize ConnectRPC with interceptors, Fastify plugin, and middlewares */
26
- async init(server, options) {
27
- if (options?.logger != null) {
28
- this.setLogger(options.logger);
29
- }
30
- if (options?.interceptors) {
31
- this.initInterceptors(options.interceptors);
32
- }
33
- await this.registerFastifyPlugin(server);
34
- if (options?.middlewares) {
35
- this.initMiddlewares(server, options.middlewares);
36
- }
37
- }
38
- /** Clear all registered controllers, routes, middlewares, and interceptors. Useful for testing */
39
- clear() {
40
- stores_1.ControllersStore.clear();
41
- stores_1.RouteMetadataStore.clear();
42
- stores_1.MiddlewareStore.clear();
43
- stores_1.InterceptorStore.clear();
44
- }
45
- initInterceptors(configs) {
46
- (0, interceptors_1.initInterceptors)(configs);
47
- }
48
- registerFastifyPlugin(server) {
49
- return (0, fastify_plugin_1.registerFastifyPlugin)(server);
50
- }
51
- initMiddlewares(server, middlewareConfigs) {
52
- return (0, middlewares_1.initMiddlewares)(server, middlewareConfigs);
53
- }
54
- }
55
- /**
56
- * Main ConnectRPC class to manage registration of controllers and middlewares
57
- */
58
- exports.ConnectRPC = new ConnectRPCClass();
59
- //# sourceMappingURL=connectrpc.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"connectrpc.js","sourceRoot":"","sources":["../src/connectrpc.ts"],"names":[],"mappings":";;;AAEA,qDAAyD;AACzD,uCAAsC;AACtC,iDAAkD;AASlD,+CAAgD;AAChD,qCAKkB;AAElB,MAAM,eAAe;IACnB,SAAS,CAAC,YAA8B;QACtC,IAAA,mBAAS,EAAC,YAAY,CAAC,CAAC;IAC1B,CAAC;IAED,iDAAiD;IACjD,kBAAkB,CAChB,IAAgB,EAChB,OAEC;QAED,wBAAe,CAAC,gBAAgB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAClD,CAAC;IAED,kDAAkD;IAClD,mBAAmB,CACjB,IAAiB,EACjB,OAEC;QAED,yBAAgB,CAAC,gBAAgB,CAAC,IAAI,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AC"}
@@ -1,32 +0,0 @@
1
- import { ContextKey, ContextValues } from '@connectrpc/connect';
2
- import { Type } from './interfaces';
3
- /**
4
- * Custom ContextValues implementation that extends the standard ConnectRPC ContextValues
5
- * with an entries() method to iterate over key-value pairs.
6
- */
7
- export declare class CustomContextValues implements ContextValues {
8
- private values;
9
- /**
10
- * Get a context value by key
11
- */
12
- get<T>(key: ContextKey<T>): T;
13
- /**
14
- * Set a context value by key
15
- */
16
- set<T>(key: ContextKey<T>, value: T): this;
17
- /**
18
- * Delete a context value by key
19
- */
20
- delete(key: ContextKey<unknown>): this;
21
- /**
22
- * Iterate over all key-value pairs in the context
23
- * Returns an iterator of [ContextKey, value] pairs
24
- */
25
- entries(): IterableIterator<[symbol, unknown]>;
26
- }
27
- /**
28
- * Create a new CustomContextValues instance
29
- */
30
- export declare function createCustomContextValues(): CustomContextValues;
31
- export declare const controllerClassContextKey: ContextKey<Type<any> | null>;
32
- export declare const controllerMethodContextKey: ContextKey<Function | null>;
@@ -1,56 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.controllerMethodContextKey = exports.controllerClassContextKey = exports.CustomContextValues = void 0;
4
- exports.createCustomContextValues = createCustomContextValues;
5
- const connect_1 = require("@connectrpc/connect");
6
- /**
7
- * Custom ContextValues implementation that extends the standard ConnectRPC ContextValues
8
- * with an entries() method to iterate over key-value pairs.
9
- */
10
- class CustomContextValues {
11
- constructor() {
12
- this.values = new Map();
13
- }
14
- /**
15
- * Get a context value by key
16
- */
17
- get(key) {
18
- if (this.values.has(key.id)) {
19
- return this.values.get(key.id);
20
- }
21
- return key.defaultValue;
22
- }
23
- /**
24
- * Set a context value by key
25
- */
26
- set(key, value) {
27
- this.values.set(key.id, value);
28
- return this;
29
- }
30
- /**
31
- * Delete a context value by key
32
- */
33
- delete(key) {
34
- this.values.delete(key.id);
35
- return this;
36
- }
37
- /**
38
- * Iterate over all key-value pairs in the context
39
- * Returns an iterator of [ContextKey, value] pairs
40
- */
41
- *entries() {
42
- for (const [id, value] of this.values.entries()) {
43
- yield [id, value];
44
- }
45
- }
46
- }
47
- exports.CustomContextValues = CustomContextValues;
48
- /**
49
- * Create a new CustomContextValues instance
50
- */
51
- function createCustomContextValues() {
52
- return new CustomContextValues();
53
- }
54
- exports.controllerClassContextKey = (0, connect_1.createContextKey)(null);
55
- exports.controllerMethodContextKey = (0, connect_1.createContextKey)(null);
56
- //# sourceMappingURL=context-values.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"context-values.js","sourceRoot":"","sources":["../src/context-values.ts"],"names":[],"mappings":";;;AAsDA,8DAEC;AAxDD,iDAI6B;AAG7B;;;GAGG;AACH,MAAa,mBAAmB;IAAhC;QACU,WAAM,GAAG,IAAI,GAAG,EAAmB,CAAC;IAqC9C,CAAC;IAnCC;;OAEG;IACH,GAAG,CAAI,GAAkB;QACvB,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC;YAC5B,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAM,CAAC;QACtC,CAAC;QACD,OAAO,GAAG,CAAC,YAAY,CAAC;IAC1B,CAAC;IAED;;OAEG;IACH,GAAG,CAAI,GAAkB,EAAE,KAAQ;QACjC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;QAC/B,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,GAAwB;QAC7B,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAC3B,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACH,CAAC,OAAO;QACN,KAAK,MAAM,CAAC,EAAE,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC;YAChD,MAAM,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;QACpB,CAAC;IACH,CAAC;CACF;AAtCD,kDAsCC;AAED;;GAEG;AACH,SAAgB,yBAAyB;IACvC,OAAO,IAAI,mBAAmB,EAAE,CAAC;AACnC,CAAC;AAEY,QAAA,yBAAyB,GAAG,IAAA,0BAAgB,EACvD,IAAI,CACL,CAAC;AACW,QAAA,0BAA0B,GAAG,IAAA,0BAAgB,EACxD,IAAI,CACL,CAAC"}
@@ -1,5 +0,0 @@
1
- import { Compression } from '@connectrpc/connect/protocol';
2
- import { FastifyInstance } from 'fastify';
3
- export declare function registerFastifyPlugin(server: FastifyInstance, options?: {
4
- acceptCompression?: Compression[];
5
- }): Promise<void>;
@@ -1,59 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.registerFastifyPlugin = registerFastifyPlugin;
4
- const connect_fastify_1 = require("@connectrpc/connect-fastify");
5
- const helpers_1 = require("./helpers");
6
- const interceptors_1 = require("./interceptors");
7
- const stores_1 = require("./stores");
8
- async function registerFastifyPlugin(server, options = {}) {
9
- // Create implementations from controller instances
10
- const implementations = new Map();
11
- for (const { instance, service } of stores_1.ControllersStore.values()) {
12
- // Create the implementation object
13
- const implementation = {};
14
- // Bind each method from the service
15
- for (const methodDesc of service.methods) {
16
- const { name: methodName } = methodDesc; // This is in PascalCase, e.g., "Say" as in service .proto file
17
- const controllerMethodName = methodName[0].toLowerCase() + methodName.slice(1); // This is in camelCase, e.g., "say" as in controller
18
- if (controllerMethodName) {
19
- const controllerMethod = instance[controllerMethodName];
20
- if (controllerMethod) {
21
- // Bind the method with proper 'this' context
22
- const bindedMethod = controllerMethod.bind(instance);
23
- implementation[controllerMethodName] = bindedMethod;
24
- // Store route metadata for interceptors
25
- stores_1.RouteMetadataStore.registerRoute(service.typeName, methodName, instance.constructor, controllerMethod, controllerMethodName, instance);
26
- helpers_1.logger.log(`Binding ${instance.constructor.name}.${controllerMethodName} to ${service.typeName}.${methodName}`);
27
- }
28
- else {
29
- helpers_1.logger.warn(`Method ${controllerMethodName} not found in ${instance.constructor.name}`);
30
- }
31
- }
32
- }
33
- implementations.set(service, implementation);
34
- }
35
- const routes = (router) => {
36
- for (const [service, implementation] of implementations.entries()) {
37
- router.service(service, implementation);
38
- helpers_1.logger.log(`Registered {/${service.typeName}} route`);
39
- }
40
- };
41
- if (routes.length === 0) {
42
- helpers_1.logger.warn('No controllers found to register');
43
- return;
44
- }
45
- await server.register(connect_fastify_1.fastifyConnectPlugin, {
46
- // For now we enable only Connect protocol by default and disable others.
47
- // grpc: this.options.grpc ?? false,
48
- // grpcWeb: this.options.grpcWeb ?? false,
49
- // connect: this.options.connect ?? true,
50
- grpc: false,
51
- grpcWeb: false,
52
- connect: true,
53
- acceptCompression: options.acceptCompression ?? [],
54
- interceptors: [interceptors_1.contextInterceptor, ...interceptors_1.initializedInterceptors],
55
- routes: routes,
56
- });
57
- helpers_1.logger.log('Ready');
58
- }
59
- //# sourceMappingURL=fastify-plugin.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fastify-plugin.js","sourceRoot":"","sources":["../src/fastify-plugin.ts"],"names":[],"mappings":";;AASA,sDA6EC;AApFD,iEAAmE;AAGnE,uCAAmC;AACnC,iDAA6E;AAC7E,qCAAgE;AAEzD,KAAK,UAAU,qBAAqB,CACzC,MAAuB,EACvB,UAEI,EAAE;IAEN,mDAAmD;IACnD,MAAM,eAAe,GAAG,IAAI,GAAG,EAAwB,CAAC;IAExD,KAAK,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,yBAAgB,CAAC,MAAM,EAAE,EAAE,CAAC;QAC9D,mCAAmC;QACnC,MAAM,cAAc,GAAQ,EAAE,CAAC;QAE/B,oCAAoC;QACpC,KAAK,MAAM,UAAU,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;YACzC,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,GAAG,UAAU,CAAC,CAAC,+DAA+D;YACxG,MAAM,oBAAoB,GACx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,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxB,gBAAM,CAAC,IAAI,CAAC,kCAAkC,CAAC,CAAC;QAChD,OAAO;IACT,CAAC;IAED,MAAM,MAAM,CAAC,QAAQ,CAAC,sCAAoB,EAAE;QAC1C,yEAAyE;QACzE,oCAAoC;QACpC,0CAA0C;QAC1C,yCAAyC;QACzC,IAAI,EAAE,KAAK;QACX,OAAO,EAAE,KAAK;QACd,OAAO,EAAE,IAAI;QACb,iBAAiB,EAAE,OAAO,CAAC,iBAAiB,IAAI,EAAE;QAClD,YAAY,EAAE,CAAC,iCAAkB,EAAE,GAAG,sCAAuB,CAAC;QAC9D,MAAM,EAAE,MAAM;KACf,CAAC,CAAC;IAEH,gBAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACtB,CAAC"}
package/dist/helpers.d.ts DELETED
@@ -1,17 +0,0 @@
1
- import { FastifyReply, FastifyRequest } from 'fastify';
2
- import { Logger, Middleware, RouteConfigGlobal, RouteConfigService } from './interfaces';
3
- export declare function callMiddlewareAsync(middleware: Middleware, req: FastifyRequest, res: FastifyReply): Promise<void>;
4
- /**
5
- * Helper to convert NestJS middleware to Fastify hook
6
- */
7
- export declare function convertMiddlewareToHook(middlewareInstance: any): (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
8
- export declare let logger: Logger;
9
- export declare function setLogger(customLogger: Logger | boolean): void;
10
- /**
11
- * Generate a unique request ID
12
- */
13
- export declare function generateRequestId(): string;
14
- /** Returns checker function for url */
15
- export declare function buildRouteConfigChecker<T extends RouteConfigGlobal | RouteConfigService<any>>(configs: T[]): (url: string, checkConfig?: T) => T[];
16
- /** Returns the pathname part of a URL which should be used to parse service name and method name */
17
- export declare function getURLPath(url: string): string;
package/dist/helpers.js DELETED
@@ -1,115 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.logger = void 0;
4
- exports.callMiddlewareAsync = callMiddlewareAsync;
5
- exports.convertMiddlewareToHook = convertMiddlewareToHook;
6
- exports.setLogger = setLogger;
7
- exports.generateRequestId = generateRequestId;
8
- exports.buildRouteConfigChecker = buildRouteConfigChecker;
9
- exports.getURLPath = getURLPath;
10
- function callMiddlewareAsync(middleware, req, res) {
11
- return new Promise((resolve, reject) => {
12
- try {
13
- middleware.use(req.raw, res.raw, (err) => {
14
- if (err) {
15
- reject(err);
16
- }
17
- else {
18
- resolve();
19
- }
20
- });
21
- }
22
- catch (error) {
23
- reject(error);
24
- }
25
- });
26
- }
27
- /**
28
- * Helper to convert NestJS middleware to Fastify hook
29
- */
30
- function convertMiddlewareToHook(middlewareInstance) {
31
- return async (request, reply) => {
32
- await callMiddlewareAsync(middlewareInstance, request, reply);
33
- };
34
- }
35
- exports.logger = {
36
- log: (...args) => {
37
- console.info(...args);
38
- },
39
- error: (...args) => {
40
- console.error(...args);
41
- },
42
- warn: (...args) => {
43
- console.warn(...args);
44
- },
45
- debug: (...args) => {
46
- console.debug(...args);
47
- },
48
- verbose: (...args) => {
49
- console.log(...args);
50
- },
51
- };
52
- function setLogger(customLogger) {
53
- if (typeof customLogger == 'boolean') {
54
- if (customLogger === false) {
55
- // Disable logging
56
- exports.logger = {
57
- log: () => { },
58
- error: () => { },
59
- warn: () => { },
60
- debug: () => { },
61
- verbose: () => { },
62
- };
63
- }
64
- return;
65
- }
66
- exports.logger = customLogger;
67
- }
68
- /**
69
- * Generate a unique request ID
70
- */
71
- function generateRequestId() {
72
- return `req_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;
73
- }
74
- /** Returns checker function for url */
75
- function buildRouteConfigChecker(configs) {
76
- const configMethods = configs.map((config) => ({
77
- config: config,
78
- methods: new Set(
79
- // Convert method names to set with PascalCase
80
- (config.methods || []).map((m) => m[0].toUpperCase() + m.slice(1))),
81
- }));
82
- /** Returns matched route configs for a given URL */
83
- return (url, checkConfig) => {
84
- // Parse the URL to get service and method
85
- // Format: /package.ServiceName/MethodName
86
- const match = url.match(/^\/([^/]+)\/([^/]+)$/);
87
- if (!match) {
88
- // Not a ConnectRPC route, skip
89
- return [];
90
- }
91
- const [, serviceName, methodName] = match;
92
- const matchedConfigs = [];
93
- for (const { config, methods } of configMethods) {
94
- // If checkConfig is provided, only match that specific config
95
- if (checkConfig && config !== checkConfig) {
96
- continue;
97
- }
98
- // Check if config should apply to this service
99
- if (config.on && config.on.typeName !== serviceName) {
100
- continue;
101
- }
102
- // Check if config should apply to this method
103
- if (methods.size && !methods.has(methodName)) {
104
- continue;
105
- }
106
- matchedConfigs.push(config);
107
- }
108
- return matchedConfigs;
109
- };
110
- }
111
- /** Returns the pathname part of a URL which should be used to parse service name and method name */
112
- function getURLPath(url) {
113
- return URL.parse(url)?.pathname || url;
114
- }
115
- //# sourceMappingURL=helpers.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":";;;AAQA,kDAkBC;AAKD,0DAMC;AAoBD,8BAeC;AAKD,8CAEC;AAGD,0DA+CC;AAGD,gCAEC;AA9HD,SAAgB,mBAAmB,CACjC,UAAsB,EACtB,GAAmB,EACnB,GAAiB;IAEjB,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC3C,IAAI,CAAC;YACH,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,CAAC,GAAS,EAAE,EAAE;gBAC7C,IAAI,GAAG,EAAE,CAAC;oBACR,MAAM,CAAC,GAAG,CAAC,CAAC;gBACd,CAAC;qBAAM,CAAC;oBACN,OAAO,EAAE,CAAC;gBACZ,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,CAAC,KAAK,CAAC,CAAC;QAChB,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,SAAgB,uBAAuB,CACrC,kBAAuB;IAEvB,OAAO,KAA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ACX,CAAC;YAED,8CAA8C;YAC9C,IAAI,OAAO,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC;gBAC7C,SAAS;YACX,CAAC;YAED,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC9B,CAAC;QAED,OAAO,cAAc,CAAC;IACxB,CAAC,CAAC;AACJ,CAAC;AAED,oGAAoG;AACpG,SAAgB,UAAU,CAAC,GAAW;IACpC,OAAO,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,QAAQ,IAAI,GAAG,CAAC;AACzC,CAAC"}
package/dist/index.d.ts DELETED
@@ -1,7 +0,0 @@
1
- export declare function printMsg(): void;
2
- export { ConnectRPC } from './connectrpc';
3
- export { CustomContextValues, controllerClassContextKey, controllerMethodContextKey, createCustomContextValues, } from './context-values';
4
- export { interceptorConfig, middlewareConfig } from './interfaces';
5
- export type { AnyFn, Interceptor, Logger, Middleware, MiddlewareConfig, MiddlewareConfigUnion, Service, } from './interfaces';
6
- export type { OmitConnectrpcFields } from './types';
7
- export { getCustomContextValues } from './middlewares';
package/dist/index.js DELETED
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getCustomContextValues = exports.middlewareConfig = exports.interceptorConfig = exports.createCustomContextValues = exports.controllerMethodContextKey = exports.controllerClassContextKey = exports.CustomContextValues = exports.ConnectRPC = void 0;
4
- exports.printMsg = printMsg;
5
- function printMsg() {
6
- console.error('connectrpc-fastify is in development mode! not ready for production yet!');
7
- }
8
- var connectrpc_1 = require("./connectrpc");
9
- Object.defineProperty(exports, "ConnectRPC", { enumerable: true, get: function () { return connectrpc_1.ConnectRPC; } });
10
- var context_values_1 = require("./context-values");
11
- Object.defineProperty(exports, "CustomContextValues", { enumerable: true, get: function () { return context_values_1.CustomContextValues; } });
12
- Object.defineProperty(exports, "controllerClassContextKey", { enumerable: true, get: function () { return context_values_1.controllerClassContextKey; } });
13
- Object.defineProperty(exports, "controllerMethodContextKey", { enumerable: true, get: function () { return context_values_1.controllerMethodContextKey; } });
14
- Object.defineProperty(exports, "createCustomContextValues", { enumerable: true, get: function () { return context_values_1.createCustomContextValues; } });
15
- var interfaces_1 = require("./interfaces");
16
- Object.defineProperty(exports, "interceptorConfig", { enumerable: true, get: function () { return interfaces_1.interceptorConfig; } });
17
- Object.defineProperty(exports, "middlewareConfig", { enumerable: true, get: function () { return interfaces_1.middlewareConfig; } });
18
- var middlewares_1 = require("./middlewares");
19
- Object.defineProperty(exports, "getCustomContextValues", { enumerable: true, get: function () { return middlewares_1.getCustomContextValues; } });
20
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,4BAIC;AAJD,SAAgB,QAAQ;IACtB,OAAO,CAAC,KAAK,CACX,0EAA0E,CAC3E,CAAC;AACJ,CAAC;AAED,2CAA0C;AAAjC,wGAAA,UAAU,OAAA;AAEnB,mDAK0B;AAJxB,qHAAA,mBAAmB,OAAA;AACnB,2HAAA,yBAAyB,OAAA;AACzB,4HAAA,0BAA0B,OAAA;AAC1B,2HAAA,yBAAyB,OAAA;AAG3B,2CAAmE;AAA1D,+GAAA,iBAAiB,OAAA;AAAE,8GAAA,gBAAgB,OAAA;AAc5C,6CAAuD;AAA9C,qHAAA,sBAAsB,OAAA"}
@@ -1,11 +0,0 @@
1
- import type { Interceptor } from '@connectrpc/connect';
2
- import { InterceptorConfigUnion } from './interfaces';
3
- /**
4
- * ContextInterceptor - copies context values from MiddlewareContextStore to HandlerContext
5
- * This interceptor should be first in the chain to ensure context values are available
6
- * to subsequent interceptors and controllers.
7
- */
8
- export declare const contextInterceptor: Interceptor;
9
- /** Here we store the initialized interceptors after configuration */
10
- export declare let initializedInterceptors: Interceptor[];
11
- export declare function initInterceptors(configs: InterceptorConfigUnion[]): void;
@@ -1,68 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.initializedInterceptors = exports.contextInterceptor = void 0;
4
- exports.initInterceptors = initInterceptors;
5
- const context_values_1 = require("./context-values");
6
- const helpers_1 = require("./helpers");
7
- const middlewares_1 = require("./middlewares");
8
- const stores_1 = require("./stores");
9
- /**
10
- * ContextInterceptor - copies context values from MiddlewareContextStore to HandlerContext
11
- * This interceptor should be first in the chain to ensure context values are available
12
- * to subsequent interceptors and controllers.
13
- */
14
- const contextInterceptor = (next) => async (req) => {
15
- // Get the request ID from headers
16
- const requestId = req.header.get(middlewares_1.xServerRequestIdHeader);
17
- if (requestId) {
18
- const middlewareContext = stores_1.MiddlewareContextStore.get(requestId);
19
- if (middlewareContext) {
20
- // Copy context values from middleware context to handler context
21
- for (const [key, value] of middlewareContext.contextValues.entries()) {
22
- req.contextValues.set({ id: key, defaultValue: undefined }, value);
23
- }
24
- }
25
- }
26
- const meta = stores_1.RouteMetadataStore.getRouteMetadata(req.url);
27
- if (meta) {
28
- // Set controller class and method in context values, so interceptors can access them
29
- req.contextValues.set(context_values_1.controllerClassContextKey, meta.controllerClass);
30
- req.contextValues.set(context_values_1.controllerMethodContextKey, meta.controllerMethod);
31
- }
32
- // Continue with the next interceptor or handler
33
- return await next(req);
34
- };
35
- exports.contextInterceptor = contextInterceptor;
36
- /** Here we store the initialized interceptors after configuration */
37
- exports.initializedInterceptors = [];
38
- function initInterceptors(configs) {
39
- const routeChecker = (0, helpers_1.buildRouteConfigChecker)(configs);
40
- exports.initializedInterceptors = [];
41
- for (const config of configs) {
42
- const interceptorInstance = stores_1.InterceptorStore.getInstance(config.use);
43
- if (!interceptorInstance) {
44
- helpers_1.logger.error(`Interceptor ${config.use.name} not registered. Make sure to decorate it with @Interceptor().`);
45
- process.exit(1);
46
- }
47
- // Create the interceptor function that checks route configs before applying
48
- const interceptor = (next) => async (req) => {
49
- const configsForUrl = routeChecker((0, helpers_1.getURLPath)(req.url), config);
50
- const shouldApply = configsForUrl.length > 0;
51
- if (shouldApply) {
52
- return await interceptorInstance.use(next)(req);
53
- }
54
- else {
55
- return await next(req);
56
- }
57
- };
58
- exports.initializedInterceptors.push(interceptor);
59
- const serviceInfo = config.on
60
- ? ` to service ${config.on.typeName}`
61
- : ' to all services';
62
- const methodInfo = config.methods
63
- ? ` methods [${config.methods.join(', ')}]`
64
- : ' all methods';
65
- helpers_1.logger.log(`Registered interceptor: ${config.use.name}${serviceInfo}${methodInfo}`);
66
- }
67
- }
68
- //# sourceMappingURL=interceptors.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"interceptors.js","sourceRoot":"","sources":["../src/interceptors.ts"],"names":[],"mappings":";;;AAmDA,4CAqCC;AAvFD,qDAG0B;AAC1B,uCAAwE;AAExE,+CAAuD;AACvD,qCAIkB;AAElB;;;;GAIG;AACI,MAAM,kBAAkB,GAAgB,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;IACrE,kCAAkC;IAClC,MAAM,SAAS,GAAG,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,oCAAsB,CAAC,CAAC;IAEzD,IAAI,SAAS,EAAE,CAAC;QACd,MAAM,iBAAiB,GAAG,+BAAsB,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAEhE,IAAI,iBAAiB,EAAE,CAAC;YACtB,iEAAiE;YACjE,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,iBAAiB,CAAC,aAAa,CAAC,OAAO,EAAE,EAAE,CAAC;gBACrE,GAAG,CAAC,aAAa,CAAC,GAAG,CACnB,EAAE,EAAE,EAAE,GAAG,EAAE,YAAY,EAAE,SAAS,EAAS,EAC3C,KAAK,CACN,CAAC;YACJ,CAAC;QACH,CAAC;IACH,CAAC;IAED,MAAM,IAAI,GAAG,2BAAkB,CAAC,gBAAgB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC1D,IAAI,IAAI,EAAE,CAAC;QACT,q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IAAI,CAAC,GAAG,CAAC,CAAC;YACzB,CAAC;QACH,CAAC,CAAC;QAEF,+BAAuB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAE1C,MAAM,WAAW,GAAG,MAAM,CAAC,EAAE;YAC3B,CAAC,CAAC,eAAe,MAAM,CAAC,EAAE,CAAC,QAAQ,EAAE;YACrC,CAAC,CAAC,kBAAkB,CAAC;QACvB,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO;YAC/B,CAAC,CAAC,aAAa,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG;YAC3C,CAAC,CAAC,cAAc,CAAC;QACnB,gBAAM,CAAC,GAAG,CACR,2BAA2B,MAAM,CAAC,GAAG,CAAC,IAAI,GAAG,WAAW,GAAG,UAAU,EAAE,CACxE,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -1,149 +0,0 @@
1
- import type { GenMessage, GenService } from '@bufbuild/protobuf/codegenv2';
2
- import { HandlerContext, StreamRequest, StreamResponse, UnaryRequest, UnaryResponse } from '@connectrpc/connect';
3
- import { FastifyReply, FastifyRequest } from 'fastify';
4
- import { OmitConnectrpcFields } from './types';
5
- export interface Logger {
6
- log: (...args: any[]) => void;
7
- error: (...args: any[]) => void;
8
- warn: (...args: any[]) => void;
9
- debug: (...args: any[]) => void;
10
- verbose: (...args: any[]) => void;
11
- }
12
- export interface Middleware {
13
- use(req: FastifyRequest['raw'], res: FastifyReply['raw'], next: (err?: any) => void): void;
14
- }
15
- /**
16
- * Copy-paste from @connectrpc/connect Interceptor types
17
- *
18
- * AnyFn represents the client-side invocation of an RPC. Interceptors can wrap
19
- * this invocation, add request headers, and wrap parts of the request or
20
- * response to inspect and log.
21
- */
22
- export type AnyFn = (req: UnaryRequest | StreamRequest) => Promise<UnaryResponse | StreamResponse>;
23
- export interface Interceptor {
24
- use(next: AnyFn): AnyFn;
25
- }
26
- export interface Type<T = any> extends Function {
27
- new (...args: any[]): T;
28
- }
29
- /**
30
- * Extract the input type from a method schema
31
- */
32
- type ExtractInput<T> = T extends {
33
- input: GenMessage<infer M>;
34
- } ? M : never;
35
- /**
36
- * Extract the output type from a method schema
37
- */
38
- type ExtractOutput<T> = T extends {
39
- output: GenMessage<infer M>;
40
- } ? M : never;
41
- /**
42
- * Convert a service method to a controller method signature
43
- *
44
- * Note: The context parameter receives a HandlerContext instance at runtime
45
- * which provides access to headers, values, signal, and other request metadata.
46
- */
47
- type ServiceMethod<T> = T extends {
48
- methodKind: 'unary';
49
- } ? (request: ExtractInput<T>, context?: HandlerContext) => Promise<OmitConnectrpcFields<ExtractOutput<T>>> : T extends {
50
- methodKind: 'server_streaming';
51
- } ? (request: ExtractInput<T>, context?: HandlerContext) => AsyncIterable<OmitConnectrpcFields<ExtractOutput<T>>> : T extends {
52
- methodKind: 'client_streaming';
53
- } ? (request: AsyncIterable<ExtractInput<T>>, context?: HandlerContext) => Promise<OmitConnectrpcFields<ExtractOutput<T>>> : T extends {
54
- methodKind: 'bidi_streaming';
55
- } ? (request: AsyncIterable<ExtractInput<T>>, context?: HandlerContext) => AsyncIterable<OmitConnectrpcFields<ExtractOutput<T>>> : never;
56
- /**
57
- * Generic interface that maps a ConnectRPC service to controller methods
58
- *
59
- * Controllers can implement any subset of the service methods.
60
- * TypeScript will enforce correct signatures for implemented methods.
61
- *
62
- * Usage:
63
- * ```typescript
64
- * export class ElizaController implements Service<typeof ElizaService> {
65
- * constructor() {
66
- * ConnectRPC.registerController(this, ElizaService);
67
- * }
68
- *
69
- * async say(request: SayRequest): Promise<SayResponse> {
70
- * // implementation
71
- * }
72
- * // Other methods are optional
73
- * }
74
- * ```
75
- */
76
- export type Service<T> = T extends GenService<infer Methods> ? {
77
- [K in keyof Methods]?: ServiceMethod<Methods[K]>;
78
- } : never;
79
- export type ServiceMethodNames<T> = T extends GenService<infer Methods> ? {
80
- [K in keyof Methods]: K;
81
- }[keyof Methods] : never;
82
- export type RouteConfigGlobal = {
83
- /**
84
- * Middleware applies to all services and all methods
85
- */
86
- on?: never;
87
- methods?: never;
88
- };
89
- export type RouteConfigService<T extends GenService<any>> = {
90
- /**
91
- * The service to apply middleware to
92
- */
93
- on: T;
94
- /**
95
- * Optional: Specific method names to apply middleware to.
96
- * If omitted, middleware applies to all methods of the service.
97
- * Method names should match the protobuf method names (e.g., 'say', 'sayMany')
98
- */
99
- methods?: Array<ServiceMethodNames<T>>;
100
- };
101
- /**
102
- * Middleware configuration for ConnectRPC routes - without service specified
103
- */
104
- export type MiddlewareConfigGlobal = {
105
- /**
106
- * The middleware class to apply (must be decorated with @Middleware())
107
- */
108
- use: Type<Middleware>;
109
- } & RouteConfigGlobal;
110
- /**
111
- * Middleware configuration for ConnectRPC routes - with service specified
112
- */
113
- export type MiddlewareConfig<T extends GenService<any>> = {
114
- /**
115
- * The middleware class to apply (must be decorated with @Middleware())
116
- */
117
- use: Type<Middleware>;
118
- } & RouteConfigService<T>;
119
- /**
120
- * Middleware configuration for ConnectRPC routes
121
- */
122
- export type MiddlewareConfigUnion = MiddlewareConfigGlobal | MiddlewareConfig<any>;
123
- /**
124
- * Helper function to create a type-safe middleware configuration
125
- * This ensures proper type inference for method names based on the service
126
- */
127
- export declare function middlewareConfig<T extends GenService<any>>(use: Type<Middleware>, on?: T, methods?: Array<ServiceMethodNames<T>>): MiddlewareConfigUnion;
128
- export type InterceptorConfigGlobal = {
129
- /**
130
- * The interceptor class to apply (must be decorated with @Interceptor())
131
- */
132
- use: Type<Interceptor>;
133
- } & RouteConfigGlobal;
134
- export type InterceptorConfig<T extends GenService<any>> = {
135
- /**
136
- * The interceptor class to apply (must be decorated with @Interceptor())
137
- */
138
- use: Type<Interceptor>;
139
- } & RouteConfigService<T>;
140
- /**
141
- * Interceptor configuration for ConnectRPC routes
142
- */
143
- export type InterceptorConfigUnion = InterceptorConfigGlobal | InterceptorConfig<any>;
144
- /**
145
- * Helper function to create a type-safe interceptor configuration
146
- * This ensures proper type inference for method names based on the service
147
- */
148
- export declare function interceptorConfig<T extends GenService<any>>(use: Type<Interceptor>, on?: T, methods?: Array<ServiceMethodNames<T>>): InterceptorConfigUnion;
149
- export {};
@@ -1,27 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.middlewareConfig = middlewareConfig;
4
- exports.interceptorConfig = interceptorConfig;
5
- /**
6
- * Helper function to create a type-safe middleware configuration
7
- * This ensures proper type inference for method names based on the service
8
- */
9
- function middlewareConfig(use, on, methods) {
10
- return {
11
- use,
12
- on,
13
- methods,
14
- };
15
- }
16
- /**
17
- * Helper function to create a type-safe interceptor configuration
18
- * This ensures proper type inference for method names based on the service
19
- */
20
- function interceptorConfig(use, on, methods) {
21
- return {
22
- use,
23
- on,
24
- methods,
25
- };
26
- }
27
- //# sourceMappingURL=interfaces.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../src/interfaces.ts"],"names":[],"mappings":";;AA2KA,4CAUC;AA2BD,8CAUC;AAnDD;;;GAGG;AACH,SAAgB,gBAAgB,CAC9B,GAAqB,EACrB,EAAM,EACN,OAAsC;IAEtC,OAAO;QACL,GAAG;QACH,EAAE;QACF,OAAO;KACR,CAAC;AACJ,CAAC;AAuBD;;;GAGG;AACH,SAAgB,iBAAiB,CAC/B,GAAsB,EACtB,EAAM,EACN,OAAsC;IAEtC,OAAO;QACL,GAAG;QACH,EAAE;QACF,OAAO;KACR,CAAC;AACJ,CAAC"}
@@ -1,6 +0,0 @@
1
- import { FastifyInstance, FastifyRequest } from 'fastify';
2
- import { MiddlewareConfigUnion } from './interfaces';
3
- export declare const xServerRequestIdHeader = "x-server-request-id";
4
- /** This method allows middleware to access custom context values associated with a request */
5
- export declare function getCustomContextValues(req: FastifyRequest['raw']): import("./context-values").CustomContextValues | null;
6
- export declare function initMiddlewares(server: FastifyInstance, middlewareConfigs: MiddlewareConfigUnion[]): Promise<void>;
@@ -1,79 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.xServerRequestIdHeader = void 0;
4
- exports.getCustomContextValues = getCustomContextValues;
5
- exports.initMiddlewares = initMiddlewares;
6
- const context_values_1 = require("./context-values");
7
- const helpers_1 = require("./helpers");
8
- const stores_1 = require("./stores");
9
- exports.xServerRequestIdHeader = 'x-server-request-id';
10
- function setupCustomContextValues(req) {
11
- const requestId = (0, helpers_1.generateRequestId)();
12
- // Store the request ID in a custom header
13
- req.headers[exports.xServerRequestIdHeader] = requestId;
14
- // Create and store the middleware context with ContextValues
15
- stores_1.MiddlewareContextStore.set(requestId, {
16
- contextValues: (0, context_values_1.createCustomContextValues)(),
17
- });
18
- }
19
- /** This method allows middleware to access custom context values associated with a request */
20
- function getCustomContextValues(req) {
21
- const requestId = req.headers[exports.xServerRequestIdHeader];
22
- if (requestId) {
23
- const middlewareContext = stores_1.MiddlewareContextStore.get(requestId);
24
- if (middlewareContext) {
25
- return middlewareContext.contextValues;
26
- }
27
- }
28
- return null;
29
- }
30
- function clearCustomContextValues(req) {
31
- const requestId = req.headers[exports.xServerRequestIdHeader];
32
- if (requestId) {
33
- stores_1.MiddlewareContextStore.delete(requestId);
34
- }
35
- }
36
- async function initMiddlewares(server, middlewareConfigs) {
37
- // Add context middleware first - this stores req/res and creates ContextValues for interceptors
38
- server.addHook('onRequest', async (request, reply) => {
39
- setupCustomContextValues(request.raw);
40
- // Clean up the context when the response finishes
41
- reply.raw.on('finish', () => {
42
- clearCustomContextValues(request.raw);
43
- });
44
- });
45
- helpers_1.logger.log('Applied context middleware for storing ContextValues');
46
- // Check all registered middlewares
47
- for (const config of middlewareConfigs) {
48
- const middlewareInstance = stores_1.MiddlewareStore.getInstance(config.use);
49
- if (!middlewareInstance) {
50
- helpers_1.logger.error(`Middleware ${config.use.name} not found in store. Did you forget to add MiddlewareStore.registerInstance(this) in the constructor? Or did you forget to instantiate the middleware?`);
51
- process.exit(1);
52
- }
53
- const serviceInfo = config.on
54
- ? ` to service ${config.on.typeName}`
55
- : ' to all services';
56
- const methodInfo = config.methods
57
- ? ` methods [${config.methods.join(', ')}]`
58
- : ' all methods';
59
- helpers_1.logger.log(`Applied middleware: ${config.use.name}${serviceInfo}${methodInfo}`);
60
- }
61
- const routeChecker = (0, helpers_1.buildRouteConfigChecker)(middlewareConfigs);
62
- server.addHook('onRequest', async (request, reply) => {
63
- const url = request.url;
64
- const configs = routeChecker(url);
65
- try {
66
- for (const config of configs) {
67
- const middlewareInstance = stores_1.MiddlewareStore.getInstance(config.use);
68
- await (0, helpers_1.callMiddlewareAsync)(middlewareInstance, request, reply);
69
- }
70
- }
71
- catch (error) {
72
- helpers_1.logger.error(`Error in middleware for request ${request.url}:`, error);
73
- if (!reply.sent) {
74
- reply.status(500).send({ error: 'Internal Server Error' });
75
- }
76
- }
77
- });
78
- }
79
- //# sourceMappingURL=middlewares.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"middlewares.js","sourceRoot":"","sources":["../src/middlewares.ts"],"names":[],"mappings":";;;AAyBA,wDAYC;AAUD,0CAwDC;AAtGD,qDAA6D;AAC7D,uCAKmB;AAEnB,qCAAmE;AAEtD,QAAA,sBAAsB,GAAG,qBAAqB,CAAC;AAE5D,SAAS,wBAAwB,CAAC,GAA0B;IAC1D,MAAM,SAAS,GAAG,IAAA,2BAAiB,GAAE,CAAC;IACtC,0CAA0C;IAC1C,GAAG,CAAC,OAAO,CAAC,8BAAsB,CAAC,GAAG,SAAS,CAAC;IAEhD,6DAA6D;IAC7D,+BAAsB,CAAC,GAAG,CAAC,SAAS,EAAE;QACpC,aAAa,EAAE,IAAA,0CAAyB,GAAE;KAC3C,CAAC,CAAC;AACL,CAAC;AAED,8FAA8F;AAC9F,SAAgB,sBAAsB,CAAC,GAA0B;IAC/D,MAAM,SAAS,GAAG,GAAG,CAAC,OAAO,CAAC,8BAAsB,CAAW,CAAC;IAEhE,IAAI,SAAS,EAAE,CAAC;QACd,MAAM,iBAAiB,GAAG,+BAAsB,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;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kB,CAAC;QACvB,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO;YAC/B,CAAC,CAAC,aAAa,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG;YAC3C,CAAC,CAAC,cAAc,CAAC;QACnB,gBAAM,CAAC,GAAG,CACR,uBAAuB,MAAM,CAAC,GAAG,CAAC,IAAI,GAAG,WAAW,GAAG,UAAU,EAAE,CACpE,CAAC;IACJ,CAAC;IAED,MAAM,YAAY,GAAG,IAAA,iCAAuB,EAAC,iBAAiB,CAAC,CAAC;IAEhE,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE;QACnD,MAAM,GAAG,GAAG,OAAO,CAAC,GAAa,CAAC;QAClC,MAAM,OAAO,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC;QAElC,IAAI,CAAC;YACH,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;gBAC7B,MAAM,kBAAkB,GAAG,wBAAe,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,CAAE,CAAC;gBACpE,MAAM,IAAA,6BAAmB,EAAC,kBAAkB,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;YAChE,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,gBAAM,CAAC,KAAK,CAAC,mCAAmC,OAAO,CAAC,GAAG,GAAG,EAAE,KAAK,CAAC,CAAC;YACvE,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;gBAChB,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,uBAAuB,EAAE,CAAC,CAAC;YAC7D,CAAC;QACH,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
package/dist/stores.d.ts DELETED
@@ -1,117 +0,0 @@
1
- import { GenService, GenServiceMethods } from '@bufbuild/protobuf/codegenv2';
2
- import { CustomContextValues } from './context-values';
3
- import { Interceptor, Middleware, Service, Type } from './interfaces';
4
- declare class ControllersStoreClass {
5
- private controllers;
6
- values(): {
7
- instance: any;
8
- service: GenService<any>;
9
- target: Type<any>;
10
- }[];
11
- clear(): void;
12
- registerInstance<T extends GenServiceMethods>(self: Service<GenService<T>>, service: GenService<T>, { allowMultipleInstances, }?: {
13
- allowMultipleInstances?: boolean;
14
- }): void;
15
- }
16
- export declare const ControllersStore: ControllersStoreClass;
17
- /**
18
- * Store for middleware classes and their instances
19
- */
20
- declare class MiddlewareStoreClass {
21
- private middlewares;
22
- clear(): void;
23
- /**
24
- * Register a middleware instance from its constructor
25
- */
26
- registerInstance(self: Middleware, { allowMultipleInstances, }?: {
27
- allowMultipleInstances?: boolean;
28
- }): void;
29
- /**
30
- * Get a middleware instance by its class
31
- */
32
- getInstance(middlewareClass: Type<Middleware>): Middleware | null;
33
- }
34
- export declare const MiddlewareStore: MiddlewareStoreClass;
35
- /**
36
- * Store for route metadata - maps URL paths to controller class and method info
37
- */
38
- declare class RouteMetadataStoreClass {
39
- private routes;
40
- clear(): void;
41
- /**
42
- * Register route metadata for a specific service method
43
- * @param serviceName - The full service name (e.g., "connectrpc.eliza.v1.ElizaService")
44
- * @param methodName - The method name in PascalCase (e.g., "Say")
45
- * @param controllerClass - The controller class
46
- * @param controllerMethod - The bound controller method
47
- * @param controllerMethodName - The name of the controller method
48
- * @param instance - The controller instance
49
- */
50
- registerRoute(serviceName: string, methodName: string, controllerClass: Type<any>, controllerMethod: Function, controllerMethodName: string, instance: any): void;
51
- /**
52
- * Get route metadata by URL path
53
- */
54
- getRouteMetadata(urlPath: string): {
55
- controllerClass: Type<any>;
56
- controllerMethod: Function;
57
- controllerMethodName: string;
58
- instance: any;
59
- serviceName: string;
60
- methodName: string;
61
- } | null;
62
- /**
63
- * Get all registered routes
64
- */
65
- getAllRoutes(): [string, {
66
- controllerClass: Type<any>;
67
- controllerMethod: Function;
68
- controllerMethodName: string;
69
- instance: any;
70
- serviceName: string;
71
- methodName: string;
72
- }][];
73
- }
74
- export declare const RouteMetadataStore: RouteMetadataStoreClass;
75
- /**
76
- * Middleware context - contains both raw req/res and context values
77
- */
78
- export interface MiddlewareContext {
79
- contextValues: CustomContextValues;
80
- }
81
- /**
82
- * Store for middleware context - maps request IDs to middleware context
83
- * This allows interceptors to access both raw request/response objects and context values
84
- */
85
- declare class MiddlewareContextStoreClass {
86
- private contexts;
87
- clear(): void;
88
- /**
89
- * Store middleware context by request ID
90
- */
91
- set(requestId: string, context: MiddlewareContext): void;
92
- /**
93
- * Get middleware context by request ID
94
- */
95
- get(requestId: string): MiddlewareContext | null;
96
- /**
97
- * Delete middleware context by request ID
98
- */
99
- delete(requestId: string): void;
100
- }
101
- export declare const MiddlewareContextStore: MiddlewareContextStoreClass;
102
- declare class InterceptorStoreClass {
103
- private interceptors;
104
- clear(): void;
105
- /**
106
- * Register an interceptor instance by a unique key
107
- */
108
- registerInstance(self: Interceptor, { allowMultipleInstances, }?: {
109
- allowMultipleInstances?: boolean;
110
- }): void;
111
- /**
112
- * Get an interceptor instance by its unique key
113
- */
114
- getInstance(interceptorClass: Type<Interceptor>): Interceptor | null;
115
- }
116
- export declare const InterceptorStore: InterceptorStoreClass;
117
- export {};
package/dist/stores.js DELETED
@@ -1,165 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InterceptorStore = exports.MiddlewareContextStore = exports.RouteMetadataStore = exports.MiddlewareStore = exports.ControllersStore = void 0;
4
- const helpers_1 = require("./helpers");
5
- class ControllersStoreClass {
6
- constructor() {
7
- this.controllers = new Map();
8
- }
9
- values() {
10
- return Array.from(this.controllers.entries()).map(([target, data]) => ({
11
- target,
12
- ...data,
13
- }));
14
- }
15
- // For testing purposes
16
- clear() {
17
- this.controllers.clear();
18
- }
19
- registerInstance(self, service, { allowMultipleInstances = false, } = {}) {
20
- const controllerClass = self.constructor;
21
- if (!allowMultipleInstances && this.controllers.has(controllerClass)) {
22
- throw new Error(`Controller ${controllerClass.name} is already registered! This may happen if you export controller as provider and also register it in some Nest module.`);
23
- }
24
- this.controllers.set(controllerClass, {
25
- instance: self,
26
- service,
27
- });
28
- }
29
- }
30
- exports.ControllersStore = new ControllersStoreClass();
31
- /**
32
- * Store for middleware classes and their instances
33
- */
34
- class MiddlewareStoreClass {
35
- constructor() {
36
- this.middlewares = new Map();
37
- }
38
- // For testing purposes
39
- clear() {
40
- this.middlewares.clear();
41
- }
42
- /**
43
- * Register a middleware instance from its constructor
44
- */
45
- registerInstance(self, { allowMultipleInstances = false, } = {}) {
46
- const middlewareClass = self.constructor;
47
- if (!allowMultipleInstances && this.middlewares.has(middlewareClass)) {
48
- throw new Error(`Middleware ${middlewareClass.name} is already registered! This may happen if you export middleware as provider and also register it in some Nest module.`);
49
- }
50
- this.middlewares.set(middlewareClass, self);
51
- }
52
- /**
53
- * Get a middleware instance by its class
54
- */
55
- getInstance(middlewareClass) {
56
- return this.middlewares.get(middlewareClass) || null;
57
- }
58
- }
59
- exports.MiddlewareStore = new MiddlewareStoreClass();
60
- /**
61
- * Store for route metadata - maps URL paths to controller class and method info
62
- */
63
- class RouteMetadataStoreClass {
64
- constructor() {
65
- this.routes = new Map();
66
- }
67
- // For testing purposes
68
- clear() {
69
- this.routes.clear();
70
- }
71
- /**
72
- * Register route metadata for a specific service method
73
- * @param serviceName - The full service name (e.g., "connectrpc.eliza.v1.ElizaService")
74
- * @param methodName - The method name in PascalCase (e.g., "Say")
75
- * @param controllerClass - The controller class
76
- * @param controllerMethod - The bound controller method
77
- * @param controllerMethodName - The name of the controller method
78
- * @param instance - The controller instance
79
- */
80
- registerRoute(serviceName, methodName, controllerClass, controllerMethod, controllerMethodName, instance) {
81
- const routeKey = `/${serviceName}/${methodName}`;
82
- this.routes.set(routeKey, {
83
- controllerClass,
84
- controllerMethod,
85
- controllerMethodName,
86
- instance,
87
- serviceName,
88
- methodName,
89
- });
90
- }
91
- /**
92
- * Get route metadata by URL path
93
- */
94
- getRouteMetadata(urlPath) {
95
- return this.routes.get((0, helpers_1.getURLPath)(urlPath)) || null;
96
- }
97
- /**
98
- * Get all registered routes
99
- */
100
- getAllRoutes() {
101
- return Array.from(this.routes.entries());
102
- }
103
- }
104
- exports.RouteMetadataStore = new RouteMetadataStoreClass();
105
- /**
106
- * Store for middleware context - maps request IDs to middleware context
107
- * This allows interceptors to access both raw request/response objects and context values
108
- */
109
- class MiddlewareContextStoreClass {
110
- constructor() {
111
- this.contexts = new Map();
112
- }
113
- // For testing purposes
114
- clear() {
115
- this.contexts.clear();
116
- }
117
- /**
118
- * Store middleware context by request ID
119
- */
120
- set(requestId, context) {
121
- this.contexts.set(requestId, context);
122
- }
123
- /**
124
- * Get middleware context by request ID
125
- */
126
- get(requestId) {
127
- return this.contexts.get(requestId) || null;
128
- }
129
- /**
130
- * Delete middleware context by request ID
131
- */
132
- delete(requestId) {
133
- this.contexts.delete(requestId);
134
- }
135
- }
136
- exports.MiddlewareContextStore = new MiddlewareContextStoreClass();
137
- class InterceptorStoreClass {
138
- constructor() {
139
- this.interceptors = new Map();
140
- }
141
- // For testing purposes
142
- clear() {
143
- this.interceptors.clear();
144
- }
145
- /**
146
- * Register an interceptor instance by a unique key
147
- */
148
- registerInstance(self, { allowMultipleInstances = false, } = {}) {
149
- const interceptorClass = self.constructor;
150
- const key = interceptorClass.name;
151
- if (!allowMultipleInstances && this.interceptors.has(key)) {
152
- throw new Error(`Interceptor ${interceptorClass.name} is already registered! This may happen if you export interceptor as provider and also register it in some Nest module.`);
153
- }
154
- this.interceptors.set(key, self);
155
- }
156
- /**
157
- * Get an interceptor instance by its unique key
158
- */
159
- getInstance(interceptorClass) {
160
- const key = interceptorClass.name;
161
- return this.interceptors.get(key) || null;
162
- }
163
- }
164
- exports.InterceptorStore = new InterceptorStoreClass();
165
- //# sourceMappingURL=stores.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"stores.js","sourceRoot":"","sources":["../src/stores.ts"],"names":[],"mappings":";;;AAEA,uCAAuC;AAG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AAG,EAAE,IAAI,CAAC,CAAC;IACnC,CAAC;IAED;;OAEG;IACH,WAAW,CAAC,gBAAmC;QAC7C,MAAM,GAAG,GAAG,gBAAgB,CAAC,IAAI,CAAC;QAClC,OAAO,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC;IAC5C,CAAC;CACF;AAEY,QAAA,gBAAgB,GAAG,IAAI,qBAAqB,EAAE,CAAC"}
package/dist/types.d.ts DELETED
@@ -1,13 +0,0 @@
1
- import type { OptionalKeysOf, Primitive, RequiredKeysOf } from 'type-fest';
2
- export type OmitFieldsDeep<T, K extends keyof any> = T extends Primitive | Date ? T : T extends Array<any> ? {
3
- [P in keyof T]?: OmitFieldsDeep<T[P], K>;
4
- } : T extends object ? {
5
- [P in Exclude<RequiredKeysOf<T>, K>]: OmitFieldsDeep<T[P], K>;
6
- } & {
7
- [P in Exclude<OptionalKeysOf<T>, K>]?: OmitFieldsDeep<T[P], K>;
8
- } : T;
9
- /**
10
- * Used to simplify types by omitting ConnectRPC specific fields like `$typeName` and `$unknown`
11
- * The fields are omitted deeply in nested structures.
12
- */
13
- export type OmitConnectrpcFields<T> = OmitFieldsDeep<T, '$typeName' | '$unknown'>;
package/dist/types.js DELETED
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=types.js.map
package/dist/types.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}