1.0.21 • Published 10 months ago

@web-atoms/fetch-builder v1.0.21

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Fetch Builder

Simple Fluent Interface to call fetch

Why?

To make REST API easier. Lets have following examples.

Download Text

const text = await FetchBuilder.get("https://someurl.com")
    .asText();

Post Json body and retrieve Json

const result = await FetchBuilder.post("https://someapi.com")
    .header("x-api-key", someKey)
    .jsonBody({
        name: "bla bla bla",
        email: "bla@bla.bla"
    })
    .asJson<IResult>();

Reuse builder

This is important as you can configure builder once with default headers.

    const client = FetchBuilder
        .url("https://somewhere.com/")
        .header("x-api-key", someKey);

    const result = await client.post("/api/orders/create")
        .jsonBody({
            productId: 3
        })
        .asJson<IResult>();

Log Failed Requests

This will log only failed requests.

    const client = FetchBuilder
        .url("https://somewhere.com/")
        .header("x-api-key", someKey)
        .logWhenFailed(console.error);

    const result = await client.post("/api/orders/create")
        .jsonBody({
            productId: 3
        })
        .asJson<IResult>();

Query String

    const client = FetchBuilder
        .url("https://somewhere.com/")
        .header("x-api-key", someKey)
        .logWhenFailed(console.error);

    const result = await client.get("/api/orders/reports")
        .query("year-start", 2015)
        .query("year-end", 2022)
        .query("search-text", "social mail") // <-- this will be encoded
        .asJson();
1.0.21

10 months ago

1.0.20

10 months ago

1.0.19

10 months ago

1.0.18

10 months ago

1.0.17

10 months ago

1.0.16

10 months ago

1.0.15

10 months ago

1.0.12

10 months ago

1.0.11

10 months ago

1.0.10

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago