Skip to content

Commit

Permalink
fix: change interface to suite GOT
Browse files Browse the repository at this point in the history
  • Loading branch information
Goodluckhf committed Oct 28, 2020
1 parent 3bb6e55 commit 4d377ba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
21 changes: 19 additions & 2 deletions src/http-client.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ describe('HTTP client service', () => {
describe.each(['get', 'post', 'delete', 'head', 'put', 'patch'])(
'Method invocation: %s',
(method: string) => {
it('Should provide client options', () => {
ctx.gotInstance[method] = jest.fn();

const service = new HttpClientService(
ctx.gotInstance,
ctx.traceDataService,
ctx.httpServiceConfigProvider,
);

service[method]('', { headers: { test: 10 }, retries: 10 });

expect(ctx.gotInstance[method]).toBeCalledWith('', {
headers: { test: 10 },
retries: 10,
});
});

it('Should not add headers if shouldTraceServiceInvoke is disabled', () => {
ctx.gotInstance[method] = jest.fn();

Expand Down Expand Up @@ -126,10 +143,10 @@ describe('HTTP client service', () => {
ctx.httpServiceConfigProvider,
);

service[method]('');
service[method]('', { headers: { test: 1 } });

expect(ctx.gotInstance[method]).toBeCalledWith('', {
headers: { 'x-trace-id': 'testId' },
headers: { 'x-trace-id': 'testId', test: 1 },
});
});
},
Expand Down
30 changes: 6 additions & 24 deletions src/http-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ export class HttpClientService {
this.clientConfig = this.httpServiceConfigProvider.getConfig();
}

get(
url: string,
{ clientOpts }: { clientOpts: HttpClientOptionsType } = { clientOpts: {} },
) {
get(url: string, clientOpts: HttpClientOptionsType = {}) {
const { headers } = clientOpts;
const traceHeaders = this.getHeaders();
return this.gotInstance.get(url, {
Expand All @@ -32,10 +29,7 @@ export class HttpClientService {
});
}

post(
url: string,
{ clientOpts }: { clientOpts: HttpClientOptionsType } = { clientOpts: {} },
) {
post(url: string, clientOpts: HttpClientOptionsType = {}) {
const { headers } = clientOpts;
const traceHeaders = this.getHeaders();
return this.gotInstance.post(url, {
Expand All @@ -44,10 +38,7 @@ export class HttpClientService {
});
}

delete(
url: string,
{ clientOpts }: { clientOpts: HttpClientOptionsType } = { clientOpts: {} },
) {
delete(url: string, clientOpts: HttpClientOptionsType = {}) {
const { headers } = clientOpts;
const traceHeaders = this.getHeaders();
return this.gotInstance.delete(url, {
Expand All @@ -56,10 +47,7 @@ export class HttpClientService {
});
}

head(
url: string,
{ clientOpts }: { clientOpts: HttpClientOptionsType } = { clientOpts: {} },
) {
head(url: string, clientOpts: HttpClientOptionsType = {}) {
const { headers } = clientOpts;
const traceHeaders = this.getHeaders();
return this.gotInstance.head(url, {
Expand All @@ -68,10 +56,7 @@ export class HttpClientService {
});
}

put(
url: string,
{ clientOpts }: { clientOpts: HttpClientOptionsType } = { clientOpts: {} },
) {
put(url: string, clientOpts: HttpClientOptionsType = {}) {
const { headers } = clientOpts;
const traceHeaders = this.getHeaders();
return this.gotInstance.put(url, {
Expand All @@ -80,10 +65,7 @@ export class HttpClientService {
});
}

patch(
url: string,
{ clientOpts }: { clientOpts: HttpClientOptionsType } = { clientOpts: {} },
) {
patch(url: string, clientOpts: HttpClientOptionsType = {}) {
const { headers } = clientOpts;
const traceHeaders = this.getHeaders();
return this.gotInstance.patch(url, {
Expand Down

0 comments on commit 4d377ba

Please sign in to comment.