Giter VIP home page Giter VIP logo

kofetch's Introduction

Kofetch πŸš€

Implement updated fetch api for using Next.js

Github

λ‚΄λΆ€ μ½”λ“œλ₯Ό 보고 μ‹ΆμœΌμ‹œλ‹€λ©΄, ν•΄λ‹Ή μ£Όμ†Œλ‘œ 접속해 μ£Όμ„Έμš”! core.ts 파일이 메인 λ‘œμ§μž…λ‹ˆλ‹€.
https://github.com/kokyusik91/kofetch

Prerequisites

ν•΄λ‹Ή ν”„λ‘œμ νŠΈλŠ” λ…Έλ“œ 버전 20.4.0μ—μ„œ λ§Œλ“€μ–΄ μ‘ŒμŠ΅λ‹ˆλ‹€. Node, NPMμ—μ„œ λ…Έλ“œλ₯Ό μ„€μΉ˜ν•΄ μ£Όμ„Έμš”.

$ npm -v && node -v
v20.4.0

Installation

npm을 μ‚¬μš©ν•œλ‹€λ©΄ λ‹€μŒ λͺ…λ Ήμ–΄λ₯Ό μ‹€ν–‰ν•΄ μ£Όμ„Έμš”.

$ npm install -i kofetch

yarn을 μ‚¬μš©ν•œλ‹€λ©΄ λ‹€μŒ λͺ…λ Ήμ–΄λ₯Ό μ‹€ν–‰ν•΄ μ£Όμ„Έμš”.

$ yarn add kofetch

Concept

보톡 데이터λ₯Ό νŽ˜μΉ­ν•˜λŠ”λ° 많이 μ‚¬μš©λ˜λŠ” axios λΌμ΄λΈŒλŸ¬λ¦¬μ™€ λΉ„μŠ·ν•˜κ²Œ μ‚¬μš©κ°€λŠ₯ν•˜λ„λ‘ 개발 ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

Usage

1. Kofetch μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“€μ–΄μ£Όμ„Έμš”.

// 아무 μ˜΅μ…˜μ„ λ„˜κΈ°μ§€ μ•Šμ•„λ„ μ‚¬μš©κ°€λŠ₯ν•©λ‹ˆλ‹€!
const fetchInstance = Kofetch.create({});

create λ©”μ„œλ“œμ˜ 인자둜 λ„˜κΈΈ 수 μžˆλŠ” ν”„λ‘œνΌν‹° 객체 Options μž…λ‹ˆλ‹€.
ν•˜λ‹¨μ—λŠ” μ£Όμš” ν”„λ‘œνΌν‹°λ“€μ˜ νƒ€μž… μž…λ‹ˆλ‹€.

  1. baseUrl : 기본이 λ˜λŠ” url μž…λ‹ˆλ‹€. (optional)
Type Default value
string ''
  1. config : μ—¬λŸ¬κ°€μ§€ config 듀이 ν¬ν•¨λ©λ‹ˆλ‹€. (optional)
    • RequestInit : μš”μ²­μ„ λ³΄λ‚Όλ•Œ 포함 μ‹œν‚¬ 수 μžˆλŠ” 메타 정보듀 (headers, body, cache, etc )
    • interceptor : 인터 μ…‰ν„° ν•¨μˆ˜λ“€λ‘œ 이루어진 κ°μ²΄μž…λ‹ˆλ‹€.
    • next.js revalidate μ˜΅μ…˜ : next.jsμ—μ„œ μ œκ³΅ν•˜λŠ” revalidate μ˜΅μ…˜μ΄ ν¬ν•¨λ©λ‹ˆλ‹€.
Type Default value
NextCustomHeaderConfig {}

μ˜΅μ…˜ 적용 βœ…

μ˜ˆμ‹œ) μ—¬λŸ¬κ°€μ§€ config μ˜΅μ…˜λ“€μ„ λ„£λŠ” μ˜ˆμ‹œμž…λ‹ˆλ‹€. (μ•„λž˜ μ˜ˆμ‹œ μ°Έκ³ μ‹œ next.jsμ—μ„œ μ œκ³΅ν•˜λŠ” revalidate 객체도 넣을 수 μžˆμŠ΅λ‹ˆλ‹€. )

