Configurations
Learn how to configure Very Good Fetch.
The Config is optional, but it's highly recommended to use it if you're working on a large scale project.
vSetupConfig
api.config.ts
import { vSetupConfig } from "very-good-fetch";
const vConfig = vSetupConfig({
fetchInstance: fetch, // optional, helps to run with any environment or library (e.g. node-fetch)
config: {
baseUrl: "https://dummyjson.com",
headers: {
// will be merged with every request, and can be overridden later on if needed
"Content-Type": "application/json",
},
muteWarnings: false, // optional, will mute warnings if set to true
muteLogs: false, // optional, will mute logs if set to true
muteErrors: false, // optional, will mute errors if set to true
responseType: "json", // optional, sets the default response type (e.g. json, text, blob, etc.)
},
interceptors: {
onBeforeRequest: (config) => {
// do something before the request is sent
return config;
},
onAfterRequest: (request) => {
// do something after the request is sent immediately before the response is received
return request;
},
onBeforeResponse: (response) => {
// do something with the response data
return response;
},
onError: (error) => {
// do something with the error
return error;
},
},
});
// The vConfig constant will hold an instance for the library
vSetupConfig Apis
Key | Type | Default | Description |
---|---|---|---|
fetchInstance | fetch instance | the default runtime fetch api | will be wrapped by the vFetch |
config | object | {} | affects how the library should behave |
config.baseUrl | string | undefined | the requests base URL, will be ignored when you add a full URL http(s)://domain.com |
config.headers | object | {} | default headers to be sent with every request (you can override them later) |
config.muteWarnings | boolean | false | mutes the library warning |
config.muteLogs | boolean | false | mutes the library logs |
config.muteErrors | boolean | true | mutes the library errors |
config.responseType | string | json | sets the default response type, set it to pure to receive the response without any modifications |
interceptors | object | {} | bunch of functions can help you to intercept and reshape The Requests, The Responses, and The Errors |
interceptors.onBeforeRequest | function | undefined | runs before the request gets sent |
interceptors.onAfterRequest | function | undefined | runs before sending the request but after the onBeforeRequest function (experimental) |
interceptors.onBeforeResponse | function | undefined | runs directly after the response gets returned. |
interceptors.onError | function | undefined | runs wherever the request returns an error |