@vitrical/utils
Advanced tools
Comparing version 1.6.0 to 1.6.1
@@ -29,5 +29,6 @@ export declare class ApiError extends Error { | ||
}; | ||
debug?: boolean; | ||
}; | ||
export declare const removeUndefined: (obj: any) => any; | ||
export declare const request: <B = any, R = any>({ method, url, body, formData, query, headers, }: CustomRequestOptions<B>) => Promise<CustomRequestResponse<R>>; | ||
export declare const request: <B = any, R = any>({ method, url, body, formData, query, headers, debug, }: CustomRequestOptions<B>) => Promise<CustomRequestResponse<R>>; | ||
export declare const makeMicroserviceRequest: (baseURL: string, key: string) => <T extends unknown>(method: 'get' | 'post' | 'put' | 'delete', url: string, options?: { | ||
@@ -34,0 +35,0 @@ body?: any; |
73
api.js
@@ -117,21 +117,19 @@ "use strict"; | ||
exports.removeUndefined = removeUndefined; | ||
const request = ({ method, url, body, formData, query, headers, }) => new Promise((resolve, reject) => { | ||
const request = ({ method, url, body, formData, query, headers, debug, }) => new Promise((resolve, reject) => { | ||
const lcaseMethod = method.toLowerCase(); | ||
const newStack = new Error().stack; | ||
if (isFetchAvailable() && (!isXMLHttpRequestAvailable() || !axios_1.default?.request)) { | ||
const request = fetch(!query || Object.keys(query).length === 0 | ||
? url | ||
: url + '?' + new URLSearchParams((0, exports.removeUndefined)(query)), { | ||
method: lcaseMethod, | ||
body: formData !== undefined | ||
? formData | ||
: body && | ||
(typeof body === 'string' || | ||
headers?.['Content-Type'] === 'multipart/form-data' || | ||
headers?.['Content-Type'] === 'text/plain' || | ||
headers?.['Content-Type'] === 'application/octet-stream') | ||
? body | ||
: JSON.stringify(body), | ||
headers: { | ||
...(body | ||
const bodyData = formData !== undefined | ||
? formData | ||
: body !== undefined && | ||
(typeof body === 'string' || | ||
headers?.['Content-Type'] === 'multipart/form-data' || | ||
headers?.['Content-Type'] === 'text/plain' || | ||
headers?.['Content-Type'] === 'application/octet-stream') | ||
? body | ||
: JSON.stringify(body); | ||
const headerData = { | ||
...(formData | ||
? {} | ||
: body | ||
? typeof body === 'string' | ||
@@ -141,4 +139,14 @@ ? { 'Content-Type': 'text/plain' } | ||
: {}), | ||
...headers, | ||
}, | ||
...headers, | ||
}; | ||
const urlData = !query || Object.keys(query).length === 0 | ||
? url | ||
: url + '?' + new URLSearchParams((0, exports.removeUndefined)(query)); | ||
if (debug) { | ||
console.log('fetch', lcaseMethod, urlData, bodyData, headerData); | ||
} | ||
const request = fetch(urlData, { | ||
method: lcaseMethod, | ||
body: bodyData, | ||
headers: headerData, | ||
}); | ||
@@ -178,19 +186,22 @@ request | ||
} | ||
const headerData = { | ||
...(formData | ||
? {} | ||
: body | ||
? typeof body === 'string' | ||
? { 'Content-Type': 'text/plain' } | ||
: { 'Content-Type': 'application/json' } | ||
: {}), | ||
...headers, | ||
}; | ||
const bodyData = formData !== undefined ? formData : body; | ||
if (debug) { | ||
console.log('axios', lcaseMethod, url, bodyData, query, headerData); | ||
} | ||
const request = axios_1.default.request({ | ||
method: lcaseMethod, | ||
url, | ||
data: formData !== undefined ? formData : body, | ||
data: bodyData, | ||
params: query, | ||
headers: { | ||
...(body | ||
? typeof body === 'string' | ||
? { 'Content-Type': 'text/plain' } | ||
: { 'Content-Type': 'application/json' } | ||
: formData | ||
? { | ||
'Content-Type': 'multipart/form-data', | ||
} | ||
: {}), | ||
...headers, | ||
}, | ||
headers: headerData, | ||
timeout: 1000 * 60 * 5, | ||
@@ -197,0 +208,0 @@ }); |
{ | ||
"name": "@vitrical/utils", | ||
"version": "1.6.0", | ||
"version": "1.6.1", | ||
"description": "Collection of useful functions and typings", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50164
755