@infineit-nestjs/services 1.0.9 → 1.0.11
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/dist/cjs/helper.module.js +1 -1
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/services/argon2.service.d.ts +5 -0
- package/dist/cjs/services/argon2.service.js +1 -0
- package/dist/cjs/services/entity.action.service.d.ts +41 -0
- package/dist/cjs/services/http.client.service.d.ts +19 -0
- package/dist/cjs/services/http.client.service.js +1 -0
- package/dist/cjs/services/index.d.ts +3 -0
- package/dist/cjs/services/index.js +1 -1
- package/dist/cjs/services/merge.service.d.ts +12 -0
- package/dist/cjs/services/merge.service.js +1 -0
- package/dist/es/helper.module.js +1 -1
- package/dist/es/index.d.ts +3 -0
- package/dist/es/index.js +1 -1
- package/dist/es/services/argon2.service.d.ts +5 -0
- package/dist/es/services/argon2.service.js +1 -0
- package/dist/es/services/entity.action.service.d.ts +41 -0
- package/dist/es/services/http.client.service.d.ts +19 -0
- package/dist/es/services/http.client.service.js +1 -0
- package/dist/es/services/index.d.ts +3 -0
- package/dist/es/services/index.js +1 -1
- package/dist/es/services/merge.service.d.ts +12 -0
- package/dist/es/services/merge.service.js +1 -0
- package/dist/types/{src/index.d.ts → index.d.ts} +3 -6
- package/dist/types/services/argon2.service.d.ts +7 -0
- package/dist/types/services/http.client.service.d.ts +22 -0
- package/dist/types/{src/services → services}/index.d.ts +3 -6
- package/dist/types/services/merge.service.d.ts +14 -0
- package/package.json +9 -3
- /package/dist/types/{src/helper.module.d.ts → helper.module.d.ts} +0 -0
- /package/dist/types/{src/services → services}/data.transformer.service.d.ts +0 -0
- /package/dist/types/{src/services → services}/logger.service.d.ts +0 -0
- /package/dist/types/{src/services → services}/message.formatter.service.d.ts +0 -0
- /package/dist/types/{src/services → services}/prisma.service.d.ts +0 -0
- /package/dist/types/{src/services → services}/validator.service.d.ts +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.HelperModule=void 0;const e=require("tslib"),r=require("@nestjs/axios"),s=require("@nestjs/common"),i=require("./services/data.transformer.service"),o=require("./services/logger.service"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.HelperModule=void 0;const e=require("tslib"),r=require("@nestjs/axios"),s=require("@nestjs/common"),i=require("./services/argon2.service"),t=require("./services/data.transformer.service"),o=require("./services/http.client.service"),c=require("./services/logger.service"),v=require("./services/merge.service"),a=require("./services/message.formatter.service"),l=require("./services/prisma.service"),u=require("./services/validator.service"),p=[i.Argon2Service,t.DataTransformerService,o.HttpServiceClient,c.LoggerService,v.MergeService,a.MessageFormatterService,l.PrismaService,u.ValidatorService];let d=class{};exports.HelperModule=d,exports.HelperModule=d=e.__decorate([(0,s.Global)(),(0,s.Module)({providers:p,imports:[r.HttpModule],exports:[...p,r.HttpModule]})],d);
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
export * from './helper.module';
|
|
2
|
+
export * from './services/argon2.service';
|
|
2
3
|
export * from './services/data.transformer.service';
|
|
4
|
+
export * from './services/http.client.service';
|
|
3
5
|
export * from './services/logger.service';
|
|
4
6
|
export * from './services/message.formatter.service';
|
|
7
|
+
export * from './services/merge.service';
|
|
5
8
|
export * from './services/prisma.service';
|
|
6
9
|
export * from './services/validator.service';
|
package/dist/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./helper.module"),exports),e.__exportStar(require("./services/data.transformer.service"),exports),e.__exportStar(require("./services/logger.service"),exports),e.__exportStar(require("./services/message.formatter.service"),exports),e.__exportStar(require("./services/prisma.service"),exports),e.__exportStar(require("./services/validator.service"),exports);
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./helper.module"),exports),e.__exportStar(require("./services/argon2.service"),exports),e.__exportStar(require("./services/data.transformer.service"),exports),e.__exportStar(require("./services/http.client.service"),exports),e.__exportStar(require("./services/logger.service"),exports),e.__exportStar(require("./services/message.formatter.service"),exports),e.__exportStar(require("./services/merge.service"),exports),e.__exportStar(require("./services/prisma.service"),exports),e.__exportStar(require("./services/validator.service"),exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Argon2Service=void 0;const r=require("tslib"),e=require("@nestjs/common"),t=r.__importStar(require("argon2"));let s=class{constructor(){}async hashPassword(r){try{return await t.hash(r,{type:t.argon2id,hashLength:32,timeCost:3,memoryCost:65536,parallelism:1})}catch(r){throw new Error(`Password hashing failed: ${r instanceof Error?r.message:String(r)}`)}}async verifyPassword(r,e){try{return await t.verify(e,r)}catch(r){throw new Error(`Password verification failed: ${r.message}`)}}};exports.Argon2Service=s,exports.Argon2Service=s=r.__decorate([(0,e.Injectable)(),r.__metadata("design:paramtypes",[])],s);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { LoggerService } from './logger.service';
|
|
2
|
+
import { PrismaService } from './prisma.service';
|
|
3
|
+
export declare class EntityActionService {
|
|
4
|
+
private prismaService;
|
|
5
|
+
private loggerService;
|
|
6
|
+
private readonly batchSize;
|
|
7
|
+
private readonly concurrencyLimit;
|
|
8
|
+
constructor(prismaService: PrismaService, loggerService: LoggerService);
|
|
9
|
+
handleEntityActions(entity: string, actionData: any[], idEntity: {
|
|
10
|
+
id: string;
|
|
11
|
+
value: any;
|
|
12
|
+
id_return: string;
|
|
13
|
+
id_table_primary_key: string;
|
|
14
|
+
}, query: any[]): Promise<{
|
|
15
|
+
entity: string;
|
|
16
|
+
message: string;
|
|
17
|
+
success: boolean;
|
|
18
|
+
totalProcessed?: number;
|
|
19
|
+
totalFailed?: number;
|
|
20
|
+
totalSucceeded?: number;
|
|
21
|
+
failedOperations?: {
|
|
22
|
+
success: boolean;
|
|
23
|
+
error?: string;
|
|
24
|
+
data?: any;
|
|
25
|
+
index: number;
|
|
26
|
+
retunrn_entity_name?: string;
|
|
27
|
+
}[];
|
|
28
|
+
succeededOperations?: {
|
|
29
|
+
success: boolean;
|
|
30
|
+
id_return: string;
|
|
31
|
+
data?: any;
|
|
32
|
+
index: number;
|
|
33
|
+
}[];
|
|
34
|
+
}>;
|
|
35
|
+
processEntities(entityResults: any[]): Promise<{
|
|
36
|
+
success: boolean;
|
|
37
|
+
errorsByEntity?: Record<string, any[]>;
|
|
38
|
+
}>;
|
|
39
|
+
private executeQuery;
|
|
40
|
+
private capitalize;
|
|
41
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ConfigService } from '@nestjs/config';
|
|
2
|
+
import { LoggerService } from './logger.service';
|
|
3
|
+
export declare class HttpServiceClient {
|
|
4
|
+
private readonly configService;
|
|
5
|
+
private readonly loggerService;
|
|
6
|
+
private circuitBreaker;
|
|
7
|
+
private requestCounter;
|
|
8
|
+
private responseTimeHistogram;
|
|
9
|
+
constructor(configService: ConfigService, loggerService: LoggerService);
|
|
10
|
+
private configureMetrics;
|
|
11
|
+
private configureAxiosRetry;
|
|
12
|
+
private configureCircuitBreaker;
|
|
13
|
+
private callService;
|
|
14
|
+
private handleServiceError;
|
|
15
|
+
getResource(service: string, method: 'get' | 'post' | 'patch' | 'delete', endpoint: string, data?: any): Promise<any>;
|
|
16
|
+
private getServiceBaseURL;
|
|
17
|
+
getMultipleResources(services: string[], method: 'get' | 'post' | 'patch' | 'delete', endpoint: string, data?: any): Promise<any[]>;
|
|
18
|
+
getMultipleResourcesInBatch(services: string[], method: 'get' | 'post' | 'patch' | 'delete', endpoint: string, data?: any): Promise<any[]>;
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.HttpServiceClient=void 0;const e=require("tslib"),r=require("@nestjs/common"),t=require("@nestjs/config"),s=e.__importDefault(require("axios")),i=e.__importDefault(require("axios-retry")),o=e.__importDefault(require("opossum")),c=require("prom-client"),a=require("./logger.service");let n=class{constructor(e,r){this.configService=e,this.loggerService=r,this.configureMetrics(),this.configureAxiosRetry(),this.configureCircuitBreaker()}configureMetrics(){this.requestCounter=new c.Counter({name:"service_client_requests_total",help:"Total number of requests made by the service client",labelNames:["service","method","status"]}),this.responseTimeHistogram=new c.Histogram({name:"service_client_response_time_seconds",help:"Response times of service client requests",labelNames:["service","method","status"],buckets:[.1,.5,1,2,5,10]})}configureAxiosRetry(){(0,i.default)(s.default,{retries:this.configService.getOrThrow("httpmicroservice.axios_retry_count"),retryDelay:e=>1e3*Math.pow(2,e),retryCondition:e=>i.default.isNetworkError(e)||!!e.response&&e.response.status>=500,onRetry:(e,r,t)=>{this.loggerService.warn(`Retry attempt #${e} for request to ${t.url}`,{sourceClass:this.constructor.name})}})}configureCircuitBreaker(){this.circuitBreaker=new o.default(this.callService.bind(this),{timeout:this.configService.getOrThrow("httpmicroservice.circuit_breaker_timeout"),errorThresholdPercentage:this.configService.getOrThrow("httpmicroservice.circuit_breaker_error_threshold"),resetTimeout:this.configService.getOrThrow("httpmicroservice.circuit_breaker_reset_timeout")}),this.circuitBreaker.fallback((e=>{this.loggerService.warn("Circuit breaker open. Fallback activated.",{sourceClass:this.constructor.name});return{message:"Service is currently unavailable. Please try again later.",error:e instanceof r.HttpException?e.getResponse():e.message}}))}async callService(e,r,t,i){const o={method:r,url:`${e}/${t}`,timeout:this.configService.getOrThrow("httpmicroservice.request_timeout")};"get"===r?o.params=i:o.data=i,this.requestCounter?.labels(e,r,"pending").inc();const c=Date.now();try{const i=await(0,s.default)(o),a=Date.now()-c;return this.responseTimeHistogram?.labels(e,r,"success").observe(a/1e3),this.requestCounter?.labels(e,r,"success").inc(),this.loggerService.info(`Successfully retrieved data from ${t} at ${e} in ${a}ms.`,{sourceClass:this.constructor.name}),i.data}catch(t){const s=Date.now()-c;throw this.responseTimeHistogram?.labels(e,r,"failure").observe(s/1e3),this.loggerService.error(`Error calling service at ${e}: ${t.message}`,{sourceClass:this.constructor.name}),this.handleServiceError(t)}}handleServiceError(e){if(e.response){const{status:t,data:s}=e.response;return new r.HttpException(s||"Service Error",t)}return e.request?new r.HttpException("Network Error",r.HttpStatus.SERVICE_UNAVAILABLE):new r.HttpException("Unexpected Error",r.HttpStatus.INTERNAL_SERVER_ERROR)}async getResource(e,t,s,i){const o=this.getServiceBaseURL(e);try{return await(this.circuitBreaker?.fire(o,t,s,i))}catch(t){const i=t;throw this.loggerService.error(`Error occurred while calling ${e} service at ${o}/${s}: ${i.message||i}`,{sourceClass:this.constructor.name}),new r.HttpException(`Service call to ${e} failed: ${i.message||"Service temporarily unavailable"}`,r.HttpStatus.SERVICE_UNAVAILABLE)}}getServiceBaseURL(e){const r=`httpmicroservice.${e}_microservice_url`,t=this.configService.getOrThrow(r);if(!t)throw new Error(`Service URL not found for service: ${e}. Please check your configuration.`);return t}async getMultipleResources(e,r,t,s){const i=e.map((e=>this.getResource(e,r,t,s)));return await Promise.all(i)}async getMultipleResourcesInBatch(e,t,s,i){let o=[];for(let c=0;c<e.length;c+=5){const a=e.slice(c,c+5);try{const e=a.map((e=>this.getResource(e,t,s,i))),r=await Promise.all(e);o=[...o,...r]}catch(e){throw this.loggerService.error("Error fetching data for batch",{sourceClass:this.constructor.name,error:e instanceof Error?e:new Error(String(e))}),new r.HttpException("Batch request failed",r.HttpStatus.INTERNAL_SERVER_ERROR)}}return o}};exports.HttpServiceClient=n,exports.HttpServiceClient=n=e.__decorate([(0,r.Injectable)(),e.__metadata("design:paramtypes",[t.ConfigService,a.LoggerService])],n);
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
export * from './argon2.service';
|
|
1
2
|
export * from './data.transformer.service';
|
|
3
|
+
export * from './http.client.service';
|
|
2
4
|
export * from './logger.service';
|
|
3
5
|
export * from './message.formatter.service';
|
|
6
|
+
export * from './merge.service';
|
|
4
7
|
export * from './prisma.service';
|
|
5
8
|
export * from './validator.service';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./data.transformer.service"),exports),e.__exportStar(require("./logger.service"),exports),e.__exportStar(require("./message.formatter.service"),exports),e.__exportStar(require("./prisma.service"),exports),e.__exportStar(require("./validator.service"),exports);
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./argon2.service"),exports),e.__exportStar(require("./data.transformer.service"),exports),e.__exportStar(require("./http.client.service"),exports),e.__exportStar(require("./logger.service"),exports),e.__exportStar(require("./message.formatter.service"),exports),e.__exportStar(require("./merge.service"),exports),e.__exportStar(require("./prisma.service"),exports),e.__exportStar(require("./validator.service"),exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare class MergeService {
|
|
2
|
+
mergeEntities<T extends Record<string, any>, R extends Record<string, any>>(baseData: T[], relatedDataMap: Record<string, any[]>, relationships: Record<string, {
|
|
3
|
+
foreignKey: keyof T;
|
|
4
|
+
filters?: Record<string, any>;
|
|
5
|
+
nestedRelationships?: Record<string, {
|
|
6
|
+
foreignKey: keyof R;
|
|
7
|
+
filters?: Record<string, any>;
|
|
8
|
+
}>;
|
|
9
|
+
}>, options?: {
|
|
10
|
+
flatten?: boolean;
|
|
11
|
+
}): Promise<T[]>;
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.MergeService=void 0;const e=require("tslib"),t=require("@nestjs/common");let r=class{async mergeEntities(e,t,r,n={}){const s={};for(const[e,n]of Object.entries(t)){const t=r[e]?.foreignKey;t&&(s[e]=new Map(n.map((e=>[String(e[t]),e]))))}const o=[];for(const i of e){let e={...i};for(const[o,c]of Object.entries(r)){const{foreignKey:r,nestedRelationships:f,filters:a}=c,l=String(i[r]);let y=s[o]?.get(l);if(a){Object.keys(a).every((e=>{const t=a[e];return Array.isArray(t)?t.includes(y?.[e]):"function"==typeof t?t(y?.[e]):y?.[e]===t}))||(y=void 0)}if(y&&(n.flatten?e={...e,...y}:e[o]={...y},f))for(const[r,s]of Object.entries(f))try{const i=String(y[s.foreignKey]),c=new Map(t[r].map((e=>[e.id_timezone,e]))).get(i);if(s.filters){if(!Object.keys(s.filters).every((e=>{const t=s.filters?.[e]??void 0,r=c?.[e];return Array.isArray(t)?t.includes(r):"function"==typeof t?t(r):r===t})))continue}c&&(n.flatten?e={...e,...c}:e[o][r]=c)}catch(e){}}o.push(e)}return o}};exports.MergeService=r,exports.MergeService=r=e.__decorate([(0,t.Injectable)()],r);
|
package/dist/es/helper.module.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,"__esModule",{value:!0}),exports.HelperModule=void 0;const e=require("tslib"),r=require("@nestjs/axios"),s=require("@nestjs/common"),i=require("./services/data.transformer.service"),o=require("./services/logger.service"),
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0}),exports.HelperModule=void 0;const e=require("tslib"),r=require("@nestjs/axios"),s=require("@nestjs/common"),i=require("./services/argon2.service"),t=require("./services/data.transformer.service"),o=require("./services/http.client.service"),c=require("./services/logger.service"),v=require("./services/merge.service"),a=require("./services/message.formatter.service"),l=require("./services/prisma.service"),u=require("./services/validator.service"),p=[i.Argon2Service,t.DataTransformerService,o.HttpServiceClient,c.LoggerService,v.MergeService,a.MessageFormatterService,l.PrismaService,u.ValidatorService];let d=class{};exports.HelperModule=d,exports.HelperModule=d=e.__decorate([(0,s.Global)(),(0,s.Module)({providers:p,imports:[r.HttpModule],exports:[...p,r.HttpModule]})],d);
|
package/dist/es/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
export * from './helper.module';
|
|
2
|
+
export * from './services/argon2.service';
|
|
2
3
|
export * from './services/data.transformer.service';
|
|
4
|
+
export * from './services/http.client.service';
|
|
3
5
|
export * from './services/logger.service';
|
|
4
6
|
export * from './services/message.formatter.service';
|
|
7
|
+
export * from './services/merge.service';
|
|
5
8
|
export * from './services/prisma.service';
|
|
6
9
|
export * from './services/validator.service';
|
package/dist/es/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./helper.module"),exports),e.__exportStar(require("./services/data.transformer.service"),exports),e.__exportStar(require("./services/logger.service"),exports),e.__exportStar(require("./services/message.formatter.service"),exports),e.__exportStar(require("./services/prisma.service"),exports),e.__exportStar(require("./services/validator.service"),exports);
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./helper.module"),exports),e.__exportStar(require("./services/argon2.service"),exports),e.__exportStar(require("./services/data.transformer.service"),exports),e.__exportStar(require("./services/http.client.service"),exports),e.__exportStar(require("./services/logger.service"),exports),e.__exportStar(require("./services/message.formatter.service"),exports),e.__exportStar(require("./services/merge.service"),exports),e.__exportStar(require("./services/prisma.service"),exports),e.__exportStar(require("./services/validator.service"),exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0}),exports.Argon2Service=void 0;const r=require("tslib"),e=require("@nestjs/common"),t=r.__importStar(require("argon2"));let a=class{constructor(){}async hashPassword(r){try{return await t.hash(r,{type:t.argon2id,hashLength:32,timeCost:3,memoryCost:65536,parallelism:1})}catch(r){throw new Error(`Password hashing failed: ${r instanceof Error?r.message:String(r)}`)}}async verifyPassword(r,e){try{return await t.verify(e,r)}catch(r){throw new Error(`Password verification failed: ${r.message}`)}}};exports.Argon2Service=a,exports.Argon2Service=a=r.__decorate([(0,e.Injectable)(),r.__metadata("design:paramtypes",[])],a);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { LoggerService } from './logger.service';
|
|
2
|
+
import { PrismaService } from './prisma.service';
|
|
3
|
+
export declare class EntityActionService {
|
|
4
|
+
private prismaService;
|
|
5
|
+
private loggerService;
|
|
6
|
+
private readonly batchSize;
|
|
7
|
+
private readonly concurrencyLimit;
|
|
8
|
+
constructor(prismaService: PrismaService, loggerService: LoggerService);
|
|
9
|
+
handleEntityActions(entity: string, actionData: any[], idEntity: {
|
|
10
|
+
id: string;
|
|
11
|
+
value: any;
|
|
12
|
+
id_return: string;
|
|
13
|
+
id_table_primary_key: string;
|
|
14
|
+
}, query: any[]): Promise<{
|
|
15
|
+
entity: string;
|
|
16
|
+
message: string;
|
|
17
|
+
success: boolean;
|
|
18
|
+
totalProcessed?: number;
|
|
19
|
+
totalFailed?: number;
|
|
20
|
+
totalSucceeded?: number;
|
|
21
|
+
failedOperations?: {
|
|
22
|
+
success: boolean;
|
|
23
|
+
error?: string;
|
|
24
|
+
data?: any;
|
|
25
|
+
index: number;
|
|
26
|
+
retunrn_entity_name?: string;
|
|
27
|
+
}[];
|
|
28
|
+
succeededOperations?: {
|
|
29
|
+
success: boolean;
|
|
30
|
+
id_return: string;
|
|
31
|
+
data?: any;
|
|
32
|
+
index: number;
|
|
33
|
+
}[];
|
|
34
|
+
}>;
|
|
35
|
+
processEntities(entityResults: any[]): Promise<{
|
|
36
|
+
success: boolean;
|
|
37
|
+
errorsByEntity?: Record<string, any[]>;
|
|
38
|
+
}>;
|
|
39
|
+
private executeQuery;
|
|
40
|
+
private capitalize;
|
|
41
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ConfigService } from '@nestjs/config';
|
|
2
|
+
import { LoggerService } from './logger.service';
|
|
3
|
+
export declare class HttpServiceClient {
|
|
4
|
+
private readonly configService;
|
|
5
|
+
private readonly loggerService;
|
|
6
|
+
private circuitBreaker;
|
|
7
|
+
private requestCounter;
|
|
8
|
+
private responseTimeHistogram;
|
|
9
|
+
constructor(configService: ConfigService, loggerService: LoggerService);
|
|
10
|
+
private configureMetrics;
|
|
11
|
+
private configureAxiosRetry;
|
|
12
|
+
private configureCircuitBreaker;
|
|
13
|
+
private callService;
|
|
14
|
+
private handleServiceError;
|
|
15
|
+
getResource(service: string, method: 'get' | 'post' | 'patch' | 'delete', endpoint: string, data?: any): Promise<any>;
|
|
16
|
+
private getServiceBaseURL;
|
|
17
|
+
getMultipleResources(services: string[], method: 'get' | 'post' | 'patch' | 'delete', endpoint: string, data?: any): Promise<any[]>;
|
|
18
|
+
getMultipleResourcesInBatch(services: string[], method: 'get' | 'post' | 'patch' | 'delete', endpoint: string, data?: any): Promise<any[]>;
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0}),exports.HttpServiceClient=void 0;const e=require("tslib"),r=require("@nestjs/common"),t=require("@nestjs/config"),s=e.__importDefault(require("axios")),i=e.__importDefault(require("axios-retry")),o=e.__importDefault(require("opossum")),c=require("prom-client"),a=require("./logger.service");let n=class{constructor(e,r){this.configService=e,this.loggerService=r,this.configureMetrics(),this.configureAxiosRetry(),this.configureCircuitBreaker()}configureMetrics(){this.requestCounter=new c.Counter({name:"service_client_requests_total",help:"Total number of requests made by the service client",labelNames:["service","method","status"]}),this.responseTimeHistogram=new c.Histogram({name:"service_client_response_time_seconds",help:"Response times of service client requests",labelNames:["service","method","status"],buckets:[.1,.5,1,2,5,10]})}configureAxiosRetry(){(0,i.default)(s.default,{retries:this.configService.getOrThrow("httpmicroservice.axios_retry_count"),retryDelay:e=>1e3*Math.pow(2,e),retryCondition:e=>i.default.isNetworkError(e)||!!e.response&&e.response.status>=500,onRetry:(e,r,t)=>{this.loggerService.warn(`Retry attempt #${e} for request to ${t.url}`,{sourceClass:this.constructor.name})}})}configureCircuitBreaker(){this.circuitBreaker=new o.default(this.callService.bind(this),{timeout:this.configService.getOrThrow("httpmicroservice.circuit_breaker_timeout"),errorThresholdPercentage:this.configService.getOrThrow("httpmicroservice.circuit_breaker_error_threshold"),resetTimeout:this.configService.getOrThrow("httpmicroservice.circuit_breaker_reset_timeout")}),this.circuitBreaker.fallback((e=>{this.loggerService.warn("Circuit breaker open. Fallback activated.",{sourceClass:this.constructor.name});return{message:"Service is currently unavailable. Please try again later.",error:e instanceof r.HttpException?e.getResponse():e.message}}))}async callService(e,r,t,i){const o={method:r,url:`${e}/${t}`,timeout:this.configService.getOrThrow("httpmicroservice.request_timeout")};"get"===r?o.params=i:o.data=i,this.requestCounter?.labels(e,r,"pending").inc();const c=Date.now();try{const i=await(0,s.default)(o),a=Date.now()-c;return this.responseTimeHistogram?.labels(e,r,"success").observe(a/1e3),this.requestCounter?.labels(e,r,"success").inc(),this.loggerService.info(`Successfully retrieved data from ${t} at ${e} in ${a}ms.`,{sourceClass:this.constructor.name}),i.data}catch(t){const s=Date.now()-c;throw this.responseTimeHistogram?.labels(e,r,"failure").observe(s/1e3),this.loggerService.error(`Error calling service at ${e}: ${t.message}`,{sourceClass:this.constructor.name}),this.handleServiceError(t)}}handleServiceError(e){if(e.response){const{status:t,data:s}=e.response;return new r.HttpException(s||"Service Error",t)}return e.request?new r.HttpException("Network Error",r.HttpStatus.SERVICE_UNAVAILABLE):new r.HttpException("Unexpected Error",r.HttpStatus.INTERNAL_SERVER_ERROR)}async getResource(e,t,s,i){const o=this.getServiceBaseURL(e);try{return await(this.circuitBreaker?.fire(o,t,s,i))}catch(t){const i=t;throw this.loggerService.error(`Error occurred while calling ${e} service at ${o}/${s}: ${i.message||i}`,{sourceClass:this.constructor.name}),new r.HttpException(`Service call to ${e} failed: ${i.message||"Service temporarily unavailable"}`,r.HttpStatus.SERVICE_UNAVAILABLE)}}getServiceBaseURL(e){const r=`httpmicroservice.${e}_microservice_url`,t=this.configService.getOrThrow(r);if(!t)throw new Error(`Service URL not found for service: ${e}. Please check your configuration.`);return t}async getMultipleResources(e,r,t,s){const i=e.map((e=>this.getResource(e,r,t,s)));return await Promise.all(i)}async getMultipleResourcesInBatch(e,t,s,i){let o=[];for(let c=0;c<e.length;c+=5){const a=e.slice(c,c+5);try{const e=a.map((e=>this.getResource(e,t,s,i))),r=await Promise.all(e);o=[...o,...r]}catch(e){throw this.loggerService.error("Error fetching data for batch",{sourceClass:this.constructor.name,error:e instanceof Error?e:new Error(String(e))}),new r.HttpException("Batch request failed",r.HttpStatus.INTERNAL_SERVER_ERROR)}}return o}};exports.HttpServiceClient=n,exports.HttpServiceClient=n=e.__decorate([(0,r.Injectable)(),e.__metadata("design:paramtypes",[t.ConfigService,a.LoggerService])],n);
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
export * from './argon2.service';
|
|
1
2
|
export * from './data.transformer.service';
|
|
3
|
+
export * from './http.client.service';
|
|
2
4
|
export * from './logger.service';
|
|
3
5
|
export * from './message.formatter.service';
|
|
6
|
+
export * from './merge.service';
|
|
4
7
|
export * from './prisma.service';
|
|
5
8
|
export * from './validator.service';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./data.transformer.service"),exports),e.__exportStar(require("./logger.service"),exports),e.__exportStar(require("./message.formatter.service"),exports),e.__exportStar(require("./prisma.service"),exports),e.__exportStar(require("./validator.service"),exports);
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./argon2.service"),exports),e.__exportStar(require("./data.transformer.service"),exports),e.__exportStar(require("./http.client.service"),exports),e.__exportStar(require("./logger.service"),exports),e.__exportStar(require("./message.formatter.service"),exports),e.__exportStar(require("./merge.service"),exports),e.__exportStar(require("./prisma.service"),exports),e.__exportStar(require("./validator.service"),exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare class MergeService {
|
|
2
|
+
mergeEntities<T extends Record<string, any>, R extends Record<string, any>>(baseData: T[], relatedDataMap: Record<string, any[]>, relationships: Record<string, {
|
|
3
|
+
foreignKey: keyof T;
|
|
4
|
+
filters?: Record<string, any>;
|
|
5
|
+
nestedRelationships?: Record<string, {
|
|
6
|
+
foreignKey: keyof R;
|
|
7
|
+
filters?: Record<string, any>;
|
|
8
|
+
}>;
|
|
9
|
+
}>, options?: {
|
|
10
|
+
flatten?: boolean;
|
|
11
|
+
}): Promise<T[]>;
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0}),exports.MergeService=void 0;const e=require("tslib"),t=require("@nestjs/common");let r=class{async mergeEntities(e,t,r,n={}){const o={};for(const[e,n]of Object.entries(t)){const t=r[e]?.foreignKey;t&&(o[e]=new Map(n.map((e=>[String(e[t]),e]))))}const s=[];for(const i of e){let e={...i};for(const[s,c]of Object.entries(r)){const{foreignKey:r,nestedRelationships:f,filters:a}=c,l=String(i[r]);let y=o[s]?.get(l);if(a){Object.keys(a).every((e=>{const t=a[e];return Array.isArray(t)?t.includes(y?.[e]):"function"==typeof t?t(y?.[e]):y?.[e]===t}))||(y=void 0)}if(y&&(n.flatten?e={...e,...y}:e[s]={...y},f))for(const[r,o]of Object.entries(f))try{const i=String(y[o.foreignKey]),c=new Map(t[r].map((e=>[e.id_timezone,e]))).get(i);if(o.filters){if(!Object.keys(o.filters).every((e=>{const t=o.filters?.[e]??void 0,r=c?.[e];return Array.isArray(t)?t.includes(r):"function"==typeof t?t(r):r===t})))continue}c&&(n.flatten?e={...e,...c}:e[s][r]=c)}catch(e){}}s.push(e)}return s}};exports.MergeService=r,exports.MergeService=r=e.__decorate([(0,t.Injectable)()],r);
|
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
export { HelperModule } from './helper.module.js';
|
|
2
|
+
export { Argon2Service } from './services/argon2.service.js';
|
|
2
3
|
export { DataTransformerService } from './services/data.transformer.service.js';
|
|
4
|
+
export { HttpServiceClient } from './services/http.client.service.js';
|
|
3
5
|
export { ILogData, LogLevel, LoggerService } from './services/logger.service.js';
|
|
4
6
|
export { MessageFormatterService } from './services/message.formatter.service.js';
|
|
7
|
+
export { MergeService } from './services/merge.service.js';
|
|
5
8
|
export { PrismaService } from './services/prisma.service.js';
|
|
6
9
|
export { ValidatorService } from './services/validator.service.js';
|
|
7
|
-
import '@infineit/winston-logger/logger/domain/logger';
|
|
8
|
-
import '@nestjs/common';
|
|
9
|
-
import '@nestjs/config';
|
|
10
|
-
import '@nestjs/terminus';
|
|
11
|
-
import '@infineit-nestjs/types';
|
|
12
|
-
import '@prisma/client';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ConfigService } from '@nestjs/config';
|
|
2
|
+
import { LoggerService } from './logger.service.js';
|
|
3
|
+
|
|
4
|
+
declare class HttpServiceClient {
|
|
5
|
+
private readonly configService;
|
|
6
|
+
private readonly loggerService;
|
|
7
|
+
private circuitBreaker;
|
|
8
|
+
private requestCounter;
|
|
9
|
+
private responseTimeHistogram;
|
|
10
|
+
constructor(configService: ConfigService, loggerService: LoggerService);
|
|
11
|
+
private configureMetrics;
|
|
12
|
+
private configureAxiosRetry;
|
|
13
|
+
private configureCircuitBreaker;
|
|
14
|
+
private callService;
|
|
15
|
+
private handleServiceError;
|
|
16
|
+
getResource(service: string, method: 'get' | 'post' | 'patch' | 'delete', endpoint: string, data?: any): Promise<any>;
|
|
17
|
+
private getServiceBaseURL;
|
|
18
|
+
getMultipleResources(services: string[], method: 'get' | 'post' | 'patch' | 'delete', endpoint: string, data?: any): Promise<any[]>;
|
|
19
|
+
getMultipleResourcesInBatch(services: string[], method: 'get' | 'post' | 'patch' | 'delete', endpoint: string, data?: any): Promise<any[]>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { HttpServiceClient };
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
+
export { Argon2Service } from './argon2.service.js';
|
|
1
2
|
export { DataTransformerService } from './data.transformer.service.js';
|
|
3
|
+
export { HttpServiceClient } from './http.client.service.js';
|
|
2
4
|
export { ILogData, LogLevel, LoggerService } from './logger.service.js';
|
|
3
5
|
export { MessageFormatterService } from './message.formatter.service.js';
|
|
6
|
+
export { MergeService } from './merge.service.js';
|
|
4
7
|
export { PrismaService } from './prisma.service.js';
|
|
5
8
|
export { ValidatorService } from './validator.service.js';
|
|
6
|
-
import '@infineit/winston-logger/logger/domain/logger';
|
|
7
|
-
import '@nestjs/common';
|
|
8
|
-
import '@nestjs/config';
|
|
9
|
-
import '@nestjs/terminus';
|
|
10
|
-
import '@infineit-nestjs/types';
|
|
11
|
-
import '@prisma/client';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
declare class MergeService {
|
|
2
|
+
mergeEntities<T extends Record<string, any>, R extends Record<string, any>>(baseData: T[], relatedDataMap: Record<string, any[]>, relationships: Record<string, {
|
|
3
|
+
foreignKey: keyof T;
|
|
4
|
+
filters?: Record<string, any>;
|
|
5
|
+
nestedRelationships?: Record<string, {
|
|
6
|
+
foreignKey: keyof R;
|
|
7
|
+
filters?: Record<string, any>;
|
|
8
|
+
}>;
|
|
9
|
+
}>, options?: {
|
|
10
|
+
flatten?: boolean;
|
|
11
|
+
}): Promise<T[]>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { MergeService };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@infineit-nestjs/services",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
4
4
|
"main": "./dist/cjs/index.js",
|
|
5
5
|
"module": "./dist/es/index.mjs",
|
|
6
6
|
"exports": {
|
|
@@ -29,7 +29,11 @@
|
|
|
29
29
|
"@nestjs/axios": "^4.0.0",
|
|
30
30
|
"@nestjs/terminus": "^11.0.0",
|
|
31
31
|
"@prisma/client": "^6.5.0",
|
|
32
|
-
"
|
|
32
|
+
"argon2": "^0.41.1",
|
|
33
|
+
"axios-retry": "^4.5.0",
|
|
34
|
+
"lodash": "^4.17.21",
|
|
35
|
+
"opossum": "^8.4.0",
|
|
36
|
+
"prom-client": "^15.1.3"
|
|
33
37
|
},
|
|
34
38
|
"devDependencies": {
|
|
35
39
|
"@nestjs/common": "^11.0.11",
|
|
@@ -44,6 +48,7 @@
|
|
|
44
48
|
"@rollup/plugin-typescript": "^12.1.2",
|
|
45
49
|
"@rollup/plugin-url": "^8.0.2",
|
|
46
50
|
"@types/lodash": "^4",
|
|
51
|
+
"@types/opossum": "^8",
|
|
47
52
|
"@types/rollup-plugin-peer-deps-external": "^2",
|
|
48
53
|
"@types/terser": "^3.12.0",
|
|
49
54
|
"axios": "^1.8.3",
|
|
@@ -53,6 +58,7 @@
|
|
|
53
58
|
"reflect-metadata": "^0.2.2",
|
|
54
59
|
"rollup": "^4.34.8",
|
|
55
60
|
"rollup-plugin-dts": "^6.1.1",
|
|
61
|
+
"rollup-plugin-multi-input": "^1.5.0",
|
|
56
62
|
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
57
63
|
"rollup-plugin-preserve-directives": "^0.4.0",
|
|
58
64
|
"rxjs": "^7.8.2",
|
|
@@ -74,5 +80,5 @@
|
|
|
74
80
|
"publishConfig": {
|
|
75
81
|
"access": "public"
|
|
76
82
|
},
|
|
77
|
-
"gitHead": "
|
|
83
|
+
"gitHead": "00f2ecdac88859957d60b018a6e7118ae6da974b"
|
|
78
84
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|