-
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.
- Loading branch information
1 parent
c008d00
commit f99cf6b
Showing
22 changed files
with
1,410 additions
and
152 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,50 @@ | ||
// (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, | ||
...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.