Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration Fetch ax in my page #154

Merged
merged 2 commits into from
Nov 4, 2024
Merged

Migration Fetch ax in my page #154

merged 2 commits into from
Nov 4, 2024

Conversation

gahyuun
Copy link
Member

@gahyuun gahyuun commented Oct 30, 2024

๐Ÿ“Œ ์ž‘์—… ๋‚ด์šฉ

๊ตฌํ˜„ ๋‚ด์šฉ ๋ฐ ์ž‘์—… ํ–ˆ๋˜ ๋‚ด์—ญ

  • my page์— fetch๋กœ ์ž‘์—…ํ•œ API๋ฅผ fetch-ax๋กœ ๊ต์ฒดํ–ˆ์Šต๋‹ˆ๋‹ค
  • content-type์ด ์„ค์ •์ด ๋˜์ง€ ์•Š์€ ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿค” ๊ณ ๋ฏผ ํ–ˆ๋˜ ๋ถ€๋ถ„

  • fetch-ax๋กœ ์ ์šฉ์„ ํ–ˆ๋Š”๋ฐ ๋งŽ์€ line์ด ์ค„์–ด๋“ ๊ฒŒ ๋ณด์ด๋„ค์š”!!
  • ๊ณผ๋ชฉ ์ถ”๊ฐ€,๊ณผ๋ชฉ ์‚ญ์ œ, ๊ณผ๋ชฉ ์กฐํšŒ๋Š” ๋ณ„๋‹ค๋ฅธ Custom Error๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ ์ฝ”๋“œ์— ๋”ฐ๋ฅธ ๋ถ„๊ธฐ์ฒ˜๋ฆฌ๋Š” ๋”ฐ๋กœ ์ง„ํ–‰ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

๐Ÿ”Š ๋„์›€์ด ํ•„์š”ํ•œ ๋ถ€๋ถ„

  • fetch-ax๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์—ฌ๋Ÿฌ ๋ฌธ์ œ์ ๋“ค์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค.
  1. requestInterceptor์—์„œ Headers์— ์„ค์ •์„ ํ–ˆ์„ ๋•Œ Headers๊ฐ€ ์ ์ ˆํžˆ ์„ค์ •์ด ๋˜์ง€ ์•Š์€ ์˜ค๋ฅ˜ ๋ฐœ์ƒ
