From f024972dfc17dffb56f9b5bceee38d5b4f631a45 Mon Sep 17 00:00:00 2001 From: Mike <104780686+swellmike@users.noreply.github.com> Date: Fri, 20 Sep 2024 08:21:44 -0700 Subject: [PATCH] Feature: Add support for request headers (#15) --- jest.config.ts | 5 +---- package-lock.json | 30 +++++++++++++++--------------- package.json | 2 +- src/client.test.ts | 20 ++++++++++++++++++++ src/client.ts | 21 ++++++++++++--------- 5 files changed, 49 insertions(+), 29 deletions(-) diff --git a/jest.config.ts b/jest.config.ts index b99dc1f..d9ac9c3 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -6,10 +6,7 @@ const config: Config = { restoreMocks: true, testEnvironment: 'node', transform: { - '^.+\\.ts$': [ - 'ts-jest', - { diagnostics: { ignoreCodes: ['TS151001'] } } - ], + '^.+\\.ts$': ['ts-jest', { diagnostics: { ignoreCodes: ['TS151001'] } }], }, }; diff --git a/package-lock.json b/package-lock.json index d88bf64..a968e50 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,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", @@ -2047,9 +2047,9 @@ } }, "node_modules/axios-mock-adapter": { - "version": "1.22.0", - "resolved": "https://registry.npmjs.org/axios-mock-adapter/-/axios-mock-adapter-1.22.0.tgz", - "integrity": "sha512-dmI0KbkyAhntUR05YY96qg2H6gg0XMl2+qTW0xmYg6Up+BFBAJYRLROMXRdDEL06/Wqwa0TJThAYvFtSFdRCZw==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/axios-mock-adapter/-/axios-mock-adapter-2.0.0.tgz", + "integrity": "sha512-D/K0J5Zm6KvaMTnsWrBQZWLzKN9GxUFZEa0mx2qeEHXDeTugCoplWehy8y36dj5vuSjhe1u/Dol8cZ8lzzmDew==", "dev": true, "dependencies": { "fast-deep-equal": "^3.1.3", @@ -4390,12 +4390,12 @@ } }, "node_modules/micromatch": { - "version": "4.0.5", - "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz", - "integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==", + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", + "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", "dev": true, "dependencies": { - "braces": "^3.0.2", + "braces": "^3.0.3", "picomatch": "^2.3.1" }, "engines": { @@ -7297,9 +7297,9 @@ } }, "axios-mock-adapter": { - "version": "1.22.0", - "resolved": "https://registry.npmjs.org/axios-mock-adapter/-/axios-mock-adapter-1.22.0.tgz", - "integrity": "sha512-dmI0KbkyAhntUR05YY96qg2H6gg0XMl2+qTW0xmYg6Up+BFBAJYRLROMXRdDEL06/Wqwa0TJThAYvFtSFdRCZw==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/axios-mock-adapter/-/axios-mock-adapter-2.0.0.tgz", + "integrity": "sha512-D/K0J5Zm6KvaMTnsWrBQZWLzKN9GxUFZEa0mx2qeEHXDeTugCoplWehy8y36dj5vuSjhe1u/Dol8cZ8lzzmDew==", "dev": true, "requires": { "fast-deep-equal": "^3.1.3", @@ -8994,12 +8994,12 @@ "dev": true }, "micromatch": { - "version": "4.0.5", - "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz", - "integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==", + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", + "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", "dev": true, "requires": { - "braces": "^3.0.2", + "braces": "^3.0.3", "picomatch": "^2.3.1" } }, diff --git a/package.json b/package.json index c9be39d..1bb99d2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/client.test.ts b/src/client.test.ts index 4fa6333..bdea971 100644 --- a/src/client.test.ts +++ b/src/client.test.ts @@ -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'); diff --git a/src/client.ts b/src/client.ts index 56855b0..fbbf47c 100644 --- a/src/client.ts +++ b/src/client.ts @@ -148,29 +148,30 @@ export class Client { }); } - get(url: string, data?: unknown): Promise { - return this.request(HttpMethod.get, url, data); + get(url: string, data?: unknown, headers?: HttpHeaders): Promise { + return this.request(HttpMethod.get, url, data, headers); } - post(url: string, data: unknown): Promise { - return this.request(HttpMethod.post, url, data); + post(url: string, data: unknown, headers?: HttpHeaders): Promise { + return this.request(HttpMethod.post, url, data, headers); } - put(url: string, data: unknown): Promise { - return this.request(HttpMethod.put, url, data); + put(url: string, data: unknown, headers?: HttpHeaders): Promise { + return this.request(HttpMethod.put, url, data, headers); } - delete(url: string, data?: unknown): Promise { - return this.request(HttpMethod.delete, url, data); + delete(url: string, data?: unknown, headers?: HttpHeaders): Promise { + return this.request(HttpMethod.delete, url, data, headers); } async request( method: HttpMethod, url: string, data?: unknown, + headers?: HttpHeaders, ): Promise { // 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; @@ -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, }; }