-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7df9bb
commit c60f641
Showing
9 changed files
with
527 additions
and
425 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,52 @@ | ||
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
import { Stubborn } from '../../src'; | ||
|
||
export interface HttpClientRequest { | ||
path?: string; | ||
headers?: Record<string, string>; | ||
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'; | ||
responseType?: 'json' | 'text' | 'arraybuffer'; | ||
data?: string | Record<string, unknown>; | ||
query?: Record<string, string> | URLSearchParams; | ||
} | ||
|
||
export interface HttpClientResponse { | ||
status: number; | ||
data: string | Record<string, unknown> | Buffer; | ||
headers: Record<string, unknown>; | ||
} | ||
|
||
export class HttpClient { | ||
constructor(private readonly sb: Stubborn) {} | ||
|
||
public async request(req: HttpClientRequest): Promise<HttpClientResponse> { | ||
const res = await axios(this.toAxiosRequest(req)); | ||
|
||
return this.toHttpClientResponse(res); | ||
} | ||
|
||
private toAxiosRequest(req: HttpClientRequest): AxiosRequestConfig { | ||
return { | ||
baseURL: this.sb.getOrigin(), | ||
url: req.path || '/', | ||
method: req.method || 'GET', | ||
data: req.data || '', | ||
responseType: req.responseType || 'json', | ||
params: req.query, | ||
headers: { | ||
accept: 'application/json', | ||
'content-type': null, | ||
...(req.headers || {}), | ||
}, | ||
validateStatus: () => true, | ||
}; | ||
} | ||
|
||
private toHttpClientResponse(res: AxiosResponse) { | ||
return { | ||
status: res.status, | ||
data: res.data, | ||
headers: JSON.parse(JSON.stringify(res.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,2 +1 @@ | ||
export * from './init'; | ||
export * from './ansi'; |
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 |
---|---|---|
@@ -1 +1,6 @@ | ||
export * from './matchers'; | ||
import { test } from './test'; | ||
|
||
beforeAll(async () => await test.setup()); | ||
afterAll(async () => await test.tearDown()); | ||
beforeEach(async () => await test.clean()); |
Oops, something went wrong.