• A wrapper around the fetch API that uses Prelude-IO's type-checking mechanisms to ensure that the response and request bodies are valid. Should mostly be compatible with the Fetch API, except Options.body's type is overwritten with the output type of the bodyBus, and the return type is an IOResult.

    Example

    import * as io from "@prelude-io/core";
    import { ioFetch } from "@prelude-io/fetch";

    const CatFact = io.Complex("CatFact", {
    fact: io.string,
    length: io.positiveNumber,
    });

    const CatFactsResponse = io.Complex("CatFactsResponse", {
    factCount: io.positiveNumber,
    facts: io.Vector(CatFact),
    });

    const CatFactRequest = io.JSON.chain(
    io.Complex("CatFactRequest", {
    facts: io.number,
    })
    );

    const catFactFetch = ioFetch(CatFactsResponse, CatFactRequest);

    const result = await catFactFetch("https://catfacts.example.com", {
    body: { facts: 4 },
    });

    console.log(result); // => IORight containing `{ factCount: 4, facts: [...] }`

    Type Parameters

    • O

      The output type of the responseBus

    • I

      The output type of the bodyBus

    • B extends BodyInit

      The input type of the bodyBus, that will be used as the body of the request

    Parameters

    • responseBus: Bus<any, O>

      The Bus that will be used to parse the response

    • bodyBus: Bus<B, I> = null

      The Bus that will be used to parse the request body. Optional.

    • responseHandler: ResponseHandler = jsonResponseHandler

      A function that parses the raw response to something your Bus can handle (i.e, gets the body and parses it to JSON). Optional, defaults to jsonResponseHandler.

    Returns WrappedFetch<O, I>

Generated using TypeDoc