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

sim-fetch

v0.0.7

Published

`SimFetch`는 TypeScript와 JavaScript에서 HTTP 요청을 쉽게 만들기 위한 유틸리티 클래스입니다. 커스텀 헤더와 요청 취소를 지원하는 GET, POST, PATCH, DELETE 요청을 위한 정적 메서드를 제공합니다.

Downloads

8

Readme

SimFetch

SimFetch는 TypeScript와 JavaScript에서 HTTP 요청을 쉽게 만들기 위한 유틸리티 클래스입니다. 커스텀 헤더와 요청 취소를 지원하는 GET, POST, PATCH, DELETE 요청을 위한 정적 메서드를 제공합니다.

기능

  • 간단한 API: 일반적인 HTTP 메서드를 위한 사용하기 쉬운 정적 메서드.
  • 요청 취소: AbortController를 사용하여 요청 취소를 자동으로 관리.
  • 커스텀 헤더: 각 요청에 대해 기본 및 커스텀 헤더를 지원.
  • TypeScript 지원: 향상된 개발 경험을 위한 완전한 타입 지원.

설치

SimFetch를 npm을 통해 설치할 수 있습니다:

npm install sim-fetch

또는 yarn을 통해 설치할 수 있습니다:

yarn add sim-fetch

사용법

설정

프로젝트에 SimFetch를 임포트합니다:

import { SimFetch } from 'sim-fetch';

기본 헤더 설정

모든 요청에 대한 기본 헤더를 설정할 수 있습니다:

SimFetch.setDefaultHeaders({
  'Content-Type': 'application/json' // default
  'Authorization': 'Bearer your-token', 
});

기본 헤더 제거

기본 헤더를 제거하려면 removeDefaultHeader 메서드를 사용합니다:

SimFetch.removeDefaultHeader('Authorization');

특정 헤더 추가

특정 헤더를 추가한 요청을 설정할 수 있습니다.

try {
  // 커스텀 헤더를 포함한 GET 요청
  const customHeaders = {
    Authorization: 'Bearer your-token',
    'X-Custom-Header': 'custom-value',
  };

  const response = await SimFetch.get<any>(
    'https://api.sampleapis.com/wines/reds',
    customHeaders,
  );

  const { status, data } = response;

  if (status === 200) {
    return data;
  }
} catch (error) {
  if (error instanceof SimFetchError) {
    const { status, message } = error;
    ...
  }
}

요청 만들기

GET 요청 : URL 통한 요청

interface ApiResponse {
  id: string;
  name: string;
  description?: string;
}

try {
  const response = await SimFetch.get<ApiResponse>('https://api.example.com');
  const { status, data } = response;

  if (status === 200) {
    return data;
  }
} catch (error) {
  if (error instanceof SimFetchError) {
    const { status, message } = error;
    ...
  }
}

GET 요청 : Query Params 활용 요청

interface ApiResponse {
  id: string;
  name: string;
  description?: string;
}

try {
  const response = await SimFetch.get<Item>('https://example.com/items', {
    params: { id: '1' },
  });
  const { status, data } = response;

  if (status === 200) {
    return data;
  }
} catch (error) {
  if (error instanceof SimFetchError) {
    const { status, message } = error;
    ...
  }
}

GET 요청 : Params 활용 요청

interface ApiResponse {
  id: string;
  name: string;
  description?: string;
}

try {
  const response = await SimFetch.get<ApiResponse>('https://api.example.com/1');
  const { status, data } = response;

  if (status === 200) {
    return data;
  }
} catch (error) {
  if (error instanceof SimFetchError) {
    const {status, message} = error
    ...
  }
}

POST 요청

interface ApiResponse {
  id: string;
  name: string;
  description?: string;
}

try {
  const requestBody = { id: '1', name: 'New Item', description: 'Item Description' };
  const response = await SimFetch.post<ApiResponse>(
    'https://api.example.com',
    requestBody,
  );

  const { status, data } = response;

  if (status === 201) {
    return data;
  }
} catch (error) {
  if (error instanceof SimFetchError) {
    const {status, message} = error
    ...
  }
}

PATCH 요청

try {
  const requestBody = { name: 'Updated Item' };
  const response = await SimFetch.patch(
    'https://api.example.com/1',
    requestBody,
  );

  const { status, data } = response;

  if (status === 200) {
    return data;
  }
} catch (error) {
  if (error instanceof SimFetchError) {
    const { status, message } = error;
    ...
  }
}

DELETE 요청

interface ApiResponse {
  id: string;
  name: string;
  description?: string;
}

try {
  const response = await SimFetch.delete<ApiResponse>(
    'https://api.example.com/1',
  );

  const { status, data } = response;

  if (status === 200) {
    return data;
  }
} catch (error) {
  if (error instanceof SimFetchError) {
    const { status, message } = error;
    ...
  }
}