const fetchInstance = Kofetch.create({
  baseUrl: "",
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json",
  },
  cache: "force-cache",
  next: { revalidate: 30 },
  // 인터셉터 객체
  interceptor: {
    beforeRequestHandler: beforereq,
    requestOnFailHandler: requestFail,
    responseOnSuccessHandler: handleResponseSuccess,
    responseOnFailHandler: handleResponseError,
  },
});

2. μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν–ˆλ‹€λ©΄, 이제 http λ©”μ„œλ“œ 기반으둜 μš”μ²­μ„ 보낼 수 μžˆμŠ΅λ‹ˆλ‹€.

μ˜ˆμ‹œ) 기본적인 get μš”μ²­μž…λ‹ˆλ‹€.

  1. μ œλ„€λ¦­μœΌλ‘œ 응닡받을 ν˜•νƒœμ˜ 데이터 νƒ€μž…μ„ λ„˜κ²¨μ€„ 수 μžˆμŠ΅λ‹ˆλ‹€.

  2. μΆ”κ°€μ μœΌλ‘œ κ°œλ³„ μš”μ²­λ§ˆλ‹€ configκ°’ 듀을 λ„˜κΈΈ 수 μžˆμŠ΅λ‹ˆλ‹€. (λŒ€μ‹ μ— configλŠ” μ„Έλ²ˆμ§Έ 인자이기 λ•Œλ¬Έμ—, getμš”μ²­μ˜ params μ΄λ‚˜, post μš”μ²­μ˜ requset body, κ°€ μ—†λ‹€λ©΄, 빈 객체둜 2번째 인자λ₯Ό μ±„μ›Œμ€˜μ•Όν•©λ‹ˆλ‹€ πŸ₯²)

  3. 응닡 받은 λ°μ΄ν„°λŠ” λ‚΄λΆ€μ—μ„œ .json()둜 객체화 되기 λ•Œλ¬Έμ— λ°˜ν™˜λ˜λŠ” response에 λ³„λ„μ˜ 처리λ₯Ό μ•ˆν•΄μ€˜λ„ λ©λ‹ˆλ‹€.

try {
  const response = await fetchInstance.get<TODO>(
    "https://jsonplaceholder.typicode.com/todos/1",
    // paramsκ°€ λ“€μ–΄μ˜¬ 자리
    {},
    // μΆ”κ°€ config μ˜΅μ…˜ 객체
    { next: { revalidate: 3000 } }
  );
  console.log(response);
} catch (error) {
  console.log("λ„€νŠΈμ›Œν¬ μš”μ²­ μ‹€νŒ¨ μ—λŸ¬", error);
}

3. get μš”μ²­

  • get μš”μ²­μ˜ 경우 λ°μ΄ν„°λŠ” 쿼리 슀트링 ν˜•νƒœλ‘œ μ „λ‹¬λ©λ‹ˆλ‹€. ν•˜μ§€λ§Œ 직접 query-stringν˜•νƒœλ‘œ λ„˜κΈΈν•„μš”μ—†μ΄, λ°±μ—”λ“œμ— 전달할 데이터λ₯Ό κ°μ²΄ν˜•νƒœλ‘œ λ„˜κ²¨μ£Όλ©΄ λ‚΄λΆ€μ—μ„œ query-string으둜 λ³€ν™˜λ˜ μš”μ²­μ‹œ endpoint 뒀에 λΆ€μ°©λ©λ‹ˆλ‹€. (μ›ν•˜μ‹ λ‹€λ©΄ params객체λ₯Ό λ„˜κΈ°μ§€ 말고, 직접 endpoint에 직접 query-string을 λͺ…μ‹œν•˜μ…”λ„ λ©λ‹ˆλ‹€!)
// get μš”μ²­μ— μ‚¬μš©λ˜λŠ” 쿼리 νŒŒλΌλ―Έν„° => λ‚΄λΆ€μ—μ„œ ?gender=male&height=186 으둜 변경됨
const queryParams = {
  gender: "male",
  height: 186,
};

try {
  const response = await fetchInstance.get<TODO>(
    "https://jsonplaceholder.typicode.com/todos/1",
    params : queryParams
  );
  console.log(response);
} catch (error) {
  console.log("λ„€νŠΈμ›Œν¬ μš”μ²­ μ‹€νŒ¨ μ—λŸ¬", error);
}

