From 345eea756e13b567f6c4a9c7bd894e2a548c2c77 Mon Sep 17 00:00:00 2001 From: Deeptanshu Singh Rathore Date: Wed, 17 Aug 2022 15:08:48 +0530 Subject: [PATCH 01/14] added errortype in the error response to get populated in the response --- src/http/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/http/index.js b/src/http/index.js index 1bcf01f..9409cc2 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -30,13 +30,14 @@ class HttpCommunication { const { method, route, request } = params; if (response.status >= 400) { if (response.data) { - const { error } = response.data; - throw new QError(error, `${this.name}_internal_service.RESPONSE_ERROR`, { + const { error, errorType = 'server.UKW' } = response.data; + throw new QError(error, errorType, { service: this.name, data: response.data, request, method, route, + type: errorType, } ); } From b0396c2a77653856364492b08a05085a2750fa64 Mon Sep 17 00:00:00 2001 From: Deeptanshu Singh Rathore Date: Wed, 17 Aug 2022 15:31:30 +0530 Subject: [PATCH 02/14] added population and addition of headers in request --- src/http/index.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/http/index.js b/src/http/index.js index 9409cc2..1333b1e 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -61,6 +61,17 @@ class HttpCommunication { return finalURL.toString(); } + populateHeadersFromContext(ctx) { + const customHeaders = { + 'X-Q-TRACEID': ctx && ctx.traceId ? ctx.traceId : uuid(), + }; + if (ctx) { + if (ctx.userId) headers['X-Q-USERID'] = ctx.userId; + if (ctx.ab) headers['Q-AB-ROUTE'] = ctx.ab; + } + return customHeaders; + } + async makeReqeust(params) { const { route, method, request } = params; const requestURL = this.createRequestURL(route, request.query); @@ -70,7 +81,7 @@ class HttpCommunication { if (requestContext) { this.axiosConfig.headers = { ...this.axiosConfig.headers, - // select which headers we want to pass and pass them + ...this.populateHeadersFromContext(requestContext), } } From bd2db61581daed83b92e000d9f14d660e10bc2b3 Mon Sep 17 00:00:00 2001 From: Deeptanshu Singh Rathore Date: Thu, 18 Aug 2022 10:29:24 +0530 Subject: [PATCH 03/14] added delete and patch methods as well --- src/http/index.js | 28 +++++++++++++++++++++++----- types/index.d.ts | 30 ++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/http/index.js b/src/http/index.js index 1333b1e..4b5a894 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -67,12 +67,12 @@ class HttpCommunication { }; if (ctx) { if (ctx.userId) headers['X-Q-USERID'] = ctx.userId; - if (ctx.ab) headers['Q-AB-ROUTE'] = ctx.ab; + if (ctx.ab) headers['X-Q-AB-ROUTE'] = ctx.ab; } return customHeaders; } - async makeReqeust(params) { + async makeRequest(params) { const { route, method, request } = params; const requestURL = this.createRequestURL(route, request.query); let response; @@ -103,7 +103,7 @@ class HttpCommunication { } async post(route, request) { - const data = await this.makeReqeust({ + const data = await this.makeRequest({ method: 'post', route, request, @@ -112,7 +112,7 @@ class HttpCommunication { } async put(route, request) { - const data = await this.makeReqeust({ + const data = await this.makeRequest({ method: 'put', route, request, @@ -120,8 +120,26 @@ class HttpCommunication { return data; } + async patch(route, request) { + const data = await this.makeRequest({ + method: 'patch', + route, + request, + }); + return data; + } + + async delete(route, request) { + const data = await this.makeRequest({ + method: 'delete', + route, + request, + }); + return data; + } + async get(route, request = {}) { - const data = await this.makeReqeust({ + const data = await this.makeRequest({ method: 'get', route, request, diff --git a/types/index.d.ts b/types/index.d.ts index 781f0a2..9ff7866 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -11,8 +11,8 @@ export class HttpCommunication { /** * Http Get Request - * @param route - * @param request + * @param route + * @param request * @typeParam T - Response type provided by the callee * @returns Returns response of the get request */ @@ -20,17 +20,35 @@ export class HttpCommunication { /** * Http Put Request - * @param route - * @param request + * @param route + * @param request * @typeParam T - Response type provided by the callee * @returns Returns response of the put request */ put(route: string, request: { query: Record, body: Record }): Promise; + /** + * Http Delete Request + * @param route + * @param request + * @typeParam T - Response type provided by the callee + * @returns Returns response of the put request + */ + delete(route: string, request: { query: Record, body: Record }): Promise; + + /** + * Http Patch Request + * @param route + * @param request + * @typeParam T - Response type provided by the callee + * @returns Returns response of the put request + */ + patch(route: string, request: { query: Record, body: Record }): Promise; + /** * Http Post Request - * @param route - * @param request + * @param route + * @param request * @typeParam T - Response type provided by the callee * @returns Returns response of the post request */ From c948286764564c62bcc81bddba5f935d349dc89d Mon Sep 17 00:00:00 2001 From: Deeptanshu Singh Rathore Date: Tue, 23 Aug 2022 13:06:35 +0530 Subject: [PATCH 04/14] updated the review changes --- src/http/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/http/index.js b/src/http/index.js index 4b5a894..d043d46 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -63,11 +63,11 @@ class HttpCommunication { populateHeadersFromContext(ctx) { const customHeaders = { - 'X-Q-TRACEID': ctx && ctx.traceId ? ctx.traceId : uuid(), + 'X-Q-TRACEID': (ctx && ctx.traceId) ? ctx.traceId : uuid(), }; if (ctx) { - if (ctx.userId) headers['X-Q-USERID'] = ctx.userId; - if (ctx.ab) headers['X-Q-AB-ROUTE'] = ctx.ab; + if (ctx.userId) customHeaders['X-Q-USERID'] = ctx.userId; + if (ctx.ab) customHeaders['X-Q-AB-ROUTE'] = ctx.ab; } return customHeaders; } From 3dddb134af3f2b9d798246144a3e05fd678b5667 Mon Sep 17 00:00:00 2001 From: Deeptanshu Singh Rathore Date: Fri, 26 Aug 2022 10:00:44 +0530 Subject: [PATCH 05/14] added static method to generate requestContext --- src/http/index.js | 9 +++++++++ types/index.d.ts | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/src/http/index.js b/src/http/index.js index d043d46..7ead846 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -26,6 +26,15 @@ class HttpCommunication { this.contextStorage = contextStorage; } + static getRequestContext(req, customContextValue) { + return { + traceId: (req.headers && req.headers['X-Q-TRACEID']) ? req.headers['X-Q-TRACEID'] : uuid(), + userId: (req.user && req.user.userId) ? String(req.user.userId) : null, + ab: (req.headers && req.headers['Q-AB-ROUTE']) ? req.headers['Q-AB-ROUTE'] : null, + ...customContextValue, + }; + } + handleError(params, response) { const { method, route, request } = params; if (response.status >= 400) { diff --git a/types/index.d.ts b/types/index.d.ts index 9ff7866..af1cfc8 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -9,6 +9,14 @@ export class HttpCommunication { */ constructor({ name, axiosConfig, contextStorage } : { name: string, axiosConfig?: AxiosRequestConfig, contextStorage?: any }); + /** + * Function to generate the context object + * @param req {Express Request} + * @param customContextValue any custom values that you want to store in the context + * Return extracted values from the req headers and any custom values pass to generate the context object + */ + static getRequestContext(req: any, customContextValue: Record): { traceId: string, userId: string, ab: string }; + /** * Http Get Request * @param route From 15f7e8e0003a5ff86ccbe46913793524a8016c9d Mon Sep 17 00:00:00 2001 From: Deeptanshu Singh Rathore Date: Fri, 26 Aug 2022 10:25:18 +0530 Subject: [PATCH 06/14] added optional parameter in req context object --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index af1cfc8..f49d16a 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -15,7 +15,7 @@ export class HttpCommunication { * @param customContextValue any custom values that you want to store in the context * Return extracted values from the req headers and any custom values pass to generate the context object */ - static getRequestContext(req: any, customContextValue: Record): { traceId: string, userId: string, ab: string }; + static getRequestContext(req: any, customContextValue?: Record): { traceId: string, userId: string, ab: string }; /** * Http Get Request From f6b53ac0255fbd4399411a27b8a6557ce0f68691 Mon Sep 17 00:00:00 2001 From: Deeptanshu Singh Rathore Date: Fri, 26 Aug 2022 23:01:24 +0530 Subject: [PATCH 07/14] added uuid --- package.json | 3 ++- src/http/index.js | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index f130a91..6d05a60 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ }, "homepage": "https://github.com/quizizz/service-communication-wrapper#readme", "dependencies": { - "axios": "^0.27.2" + "axios": "^0.27.2", + "uuid": "3.0.1" } } diff --git a/src/http/index.js b/src/http/index.js index 7ead846..b9e54fc 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -1,6 +1,7 @@ const QError = require('../helpers/error'); const path = require('path'); const Axios = require('axios'); +const uuid = require('uuid/v4'); class HttpCommunication { name; From ef32ba75b7e57313bfa573e41bd4bfab3036215e Mon Sep 17 00:00:00 2001 From: Deeptanshu Singh Rathore Date: Tue, 30 Aug 2022 10:20:56 +0530 Subject: [PATCH 08/14] added startTime in the request context --- src/http/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/http/index.js b/src/http/index.js index b9e54fc..f4f913f 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -32,6 +32,7 @@ class HttpCommunication { traceId: (req.headers && req.headers['X-Q-TRACEID']) ? req.headers['X-Q-TRACEID'] : uuid(), userId: (req.user && req.user.userId) ? String(req.user.userId) : null, ab: (req.headers && req.headers['Q-AB-ROUTE']) ? req.headers['Q-AB-ROUTE'] : null, + reqStartTime: process.hrtime(), ...customContextValue, }; } From 1a8a383c9c8c15e4602bca1e9419959db3521ea1 Mon Sep 17 00:00:00 2001 From: Deeptanshu Singh Rathore Date: Tue, 30 Aug 2022 10:51:35 +0530 Subject: [PATCH 09/14] updated the header values --- src/http/index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/http/index.js b/src/http/index.js index f4f913f..ab7d6f1 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -30,8 +30,12 @@ class HttpCommunication { static getRequestContext(req, customContextValue) { return { traceId: (req.headers && req.headers['X-Q-TRACEID']) ? req.headers['X-Q-TRACEID'] : uuid(), - userId: (req.user && req.user.userId) ? String(req.user.userId) : null, - ab: (req.headers && req.headers['Q-AB-ROUTE']) ? req.headers['Q-AB-ROUTE'] : null, + userId: (req.user && req.user.id) + ? String(req.user.id) + : req.headers['X-Q-USERID'] + ? req.headers['X-Q-USERID'] + : null, + ab: (req.headers && req.headers['X-Q-AB-ROUTE']) ? req.headers['X-Q-AB-ROUTE'] : null, reqStartTime: process.hrtime(), ...customContextValue, }; From 8d88b80d92b2c9b83c0e3d31c11493631337ed1d Mon Sep 17 00:00:00 2001 From: Deeptanshu Singh Rathore Date: Tue, 30 Aug 2022 14:33:08 +0530 Subject: [PATCH 10/14] added changes for version update --- src/http/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/http/index.js b/src/http/index.js index ab7d6f1..3066fbb 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -2,6 +2,7 @@ const QError = require('../helpers/error'); const path = require('path'); const Axios = require('axios'); const uuid = require('uuid/v4'); +const { hrtime } = require('node:process'); class HttpCommunication { name; @@ -28,6 +29,7 @@ class HttpCommunication { } static getRequestContext(req, customContextValue) { + const start = hrtime.bigint(); return { traceId: (req.headers && req.headers['X-Q-TRACEID']) ? req.headers['X-Q-TRACEID'] : uuid(), userId: (req.user && req.user.id) @@ -36,7 +38,7 @@ class HttpCommunication { ? req.headers['X-Q-USERID'] : null, ab: (req.headers && req.headers['X-Q-AB-ROUTE']) ? req.headers['X-Q-AB-ROUTE'] : null, - reqStartTime: process.hrtime(), + reqStartTime: start, ...customContextValue, }; } From 5dd3f8772423862bc0779eba1c1796fa9822b8da Mon Sep 17 00:00:00 2001 From: Deeptanshu Singh Rathore Date: Tue, 30 Aug 2022 16:06:03 +0530 Subject: [PATCH 11/14] updated to use axios constructor --- src/http/index.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/http/index.js b/src/http/index.js index 3066fbb..3dfa632 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -102,18 +102,15 @@ class HttpCommunication { } } - if (method === 'get') { - response = await Axios.get( - requestURL, - this.axiosConfig, - ); - } else { - response = await Axios[method]( - requestURL, - request.body || {}, - this.axiosConfig, - ); + const req = { + method, + url: requestURL, + ...this.axiosConfig, + } + if (request.body) { + req['data'] = request.body; } + response = await Axios(req); this.handleError(params, response); return response.data; From 0543e40d3c7cf701c7640e7e890cc26796062788 Mon Sep 17 00:00:00 2001 From: Deeptanshu Singh Rathore Date: Tue, 30 Aug 2022 16:33:06 +0530 Subject: [PATCH 12/14] reverted hr time --- src/http/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/http/index.js b/src/http/index.js index 3dfa632..d7a184c 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -29,7 +29,7 @@ class HttpCommunication { } static getRequestContext(req, customContextValue) { - const start = hrtime.bigint(); + const start = hrtime(); return { traceId: (req.headers && req.headers['X-Q-TRACEID']) ? req.headers['X-Q-TRACEID'] : uuid(), userId: (req.user && req.user.id) From e14b0e1ea37d35425e6e1ae71d507441b32e35ff Mon Sep 17 00:00:00 2001 From: Deeptanshu Singh Rathore Date: Tue, 30 Aug 2022 16:47:38 +0530 Subject: [PATCH 13/14] added changes for hrtime big int --- src/http/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/http/index.js b/src/http/index.js index d7a184c..3dfa632 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -29,7 +29,7 @@ class HttpCommunication { } static getRequestContext(req, customContextValue) { - const start = hrtime(); + const start = hrtime.bigint(); return { traceId: (req.headers && req.headers['X-Q-TRACEID']) ? req.headers['X-Q-TRACEID'] : uuid(), userId: (req.user && req.user.id) From cfc34f0ab2f4f080941028ce9c235f7b30c04a4d Mon Sep 17 00:00:00 2001 From: Deeptanshu Singh Rathore Date: Tue, 30 Aug 2022 18:41:14 +0530 Subject: [PATCH 14/14] updated the tracing logic --- src/http/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/http/index.js b/src/http/index.js index 3dfa632..77480e6 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -31,13 +31,13 @@ class HttpCommunication { static getRequestContext(req, customContextValue) { const start = hrtime.bigint(); return { - traceId: (req.headers && req.headers['X-Q-TRACEID']) ? req.headers['X-Q-TRACEID'] : uuid(), + traceId: (req.headers && req.headers['x-q-traceid']) ? req.headers['x-q-traceid'] : uuid(), userId: (req.user && req.user.id) ? String(req.user.id) - : req.headers['X-Q-USERID'] - ? req.headers['X-Q-USERID'] + : req.headers['x-q-userid'] + ? req.headers['x-q-userid'] : null, - ab: (req.headers && req.headers['X-Q-AB-ROUTE']) ? req.headers['X-Q-AB-ROUTE'] : null, + ab: (req.headers && (req.headers['x-q-ab-route'] || req.headers['X-Q-AB-ROUTE'])) ? req.headers['x-q-ab-route'] || req.headers['X-Q-AB-ROUTE'] : null, reqStartTime: start, ...customContextValue, };