await instance.post(
      API_PATH.takenLectures,
      { lectureId },
      {
        responseType: 'text',
      },

์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด instance๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ณผ๋ชฉ ์ถ”๊ฐ€ api ์š”์ฒญ์„ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ Content-Type์€ instance์—์„œ ์„ค์ •์„ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„์˜ ์„ค์ •์„ ํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ ์š”์ฒญ ์‹œ 415 Error๊ฐ€ ๋ฐœ์ƒํ–ˆ๊ณ  ํ™•์ธ์„ ํ•ด๋ณด๋‹ˆ instance์˜ requestInterceptor์— headers๊ฐ€ ์ œ๋Œ€๋กœ ์„ค์ •์ด ๋˜์ง€ ์•Š์€ ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ ์›์ธ์„ ํŒŒ์•…ํ•˜์ง€๋Š” ๋ชปํ–ˆ์œผ๋‚˜, config.headers๋ฅผ ์ผ๋ฐ˜ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ Headers ๊ฐ์ฒด๋กœ ๋ณ€๊ฒฝ์ด ๋˜์—ˆ์„๋•Œ ํ•ด๊ฒฐ์ด ๋์Šต๋‹ˆ๋‹ค. ํ˜น์‹œ ์ด ์›์ธ ์•„์‹œ๋‚˜์š” ,,
์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋Š” responseType, Invalid URL Error ํ•ด๊ฒฐํ•˜๋ฉด์„œ ๊ฐ™์ด ํŒŒ์•…ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๐Ÿฅน
Myongji-Graduate/fetch-ax#19

@gahyuun gahyuun self-assigned this Oct 30, 2024
Copy link

Copy link
Collaborator

@seonghunYang seonghunYang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค. Header ๋ฌธ์ œ๋Š” HeaderInit ํƒ€์ž…์— Headers, Record, string[][] ๋ชจ๋‘ ํฌํ•จ๋˜์–ด ์žˆ์–ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ์˜ ๋ณธ์งˆ๊ณผ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณ ๋ฏผํ•ด๋ดค๋Š”๋ฐ์š”, ์šฐ๋ฆฌ ๋กœ์ง์ƒ RequestInterceptor์—์„œ header๊ฐ€ ๋ฐ˜๋“œ์‹œ Headers ํƒ€์ž…์ด๋ผ๋ฉด Headers ํƒ€์ž…์œผ๋กœ ๋ณด๋‚ด์ฃผ๋„๋ก ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด ๋ฌธ์ œ๋ฅผ ์˜ˆ๋ฐฉํ•˜๋Š” ๋ฐ ํšจ๊ณผ์ ์ผ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

@gahyuun
Copy link
Member Author

gahyuun commented Oct 31, 2024

  • ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค. Header ๋ฌธ์ œ๋Š” HeaderInit ํƒ€์ž…์— Headers, Record, string[][] ๋ชจ๋‘ ํฌํ•จ๋˜์–ด ์žˆ์–ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ์˜ ๋ณธ์งˆ๊ณผ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณ ๋ฏผํ•ด๋ดค๋Š”๋ฐ์š”, ์šฐ๋ฆฌ ๋กœ์ง์ƒ RequestInterceptor์—์„œ header๊ฐ€ ๋ฐ˜๋“œ์‹œ Headers ํƒ€์ž…์ด๋ผ๋ฉด Headers ํƒ€์ž…์œผ๋กœ ๋ณด๋‚ด์ฃผ๋„๋ก ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด ๋ฌธ์ œ๋ฅผ ์˜ˆ๋ฐฉํ•˜๋Š” ๋ฐ ํšจ๊ณผ์ ์ผ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์˜ค๋น ๋Š” fetch-ax ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ  ์ง€๊ธˆ์ฒ˜๋Ÿผ requestInterceptor๋งŒ์„ ์ˆ˜์ •ํ•˜๋Š”๊ฒŒ ๋‚ซ๋‹ค๊ณ  ๋ง์”€ํ•˜์‹œ๋Š”๊ฒŒ ๋งž์„๊นŒ์š”!?
์ €๋Š” fetch-ax ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค..!

// ์กธ๋ถ€ ์ฝ”๋“œ
  requestInterceptor: (config) => {
 // 
    return config;
  },
// fetch-ax ์ฝ”๋“œ 
  const requestHeaders = new Headers();
  if (defaultOptions?.headers) {
    new Headers(defaultOptions.headers).forEach((value, key) => {
      requestHeaders.set(key, value);
    });
  }
  if (requestInit?.headers) {
    new Headers(requestInit.headers).forEach((value, key) => {
      requestHeaders.set(key, value);
    });
  }

requestInterceptor์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ฒจ์ฃผ๋Š” config์˜ header๋Š” Headers ํƒ€์ž…์ด๋ฏ€๋กœ ( type HeadersInit = [string, string][] | Record<string, string> | Headers; ) ์ผ๋ฐ˜ ๊ฐ์ฒด์ฒ˜๋Ÿผ ๊ตฌ์กฐ ๋ถ„ํ•ดํ• ๋‹น์„ ํ–ˆ์„ ๋•Œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค

๊ทธ๋Ÿฐ๋ฐ ์‚ฌ์šฉ์ž ์ž…์žฅ์—์„œ๋Š” instance์‹œ default option์œผ๋กœ Record<string,string> ํƒ€์ž…์œผ๋กœ ์„ค์ •ํ–ˆ์œผ๋ฉด config.headers๋กœ Headers ํƒ€์ž…์ด ์•„๋‹Œ Record<string,string> ํƒ€์ž…์œผ๋กœ ์˜ˆ์ƒํ•˜์ง€ ์•Š์„๊นŒ ์‹ถ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ €๋„ ๊ทธ๋ ‡๊ฒŒ ์˜ˆ์ƒํ–ˆ๊ณ  ๋ฌด์ž‘์ • 415 Error๊ฐ€ ๋–ด์œผ๋‹ˆ๊นŒ,, fetch-ax ์ฝ”๋“œ๋ฅผ ๊นŒ๋ณด๊ณ  ์•Œ์•„๋‚ด๋Š”๋ฐ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค.

๊ฒฐ๋ก ์ ์œผ๋กœ ์ €๋Š” ์‚ฌ์šฉ์ž๊ฐ€ default Options ์‹œ ์„ค์ •ํ–ˆ๋˜ ํ—ค๋”์˜ ํƒ€์ž… ๊ธฐ๋ฐ˜์œผ๋กœ config.headers์— ๋„˜๊ฒจ์ฃผ๋Š”๊ฒŒ ์ข‹์ง€ ์•Š์„๊นŒ ์‹ถ์€๋ฐ ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?!
ex)
Record<string,string> ํƒ€์ž…์œผ๋กœ header ์„ค์ •ํ–ˆ๋‹ค๋ฉด config.headers์˜ ํƒ€์ž…์€ Record<string,string>
Headers ํƒ€์ž…์œผ๋กœ header ์„ค์ •ํ–ˆ๋‹ค๋ฉด config.headers์˜ ํƒ€์ž…์€ Headers

@seonghunYang
Copy link
Collaborator

config.headers

์Œ, ์ „๋‹ฌ ๊ณผ์ •์—์„œ ์˜คํ•ด๊ฐ€ ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ €๋„ fetch-ax๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ž…์žฅ์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ œ๊ฐ€ ์•ž์„œ ๋ง์”€๋“œ๋ฆฐ ๊ฒƒ์€ fetch-ax์˜ ํ˜„์žฌ ๋กœ์ง์„ ์ˆ˜์ •ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ ๋กœ์ง์ƒ์œผ๋กœ ๋ณด๋ฉด RequestInterceptor์—์„œ config์˜ header ํƒ€์ž…์ด ๋ฐ˜๋“œ์‹œ Headers์ธ๋ฐ, ํ˜„์žฌ HeaderInit์œผ๋กœ ๋˜์–ด ์žˆ์–ด ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด ์ผ๋‹จ ํƒ€์ž…์„ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ํ•ด๊ฒฐ์ฑ…์ด๋ผ๊ณ  ์ƒ๊ฐ๋˜์–ด ๋ง์”€๋“œ๋ ธ์Šต๋‹ˆ๋‹ค.

์ €๋„ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด default Options ์„ค์ • ์‹œ ์ง€์ •ํ•œ ํ—ค๋”์˜ ํƒ€์ž…์„ ๊ธฐ๋ฐ˜์œผ๋กœ config.headers๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒŒ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ์‹œ ํ—ค๋” ์„ค์ • ํƒ€์ž…๊ณผ ์š”์ฒญ ํ˜ธ์ถœ ์‹œ ํ—ค๋” ํƒ€์ž…์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ์—๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? ์ด๋Ÿฐ ๊ณ ๋ฏผ ๋•Œ๋ฌธ์— Headers ํƒ€์ž…์œผ๋กœ ํ†ต์ผํ–ˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋Ÿฌํ•œ ๋ฌธ์ œ ๋•Œ๋ฌธ์— ์ธํ„ฐ์…‰ํ„ฐ์—์„œ๋Š” Header์˜ ํƒ€์ž…์„ Headers๋กœ ๊ณ ์ •ํ•˜๋Š” ๊ฒƒ์ด ์ ์ ˆํ•œ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํŒ๋‹จํ•˜์—ฌ ์˜๊ฒฌ์„ ์ œ์‹œํ–ˆ์Šต๋‹ˆ๋‹ค. ๋กœ์ง ์ƒ์—์„œ ๋ถ„๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•œ ํ—ค๋”์˜ ํƒ€์ž…์„ ๊ธฐ๋ฐ˜์œผ๋กœ config.headers๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒƒ๋„ ์ €๋Š” ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

@yougyung
Copy link
Member