4. post, put, patch μš”μ²­

  • post, put, patch의 경우 λ°μ΄ν„°λŠ” JSON 객체둜 μ „λ‹¬λ©λ‹ˆλ‹€. 2번째 인자둜 λ°±μ—”λ“œμ— λ„˜κΈΈ 데이터λ₯Ό λ„˜κ²¨μ£Όλ©΄ λ©λ‹ˆλ‹€. λ‚΄λΆ€μ—μ„œ JSON.stringify()둜 직렬화 μ‹œν‚€κΈ° λ•Œλ¬Έμ— 별도 처리λ₯Ό μ•ˆν•΄μ€˜λ„ λ©λ‹ˆλ‹€.
// post μš”μ²­μ— μ‚¬μš©λ˜λŠ” request body
const requstBody = {
  age: 28,
  like: false,
};

try {
  const response = await fetchInstance.get<TODO>(
    "https://jsonplaceholder.typicode.com/todos/1",
    data : requstBody
  );
  console.log(response);
} catch (error) {
  console.log("λ„€νŠΈμ›Œν¬ μš”μ²­ μ‹€νŒ¨ μ—λŸ¬", error);
}

5. delete μš”μ²­

  • delete μš”μ²­μ€ λŒ€κ²Œ path parameter둜 데이터λ₯Ό μ „λ‹¬ν•˜λ―€λ‘œ, μΆ”κ°€ request bodyλ₯Ό λ°›μ§€λŠ” μ•ŠμŠ΅λ‹ˆλ‹€.

6. 인터셉터 (μ€‘μš”! 🚨)

  • axiosμ—λŠ” 기본적으둜 api interceptorκ°€ 기본적으둜 κ΅¬ν˜„μ΄ λ˜μ–΄μžˆμ§€λ§Œ, fetch apiλŠ” ν•΄λ‹Ή κΈ°λŠ₯이 μ—†μŠ΅λ‹ˆλ‹€. ν•˜μ§€λ§Œ fetchλ₯Ό ν•œλ²ˆ wrappingν•œ kofetchλ‚΄λΆ€μ—λŠ” interceptorκ°€ λ‚΄μž₯λ˜μ–΄μžˆμŠ΅λ‹ˆλ‹€.
  • axiosμ—μ„œμ™€ λ§ˆμ°¬κ°€μ§€λ‘œ 총 4가지 μ‹œμ μ˜ interceptor handlerκ°€ κ΅¬μ„±λ©λ‹ˆλ‹€.
    1. μš”μ²­ 직전 μˆ˜ν–‰ν•˜λŠ” ν•¨μˆ˜ beforeRequestHandler
    2. μš”μ²­ μ‹€νŒ¨μ‹œ μˆ˜ν–‰λ˜λŠ” ν•¨μˆ˜ requestOnFailHandler (주둜 λ„€νŠΈμ›Œν¬ μ—λŸ¬μ‹œ 싀행됨)
    3. 응닡 μ„±κ³΅μ‹œ μˆ˜ν–‰λ˜λŠ” ν•¨μˆ˜ responseOnSuccessHandler
    4. 응닡 μ‹€νŒ¨μ‹œ μˆ˜ν–‰λ˜λŠ” ν•¨μˆ˜ responseOnFailHandler
  • 각 ν•¨μˆ˜μ˜ μ‹€ν–‰ μ‹œμ μ€ kofetch λ‚΄λΆ€μ—μ„œ μˆ˜ν–‰λ©λ‹ˆλ‹€.
    μ‚¬μš©μžλŠ” ν•΄λ‹Ή ν•¨μˆ˜μ˜ μ‹€ν–‰ μ‹œμ μ— μ‹ κ²½μ“Έ ν•„μš”μ—†μ΄, ν•΄λ‹Ή μ‹œμ μ— μˆ˜ν–‰ν•˜κ³  싢은 μ½œλ°±ν•¨μˆ˜λ§Œ μ„ μ–Έν•˜κ³  λ„˜κΈ°λ©΄ λ©λ‹ˆλ‹€.
  • μΈν„°μ…‰ν„°λŠ” μ˜΅μ…”λ„μ΄κΈ° λ•Œλ¬Έμ— 넣지 μ•Šμ•„λ„ λ™μž‘ν•˜λŠ”λ° λ¬Έμ œκ°€ μ—†μŠ΅λ‹ˆλ‹€.
  • μΆ”κ°€λ‘œ μš”μ²­μ „μ— μ‹€ν–‰λ˜λŠ” ν•¨μˆ˜μΈ beforeRequestHandlerμ—μ„œ 직접 configλ₯Ό μ‘°μž‘ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 예λ₯Όλ“€μ–΄ request header의 Authorization에 token을 μ‚½μž…ν•˜λŠ”, 등을 μ‹€ν–‰ ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μš”μ²­ 전에 μ‹€ν–‰ν•˜κ³ , κ·Έλ‹€μŒμ— fetch ν•¨μˆ˜κ°€ μ‹€ν–‰λ©λ‹ˆλ‹€.

6-1. 인터셉터 μ‚¬μš©λ°©λ²•

// 각 μ‹œμ μ— μ‚¬μš©λ  콜백 ν•¨μˆ˜λ₯Ό μ„ μ–Έν•©λ‹ˆλ‹€.
const beforereq = (config: NextCustomHeaderConfig) => {
  console.log("μš”μ²­μ „ 인터셉터 ν•Έλ“€λŸ¬ μ‹€ν–‰!");
  return config;
};

const requestFail = (error: any) => {
  console.log("μš”μ²­ μ‹€νŒ¨ ν–ˆμ„λ•Œ 인터셉터 ν•Έλ“€λŸ¬ μ‹€ν–‰!");
  return Promise.reject(error);
};

const responsefail = (error: any) => {
  console.log("응닡 μ‹€νŒ¨ ν–ˆμ„λ•Œ 인터셉터 ν•Έλ“€λŸ¬ μ‹€ν–‰!");
  return Promise.reject(error);
};

const responseSuccess = (res: Promise<any>) => {
  console.log("응닡 μ„±κ³΅ν–ˆμ„λ•Œ 인터셉터 ν•Έλ“€λŸ¬ μ‹€ν–‰!");
  return res;
};

// interceptor μ‚¬μš©μ˜ˆμ‹œ
const fetchInstance = Kofetch.create({
  baseUrl: "",
  interceptor: {
    beforeRequestHandler: beforereq,
    requestOnFailHandler: requestFail,
    responseOnSuccessHandler: responseSuccess,
    responseOnFailHandler: responsefail,
  },
});

7. 응닡 μ‹€νŒ¨ μ—λŸ¬ 객체

axiosμ—μ„œλŠ” HTTPμš”μ²­μ˜ 응닡이 μ‹€νŒ¨ν–ˆμ„λ•Œ, AxiosErrorνƒ€μž…μ˜ 객체λ₯Ό 자체적으둜 μƒμ„±ν•˜μ—¬, 응닡 μ—λŸ¬κ°μ²΄λ₯Ό λ˜μ§‘λ‹ˆλ‹€. 이와 λ§ˆμ°¬κ°€μ§€λ‘œ koFetchμ—μ„œλŠ” ν˜„μž¬ λ‚΄λΆ€μ—μ„œ 자체적으둜 μ—λŸ¬ dto객체λ₯Ό μƒμ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€. ν˜„μž¬ κ΅¬μ„±λ˜μžˆλŠ” μ—λŸ¬ dto κ°μ²΄λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€. (μ—λŸ¬ 객체 λ‚΄λΆ€λŠ” μΆ”ν›„ 고도화 μ˜ˆμ •μž…λ‹ˆλ‹€ πŸ˜…)

{
  stausCode, // μ—λŸ¬ statusCode
  name, // μ—λŸ¬ 이름
  responseDto, // λ°±μ—”λ“œμ˜ μ—λŸ¬ 응닡 객체
  requestUrl, // μš”μ²­ url
  requestheaders, // μš”μ²­μ‹œ ν¬ν•¨λœ header
  message, // μ—λŸ¬ λ©”μ‹œμ§€
}

