Skip to content

fetchData

Requires a Node.js runtime ('qsu/node')

This function is used to make node:fetch easier to use while ensuring data is returned regardless of the response status. By default, it uses the GET method, but other methods such as POST can also be used with separate options.

Depending on the Content-Type of the request URL, the returned data can be either a string or an object.

If the request fails, returns a 204 (No Content) status code, or an error occurs, it returns null without throwing a separate exception.

However, you can receive error details via the onError event.

Parameters

NameTypeRequiredDefault
urlstring
httpRequestOptionsHTTPRequestOption
Request options. See the table below.
HTTPRequestOption
NameTypeRequiredDefault
authobject
Authentication credentials. See the table below.
getboolean
Same as method: 'get'
postboolean
Same as method: 'post'
putboolean
Same as method: 'put'
deleteboolean
Same as method: 'delete'
patchboolean
Same as method: 'patch'
toStreamboolean
timeoutnumber
method'get' | 'post' | 'put' | 'delete' | 'patch'
hoststring
If this value is not specified, the URL must be a full path.
queryParametersobject
bodyobject | string | FormData | null
bodyType'text' | 'json' | 'form-data' | 'x-www-form-urlencoded'
headersobject | null
onErrorfunction
(error) => void
auth
NameTypeRequiredDefault
apiKeystring
'x-API-key' header
bearerstring
'Authorization Bearer' header

Returns

string | null | object

Examples

javascript
console.log(await fetchData('https://github.com'), { get: true });
python
print(fetchData('https://github.com'), { 'get': True })

Released under the MIT License