์ˆ˜๊ณ  ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค!
๋จผ์ € ๊ด€๋ จ ๋ ˆํผ๋Ÿฐ์Šค&ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋‹ค ๋ฆฌ๋ทฐ๊ฐ€ ๋Šฆ์–ด์ง€๊ฒŒ ๋˜์–ด ์ฃ„์†กํ•ฉ๋‹ˆ๋‹คใ… ใ… 
์ €๋„ fetch-ax์˜ ์ˆ˜์ •์ด ํ•„์š”ํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜๋Š”๋ฐ์š”. new Header๋ฅผ ํ†ตํ•œ ์ƒ์„ฑ๋ณด๋‹ค๋Š” { Authorization: Bearer ...}; ํ˜•์‹์„ ํ†ตํ•ด header๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด ๊ฐ€๋Šฅํ•œ HeadersInit์„ ์œ ์ง€ํ•˜๋ฉฐ ๋กœ์ง์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๊ณ ๋ฏผ์„ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. axios๋„ [key: string]: any;type์„ ์ทจ๊ธ‰ํ•˜๊ณ  ์žˆ์—ˆ๊ณ ์š”.
test๋ฅผ ํ•ด๋ณด๋‹ˆ ์ผ๋ฐ˜ ๊ฐ์ฒด์™€ ๊ฐ™์ด ์ „๋‹ฌํ•œ ๊ฒฝ์šฐ์— header๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ „๋‹ฌ๋˜๊ณ  ์žˆ์—ˆ๊ณ ,

{
  Authorization: 'Bearer ',
  [Symbol(map)]: [Object: null prototype] {
  'content-type': [ 'application/json' ],
  ab: [ 's' ]
}
}

new Header๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•œ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด ์ „๋‹ฌ๋˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

Headers {
    [Symbol(map)]: [Object: null prototype] {
    'content-type': [ 'application/json' ],
    ab: [ 's' ],
    Authorization: [ 'Bearer ' ]
  }
}

๊ทธ๋ž˜์„œ requestInterceptorํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ ์ดํ›„์— ๋‹ค์‹œ new Header๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๋ฐฉ์‹์„ ์ทจํ•ด๋ดค๋Š”๋ฐ, ์ž…๋ ฅ ํƒ€์ž…์— ์ƒ๊ด€์—†์ด 2๋ฒˆ ์‘๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ๋กœ์ง์„ ๋ณ€๊ฒฝํ•ด๋ณด๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”? ์•„๋ž˜๋Š” ์˜ˆ์‹œ๋กœ ์ž‘์„ฑํ•ด๋ณธ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

    requestArgs = requestInit.requestInterceptor(requestArgs);// ๊ธฐ์กด์ฝ”๋“œ
    //์ถ”๊ฐ€๋œ ์ฝ”๋“œ
    const headers = new Headers(requestArgs.headers);
    headers.forEach((value, key) => {
      requestHeaders.set(key, value);
    });
    requestArgs.headers = requestHeaders;

