@funduck/connectrpc-fastify-nestjs 1.0.1 → 1.0.2

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.
@@ -1,66 +0,0 @@
1
- import type { GenMessage, GenService } from '@bufbuild/protobuf/codegenv2';
2
- import { FastifyReply, FastifyRequest } from 'fastify';
3
- import { OmitConnectrpcFields } from './types';
4
- export interface Logger {
5
- log: (...args: any[]) => void;
6
- error: (...args: any[]) => void;
7
- warn: (...args: any[]) => void;
8
- debug: (...args: any[]) => void;
9
- verbose: (...args: any[]) => void;
10
- }
11
- export interface Middleware {
12
- use(req: FastifyRequest['raw'], res: FastifyReply['raw'], next: (err?: any) => void): void;
13
- }
14
- export interface Type<T = any> extends Function {
15
- new (...args: any[]): T;
16
- }
17
- type ExtractInput<T> = T extends {
18
- input: GenMessage<infer M>;
19
- } ? M : never;
20
- type ExtractOutput<T> = T extends {
21
- output: GenMessage<infer M>;
22
- } ? M : never;
23
- type ServiceMethod<T> = T extends {
24
- methodKind: 'unary';
25
- } ? (request: ExtractInput<T>) => Promise<OmitConnectrpcFields<ExtractOutput<T>>> : T extends {
26
- methodKind: 'server_streaming';
27
- } ? (request: ExtractInput<T>) => AsyncIterable<OmitConnectrpcFields<ExtractOutput<T>>> : T extends {
28
- methodKind: 'client_streaming';
29
- } ? (request: AsyncIterable<ExtractInput<T>>) => Promise<OmitConnectrpcFields<ExtractOutput<T>>> : T extends {
30
- methodKind: 'bidi_streaming';
31
- } ? (request: AsyncIterable<ExtractInput<T>>) => AsyncIterable<OmitConnectrpcFields<ExtractOutput<T>>> : never;
32
- export type Service<T> = T extends GenService<infer Methods> ? {
33
- [K in keyof Methods]?: ServiceMethod<Methods[K]>;
34
- } : never;
35
- export type ServiceMethodNames<T> = T extends GenService<infer Methods> ? {
36
- [K in keyof Methods]: K;
37
- }[keyof Methods] : never;
38
- export type MiddlewareConfigGlobal = {
39
- use: Type<Middleware>;
40
- on?: never;
41
- methods?: never;
42
- };
43
- export type MiddlewareConfig<T extends GenService<any>> = {
44
- use: Type<Middleware>;
45
- on: T;
46
- methods?: Array<ServiceMethodNames<T>>;
47
- };
48
- export type MiddlewareConfigUnion = MiddlewareConfigGlobal | MiddlewareConfig<any>;
49
- export declare function middlewareConfig<T extends GenService<any>>(use: Type<Middleware>, on?: T, methods?: Array<ServiceMethodNames<T>>): MiddlewareConfigUnion;
50
- export interface ExecutionContext {
51
- getClass<T = any>(): Type<T>;
52
- getHandler(): Function;
53
- getArgs<T extends Array<any> = any[]>(): T;
54
- getArgByIndex<T = any>(index: number): T;
55
- switchToHttp(): {
56
- getRequest(): FastifyRequest['raw'];
57
- getResponse(): FastifyReply['raw'];
58
- getNext<T = any>(): () => T;
59
- };
60
- switchToRpc(): any;
61
- switchToWs(): any;
62
- }
63
- export interface Guard {
64
- canActivate(context: ExecutionContext): boolean | Promise<boolean>;
65
- }
66
- export {};
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.middlewareConfig = middlewareConfig;
4
- function middlewareConfig(use, on, methods) {
5
- return {
6
- use,
7
- on,
8
- methods,
9
- };
10
- }
11
- //# sourceMappingURL=interfaces.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../src/interfaces.ts"],"names":[],"mappings":";;AA0IA,4CAUC;AAVD,SAAgB,gBAAgB,CAC9B,GAAqB,EACrB,EAAM,EACN,OAAsC;IAEtC,OAAO;QACL,GAAG;QACH,EAAE;QACF,OAAO;KACR,CAAC;AACJ,CAAC"}
@@ -1,3 +0,0 @@
1
- import { FastifyInstance } from 'fastify';
2
- import { MiddlewareConfigUnion } from './interfaces';
3
- export declare function initMiddlewares(server: FastifyInstance, middlewareConfigs: MiddlewareConfigUnion[]): Promise<void>;
@@ -1,42 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.initMiddlewares = initMiddlewares;
4
- const helpers_1 = require("./helpers");
5
- const stores_1 = require("./stores");
6
- async function initMiddlewares(server, middlewareConfigs) {
7
- for (const config of middlewareConfigs) {
8
- const methods = new Set((config.methods || []).map((m) => m[0].toUpperCase() + m.slice(1)));
9
- const middlewareInstance = stores_1.MiddlewareStore.getInstance(config.use);
10
- if (!middlewareInstance) {
11
- helpers_1.logger.warn(`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?`);
12
- continue;
13
- }
14
- if (typeof middlewareInstance.use === 'function') {
15
- const hook = (0, helpers_1.convertMiddlewareToHook)(middlewareInstance);
16
- const filteredHook = async (request, reply) => {
17
- const url = request.url;
18
- const match = url.match(/^\/([^/]+)\/([^/]+)$/);
19
- if (!match) {
20
- return;
21
- }
22
- const [, serviceName, methodName] = match;
23
- if (config.on && config.on.typeName !== serviceName) {
24
- return;
25
- }
26
- if (methods.size && !methods.has(methodName)) {
27
- return;
28
- }
29
- await hook(request, reply);
30
- };
31
- server.addHook('onRequest', filteredHook);
32
- const serviceInfo = config.on
33
- ? ` to service ${config.on.typeName}`
34
- : ' to all services';
35
- const methodInfo = config.methods
36
- ? ` methods [${config.methods.join(', ')}]`
37
- : ' all methods';
38
- helpers_1.logger.log(`Applied middleware: ${config.use.name}${serviceInfo}${methodInfo}`);
39
- }
40
- }
41
- }
42
- //# sourceMappingURL=middlewares.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"middlewares.js","sourceRoot":"","sources":["../../src/middlewares.ts"],"names":[],"mappings":";;AAKA,0CAiEC;AArED,uCAA4D;AAE5D,qCAA2C;AAEpC,KAAK,UAAU,eAAe,CACnC,MAAuB,EACvB,iBAA0C;IAE1C,KAAK,MAAM,MAAM,IAAI,iBAAiB,EAAE,CAAC;QAEvC,MAAM,OAAO,GAAG,IAAI,GAAG,CACrB,CAAC,MAAM,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CA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C,EAAE,CAAC,QAAQ,EAAE;gBACrC,CAAC,CAAC,kBAAkB,CAAC;YACvB,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO;gBAC/B,CAAC,CAAC,aAAa,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG;gBAC3C,CAAC,CAAC,cAAc,CAAC;YACnB,gBAAM,CAAC,GAAG,CACR,uBAAuB,MAAM,CAAC,GAAG,CAAC,IAAI,GAAG,WAAW,GAAG,UAAU,EAAE,CACpE,CAAC;QACJ,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -1,53 +0,0 @@
1
- import { GenService, GenServiceMethods } from '@bufbuild/protobuf/codegenv2';
2
- import { Guard, Middleware, Service, Type } from './interfaces';
3
- declare class ControllersStoreClass {
4
- private controllers;
5
- values(): {
6
- instance: any;
7
- service: GenService<any>;
8
- target: Type<any>;
9
- }[];
10
- registerInstance<T extends GenServiceMethods>(self: Service<GenService<T>>, service: GenService<T>, { allowMultipleInstances, }?: {
11
- allowMultipleInstances?: boolean;
12
- }): void;
13
- }
14
- export declare const ControllersStore: ControllersStoreClass;
15
- declare class MiddlewareStoreClass {
16
- private middlewares;
17
- registerInstance(self: Middleware, { allowMultipleInstances, }?: {
18
- allowMultipleInstances?: boolean;
19
- }): void;
20
- getInstance(middlewareClass: Type<Middleware>): Middleware | null;
21
- }
22
- export declare const MiddlewareStore: MiddlewareStoreClass;
23
- declare class RouteMetadataStoreClass {
24
- private routes;
25
- registerRoute(serviceName: string, methodName: string, controllerClass: Type<any>, controllerMethod: Function, controllerMethodName: string, instance: any): void;
26
- getRouteMetadata(urlPath: string): {
27
- controllerClass: Type<any>;
28
- controllerMethod: Function;
29
- controllerMethodName: string;
30
- instance: any;
31
- serviceName: string;
32
- methodName: string;
33
- } | null;
34
- getAllRoutes(): [string, {
35
- controllerClass: Type<any>;
36
- controllerMethod: Function;
37
- controllerMethodName: string;
38
- instance: any;
39
- serviceName: string;
40
- methodName: string;
41
- }][];
42
- }
43
- export declare const RouteMetadataStore: RouteMetadataStoreClass;
44
- declare class GuardsStoreClass {
45
- private guards;
46
- registerInstance(self: Guard, { allowMultipleInstances, }?: {
47
- allowMultipleInstances?: boolean;
48
- }): void;
49
- getInstance(guardClass: Type<Guard>): Guard | null;
50
- getAllGuards(): Guard[];
51
- }
52
- export declare const GuardsStore: GuardsStoreClass;
53
- export {};
@@ -1,76 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GuardsStore = exports.RouteMetadataStore = exports.MiddlewareStore = exports.ControllersStore = void 0;
4
- class ControllersStoreClass {
5
- controllers = new Map();
6
- values() {
7
- return Array.from(this.controllers.entries()).map(([target, data]) => ({
8
- target,
9
- ...data,
10
- }));
11
- }
12
- registerInstance(self, service, { allowMultipleInstances = false, } = {}) {
13
- const controllerClass = self.constructor;
14
- if (!allowMultipleInstances && this.controllers.has(controllerClass)) {
15
- 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.`);
16
- }
17
- this.controllers.set(controllerClass, {
18
- instance: self,
19
- service,
20
- });
21
- }
22
- }
23
- exports.ControllersStore = new ControllersStoreClass();
24
- class MiddlewareStoreClass {
25
- middlewares = new Map();
26
- registerInstance(self, { allowMultipleInstances = false, } = {}) {
27
- const middlewareClass = self.constructor;
28
- if (!allowMultipleInstances && this.middlewares.has(middlewareClass)) {
29
- 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.`);
30
- }
31
- this.middlewares.set(middlewareClass, self);
32
- }
33
- getInstance(middlewareClass) {
34
- return this.middlewares.get(middlewareClass) || null;
35
- }
36
- }
37
- exports.MiddlewareStore = new MiddlewareStoreClass();
38
- class RouteMetadataStoreClass {
39
- routes = new Map();
40
- registerRoute(serviceName, methodName, controllerClass, controllerMethod, controllerMethodName, instance) {
41
- const routeKey = `/${serviceName}/${methodName}`;
42
- this.routes.set(routeKey, {
43
- controllerClass,
44
- controllerMethod,
45
- controllerMethodName,
46
- instance,
47
- serviceName,
48
- methodName,
49
- });
50
- }
51
- getRouteMetadata(urlPath) {
52
- return this.routes.get(urlPath) || null;
53
- }
54
- getAllRoutes() {
55
- return Array.from(this.routes.entries());
56
- }
57
- }
58
- exports.RouteMetadataStore = new RouteMetadataStoreClass();
59
- class GuardsStoreClass {
60
- guards = new Map();
61
- registerInstance(self, { allowMultipleInstances = false, } = {}) {
62
- const guardClass = self.constructor;
63
- if (!allowMultipleInstances && this.guards.has(guardClass)) {
64
- throw new Error(`Guard ${guardClass.name} is already registered! This may happen if you export guard as provider and also register it in some Nest module.`);
65
- }
66
- this.guards.set(guardClass, self);
67
- }
68
- getInstance(guardClass) {
69
- return this.guards.get(guardClass) || null;
70
- }
71
- getAllGuards() {
72
- return Array.from(this.guards.values());
73
- }
74
- }
75
- exports.GuardsStore = new GuardsStoreClass();
76
- //# sourceMappingURL=stores.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"stores.js","sourceRoot":"","sources":["../../src/stores.ts"],"names":[],"mappings":";;;AAGA,MAAM,qBAAqB;IACjB,WAAW,GAAG,IAAI,GAAG,EAM1B,CAAC;IAEJ,MAAM;QACJ,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;YACrE,MAAM;YACN,GAAG,IAAI;SACR,CAAC,CAAC,CAAC;IACN,CAAC;IAED,gBAAgB,CACd,IAA4B,EAC5B,OAAsB,EACtB,EACE,sBAAsB,GAAG,KAAK,MAG5B,EAAE;QAEN,MAAM,eAAe,GAAG,IAAI,CAAC,WAAwB,CAAC;QACtD,IAAI,CAAC,sBAAsB,IAAI,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,eAAe,CAAC,EAAE,CAAC;YACrE,MAAM,IAAI,KAAK,CACb,cAAc,eAAe,CAAC,IAAI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,OAAe;QAC9B,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC;IAC1C,CAAC;IAKD,YAAY;QACV,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;IAC3C,CAAC;CACF;AAEY,QAAA,kBAAkB,GAAG,IAAI,uBAAuB,EAAE,CAAC;AAKhE,MAAM,gBAAgB;IACZ,MAAM,GAAG,IAAI,GAAG,EAAsB,CAAC;IAK/C,gBAAgB,CACd,IAAW,EACX,EACE,sBAAsB,GAAG,KAAK,MACU,EAAE;QAE5C,MAAM,UAAU,GAAG,IAAI,CAAC,WAA0B,CAAC;QACnD,IAAI,CAAC,sBAAsB,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC;YAC3D,MAAM,IAAI,KAAK,CACb,SAAS,UAAU,CAAC,IAAI,mHAAmH,CAC5I,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;IACpC,CAAC;IAKD,WAAW,CAAC,UAAuB;QACjC,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,IAAI,CAAC;IAC7C,CAAC;IAKD,YAAY;QACV,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;IAC1C,CAAC;CACF;AAEY,QAAA,WAAW,GAAG,IAAI,gBAAgB,EAAE,CAAC"}
@@ -1,9 +0,0 @@
1
- import { 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
- export type OmitConnectrpcFields<T> = OmitFieldsDeep<T, '$typeName' | '$unknown'>;
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}