-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use type instead of string for HTTP method. * Add AxiosHttpAdapter. * Add documentation for AxiosHttpAdapter.
- Loading branch information
Showing
8 changed files
with
193 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import axios, { AxiosInstance } from 'axios'; | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import { HttpAdapters, HttpAdapter } from '../../src'; | ||
|
||
describe.only('HttpAdapters.AxiosHttpAdapter', () => { | ||
let axiosInstance: AxiosInstance; | ||
let axiosMock: MockAdapter; | ||
let request: HttpAdapter.AdapterRequest; | ||
let httpAdapter: HttpAdapters.AxiosHttpAdapter; | ||
|
||
beforeAll(() => { | ||
axiosInstance = axios.create(); | ||
axiosMock = new MockAdapter(axiosInstance); | ||
httpAdapter = new HttpAdapters.AxiosHttpAdapter(axiosInstance); | ||
}); | ||
|
||
beforeEach(() => { | ||
request = { | ||
url: 'https://examples.com/', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
method: 'POST', | ||
body: '{"foo":"bar"}' | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
axiosMock.reset(); | ||
}); | ||
|
||
describe('request', () => { | ||
beforeEach(() => { | ||
axiosMock | ||
.onPost('https://examples.com/') | ||
.reply(200, '{"message":"ok"}', { 'Content-Type': 'application/json' }); | ||
}); | ||
|
||
test('calls request on the axios instance', async () => { | ||
await httpAdapter.request(request); | ||
|
||
expect(axiosMock.history.post.length).toBe(1); | ||
expect(axiosMock.history.post[0]).toMatchObject({ | ||
url: 'https://examples.com/', | ||
method: expect.stringMatching(/post/i), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
data: '{"foo":"bar"}', | ||
transitional: { | ||
silentJSONParsing: false, | ||
forcedJSONParsing: false | ||
} | ||
}); | ||
}); | ||
|
||
test('resolves with response', async () => { | ||
await expect(httpAdapter.request(request)).resolves.toEqual({ | ||
body: '{"message":"ok"}', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
status: 200 | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { AxiosInstance } from 'axios'; | ||
import * as HttpAdapter from '../HttpAdapter'; | ||
|
||
/** | ||
* {@link HttpAdapter.Adapter} using {@link https://github.com/axios/axios|axios} for make HTTP requests. | ||
*/ | ||
export class AxiosHttpAdapter implements HttpAdapter.Adapter { | ||
/** | ||
* The Axios instance. | ||
*/ | ||
instance: AxiosInstance; | ||
|
||
/** | ||
* Create an axios HTTP adapter. | ||
* @param instance - The Axios instance. | ||
*/ | ||
constructor(instance: AxiosInstance) { | ||
this.instance = instance; | ||
} | ||
|
||
/** | ||
* @see {@link HttpAdapter.Adapter.request} | ||
* @param options | ||
* @returns | ||
*/ | ||
async request( | ||
options: HttpAdapter.AdapterRequest | ||
): Promise<HttpAdapter.AdapterResponse> { | ||
const { url, method, headers, body } = options; | ||
const response = await this.instance.request({ | ||
headers, | ||
method, | ||
url, | ||
data: body, | ||
// Specify not to parse JSON as the adapter should return the raw response body. | ||
transitional: { | ||
silentJSONParsing: false, | ||
forcedJSONParsing: false | ||
} | ||
}); | ||
return { | ||
status: response.status, | ||
body: response.data, | ||
headers: response.headers | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { FetchHttpAdapter } from './FetchHttpAdapter'; | ||
export { AxiosHttpAdapter } from './AxiosHttpAdapter'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters