Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x | // Product-independent general-purpose methods for file operations
// Methods:
// fetchData,
/**
* Fetch data using the specified URL and method.
* This function serves as a wrapper for the Fetch API.
*
* @param {FetchDataOptions} options - The options for fetching data.
* @returns {Promise<any>} - A promise that resolves to the fetched data.
*/
interface FetchDataOptions {
url?: string;
method?: string;
data?: Record<string, any>;
datatype?: string;
timeout?: number;// default to `5e3` ms (= 5 sec.)
}
export const fetchData = async ({
url = '',
method = 'get',
data = {},
datatype = 'json',
timeout = 5e3
}: FetchDataOptions = {}): Promise<any> => {
const controller = new AbortController()
const timeoutId = timeout > 0 ? setTimeout(() => {
controller.abort()
}, timeout) : null
if (!url || !/^(get|post|put|delete|patch)$/i.test(method)) {
return Promise.reject({
type: 'bad_request',
status: 400,
message: 'Invalid argument(s) given.'
})
}
const params = new URLSearchParams()
let sendData: RequestInit = {
method: method,
mode: 'cors',
cache: 'no-cache',
credentials: 'omit',
redirect: 'follow',
referrerPolicy: 'no-referrer',
signal: controller.signal
}
if (data) {
for (const key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
params.append(key, data[key])
}
}
}
if (method.toLowerCase() !== 'get') {
sendData.body = params
} else {
if (params.toString()) {
url += '?' + params
}
}
try {
const response = await fetch(url, sendData)
if (response.ok) {
const retval = datatype === 'json' ? await response.json() : await response.text()
return Promise.resolve(retval)
} else {
const errObj = await response.json()
return Promise.reject({
code: errObj.code,
status: errObj.status || errObj.statusText,
message: errObj.message
})
}
} catch (err) {
if (err instanceof SyntaxError) {
console.error(`Response is not valid ${datatype.toUpperCase()}.`)
} else {
console.error('Fetch error:', err)
}
} finally {
if (timeoutId) {
clearTimeout(timeoutId)
}
}
}
|