-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from pactflow/fix/upstream-merge
fix: replace unmaintained dependency request for axios (merge from upstream)
- Loading branch information
Showing
5 changed files
with
17 additions
and
56 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
.vs | ||
node_modules | ||
.idea | ||
.npmrc | ||
build-output | ||
npm-debug.log | ||
package-lock.json | ||
|
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,60 +1,23 @@ | ||
import * as request from 'request'; | ||
|
||
type RequestOptions = request.Options; | ||
|
||
const hasHttp2xxStatusCode = (response: request.RequestResponse) => | ||
response.statusCode && response.statusCode >= 200 && response.statusCode <= 299; | ||
import axios, {AxiosRequestHeaders} from 'axios'; | ||
|
||
export class HttpClient { | ||
private static getRequestOptions(url: string, auth?: string): RequestOptions { | ||
let requestOptions: RequestOptions = { | ||
public async get(url: string, auth?: string): Promise<string> { | ||
const headers: AxiosRequestHeaders = auth | ||
? {authorization: 'Basic ' + Buffer.from(auth).toString('base64')} | ||
: {} | ||
const response = await axios.get(url, { | ||
headers, | ||
timeout: 30000, | ||
url | ||
}; | ||
if (auth) { | ||
requestOptions = {...requestOptions, headers: { | ||
authorization: 'Basic ' + Buffer.from(auth).toString('base64') | ||
}}; | ||
} | ||
|
||
return requestOptions; | ||
} | ||
|
||
public get(url: string, auth?: string): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
const requestOptions = HttpClient.getRequestOptions(url, auth); | ||
|
||
request(requestOptions, (error, response, body) => { | ||
if (error) { | ||
reject(error); | ||
} else if (response.statusCode !== 200) { | ||
reject(new Error(`Expected 200 but received ${response.statusCode}`)); | ||
} else { | ||
resolve(body); | ||
} | ||
}); | ||
transformResponse: (data) => data, | ||
validateStatus: (status) => status === 200 | ||
}); | ||
return response.data; | ||
} | ||
|
||
public post(url: string, body: any): Promise<void> { | ||
const requestOptions = { | ||
body, | ||
json: true, | ||
method: 'POST', | ||
public async post(url: string, body: any): Promise<void> { | ||
await axios.post(url, body, { | ||
timeout: 5000, | ||
url | ||
}; | ||
|
||
return new Promise<void>((resolve, reject) => { | ||
request(requestOptions, (error, response) => { | ||
if (error) { | ||
reject(error); | ||
} else if (!hasHttp2xxStatusCode(response)) { | ||
reject(new Error(`Expected 2xx but received ${response.statusCode}}`)); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
validateStatus: (status) => status >= 200 && status <= 299 | ||
}); | ||
} | ||
} |
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