@anissoft/request v2.0.7
Welcome to @anissoft/request š
Wrap on top of the standart fetch API with few tweaks, which make may life easier
Installation
npm install @anissoft/requestUsage
You just need to intitalize and export request function:
// request.ts
import { initialize } from "@anissoft/request";
export const request = initialize(fetch, options);After it you can import and use it:
import request from "../request";
(async () => {
  try {
    const response1 = await request("https://example.com/api/method", {
      method: "POST",
      body: { foo: "bar" },
      isOk: ({ status }) => status < 400,
      shouldThrow: true,
    });
    console.log("Response body:", response1.text());
    console.log("Parsed json:", response1.json());
  } catch (response1) {
    console.error(response1.statusText);
  }
  // equivalent for
  const response2 = await fetch("https://example.com/api/method", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ foo: "bar" }),
  });
  if (response2.status >= 400) {
    console.error(response2.statusText);
  } else {
    const text = await response2.text();
    console.log("Response body:", text);
    try {
      console.log("Parsed json:", JSON.parse(text));
    } catch (e) {
      console.log("Parsed json:", undefined);
    }
  }
})();Features
isOk
You can specify custom condition in RequestInit to define if response is ok:
request("https://example.com/api/method", {
  isOk: ({ status }) => status < 400 || status === 429,
});shouldThrow
If shouldThrow set true in RequestInit - request will be rejected if response.ok is falsy:
request("https://example.com/api/method", {
  shouldThrow: true,
}).catch((response) => {
  console.log(response.status);
});Stringify object-like body
... and automaticaly set 'Content-Type' header in 'application/json':
request("https://example.com/api/method", {
  method: "POST",
  body: { foo: "bar" },
});Sync .json() and .text() methods
.json() and .text() methods contains preparsed data and can be executed synchronously multiple times:
const response = request("https://example.com/api/method");
console.log(response.text());
console.log(response.json());actions object
You can assign actions for specific status codes, or general events:
request("https://example.com/api/method", {
  method: "POST",
  isOk: ({ status, ok }) => ok || status === 429,
  actions: {
    ok: (response) => {
      console.log("Executes if response.ok === true, or if isOk() returns true");
    },
    "403": (response) => {
      console.log("Executes if response.status === 403");
    },
    "401,402": (response) => {
      console.log("Executes if response.status is 401 or 402");
    },
    "500-516": (response) => {
      console.log("Executes if response.status >= 500 and response.status <= 516");
    },
    network: (response) => {
      console.log("Executes network exception was thrown");
    },
    default: (response) => {
      console.log("Executes in all other cases");
    },
  },
});Helpers
getParameterByName(name: string, url: string)
Returns the last value of query parameter from given url. If no url specified - will use global location.href in browser, or empty string in NodeJS.
const { getParameterByName } = require("@anissoft/request");
const search = getParameterByName("search", "https://example.com/api/method?search=123");
console.log(search); // '123';Author
š¤ Alexey Anisimov
- Github: @Anissoft
š¤ Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
3 years ago
3 years ago
3 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago