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

    Class SoFetchPromise<T>

    An awaitable promise-like class that additionally allows event and error handlers to be attached to the HTTP request

    const unicorn = await soFetch("https://unicorns.com/1234")
    .beforeSend(req:SoFetchRequest) => {
    console.info(`Finding my unicorn at ${req.url}`)
    })
    .catchHttp(404, (res:Response) => {
    console.error("This unicorn can't be found")
    })

    Type Parameters

    • T
    Index

    Constructors

    • Type Parameters

      • T

      Parameters

      • executor: (
            resolve: (value: T | PromiseLike<T>) => void,
            reject: (reason?: any) => void,
        ) => void

      Returns SoFetchPromise<T>

    Properties

    beforeFetchSendHandlers: (
        (init: RequestInit) => void | RequestInit | Promise<void | RequestInit>
    )[] = []
    beforeSendHandlers: (
        (
            request: SoFetchRequest,
        ) => void | SoFetchRequest | Promise<void | SoFetchRequest>
    )[] = []
    catch: <TResult = never>(
        onrejected?: null | ((reason: any) => TResult | PromiseLike<TResult>),
    ) => Promise<T | TResult>

    Type Declaration

      • <TResult = never>(
            onrejected?: null | ((reason: any) => TResult | PromiseLike<TResult>),
        ): Promise<T | TResult>
      • Attaches a callback for only the rejection of the Promise.

        Type Parameters

        • TResult = never

        Parameters

        • Optionalonrejected: null | ((reason: any) => TResult | PromiseLike<TResult>)

          The callback to execute when the Promise is rejected.

        Returns Promise<T | TResult>

        A Promise for the completion of the callback.

    errorHandlers: ErrorHandlerDict = {}
    finally: (onfinally?: null | (() => void)) => Promise<T>

    Type Declaration

      • (onfinally?: null | (() => void)): Promise<T>
      • Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The resolved value cannot be modified from the callback.

        Parameters

        • Optionalonfinally: null | (() => void)

          The callback to execute when the Promise is settled (fulfilled or rejected).

        Returns Promise<T>

        A Promise for the completion of the callback.

    onRequestCompleteHandlers: (
        (
            response: Response,
            requestData: { duration: number; method: string },
        ) => void | Promise<void>
    )[] = []
    then: <TResult1 = T, TResult2 = never>(
        onfulfilled?: null | ((value: T) => TResult1 | PromiseLike<TResult1>),
        onrejected?: null | ((reason: any) => TResult2 | PromiseLike<TResult2>),
    ) => Promise<TResult1 | TResult2>

    Type Declaration

      • <TResult1 = T, TResult2 = never>(
            onfulfilled?: null | ((value: T) => TResult1 | PromiseLike<TResult1>),
            onrejected?: null | ((reason: any) => TResult2 | PromiseLike<TResult2>),
        ): Promise<TResult1 | TResult2>
      • Attaches callbacks for the resolution and/or rejection of the Promise.

        Type Parameters

        • TResult1 = T
        • TResult2 = never

        Parameters

        • Optionalonfulfilled: null | ((value: T) => TResult1 | PromiseLike<TResult1>)

          The callback to execute when the Promise is resolved.

        • Optionalonrejected: null | ((reason: any) => TResult2 | PromiseLike<TResult2>)

          The callback to execute when the Promise is rejected.

        Returns Promise<TResult1 | TResult2>

        A Promise for the completion of which ever callback is executed.

    timeout: number = 30000

    Methods

    • Adds a handler which allows developers to modify the low-level fetch RequestInit object before the HTTP request is made. These handlers execute after beforeSend handlers. This is useful for one-off occasions when you need to access some aspect of the low-level Fetch API. If you're using this a lot it might make more sense for you to use the Fetch API directly.

      Parameters

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

      Returns SoFetchPromise<T>

      //An example of how you might send both files and data in a single request.
      const postFilesAndDataResponse = await soFetch.put<PostFilesAndDataResponse>("https://example.com/files-and-data").beforeFetchSend((init:RequestInit) => {
      const formData = new FormData()
      formData.append("company", "Antoinette");
      formData.append("file1", myFile)
      const headers = {...init.headers} as Record<string,string>
      if (headers["content-type"]) {
      delete headers["content-type"]
      }
      init.body = formData
      init.headers = headers
      return init
      })

      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 you add an error handler for a specific status code here any corresponding handlers in the config will not be executed.

      Parameters

      • status: number

        An HTTP status code

      • handler: (response: Response) => void

        A function which accepts a Fetch Response as an argument

      Returns SoFetchPromise<T>

      const unicorn = await soFetch("https://unicorns.com/1234")
      .catchHttp(404, (res:Response) => {
      console.error("This unicorn can't be found")
      })

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

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

      Parameters

      • handler: (response: Response) => void | Promise<void>

      Returns SoFetchPromise<T>

      await soFetch("https://example.com/users",{name:"Sarah", id:1234}).onRequestComplete((r: Response) => {
      console.info(`Response received from ${r.url} with status ${r.status}`
      })

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