@antoinette-agency/sofetch
    Preparing search index...

    Class SoFetchConfig

    Configures all requests for a specific soFetch instance

    Index

    Constructors

    Properties

    authenticationKey: string = "SOFETCH_AUTHENTICATION"

    The key which is used if an authentication token is persisted via cookies, localStorage or sessionStorage

    authenticationType: AuthenticationType = null

    Specifies how soFetch should send authentication credentials to the server

    authHeaderKey: string = ""

    The key which is used if an authentication token is sent to the server via a custom header

    authQueryStringKey: string = ""

    The key which is used if an authentication token is sent to the server via the query string

    authTokenStorage: AuthTokenStorageType = null

    Specifies how (or if) soFetch should persist and authentication

    baseUrl: string = ""

    The base URL for all HTTP requests in the instance. If absent this is assumed to be the current base url. If running in Node relative requests without a baseUrl will throw an error.

    beforeFetchSendHandlers: (
        (
            request: RequestInit,
        ) => void | RequestInit | Promise<void | RequestInit>
    )[] = []
    beforeSendHandlers: (
        (
            request: SoFetchRequest,
        ) => void | SoFetchRequest | Promise<void | SoFetchRequest>
    )[] = []
    onRequestCompleteHandlers: (
        (
            response: Response,
            requestData: { duration: number; method: string },
        ) => void | Promise<void>
    )[] = []

    Methods

    • Adds a handler which will be executed before every request. beforeSend handlers on the config will be executed before request-specific handlers

      Parameters

      • handler: (request: RequestInit) => void | RequestInit | Promise<void | RequestInit>

      Returns void

      soFetch.config.beforeSend((req:SoFetchRequest) => {
      console.info(`Sending ${req.method} request to URL ${req.url}`
      })

      For more examples see https://sofetch.antoinette.agency

    • Adds a handler which will be executed on receipt from the server of the specified status code. Multiple handlers will be executed in the order in which they are added. If a request has it's own handler(s) for a given status code the corresponding handlers in the config will not be executed.

      Parameters

      • status: number

        An HTTP status code

      • handler: (res: Response) => void

        A function which accepts a Fetch Response as an argument

      Returns void

      soFetchConfig.catchHttp(404, (res:Response) => {
      alert("This object can't be found")
      })

      For more examples see https://sofetch.antoinette.agency

    • Adds a handler which will be executed after every request. Handlers will fire regardless of whether the response status code indicated an error

      Parameters

      • handler: (
            r: Response,
            metaData: { duration: number; method: string },
        ) => void | Promise<void>

      Returns void

      soFetch.config.onRequestComplete((r: Response) => {
      console.info(`Response received from ${r.url} with status ${r.status}`
      })

      For more examples see https://sofetch.antoinette.agency

    • Use this method to set an auth token after it's been received from a server, typically as the response to a login request

      Parameters

      • authToken: string

      Returns void

    • Tells soFetch to use basic authorization when communicating with the server

      Parameters

      • __namedParameters: {
            authenticationKey?: string;
            authTokenStorage?: AuthTokenStorageType;
            password?: string;
            username?: string;
        }

      Returns void

    • Tells soFetch to use bearer authentication to send an authentication token to the server

      Parameters

      • props: {
            authenticationKey?: string;
            authToken?: string;
            authTokenStorage?: AuthTokenStorageType;
        } = {}

      Returns void

    • Tells soFetch to authenticate using cookies.

      Parameters

      • Optionalprops: { authenticationKey?: string; authToken?: string }

      Returns void

    • Tells soFetch to send an authentication token to the server

      Parameters

      • __namedParameters: {
            authenticationKey?: string;
            authToken?: string;
            authTokenStorage?: AuthTokenStorageType;
            headerKey: string;
        }

      Returns void

    • Tells soFetch to send append an authentication token to the request query string

      Parameters

      • __namedParameters: {
            authenticationKey?: string;
            authToken?: string;
            authTokenStorage?: AuthTokenStorageType;
            queryStringKey: string;
        }

      Returns void