Fetcher class for fetching data from a URL

This class provides methods for fetching data from a URL with different content types. It uses the URLBuilder class to build the URL and the Zod schema to parse the response. It also uses the fetch API to make the request.

const data = await Fetcher.get({
urlBuilder: new URLBuilder({ baseUrl: 'https://api.example.com' }),
schema: z.object({ name: z.string() }),
});

Constructors

Methods

  • GET request

    Fetches data from a URL with a GET request.

    Type Parameters

    • T = unknown

    Parameters

    • params: GetParams<T>

      The parameters for the GET request

    Returns Promise<T>

    The parsed response

    HTTPError if the response is not ok

    ZodError if the response is not valid

    const data = await Fetcher.get({
    urlBuilder: new URLBuilder({ baseUrl: 'https://api.example.com' }),
    schema: z.object({ name: z.string() }),
    });
  • Posts array buffer data to a URL

    Posts array buffer data to a URL with a POST request.

    Type Parameters

    • T = unknown

    Parameters

    Returns Promise<T>

    The parsed response

    HTTPError if the response is not ok

    ZodError if the response is not valid

    const data = await Fetcher.postArrayBuffer({
    urlBuilder: new URLBuilder({ baseUrl: 'https://api.example.com' }),
    body: new ArrayBuffer(),
    schema: z.object({ name: z.string() }),
    contentType: 'application/octet-stream',
  • Posts blob data to a URL

    Posts blob data to a URL with a POST request.

    Type Parameters

    • T = unknown

    Parameters

    Returns Promise<T>

    The parsed response

    HTTPError if the response is not ok

    ZodError if the response is not valid

    const data = await Fetcher.postBlob({
    urlBuilder: new URLBuilder({ baseUrl: 'https://api.example.com' }),
    body: new Blob(),
    schema: z.object({ name: z.string() }),
    contentType: 'image/png',
  • Posts form data to a URL

    Posts form data to a URL with a POST request.

    Type Parameters

    • T = unknown

    Parameters

    Returns Promise<T>

    The parsed response

    HTTPError if the response is not ok

    ZodError if the response is not valid

    const data = await Fetcher.postFormData({
    urlBuilder: new URLBuilder({ baseUrl: 'https://api.example.com' }),
    body: new FormData(),
    schema: z.object({ name: z.string() }),
    contentType: 'application/x-www-form-urlencoded',
  • Posts JSON data to a URL

    Posts JSON data to a URL with a POST request.

    Type Parameters

    • T = unknown

    Parameters

    Returns Promise<T>

    The parsed response

    HTTPError if the response is not ok

    ZodError if the response is not valid

    const data = await Fetcher.postJson({
    urlBuilder: new URLBuilder({ baseUrl: 'https://api.example.com' }),
    body: { name: 'example' },
    schema: z.object({ name: z.string() }),
    contentType: 'application/json',
    });
  • POST text data to a URL

    Posts text data to a URL with a POST request.

    Type Parameters

    • T = unknown

    Parameters

    Returns Promise<T>

    The parsed response

    HTTPError if the response is not ok

    ZodError if the response is not valid

    const data = await Fetcher.postText({
    urlBuilder: new URLBuilder({ baseUrl: 'https://api.example.com' }),
    body: 'Hello, world!',
    schema: z.object({ name: z.string() }),
    contentType: 'text/plain',
    });