Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Feature: Add support for request headers #15

Merged
merged 4 commits into from
Sep 20, 2024
Merged
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
5 changes: 1 addition & 4 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ const config: Config = {
restoreMocks: true,
testEnvironment: 'node',
transform: {
'^.+\\.ts$': [
'ts-jest',
{ diagnostics: { ignoreCodes: ['TS151001'] } }
],
'^.+\\.ts$': ['ts-jest', { diagnostics: { ignoreCodes: ['TS151001'] } }],
},
};

Expand Down
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@types/tough-cookie": "^4.0.5",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@typescript-eslint/parser": "^6.18.1",
"axios-mock-adapter": "^1.22.0",
"axios-mock-adapter": "^2.0.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.2",
Expand Down
20 changes: 20 additions & 0 deletions src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,26 @@ describe('Client', () => {
expect(response).toEqual('result');
});

test('makes a request with headers', async () => {
const client = new Client('id', 'key');

mock.onGet('/products/:count').reply((config) => {
const headers = Object.fromEntries(
Object.entries(config.headers || {}),
);
return [200, headers['X-Foo']];
});

const response = await client.request(
HttpMethod.get,
'/products/:count',
{},
{ 'X-Foo': 'bar' },
);

expect(response).toEqual('bar');
});

test('handles an error response', async () => {
const client = new Client('id', 'key');

Expand Down
21 changes: 12 additions & 9 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,29 +148,30 @@ export class Client {
});
}

get<T>(url: string, data?: unknown): Promise<T> {
return this.request(HttpMethod.get, url, data);
get<T>(url: string, data?: unknown, headers?: HttpHeaders): Promise<T> {
return this.request(HttpMethod.get, url, data, headers);
}

post<T>(url: string, data: unknown): Promise<T> {
return this.request(HttpMethod.post, url, data);
post<T>(url: string, data: unknown, headers?: HttpHeaders): Promise<T> {
return this.request(HttpMethod.post, url, data, headers);
}

put<T>(url: string, data: unknown): Promise<T> {
return this.request(HttpMethod.put, url, data);
put<T>(url: string, data: unknown, headers?: HttpHeaders): Promise<T> {
return this.request(HttpMethod.put, url, data, headers);
}

delete<T>(url: string, data?: unknown): Promise<T> {
return this.request(HttpMethod.delete, url, data);
delete<T>(url: string, data?: unknown, headers?: HttpHeaders): Promise<T> {
return this.request(HttpMethod.delete, url, data, headers);
}

async request<T>(
method: HttpMethod,
url: string,
data?: unknown,
headers?: HttpHeaders,
): Promise<T> {
// Prepare url and data for request
const requestParams = transformRequest(method, url, data);
const requestParams = transformRequest(method, url, data, headers);

return new Promise((resolve, reject) => {
const { retries } = this.options;
Expand Down Expand Up @@ -221,11 +222,13 @@ function transformRequest(
method: HttpMethod,
url: string,
data: unknown,
headers?: HttpHeaders,
): axios.AxiosRequestConfig {
return {
method,
url: typeof url?.toString === 'function' ? url.toString() : '',
data: data !== undefined ? data : null,
headers,
};
}

Expand Down
Loading