-
Notifications
You must be signed in to change notification settings - Fork 0
remove request from shd #316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Loki-Afro
wants to merge
3
commits into
main
Choose a base branch
from
fixup-request-promise
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,9 +1,8 @@ | ||
| const request = require("request"); | ||
| const rp = require("request-promise"); | ||
|
|
||
| const api = ( | ||
| req, | ||
| { | ||
| // The `useCallback` option is deprecated as native fetch is promise-based. | ||
| // The returned client will always be promise-based. | ||
| useCallback = false, | ||
| json = true, | ||
| version = "v1", | ||
|
|
@@ -22,24 +21,123 @@ | |
| "thisisasupersecureapikeythatisabsolutelysave"; | ||
| } else if (filesStorageApi) { | ||
| baseUrl = process.env.FILES_STORAGE_API_URL || "http://localhost:4444/api/"; | ||
| headers["Authorization"] = | ||
| (req.cookies.jwt.startsWith("Bearer ") ? "" : "Bearer ") + | ||
| req.cookies.jwt; | ||
| if (req && req.cookies && req.cookies.jwt) { | ||
| headers["Authorization"] = | ||
| (req.cookies.jwt.startsWith("Bearer ") ? "" : "Bearer ") + | ||
| req.cookies.jwt; | ||
| } | ||
| } else if (req && req.cookies && req.cookies.jwt) { | ||
| headers["Authorization"] = | ||
| (req.cookies.jwt.startsWith("Bearer ") ? "" : "Bearer ") + | ||
| req.cookies.jwt; | ||
| } | ||
|
|
||
| const handler = useCallback ? request : rp; | ||
| const urlJoin = (base, p) => { | ||
| const newBase = base.endsWith("/") ? base : base + "/"; | ||
| const newPath = p.startsWith("/") ? p.substring(1) : p; | ||
| return newBase + newPath; | ||
| }; | ||
|
|
||
| const fetchWithDefaults = async (method, uri, options = {}) => { | ||
| let path; | ||
| let mergedOptions = { ...options }; | ||
|
|
||
| if (typeof uri === "object") { | ||
| mergedOptions = { ...uri, ...options }; | ||
| path = mergedOptions.uri; | ||
| delete mergedOptions.uri; | ||
| } else { | ||
| path = uri; | ||
| } | ||
|
|
||
| const baseApiUrl = new URL(version, baseUrl).href; | ||
|
|
||
| let url = urlJoin(baseApiUrl, path || ''); | ||
|
|
||
| const fetchOptions = { | ||
| method, | ||
| headers: { ...headers, ...mergedOptions.headers }, | ||
| ...mergedOptions, | ||
| }; | ||
|
|
||
| const apiRequest = { | ||
| baseUrl: new URL(version, baseUrl).href, | ||
| json, | ||
| headers, | ||
| if (mergedOptions.body) { | ||
| if ( | ||
| json && | ||
| typeof mergedOptions.body === "object" && | ||
| !(mergedOptions.body instanceof FormData) && | ||
| !(mergedOptions.body instanceof URLSearchParams) | ||
| ) { | ||
| fetchOptions.body = JSON.stringify(mergedOptions.body); | ||
| if (!fetchOptions.headers["Content-Type"]) { | ||
| fetchOptions.headers["Content-Type"] = "application/json"; | ||
| } | ||
| } | ||
| } else if (mergedOptions.json && typeof mergedOptions.json === 'object') { | ||
| fetchOptions.body = JSON.stringify(mergedOptions.json); | ||
| if (!fetchOptions.headers["Content-Type"]) { | ||
| fetchOptions.headers["Content-Type"] = "application/json"; | ||
| } | ||
| } else if (mergedOptions.form) { | ||
| fetchOptions.body = new URLSearchParams(mergedOptions.form); | ||
| if (!fetchOptions.headers["Content-Type"]) { | ||
| fetchOptions.headers["Content-Type"] = | ||
| "application/x-www-form-urlencoded"; | ||
| } | ||
| } else if (mergedOptions.formData) { | ||
| fetchOptions.body = mergedOptions.formData; | ||
| } | ||
|
|
||
| if (mergedOptions.qs) { | ||
| const params = new URLSearchParams(mergedOptions.qs); | ||
| url = `${url}?${params}`; | ||
| } | ||
|
|
||
| const response = await fetch(url, fetchOptions); | ||
|
|
||
| if (!response.ok) { | ||
| const error = new Error( | ||
| `Request failed with status code ${response.status}` | ||
| ); | ||
| error.statusCode = response.status; | ||
| error.response = response; | ||
| try { | ||
| error.error = await response.json(); | ||
| } catch (e) { | ||
| try { | ||
| error.error = await response.text(); | ||
| } catch (e2) { | ||
| // ignore | ||
| } | ||
| } | ||
|
Comment on lines
+105
to
+111
Check noticeCode scanning / SonarCloud Exceptions should not be ignored Low
Handle this exception or don't catch it at all. See more on SonarQube Cloud
|
||
| throw error; | ||
| } | ||
|
|
||
| if (mergedOptions.resolveWithFullResponse) { | ||
| return response; | ||
| } | ||
|
|
||
| if (filesStorageApi) { | ||
| return response; | ||
| } | ||
|
|
||
| if (json) { | ||
| const contentType = response.headers.get("content-type"); | ||
| if (contentType && contentType.includes("application/json")) { | ||
| return response.json(); | ||
| } | ||
| } | ||
|
|
||
| return response.text(); | ||
| }; | ||
|
|
||
| return handler.defaults(apiRequest); | ||
| const client = {}; | ||
| const methods = ["get", "post", "put", "delete", "patch", "head", "options"]; | ||
| for (const method of methods) { | ||
| client[method] = (uri, options) => | ||
| fetchWithDefaults(method.toUpperCase(), uri, options); | ||
| } | ||
|
|
||
| return client; | ||
| }; | ||
|
|
||
| module.exports = { api }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / SonarCloud
Exceptions should not be ignored Low