npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

http-request-fetch

v1.0.1

Published

The http request module based on the fetch API.

Downloads

7

Readme

http-request-fetch

fetch API에 기반한 http 요청 모듈입니다.

설치 방법

Using npm:

npm i http-request-fetch

Using yarn:

yarn add http-request-fetch

사용 방법

in ES Module:

import createHttp from "http-request-fetch";

in CommonJS:

const createHttp = require("http-request-fetch").default;

예시

import createHttp from "http-request-fetch";

const http = createHttp();

http.request("/api/1").then(function (response) {
  // ...
});

Base URL, Default Config

http 함수를 생성할 때, 요청시 기본 url과 기본 config를 설정할 수 있습니다.

const BASE_URL = "https://api.example.com";

const DEFAULT_CONFIG = { headers: { "Content-Type": "application/json" } };

const http = createHttp(BASE_URL, DEFAULT_CONFIG);

http.request("/api/1"); // https://api.example.com/api/1로 요청

Interceptors

http 요청이나 응답이 처리되기 전에 가로채서 특정 로직을 수행하도록 설정할 수 있습니다.

Interceptors 사용법 및 Interceptor 종류

const http = createHttp();

http.registerInterceptor({
  onRequest: function (config) {
    // request를 보내기 전 수행할 로직
    return config;
  },
  onResponse: function (response) {
    // response를 받고나서 수행할 로직
    return response;
  },
  onRequestError: function (reason) {
    // request 과정에서 에러가 발생할 경우 수행할 로직
    return Promise.reject(reason);
  },
  onResponseError: function (reason) {
    // response 과정에서 에러가 발생할 경우 수행할 로직
    return Promise.reject(reason);
  },
});

Interceptor를 활용해서 인증 토큰 갱신, 응답 에러 처리 등 기존의 반복적이고 불편했던 통신 로직들을 쉽게 처리할 수 있습니다.

HttpResponse Type

http의 응답 값은 fetch API의 Response 타입을 확장한 HttpResponse 타입의 응답 값을 반환 받습니다.

HttpResponse

HttpResponse 타입은 Response 타입에서 data 필드가 추가되었습니다.

type HttpResponse<T extends object> = {
  data: T;
  config: RequestInit;
  headers: Headers;
  ok: boolean;
  redirected: boolean;
  status: number;
  statusText: string;
  type: ResponseType;
  url: string;
};

Response의 body가 JSON 형식일 경우, JSON 파싱을 해서 data 필드에 담겨지게 됩니다.

사용 예시

// before using http
fetch("/api/1")
  .then((response) => response.json())
  .then((data) => {
    console.log(data);
  });

// after using http
http.request("/api/1").then((response) => {
  console.log(response.data);
});

기존의 fetch 요청에서의 json 파싱 과정은 반복되는 로직이고, 에러 발생 가능성도 있습니다.

http에서는 별도의 json 파싱 과정 없이 바로 data를 사용할 수 있습니다.

HTTP Method alias

편의를 위해 http 메소드별 alias를 사용하여 요청할 수 있습니다.

  • http.get(url, config?)
  • http.post(url, config?)
  • http.patch(url, config?)
  • http.put(url, config?)
  • http.delete(url, config?)

사용 예시

// GET 요청
http.get("/api/1");

// POST 요청
http.post("/api/1", { body: { example: 1 } });

License

MIT