Authorization: `Bearer ${cookies().get('accessToken')?.value}`,
},
await instance.delete(`${API_PATH.takenLectures}/${lectureId}`, {
responseType: 'text',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ถ”๊ฐ€์ ์œผ๋กœ โ€˜โ€™๋กœ ์ „๋‹ฌ๋ฐ›๋Š” ์‘๋‹ต์—๋Œ€ํ•ด์„œ responseType: 'text',๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ์ด ์˜ˆ์ธกํ•˜๊ธฐ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์–ด์š”. ํ•ด๋‹น ์ผ€์ด์Šค๊ฐ€ Post, patch, delete ๋“ฑ์—์„œ ๋นˆ๋ฒˆํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜์–ด์„œ fetch-ax์—์„œ ๋ฌธ์ž์—ด ๋นˆ๊ฐ’์— ๋Œ€ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์–ด๋–จ๊นŒ? ์ƒ๊ฐ์ด ๋“ค์—ˆ๋Š”๋ฐ ํŒ€์›๋ถ„๋“ค์˜ ์˜๊ฒฌ์ด ๊ถ๊ธˆํ•ด์š”!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋Š” responseType, Invalid URL Error ํ•ด๊ฒฐํ•˜๋ฉด์„œ ๊ฐ™์ด ํŒŒ์•…ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๐Ÿฅน
PR ์ฝ”๋ฉ˜ํŠธ์— ๋‚˜์™€์žˆ๋“ฏ์ด Headers์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค~!

@gahyuun
Copy link
Member Author

gahyuun commented Nov 1, 2024

const headers = new Headers(requestArgs.headers);
    headers.forEach((value, key) => {
      requestHeaders.set(key, value);
    });
    requestArgs.headers = requestHeaders;

๋ง์”€ํ•˜์…จ๋˜, "requestInterceptor์ดํ›„ ์œ„ ์ฝ”๋“œ ์ถ”๊ฐ€" ์— ๋Œ€ํ•œ ๋ฐฉ๋ฒ•์„ ์ ์šฉํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค! ๋‹ค๋งŒ ํŽธ๋ฆฌ์„ฑ์„ ์œ„ํ•ด fetch-ax์— ์ ์šฉํ•˜๋Š”๊ฒŒ ์•„๋‹Œ interceptor์— ์ ์šฉ์„ ํ•˜์—ฌ์„œ Content-Type์ด ์ž˜ ์กด์žฌํ•˜๋Š”์ง€๋งŒ ์ฒดํฌ๋ฅผ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

export const instance = fetchAX.create({
  headers: {
    'Content-Type': 'application/json',
  },
  responseRejectedInterceptor: (error) => {
    fetchAxErrorHandler(error);
  },
  requestInterceptor: (config) => {
    const accessToken = cookies().get('accessToken')?.value;

    config.headers = {
      ...config.headers,
      Authorization: `Bearer ${accessToken}`,
    };
    const requestHeaders = new Headers();
    const headers = new Headers(config.headers);
    headers.forEach((value, key) => {
      requestHeaders.set(key, value);
    });

    console.log(requestHeaders.get('Content-Type'));
    return config;
  },
});

config.headers๋Š” ์ผ๋ฐ˜ ๊ฐ์ฒด ๋ณ€ํ˜•์œผ๋กœ ์ธํ•ด Content-Type์ด ๋‚ ๋ผ๊ฐ„ ์ƒํƒœ์ด๊ณ  ํ•ด๋‹น config.headers๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ new Headers๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  requestHeaders์— set์„ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ํ˜•ํƒœ์—๋„ requestHeaders.get('Content-Type') ์‹œ null์ด ๋‚˜์˜ค๋Š” ์ƒํ™ฉ์ธ๋ฐ ํ˜น์‹œ ์ œ๊ฐ€ ํ…Œ์ŠคํŠธ๋ฅผ ์ž˜๋ชป ์ง„ํ–‰ํ–ˆ์„๊นŒ์š”?

@gahyuun
Copy link
Member Author

gahyuun commented Nov 1, 2024

@seonghunYang @yougyung
์ผ๋‹จ ์ œ๊ฐ€ ์ƒ๊ฐํ•œ ๊ฒฐ๋ก ์€ "config.headers๋ฅผ Headers ํƒ€์ž…์œผ๋กœ ๊ฐ•์ œํ•˜๊ธฐ" ์ž…๋‹ˆ๋‹ค

Headers ํƒ€์ž…์œผ๋กœ header ์„ค์ •ํ–ˆ๋‹ค๋ฉด config.headers์˜ ํƒ€์ž…์€ Headers ์ฒ˜๋Ÿผ ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ํ—ค๋” ํƒ€์ž…์œผ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค
๋‹ค๋ฅธ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ฐธ๊ณ ํ–ˆ์„๋•Œ

  1. axios
    ์œ„์—์„œ ์–ธ๋‹ˆ๊ฐ€ axios๋Š” [key: string]: any ํ•ด๋‹น ํƒ€์ž…์œผ๋กœ ๋„˜๊ฒจ์ค€๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ, axios๋Š” XMLHttpRequest ๊ธฐ๋ฐ˜์ด๊ณ  XMLHttpRequest์—์„œ setRequestHeader ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Headers ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์•„๋‹Œ ์ง์ ‘ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  2. ofetch
    ofetch๋Š” Headers ํƒ€์ž…์œผ๋กœ ๊ฐ•์ œํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ofetch onRequest์—์„œ headers์— ์ผ๋ฐ˜ ๊ฐ์ฒด๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ Headers ํƒ€์ž…์œผ๋กœ ๊ฐ•์ œํ•˜๊ฒŒ ๋˜๋ฉด ์ฝ”๋“œ ๊ตฌํ˜„์—์„œ ๋ฐ”๋กœ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธฐ๋‹ˆ๊นŒ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๊ฒฐ๊ณผ ๋ฐœ์ƒ์„ ์ค„์ผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๋ฐฉ์‹์ด ๊ฐ€์žฅ ์ ํ•ฉํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.
๋ฌผ๋ก  ์ œ๊ฐ€ ์ฒ˜์Œ์— ์ œ์•ˆํ•œ ๋ฐฉ์‹์ธ ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ํ—ค๋” ํƒ€์ž…์œผ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š”๊ฒŒ ๋” ์œ ์—ฐํ•˜์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ์˜ค๋น ๊ฐ€ ๋งํ•œ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ์‹œ ํ—ค๋” ์„ค์ • ํƒ€์ž…๊ณผ ์š”์ฒญ ํ˜ธ์ถœ ์‹œ ํ—ค๋” ํƒ€์ž…์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ํ˜ธ์ถœ๋ถ€ ํƒ€์ž…์ด ์ธ์Šคํ„ด์Šค ํƒ€์ž…์„ ๋ฎ์–ด์”Œ์šฐ๋„๋ก ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ์‚ฌ์šฉ์ž ์ž…์žฅ์—์„œ ์ถฉ๋ถ„ํžˆ ์˜ˆ์ƒ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ™•์‹ ์ด ๋“ค์ง€ ์•Š๊ณ  ์œ ์—ฐ์„ฑ๋ณด๋‹ค ํ•ด๋‹น ๋‹จ์ ์ด ๋” ํฌ๊ฒŒ ๋Š๊ปด์ ธ Headers ํƒ€์ž…์œผ๋กœ ๊ฐ•์ œํ•˜๋Š” ๋ฐฉ์‹์„ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ถ„๋“ค์€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์šฉ?

์œ„ ๋ฐฉ์‹๋Œ€๋กœ ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด
fetch-ax์—์„œ ํ˜„์žฌ config.headers์˜ HeadersInit ํƒ€์ž…์„ Headers ํƒ€์ž…์œผ๋กœ ์ˆ˜์ •์„ ์ง„ํ–‰ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค~!

@yougyung
Copy link
Member

yougyung commented Nov 2, 2024

config.headers๋Š” ์ผ๋ฐ˜ ๊ฐ์ฒด ๋ณ€ํ˜•์œผ๋กœ ์ธํ•ด Content-Type์ด ๋‚ ๋ผ๊ฐ„ ์ƒํƒœ์ด๊ณ  ํ•ด๋‹น config.headers๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ new Headers๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  requestHeaders์— set์„ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ํ˜•ํƒœ์—๋„ requestHeaders.get('Content-Type') ์‹œ null์ด ๋‚˜์˜ค๋Š” ์ƒํ™ฉ์ธ๋ฐ ํ˜น์‹œ ์ œ๊ฐ€ ํ…Œ์ŠคํŠธ๋ฅผ ์ž˜๋ชป ์ง„ํ–‰ํ–ˆ์„๊นŒ์š”?

์ œ๊ฐ€ ์ž‘์—…ํ•œ ํ™˜๊ฒฝ๊ณผ ์กฐ๊ธˆ ๋‹ค๋ฅธ ์ ์€ ์š”์ฒญ๊ฐ์ฒดrequestHeaders์ƒ์„ฑ ์‹œ, ๊ธฐ์กด headers๊ฐ€ ๋ฐ˜์˜๋˜์–ด์ ธ์žˆ์ง€์•Š์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ์•„๋ž˜ ์ฝ”๋“œ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•ด๋ณด์‹œ๋ฉด ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-11-02 แ„‹แ…ฉแ„’แ…ฎ 4 44 11

๋”๋ถˆ์–ด ์ œ์•ˆํ•ด์ฃผ์‹  ๊ฒฐ๋ก  ์ค‘์—, ๊ฐœ๋ฐœ๊ณผ์ •์—์„œ ์ž ์žฌ์ ์˜ค๋ฅ˜ํŒŒ์•…์ด ์šฉ์ดํ•˜๋‹ค๋Š” ์ ์€ Headersํƒ€์ž…๋งŒ์ด ๊ฐ–๋Š” ์žฅ์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์š”! ๋ฐ˜๋ฉด axios์™€ ๋น„์Šทํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ทจํ•˜์ง€๋ชปํ•œ๋‹ค๋Š” ์ ์„ ๋‹จ์ ์œผ๋กœ ๊ฐ–๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๋ฐฉ์‹์œผ๋กœ ๋งŒ์ผ ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋œ๋‹ค๊ณ  ํ•ด๋„ ์—ฌ์ „ํžˆ ํ•ด๋‹น ๊ฒฐ๋ก ์ด ์ ํ•ฉํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋Š”์ง€ ์˜๊ฒฌ ๊ถ๊ธˆํ•ด์š”.

  requestInterceptor: (config) => {
    const accessToken = cookies().get('accessToken')?.value;
    const requestHeaders = new Headers(config.headers);
    config.headers = {
      ...config.headers,
      Authorization: `Bearer ${accessToken}`,
    };
    const headers = new Headers(config.headers);
    headers.forEach((value, key) => {
      requestHeaders.set(key, value);
    });

    console.log(requestHeaders.get('Content-Type'));
    return config;
  },

@gahyuun
Copy link
Member Author

gahyuun commented Nov 3, 2024

config.headers๋Š” ์ผ๋ฐ˜ ๊ฐ์ฒด ๋ณ€ํ˜•์œผ๋กœ ์ธํ•ด Content-Type์ด ๋‚ ๋ผ๊ฐ„ ์ƒํƒœ์ด๊ณ  ํ•ด๋‹น config.headers๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ new Headers๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  requestHeaders์— set์„ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ํ˜•ํƒœ์—๋„ requestHeaders.get('Content-Type') ์‹œ null์ด ๋‚˜์˜ค๋Š” ์ƒํ™ฉ์ธ๋ฐ ํ˜น์‹œ ์ œ๊ฐ€ ํ…Œ์ŠคํŠธ๋ฅผ ์ž˜๋ชป ์ง„ํ–‰ํ–ˆ์„๊นŒ์š”?

์ œ๊ฐ€ ์ž‘์—…ํ•œ ํ™˜๊ฒฝ๊ณผ ์กฐ๊ธˆ ๋‹ค๋ฅธ ์ ์€ ์š”์ฒญ๊ฐ์ฒดrequestHeaders์ƒ์„ฑ ์‹œ, ๊ธฐ์กด headers๊ฐ€ ๋ฐ˜์˜๋˜์–ด์ ธ์žˆ์ง€์•Š์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ์•„๋ž˜ ์ฝ”๋“œ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•ด๋ณด์‹œ๋ฉด ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-11-02 แ„‹แ…ฉแ„’แ…ฎ 4 44 11

๋”๋ถˆ์–ด ์ œ์•ˆํ•ด์ฃผ์‹  ๊ฒฐ๋ก  ์ค‘์—, ๊ฐœ๋ฐœ๊ณผ์ •์—์„œ ์ž ์žฌ์ ์˜ค๋ฅ˜ํŒŒ์•…์ด ์šฉ์ดํ•˜๋‹ค๋Š” ์ ์€ Headersํƒ€์ž…๋งŒ์ด ๊ฐ–๋Š” ์žฅ์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์š”! ๋ฐ˜๋ฉด axios์™€ ๋น„์Šทํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ทจํ•˜์ง€๋ชปํ•œ๋‹ค๋Š” ์ ์„ ๋‹จ์ ์œผ๋กœ ๊ฐ–๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๋ฐฉ์‹์œผ๋กœ ๋งŒ์ผ ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋œ๋‹ค๊ณ  ํ•ด๋„ ์—ฌ์ „ํžˆ ํ•ด๋‹น ๊ฒฐ๋ก ์ด ์ ํ•ฉํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋Š”์ง€ ์˜๊ฒฌ ๊ถ๊ธˆํ•ด์š”.

  requestInterceptor: (config) => {
    const accessToken = cookies().get('accessToken')?.value;
    const requestHeaders = new Headers(config.headers);
    config.headers = {
      ...config.headers,
      Authorization: `Bearer ${accessToken}`,
    };
    const headers = new Headers(config.headers);
    headers.forEach((value, key) => {
      requestHeaders.set(key, value);
    });

    console.log(requestHeaders.get('Content-Type'));
    return config;
  },

๋ง์”€ํ•ด์ฃผ์‹  ์ฝ”๋“œ ์ž˜ ๋Œ์•„๊ฐ€๋Š” ๊ฑฐ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค!
ํ•˜์ง€๋งŒ ์ €๋Š” config.headers์˜ ํƒ€์ž…์„ Headers ํƒ€์ž…์ด๋‚˜ Record<string,string> ํƒ€์ž…์œผ๋กœ ๊ฐ•์ œํ•˜๋Š” ๊ฒƒ์ด ๋” ์ ์ ˆํ•˜์ง€ ์•Š์„๊นŒ? ๋ผ๋Š” ์ƒ๊ฐ์„ ํ–ˆ์–ด์š”
๋ง์”€ํ•˜์‹  ํ•ด๊ฒฐ ๋ฐฉ์•ˆ์€ "Headers๊ฐ€ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น์ด ์•ˆ๋˜๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ" ํ•ด์ค€๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ, ๋ณธ์งˆ์ ์œผ๋กœ๋Š” "config.headers๊ฐ€ ์–ด๋–ค ํƒ€์ž…์ธ์ง€ ๋ช…์‹œํ•ด์ฃผ์ง€ ์•Š์€ ๊ฒƒ"์ด ๋ฌธ์ œ์ด๊ณ  ํƒ€์ž…์„ ๋ช…์‹œํ•จ์œผ๋กœ์จ ์‚ฌ์šฉ์ž๊ฐ€ ๊ฐ์ฒด์— ๋งž๊ฒŒ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜๋Š” ๊ฒŒ ๋” ์ข‹์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค
๋˜ํ•œ Headers ํƒ€์ž…์œผ๋กœ ๊ฐ•์ œํ•˜๊ฒŒ ๋˜๋ฉด axios์™€ ๋น„์Šทํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ทจํ•˜์ง€ ๋ชปํ•˜๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค๊ณ  ํ•˜์…จ๋Š”๋ฐ XMLHttpRequest์—๋Š” Headers ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ ์„ ๊ณ ๋ คํ•˜๋ฉด ์–ด์ฉ” ์ˆ˜ ์—†๋Š” ๋ถ€๋ถ„์ด๊ธฐ๋„ ํ•˜์ง€๋งŒ ์ด๋Ÿฐ ์ ์„ ๊ณ ๋ คํ•˜๋ฉด config.headers๋ฅผ Record<string,string> ํƒ€์ž…์œผ๋กœ ๊ฐ•์ œํ•˜๋Š” ๊ฒŒ ๋” ๋‚˜์„ ๊ฒƒ ๊ฐ™๋„ค์š”

@yougyung
Copy link
Member

yougyung commented Nov 3, 2024

config.headers์˜ ํƒ€์ž… ๋ฒ”์ฃผ๊ฐ€ ๋„“๊ณ , ํŠนํžˆ Headers์™€ ์ผ๋ฐ˜๊ฐ์ฒด์˜ ๊ฐ ๋‚ด๋ถ€ ์†์„ฑ์ด ํ˜ผ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ฐจ์›์—์„œ ์ž…์ถœ๋ ฅํƒ€์ž… ๋ชจ๋‘ interceptor๋‚ด์—์„œ ๋งŒํผ์€ ํƒ€์ž… ๊ฐ•์ œ๊ฐ€ ํ•„์š”ํ•˜๊ฒ ๋„ค์š”. ํ•˜๋‚˜๋กœ ํ†ต์ผํ•˜๋Š” ๋ชฉ์ ๋งŒ ์ถฉ์กฑ์‹œํ‚ค๋ฉด ๋˜์ง€๋งŒ, ์ €๋„ ์ด์šฉ์ด ์ž์œ ๋กœ์šด Record<string,string>ํƒ€์ž…์ด ๋”์šฑ ์ ์ ˆํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜์š”!

@gahyuun gahyuun merged commit dd8df38 into main Nov 4, 2024
3 checks passed
@gahyuun gahyuun deleted the migration-my-fetch-ax branch November 4, 2024 01:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants