Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V0.0.10 #7

Open
wants to merge 2 commits into
base: v0.0.9
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
30 changes: 21 additions & 9 deletions src/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -119,47 +126,52 @@ class HttpCommunication {
return response.data;
}

async post(route, request) {
async post(route, request, headers = {}) {
const data = await this.makeRequest({
method: 'post',
route,
request,
headers,
});
return data;
}

async put(route, request) {
async put(route, request, headers = {}) {
const data = await this.makeRequest({
method: 'put',
route,
request,
headers,
});
return data;
}

async patch(route, request) {
async patch(route, request, headers = {}) {
const data = await this.makeRequest({
method: 'patch',
route,
request,
headers,
});
return data;
}

async delete(route, request) {
async delete(route, request, headers = {}) {
const data = await this.makeRequest({
method: 'delete',
route,
request,
headers,
});
return data;
}

async get(route, request = {}) {
async get(route, request = {}, headers = {}) {
const data = await this.makeRequest({
method: 'get',
route,
request,
headers,
});
return data;
}
Expand Down
10 changes: 5 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class HttpCommunication {
* @typeParam T - Response type provided by the callee
* @returns Returns response of the get request
*/
get<T>(route: string, request?: { query: Record<string, string> }): Promise<T>;
get<T>(route: string, request?: { query: Record<string, string> }, headers?: Record<string, unknown>): Promise<T>;

/**
* Http Put Request
Expand All @@ -33,7 +33,7 @@ export class HttpCommunication {
* @typeParam T - Response type provided by the callee
* @returns Returns response of the put request
*/
put<T>(route: string, request: { query: Record<string, string>, body: Record<string, unknown> }): Promise<T>;
put<T>(route: string, request: { query: Record<string, string>, body: Record<string, unknown> }, headers?: Record<string, unknown>): Promise<T>;

/**
* Http Delete Request
Expand All @@ -42,7 +42,7 @@ export class HttpCommunication {
* @typeParam T - Response type provided by the callee
* @returns Returns response of the put request
*/
delete<T>(route: string, request: { query: Record<string, string>, body: Record<string, unknown> }): Promise<T>;
delete<T>(route: string, request: { query: Record<string, string>, body: Record<string, unknown> }, headers?: Record<string, unknown>): Promise<T>;

/**
* Http Patch Request
Expand All @@ -51,7 +51,7 @@ export class HttpCommunication {
* @typeParam T - Response type provided by the callee
* @returns Returns response of the put request
*/
patch<T>(route: string, request: { query: Record<string, string>, body: Record<string, unknown> }): Promise<T>;
patch<T>(route: string, request: { query: Record<string, string>, body: Record<string, unknown> }, headers?: Record<string, unknown>): Promise<T>;

/**
* Http Post Request
Expand All @@ -60,5 +60,5 @@ export class HttpCommunication {
* @typeParam T - Response type provided by the callee
* @returns Returns response of the post request
*/
post<T>(route: string, request: { query: Record<string, string>, body: Record<string, unknown> }): Promise<T>;
post<T>(route: string, request: { query: Record<string, string>, body: Record<string, unknown> }, headers?: Record<string, unknown>): Promise<T>;
}