ν˜„μž¬λŠ” Kofetch μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν• λ•Œ 인터셉터 객체 μ•ˆμ— 각 μ‹œμ μ— λ§žλŠ” ν•¨μˆ˜λ“€μ„ λ„£μ–΄ 주기만 ν•˜λ©΄λ©λ‹ˆλ‹€. κ°œλ³„ μš”μ²­λ§ˆλ‹€ 인터셉터λ₯Ό μΆ”κ°€ν•˜λŠ” κΈ°λŠ₯은 μΆ”ν›„ 개발될 μ˜ˆμ •μž…λ‹ˆλ‹€. λ§Œμ•½ μ§€κΈˆ λ‹Ήμž₯ μ—¬λŸ¬ 인터셉터가 ν•„μš”ν•œ 상황이라면, μƒˆλ‘œμš΄ Kofetch μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“€κ³ , κ·Έ μΈμŠ€ν„΄μŠ€λ₯Ό μ‚¬μš©ν•΄μ£Όμ„Έμš”!

8. 정리

ν˜„μž¬ ν•„μˆ˜ κΈ°λŠ₯λ“€λ§Œ μΆ”κ°€ν•˜μ—¬ μ§„ν–‰ν•œ μƒνƒœμž…λ‹ˆλ‹€. 아직은 λΆˆμ•ˆμ •ν•˜λ‹ˆ μ‚¬μš©μ€ 지양 πŸ« ν•΄μ£Όμ„Έμš”! ν˜„μž¬ κΈ°μ€€μœΌλ‘œ κ°€λŠ₯ν•œ κΈ°λŠ₯λ“€ μž…λ‹ˆλ‹€.

ν˜„μž¬ 개발된 κΈ°λŠ₯ πŸ”¨

  • 기본적인 http μš”μ²­ 응닡 κ°€λŠ₯ (get, post, put, patch, delete) βœ…
  • 졜초, fetch instance λ§Œλ“€λ•Œ λΆ€ν„° config(header, interceptor, cache λ“±λ“±) μ‚½μž… ν•  수 μžˆμ–΄μ•Όν•¨. βœ…
  • 각 μš”μ²­λ§ˆλ‹€ μ‚¬μš©μžκ°€ ν•„μš”ν• λ•Œ config 넣을 수 있게 λ§Œλ“¬. βœ…
  • response type μ œλ„€λ¦­μœΌλ‘œ λ„˜κΈΈ 수 있음.
  • μš”μ²­ λ³΄λ‚Όλ•Œ, json stringify, μ‘λ‹΅λ°›μ„λ•Œ json Parse 처리. βœ…
  • config에 nextμ—μ„œ μ œκ³΅ν•˜λŠ” revalidate ν”„λ‘œνΌν‹°λ₯Ό λ„˜κΈΈ 수 있게 λ§Œλ“¬. βœ…
  • interceptor 총 4가지 μ‹œμ  핸듀링 (μš”μ²­μ „, μš”μ²­μ‹€νŒ¨, 응닡성곡, μ‘λ‹΅μ‹€νŒ¨) βœ…
  • axios 처럼, μš”μ²­κ°μ²΄λ₯Ό κ°€μ Έμ˜¬ 수 μžˆμ–΄μ•Όν•œλ‹€ βœ…

μΆ”κ°€ μ˜ˆμ • κΈ°λŠ₯ πŸ”₯

  • μ—λŸ¬ νƒ€μž… κ°œμ„  (λ‚΄λΆ€)
  • 인터셉터λ₯Ό 각 μš”μ²­λ§ˆλ‹€ 직접 컨트둀 ν•  수 μžˆλ„λ‘ λ§Œλ“€μ–΄μ•Όν•¨.
  • refresh token 둜직 λŒ€μ‘λ˜λŠ”μ§€ 확인

9. 1.0.5 버전 패치

  • kofetch λ‚΄λΆ€μ˜ type듀을 λ³„λ„μ˜ type.ts파일둜 λΆ„λ¦¬ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
  • kofetch 내뢀에 μ‚¬μš©λœ λͺ¨λ“  νƒ€μž…λ“€μ€ kofetch νŒ¨ν‚€μ§€μ— ν¬ν•¨λ˜μ–΄μ Έ μžˆμœΌλ―€λ‘œ, import ν•΄μ„œ μ‚¬μš©ν•˜λ©΄ λ©λ‹ˆλ‹€.

kofetch's People

Contributors

fnrkp089 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.