@teacinema/common 1.0.3 → 1.1.0

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.
@@ -0,0 +1 @@
1
+ export declare const GRPC_CLIENT_PREFIX = "GRPC_CLIENT";
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GRPC_CLIENT_PREFIX = void 0;
4
+ exports.GRPC_CLIENT_PREFIX = 'GRPC_CLIENT';
@@ -0,0 +1 @@
1
+ export * from './inject-grpc-client.decorator';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./inject-grpc-client.decorator"), exports);
@@ -0,0 +1 @@
1
+ export declare const InjectGrpcClient: (name: string) => PropertyDecorator & ParameterDecorator;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InjectGrpcClient = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ const grpc_constants_1 = require("../constants/grpc.constants");
6
+ const InjectGrpcClient = (name) => (0, common_1.Inject)(`${grpc_constants_1.GRPC_CLIENT_PREFIX}_${name}`);
7
+ exports.InjectGrpcClient = InjectGrpcClient;
@@ -0,0 +1,11 @@
1
+ import { ClientGrpc } from '@nestjs/microservices';
2
+ export declare class GrpcClientFactory {
3
+ private clients;
4
+ createClient(options: {
5
+ package: string;
6
+ protoPath: string;
7
+ url: string;
8
+ }): ClientGrpc;
9
+ register(token: string, client: ClientGrpc): void;
10
+ getClient<T extends ClientGrpc = ClientGrpc>(token: string): T;
11
+ }
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
3
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
4
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
5
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
6
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
7
+ var _, done = false;
8
+ for (var i = decorators.length - 1; i >= 0; i--) {
9
+ var context = {};
10
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
11
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
12
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
13
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
14
+ if (kind === "accessor") {
15
+ if (result === void 0) continue;
16
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
17
+ if (_ = accept(result.get)) descriptor.get = _;
18
+ if (_ = accept(result.set)) descriptor.set = _;
19
+ if (_ = accept(result.init)) initializers.unshift(_);
20
+ }
21
+ else if (_ = accept(result)) {
22
+ if (kind === "field") initializers.unshift(_);
23
+ else descriptor[key] = _;
24
+ }
25
+ }
26
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
27
+ done = true;
28
+ };
29
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
30
+ var useValue = arguments.length > 2;
31
+ for (var i = 0; i < initializers.length; i++) {
32
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
33
+ }
34
+ return useValue ? value : void 0;
35
+ };
36
+ Object.defineProperty(exports, "__esModule", { value: true });
37
+ exports.GrpcClientFactory = void 0;
38
+ const common_1 = require("@nestjs/common");
39
+ const microservices_1 = require("@nestjs/microservices");
40
+ let GrpcClientFactory = (() => {
41
+ let _classDecorators = [(0, common_1.Injectable)()];
42
+ let _classDescriptor;
43
+ let _classExtraInitializers = [];
44
+ let _classThis;
45
+ var GrpcClientFactory = class {
46
+ static { _classThis = this; }
47
+ static {
48
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
49
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
50
+ GrpcClientFactory = _classThis = _classDescriptor.value;
51
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
52
+ __runInitializers(_classThis, _classExtraInitializers);
53
+ }
54
+ clients = new Map();
55
+ createClient(options) {
56
+ return microservices_1.ClientProxyFactory.create({
57
+ transport: microservices_1.Transport.GRPC,
58
+ options
59
+ });
60
+ }
61
+ register(token, client) {
62
+ this.clients.set(token, client);
63
+ }
64
+ getClient(token) {
65
+ const client = this.clients.get(token);
66
+ if (!client)
67
+ throw new Error(`Grpc client "${token}" not found`);
68
+ return client;
69
+ }
70
+ };
71
+ return GrpcClientFactory = _classThis;
72
+ })();
73
+ exports.GrpcClientFactory = GrpcClientFactory;
@@ -0,0 +1,5 @@
1
+ import { type DynamicModule } from '@nestjs/common';
2
+ import { GRPC_CLIENTS } from './registry/grpc.registry';
3
+ export declare class GrpcModule {
4
+ static register(clients: Array<keyof typeof GRPC_CLIENTS>): DynamicModule;
5
+ }
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
3
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
4
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
5
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
6
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
7
+ var _, done = false;
8
+ for (var i = decorators.length - 1; i >= 0; i--) {
9
+ var context = {};
10
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
11
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
12
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
13
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
14
+ if (kind === "accessor") {
15
+ if (result === void 0) continue;
16
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
17
+ if (_ = accept(result.get)) descriptor.get = _;
18
+ if (_ = accept(result.set)) descriptor.set = _;
19
+ if (_ = accept(result.init)) initializers.unshift(_);
20
+ }
21
+ else if (_ = accept(result)) {
22
+ if (kind === "field") initializers.unshift(_);
23
+ else descriptor[key] = _;
24
+ }
25
+ }
26
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
27
+ done = true;
28
+ };
29
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
30
+ var useValue = arguments.length > 2;
31
+ for (var i = 0; i < initializers.length; i++) {
32
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
33
+ }
34
+ return useValue ? value : void 0;
35
+ };
36
+ Object.defineProperty(exports, "__esModule", { value: true });
37
+ exports.GrpcModule = void 0;
38
+ const common_1 = require("@nestjs/common");
39
+ const config_1 = require("@nestjs/config");
40
+ const grpc_constants_1 = require("./constants/grpc.constants");
41
+ const grpc_client_factory_1 = require("./factory/grpc-client.factory");
42
+ const grpc_registry_1 = require("./registry/grpc.registry");
43
+ let GrpcModule = (() => {
44
+ let _classDecorators = [(0, common_1.Module)({})];
45
+ let _classDescriptor;
46
+ let _classExtraInitializers = [];
47
+ let _classThis;
48
+ var GrpcModule = class {
49
+ static { _classThis = this; }
50
+ static {
51
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
52
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
53
+ GrpcModule = _classThis = _classDescriptor.value;
54
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
55
+ __runInitializers(_classThis, _classExtraInitializers);
56
+ }
57
+ static register(clients) {
58
+ return {
59
+ module: GrpcModule,
60
+ providers: [
61
+ grpc_client_factory_1.GrpcClientFactory,
62
+ ...clients.map(token => {
63
+ const cfg = grpc_registry_1.GRPC_CLIENTS[token];
64
+ return {
65
+ provide: `${grpc_constants_1.GRPC_CLIENT_PREFIX}_${token}`,
66
+ useFactory: (factory, config) => {
67
+ const url = config.getOrThrow(cfg.env);
68
+ const client = factory.createClient({
69
+ package: cfg.package,
70
+ protoPath: cfg.protoPath,
71
+ url
72
+ });
73
+ factory.register(token, client);
74
+ return client;
75
+ },
76
+ inject: [grpc_client_factory_1.GrpcClientFactory, config_1.ConfigService]
77
+ };
78
+ })
79
+ ],
80
+ exports: [
81
+ grpc_client_factory_1.GrpcClientFactory,
82
+ ...clients.map(token => `${grpc_constants_1.GRPC_CLIENT_PREFIX}_${token}`)
83
+ ]
84
+ };
85
+ }
86
+ };
87
+ return GrpcModule = _classThis;
88
+ })();
89
+ exports.GrpcModule = GrpcModule;
@@ -0,0 +1,2 @@
1
+ export * from './decorators';
2
+ export * from './grpc.module';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./decorators"), exports);
18
+ __exportStar(require("./grpc.module"), exports);
@@ -0,0 +1,17 @@
1
+ export declare const GRPC_CLIENTS: {
2
+ readonly AUTH_PACKAGE: {
3
+ readonly package: "auth.v1";
4
+ readonly protoPath: string;
5
+ readonly env: "AUTH_GRPC_URL";
6
+ };
7
+ readonly ACCOUNT_PACKAGE: {
8
+ readonly package: "account.v1";
9
+ readonly protoPath: string;
10
+ readonly env: "AUTH_GRPC_URL";
11
+ };
12
+ readonly USERS_PACKAGE: {
13
+ readonly package: "users.v1";
14
+ readonly protoPath: string;
15
+ readonly env: "USERS_GRPC_URL";
16
+ };
17
+ };
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GRPC_CLIENTS = void 0;
4
+ const contracts_1 = require("@teacinema/contracts");
5
+ exports.GRPC_CLIENTS = {
6
+ AUTH_PACKAGE: {
7
+ package: 'auth.v1',
8
+ protoPath: contracts_1.PROTO_PATHS.AUTH,
9
+ env: 'AUTH_GRPC_URL'
10
+ },
11
+ ACCOUNT_PACKAGE: {
12
+ package: 'account.v1',
13
+ protoPath: contracts_1.PROTO_PATHS.ACCOUNT,
14
+ env: 'AUTH_GRPC_URL'
15
+ },
16
+ USERS_PACKAGE: {
17
+ package: 'users.v1',
18
+ protoPath: contracts_1.PROTO_PATHS.USERS,
19
+ env: 'USERS_GRPC_URL'
20
+ }
21
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teacinema/common",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
4
4
  "description": "Core shared components for TeaCinema microservice ecosystem",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -19,5 +19,11 @@
19
19
  "@types/node": "^24.10.1",
20
20
  "prettier": "^3.7.3",
21
21
  "typescript": "^5.9.3"
22
+ },
23
+ "dependencies": {
24
+ "@nestjs/common": "^11.1.9",
25
+ "@nestjs/config": "^4.0.2",
26
+ "@nestjs/microservices": "^11.1.9",
27
+ "@teacinema/contracts": "^1.1.6"
22
28
  }
23
29
  }