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

@rivermountain/fetch-to-axios

v0.0.21

Published

Advanced fetch API wrapper with Axios-like interface

Downloads

1,192

Readme

@rivermountain/fetch-to-axios

Korean

Axios와 유사한 인터페이스를 가진 Fetch API 기반의 HTTP 클라이언트 라이브러리입니다. TypeScript로 작성되었으며, 타입 안정성과 가벼운 번들 사이즈가 특징입니다.

특징

  • 🚀 Fetch API 기반 구현
  • 📦 의존성 없는 경량 패키지 (16.5kB)
  • 💪 TypeScript로 작성된 견고한 타입 지원
  • 🔄 인터셉터 지원
  • ⚡ 자동 재시도 기능
  • ⏱️ 타임아웃 설정
  • 🔍 쿼리 파라미터 지원

설치

npm install @rivermountain/fetch-to-axios

기본 사용법

import { createClient } from "@rivermountain/fetch-to-axios";

// 클라이언트 인스턴스 생성
const client = createClient({
  baseURL: "https://api.example.com",
  headers: {
    "Content-Type": "application/json",
  },
});

// GET 요청
const getData = async () => {
  const response = await client.get("/users");
  return response;
};

// POST 요청
const createUser = async (userData) => {
  const response = await client.post("/users", userData);
  return response;
};

고급 기능

인터셉터 사용

// 요청 인터셉터
client.interceptors.request.push({
  onFulfilled: (config) => {
    // 브라우저 환경에서만 실행
    if (typeof window !== "undefined") {
      const token = localStorage.getItem("token");
      if (token) {
        config.headers = {
          ...config.headers,
          Authorization: `Bearer ${token}`,
        };
      }
    }
    return config;
  },
});

// 응답 인터셉터
client.interceptors.response.push({
  onFulfilled: (response) => response,
  onRejected: async (error) => {
    // 에러 처리
    return Promise.reject(error);
  },
});

재시도 설정

const response = await client.get("/users", {
  retryConfig: {
    maxRetries: 3,
    retryDelay: 1000,
    retryCondition: (error) => error.status === 500,
  },
});

타임아웃 설정

const response = await client.get("/users", {
  timeout: 5000, // 5초
});

쿼리 파라미터

const response = await client.get("/users", {
  params: {
    page: 1,
    limit: 10,
    search: "john",
  },
});

Next.js 지원

const response = await client.get("/users", {
  cache: "force-cache",
  next: {
    revalidate: 3600,
    tags: ["users"],
  },
});

API 에러 처리

import { APIError } from "@rivermountain/fetch-to-axios";

try {
  const response = await client.get("/users");
} catch (error) {
  if (error instanceof APIError) {
    console.log(error.status); // HTTP 상태 코드
    console.log(error.message); // 에러 메시지
  }
}

Config 타입 정의

interface Config {
  baseURL?: string;
  headers?: Record<string, string>;
  timeout?: number;
  method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
  params?: Record<string, any>;
  body?: any;
  credentials?: "same-origin" | "include" | "omit";
  retryConfig?: {
    maxRetries: number;
    retryDelay: number;
    retryCondition?: (error: any) => boolean;
  };
  cache?: RequestCache;
  next?: {
    revalidate?: number | false;
    tags?: string[];
  };
}

주의사항

  • Node.js 환경에서는 fetch가 전역으로 제공되어야 합니다 (Node.js 18+ 또는 polyfill 필요)
  • 브라우저 환경에서 CORS 설정이 필요한 경우 적절한 credentials 옵션을 설정해야 합니다

English

A lightweight HTTP client library based on Fetch API with an Axios-like interface. Built with TypeScript for robust type safety and minimal bundle size.

Features

  • 🚀 Built on Fetch API
  • 📦 Zero dependencies (16.5kB)
  • 💪 First-class TypeScript support
  • 🔄 Interceptors support
  • ⚡ Automatic retry functionality
  • ⏱️ Timeout configuration
  • 🔍 Query parameters support

Installation

npm install @rivermountain/fetch-to-axios
Basic Usage
typescriptCopyimport { createClient } from '@rivermountain/fetch-to-axios';

// Create client instance
const client = createClient({
  baseURL: 'https://api.example.com',
  headers: {
    'Content-Type': 'application/json'
  }
});

// GET request
const getData = async () => {
  const response = await client.get('/users');
  return response;
};

// POST request
const createUser = async (userData) => {
  const response = await client.post('/users', userData);
  return response;
};
Advanced Features
Using Interceptors
typescriptCopy// Request interceptor
client.interceptors.request.push({
  onFulfilled: (config) => {
    // Execute only in browser environment
    if (typeof window !== 'undefined') {
      const token = localStorage.getItem('token');
      if (token) {
        config.headers = {
          ...config.headers,
          Authorization: `Bearer ${token}`
        };
      }
    }
    return config;
  }
});

// Response interceptor
client.interceptors.response.push({
  onFulfilled: (response) => response,
  onRejected: async (error) => {
    // Error handling
    return Promise.reject(error);
  }
});
Retry Configuration
typescriptCopyconst response = await client.get('/users', {
  retryConfig: {
    maxRetries: 3,
    retryDelay: 1000,
    retryCondition: (error) => error.status === 500
  }
});
Timeout Configuration
typescriptCopyconst response = await client.get('/users', {
  timeout: 5000  // 5 seconds
});
Query Parameters
typescriptCopyconst response = await client.get('/users', {
  params: {
    page: 1,
    limit: 10,
    search: 'john'
  }
});
Next.js Support
typescriptCopyconst response = await client.get('/users', {
  cache: 'force-cache',
  next: {
    revalidate: 3600,
    tags: ['users']
  }
});
API Error Handling
typescriptCopyimport { APIError } from '@rivermountain/fetch-to-axios';

try {
  const response = await client.get('/users');
} catch (error) {
  if (error instanceof APIError) {
    console.log(error.status);  // HTTP status code
    console.log(error.message); // Error message
  }
}
Config Type Definition
typescriptCopyinterface Config {
  baseURL?: string;
  headers?: Record<string, string>;
  timeout?: number;
  method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
  params?: Record<string, any>;
  body?: any;
  credentials?: 'same-origin' | 'include' | 'omit';
  retryConfig?: {
    maxRetries: number;
    retryDelay: number;
    retryCondition?: (error: any) => boolean;
  };
  cache?: RequestCache;
  next?: {
    revalidate?: number | false;
    tags?: string[];
  };
}
Important Notes

In Node.js environments, fetch must be globally available (Node.js 18+ or polyfill required)
For browser environments requiring CORS, appropriate credentials options must be configured

License
MIT