-
Notifications
You must be signed in to change notification settings - Fork 41
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 #1163 from yashkohli88/yk/update-request-promise
Updated deprecated dependency request-promise-native
- Loading branch information
Showing
23 changed files
with
1,420 additions
and
153 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,51 @@ | ||
// (c) Copyright 2024, SAP SE and ClearlyDefined contributors. Licensed under the MIT license. | ||
// SPDX-License-Identifier: MIT | ||
|
||
const axios = require('axios') | ||
|
||
function buildRequestOptions(request) { | ||
let responseType = 'text' | ||
if (request.json) { | ||
responseType = 'json' | ||
} else if (request.encoding === null) { | ||
responseType = 'stream' | ||
} | ||
|
||
const validateOptions = {} | ||
if (request.simple === false) { | ||
validateOptions.validateStatus = () => true | ||
} | ||
|
||
return { | ||
method: request.method, | ||
url: request.url || request.uri, | ||
responseType, | ||
headers: request.headers, | ||
data: request.body, | ||
withCredentials: request.withCredentials, | ||
...validateOptions | ||
} | ||
} | ||
|
||
async function callFetch(request, axiosInstance = axios) { | ||
try { | ||
const options = buildRequestOptions(request) | ||
// @ts-ignore | ||
const response = await axiosInstance(options) | ||
if (!request.resolveWithFullResponse) return response.data | ||
response.statusCode = response.status | ||
response.statusMessage = response.statusText | ||
return response | ||
} catch (error) { | ||
error.statusCode = error.response?.status | ||
throw error | ||
} | ||
} | ||
|
||
function withDefaults(opts) { | ||
// @ts-ignore | ||
const axiosInstance = axios.create(opts) | ||
return request => callFetch(request, axiosInstance) | ||
} | ||
|
||
module.exports = { callFetch, withDefaults } |
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
Oops, something went wrong.