From 893347e991a775d43b7557bfb74dc13bd85ea367 Mon Sep 17 00:00:00 2001 From: Akshendra Pratap Singh Date: Tue, 13 Sep 2022 09:35:32 +0530 Subject: [PATCH 1/2] feat(headers): support headers per request --- package.json | 2 +- src/http/index.js | 25 ++++++++++++++++--------- types/index.d.ts | 10 +++++----- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index f130a91..c27a137 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "service-communication-wrapper", + "name": "@quizizz/service-communication", "version": "1.0.0", "description": "A communication wrapper to support any interservice communication", "main": "index.js", diff --git a/src/http/index.js b/src/http/index.js index b32ba7a..a432526 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -93,22 +93,29 @@ class HttpCommunication { } async makeRequest(params) { - const { route, method, request } = params; + const { route, method, request, headers = {} } = params; const requestURL = this.createRequestURL(route, request.query); let response; const requestContext = this.contextStorage ? this.contextStorage.getStore() : null; + let finalHeaders = {}; if (requestContext) { - this.axiosConfig.headers = { + finalHeaders = { ...this.axiosConfig.headers, ...this.populateHeadersFromContext(requestContext), - } + ...headers, + }; } + const axiosConfig = { + ...this.axiosConfig, + headers: finalHeaders, + }; + const req = { method, url: requestURL, - ...this.axiosConfig, + axiosConfig, } if (request.body) { req['data'] = request.body; @@ -119,7 +126,7 @@ class HttpCommunication { return response.data; } - async post(route, request) { + async post(route, request, headers = {}) { const data = await this.makeRequest({ method: 'post', route, @@ -128,7 +135,7 @@ class HttpCommunication { return data; } - async put(route, request) { + async put(route, request, headers = {}) { const data = await this.makeRequest({ method: 'put', route, @@ -137,7 +144,7 @@ class HttpCommunication { return data; } - async patch(route, request) { + async patch(route, request, headers = {}) { const data = await this.makeRequest({ method: 'patch', route, @@ -146,7 +153,7 @@ class HttpCommunication { return data; } - async delete(route, request) { + async delete(route, request, headers = {}) { const data = await this.makeRequest({ method: 'delete', route, @@ -155,7 +162,7 @@ class HttpCommunication { return data; } - async get(route, request = {}) { + async get(route, request = {}, headers = {}) { const data = await this.makeRequest({ method: 'get', route, diff --git a/types/index.d.ts b/types/index.d.ts index a1a08dc..5aa3c16 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -24,7 +24,7 @@ export class HttpCommunication { * @typeParam T - Response type provided by the callee * @returns Returns response of the get request */ - get(route: string, request?: { query: Record }): Promise; + get(route: string, request?: { query: Record }, headers?: Record): Promise; /** * Http Put Request @@ -33,7 +33,7 @@ export class HttpCommunication { * @typeParam T - Response type provided by the callee * @returns Returns response of the put request */ - put(route: string, request: { query: Record, body: Record }): Promise; + put(route: string, request: { query: Record, body: Record }, headers?: Record): Promise; /** * Http Delete Request @@ -42,7 +42,7 @@ export class HttpCommunication { * @typeParam T - Response type provided by the callee * @returns Returns response of the put request */ - delete(route: string, request: { query: Record, body: Record }): Promise; + delete(route: string, request: { query: Record, body: Record }, headers?: Record): Promise; /** * Http Patch Request @@ -51,7 +51,7 @@ export class HttpCommunication { * @typeParam T - Response type provided by the callee * @returns Returns response of the put request */ - patch(route: string, request: { query: Record, body: Record }): Promise; + patch(route: string, request: { query: Record, body: Record }, headers?: Record): Promise; /** * Http Post Request @@ -60,5 +60,5 @@ export class HttpCommunication { * @typeParam T - Response type provided by the callee * @returns Returns response of the post request */ - post(route: string, request: { query: Record, body: Record }): Promise; + post(route: string, request: { query: Record, body: Record }, headers?: Record): Promise; } \ No newline at end of file From 50b28e16e85c99865445398b6c5a6253b1cc1f4f Mon Sep 17 00:00:00 2001 From: Akshendra Pratap Singh Date: Tue, 13 Sep 2022 12:55:56 +0530 Subject: [PATCH 2/2] fix(headers): pass headers to makeRequest --- src/http/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/http/index.js b/src/http/index.js index a432526..944f868 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -115,7 +115,7 @@ class HttpCommunication { const req = { method, url: requestURL, - axiosConfig, + ...axiosConfig, } if (request.body) { req['data'] = request.body; @@ -131,6 +131,7 @@ class HttpCommunication { method: 'post', route, request, + headers, }); return data; } @@ -140,6 +141,7 @@ class HttpCommunication { method: 'put', route, request, + headers, }); return data; } @@ -149,6 +151,7 @@ class HttpCommunication { method: 'patch', route, request, + headers, }); return data; } @@ -158,6 +161,7 @@ class HttpCommunication { method: 'delete', route, request, + headers, }); return data; } @@ -167,6 +171,7 @@ class HttpCommunication { method: 'get', route, request, + headers, }); return data; }