1.0.21 • Published 1 year ago

@web-atoms/fetch-builder v1.0.21

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year 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

1 year ago

1.0.20

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago