Skip to content

Commit

Permalink
Updated request-promise-native
Browse files Browse the repository at this point in the history
  • Loading branch information
yashkohli88 committed Sep 16, 2024
1 parent c008d00 commit f99cf6b
Show file tree
Hide file tree
Showing 22 changed files with 1,410 additions and 152 deletions.
2 changes: 1 addition & 1 deletion business/statusService.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation and others. Licensed under the MIT license.
// SPDX-License-Identifier: MIT

const requestPromise = require('request-promise-native')
const { callFetch: requestPromise } = require('../lib/fetch')
const logger = require('../providers/logging/logger')

class StatusService {
Expand Down
50 changes: 50 additions & 0 deletions lib/fetch.js
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 }
2 changes: 1 addition & 1 deletion lib/gradleCoordinatesMapper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// (c) Copyright 2021, SAP SE and ClearlyDefined contributors. Licensed under the MIT license.
// SPDX-License-Identifier: MIT

const requestPromise = require('request-promise-native')
const { callFetch: requestPromise } = require('../lib/fetch')
const { promisify } = require('util')
const parseXml = promisify(require('xml2js').parseString)
const { get } = require('lodash')
Expand Down
2 changes: 1 addition & 1 deletion lib/pypiCoordinatesMapper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// (c) Copyright 2021, SAP SE and ClearlyDefined contributors. Licensed under the MIT license.
// SPDX-License-Identifier: MIT

const requestPromise = require('request-promise-native')
const { callFetch: requestPromise } = require('../lib/fetch')
const EntityCoordinates = require('./entityCoordinates')

class PypiCoordinatesMapper {
Expand Down
Loading

0 comments on commit f99cf6b

Please sign in to comment.