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
KeyTypeDefaultDescription
fetchInstancefetch instancethe default runtime fetch apiwill be wrapped by the vFetch
configobject{}affects how the library should behave
config.baseUrlstringundefinedthe requests base URL, will be ignored when you add a full URL http(s)://domain.com
config.headersobject{}default headers to be sent with every request (you can override them later)
config.muteWarningsbooleanfalsemutes the library warning
config.muteLogsbooleanfalsemutes the library logs
config.muteErrorsbooleantruemutes the library errors
config.responseTypestringjsonsets the default response type, set it to pure to receive the response without any modifications
interceptorsobject{}bunch of functions can help you to intercept and reshape The Requests, The Responses, and The Errors
interceptors.onBeforeRequestfunctionundefinedruns before the request gets sent
interceptors.onAfterRequestfunctionundefinedruns before sending the request but after the onBeforeRequest function (experimental)
interceptors.onBeforeResponsefunctionundefinedruns directly after the response gets returned.
interceptors.onErrorfunctionundefinedruns wherever the request returns an error