@bracket-games/common 1.1.5 → 1.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.
@@ -0,0 +1,13 @@
1
+ import type { OnModuleInit } from "@nestjs/common";
2
+ import type { ClientGrpc } from "@nestjs/microservices";
3
+ import { type Observable } from "rxjs";
4
+ type UnwrapObservable<U> = U extends Observable<infer R> ? R : U;
5
+ export declare abstract class AbstractClientGrpc<T extends Record<string, any>> implements OnModuleInit {
6
+ private readonly client;
7
+ private readonly serviceName;
8
+ protected service: T;
9
+ protected constructor(client: ClientGrpc, serviceName: string);
10
+ onModuleInit(): void;
11
+ call<K extends keyof T>(method: K, payload: Parameters<T[K]>[0]): Promise<UnwrapObservable<ReturnType<T[K]>>>;
12
+ }
13
+ export {};
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AbstractClientGrpc = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ class AbstractClientGrpc {
6
+ client;
7
+ serviceName;
8
+ service;
9
+ constructor(client, serviceName) {
10
+ this.client = client;
11
+ this.serviceName = serviceName;
12
+ }
13
+ onModuleInit() {
14
+ this.service = this.client.getService(this.serviceName);
15
+ if (!this.service) {
16
+ console.error(`Service [${this.serviceName}] not found`);
17
+ }
18
+ else {
19
+ console.log(`${this.serviceName}: [${Object.keys(this.service)}]`);
20
+ }
21
+ }
22
+ async call(method, payload) {
23
+ try {
24
+ if (!this.service) {
25
+ throw new Error(`Service ${this.serviceName} is not initialized`);
26
+ }
27
+ const methodFunc = this.service[method];
28
+ if (typeof methodFunc !== 'function') {
29
+ throw new Error(`Method ${String(method)} not found in service ${this.serviceName}. Available methods: ${Object.keys(this.service).join(', ')}`);
30
+ }
31
+ const observable = methodFunc.call(this.service, payload);
32
+ if (!observable) {
33
+ throw new Error(`Method ${String(method)} returned undefined observable`);
34
+ }
35
+ const result = await (0, rxjs_1.lastValueFrom)(observable);
36
+ return result;
37
+ }
38
+ catch (error) {
39
+ console.error('Error in gRPC call:', error);
40
+ throw error;
41
+ }
42
+ }
43
+ }
44
+ exports.AbstractClientGrpc = AbstractClientGrpc;
@@ -1 +1,3 @@
1
1
  export declare function convertEnum<T extends object>(target: T, value: string): T[keyof T];
2
+ export declare function convertEnumToNumber<T extends object>(target: T, value: string): T[keyof T];
3
+ export declare function convertNumberToEnum<T extends object>(target: number, value: T): string;
@@ -1,6 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.convertEnum = convertEnum;
4
+ exports.convertEnumToNumber = convertEnumToNumber;
5
+ exports.convertNumberToEnum = convertNumberToEnum;
4
6
  function convertEnum(target, value) {
5
7
  return target[value];
6
8
  }
9
+ function convertEnumToNumber(target, value) {
10
+ return target[value];
11
+ }
12
+ function convertNumberToEnum(target, value) {
13
+ return Object.keys(value).find(key => value[key] === target);
14
+ }
@@ -0,0 +1 @@
1
+ export declare const grpcToHttpStatus: Record<number, number>;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.grpcToHttpStatus = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ const enums_1 = require("../enums");
6
+ exports.grpcToHttpStatus = {
7
+ [enums_1.RpcStatus.OK]: common_1.HttpStatus.OK,
8
+ [enums_1.RpcStatus.CANCELLED]: 499,
9
+ [enums_1.RpcStatus.UNKNOWN]: common_1.HttpStatus.INTERNAL_SERVER_ERROR,
10
+ [enums_1.RpcStatus.INVALID_ARGUMENT]: common_1.HttpStatus.BAD_REQUEST,
11
+ [enums_1.RpcStatus.DEADLINE_EXCEEDED]: common_1.HttpStatus.GATEWAY_TIMEOUT,
12
+ [enums_1.RpcStatus.NOT_FOUND]: common_1.HttpStatus.NOT_FOUND,
13
+ [enums_1.RpcStatus.ALREADY_EXISTS]: common_1.HttpStatus.CONFLICT,
14
+ [enums_1.RpcStatus.PERMISSION_DENIED]: common_1.HttpStatus.FORBIDDEN,
15
+ [enums_1.RpcStatus.RESOURCE_EXHAUSTED]: common_1.HttpStatus.TOO_MANY_REQUESTS,
16
+ [enums_1.RpcStatus.FAILED_PRECONDITION]: common_1.HttpStatus.BAD_REQUEST,
17
+ [enums_1.RpcStatus.ABORTED]: common_1.HttpStatus.CONFLICT,
18
+ [enums_1.RpcStatus.OUT_OF_RANGE]: common_1.HttpStatus.BAD_REQUEST,
19
+ [enums_1.RpcStatus.UNIMPLEMENTED]: common_1.HttpStatus.NOT_IMPLEMENTED,
20
+ [enums_1.RpcStatus.INTERNAL]: common_1.HttpStatus.INTERNAL_SERVER_ERROR,
21
+ [enums_1.RpcStatus.UNAVAILABLE]: common_1.HttpStatus.SERVICE_UNAVAILABLE,
22
+ [enums_1.RpcStatus.DATA_LOSS]: common_1.HttpStatus.INTERNAL_SERVER_ERROR,
23
+ };
@@ -1 +1,2 @@
1
1
  export * from './convert-enum.util';
2
+ export * from './grpc-to-http-status.util';
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./convert-enum.util"), exports);
18
+ __exportStar(require("./grpc-to-http-status.util"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bracket-games/common",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "description": "Common utils",
5
5
  "publishConfig": {
6
6
  "access": "public"