1.0.7 • Published 2 years ago

@co-labo-hub/fetcha v1.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

fetcha!

Comparison

  • fetch
fetch("https://example.com/api", {
  method: "POS", // typo
  headers: {
    "Content-Typ": "application/jso", // typo
    Authorization: `Bearer ${"foo"}`,
  },
  credentials: "include",
  mode: "cors",
  body: JSON.stringify({ name: "bar" }), // JSON.stringify
})
  .then((resp) => {
    // normal and abnormal.

    if (resp.ok) return resp.json();

    switch (resp.status) {
      case 500:
        location.href = "/500.html";
        return;
      case 404:
        location.href = "/404.html";
        return;
      case 401:
        location.href = "/login.html";
        return;
      default:
        alert(resp.statusText);
    }
  })
  .then(({ id }: { id: number }) => console.log(id))
  .catch((e) => alert(e.message));
  • fetcha!
fetcha("https://example.com/api")
  .contentType("application/json")
  .header("Authrization", `Bearer ${"foo"}`)
  .credentials("include")
  .mode("cors")
  .post({ name: "bar" })
  .then((resp) => resp.toJson<{ id: number }>())
  .then(({id}) => console.(id))
  .catch((e: FetchaError) => {
    switch (e.status) {
      case 500:
        location.href = "/500.html";
        return;
      case 404:
        location.href = "/404.html";
        return;
      case 401:
        location.href = "/login.html";
        return;
      default:
        alert(e.message);
    }
  });

Install

npm install @co-labo-hub/fetcha
import { fetcha, FetchaError } from "@co-labo-hub/fetcha";

Examples

  • /src/example.ts

Methods

methodstypedescription
url(url: string \| URL)setting
origin(origin?: string \| URL)setting
header(name: string, value?: string)setting"": delete
contentType(mime: "application/json" \| "")setting"": delete
body(body: BodyInit \| Json)setting
mode(mode: RequestMode)setting
credentials(credentials: RequestCredentials)setting
cache(cache: RequestCache)setting
redirect(redirect: RequestRedirect)setting
referrer(referrer: string)setting
referrerPolicy(referrerPolicy: ReferrerPolicy)setting
integrity(integrity: string)setting
keepalive(keepalive: boolean)setting
signal(signal: AbortSignal)setting
fetch(method: Method = "GET")execution
get()execution
head()execution
delete()execution
post(body?: BodyInit \| Json)execution
patch(body?: BodyInit \| Json)execution
put(body?: BodyInit \| Json)execution

Response

additional properties

propertiesreturn
toJson()Promise

Error

FetchaError: constructor({ message, request, response }: FetchaErrorProps)

propertiesdescriptiontype
requestrequest and urlRequestInit & { url?: string | URL }
responseoriginal responseResponse | undefined
headersof responseHeaders | undefined
okof responseboolean | undefined
redirectedof responseboolean | undefined
statusof responsenumber | undefined
statusTextof responsestring | undefined
typeof responseResponseType | undefined
urlof responsestring | undefined
bodyof responseReadableStream | null | undefined
bodyUsedof responseboolean | undefined
1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago