rtk query ์„œ๋ฒ„ response body payload ๊ฐ’ ๋ฐ”๋กœ ์•ˆ ๊ฐ€์ ธ์™€์ง€๋Š” ์ด์Šˆ

2023. 11. 22. 18:39ยท React
๋ชฉ์ฐจ
  1. ๊ณต์‹๋ฌธ์„œ ์ฐธ๊ณ 

์ด์Šˆ์‚ฌํ•ญ

 

postTokenQuery(token)
.then((res) => {
console.log(res, '์ฟผ๋ฆฌ res');
if (res.data) {
setTokenInStorage(token);
}
})
.catch(() => toast.error('ํ† ํฐ ์—๋Ÿฌ ๋ฐœ์ƒ')); //์„œ๋ฒ„ ์‘๋‹ต ์‹คํŒจ์‹œ ์‹คํ–‰ ์•ˆ๋จ

 

rtk query ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉํ•ด์„œ ์„œ๋ฒ„์™€ ํ†ต์‹ ํ•˜๋Š”๋ฐ ์„œ๋ฒ„์—์„œ ๋ณด๋‚ด์ฃผ๋Š” ์‘๋‹ต์— ๋”ฐ๋ผ 

 ์„ฑ๊ณตํ•˜๋ฉด?  .then ์œผ๋กœ ํŠน์ • ๋กœ์ง์„ ์‹คํ–‰ํ•˜๊ณ 

 ์‹คํŒจํ•˜๋ฉด? .catch ๋กœ ํ† ์ŠคํŠธ๋ฅผ ๋„์šฐ๋Š” ๋กœ์ง์ด๋‹ค. 

 

๊ทธ๋Ÿฐ๋ฐ ์„œ๋ฒ„์˜ ์‘๋‹ต์— ์‹คํŒจํ•ด๋„, catch ๋ฌธ์œผ๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š๊ณ , then์—์„œ ๊ฑธ๋ ค res ์‘๋‹ต์„ ์ฝ˜์†”์— ๋„์šฐ๋Š” ์ด์ƒํ•œ ์ ์ด ๋ฐœ๊ฒฌ๋˜์—ˆ๋‹ค. 

์„ฑ๊ณต์‹œ {data: {โ€ฆ}} ๊ฐ€ ์ฝ˜์†”์— ์ฐํžˆ๊ณ , ์‹คํŒจ์‹œ {error: {...}}๊ฐ€ ์ฝ˜์†”์— ์ฐํžˆ๋Š” ๊ฑธ ํ™•์ธํ•˜์˜€๋‹ค.

 

์›์ธ 

HTTP requests ๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” fetchBaseQuery๋ฉ”์„œ๋“œ๋กœ ๊ธฐ๋ณธ baseQuery๋ฅผ ์„ค์ •ํ–ˆ๋Š”๋ฐ, fetchBaseQuery์˜ ๊ฒฐ๊ณผ๋กœ Promise ๊ฒฐ๊ณผ ํƒ€์ž…์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. 

 

type FetchBaseQueryResult = Promise<
| {
data: any
error?: undefined
meta?: { request: Request; response: Response }
}
| {
error: {
status: number
data: any
}
data?: undefined
meta?: { request: Request; response: Response }
}
>

 

์œ„์™€ ๊ฐ™์€ ํƒ€์ž…์œผ๋กœ ์ •์˜๋˜์–ด์žˆ๊ธฐ์—, api ํ˜ธ์ถœ์ด ์„ฑ๊ณตํ•˜๋ฉด, ์„œ๋ฒ„์—์„œ ์ฃผ๋Š” ์‘๋‹ต payload body๊ฐ€ {data: {โ€ฆ}} ํ˜•ํƒœ๋กœ ์‘๋‹ต์„ ์ฃผ๋Š” ๊ฒƒ์ด์—ˆ๊ณ 

์‹คํŒจํ•˜๋ฉด, {error: {โ€ฆ}} ํ˜•ํƒœ๋กœ ์‘๋‹ต์„ ์ฃผ๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. 

๋”ฐ๋ผ์„œ ์ฟผ๋ฆฌ๋กœ ๋ฐ›๋Š” response ํƒ€์ž…์„ ์„ฑ๊ณต SuccessType, ์‹คํŒจ ErrorType์˜ union type์œผ๋กœ ์ง€์ •ํ•˜์—ฌ .then() ์ฒด์ธ ๋‚ด๋ถ€์—์„œ ์„ฑ๊ณต์‹œ? ~~ ์‹คํŒจ์‹œ? ~~ ๋ฐ์ดํ„ฐ๋ฅผ ์ผ์ผ์ด ํƒ€์ž…์— ๋”ฐ๋ผ ๋ฐ›์•„์™€์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ์—ˆ๋‹ค. 

 

ํ•ด๊ฒฐ 

rtk query์˜ unwrap() ์ด๋ผ๋Š” ์ฒด์ธ์„ ์‚ฌ์šฉํ•ด์„œ ์„ฑ๊ณต / ์‹คํŒจ์˜ payload๋ฅผ ๋ฐ”๋กœ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค. 

 

postTokenQuery(token)
.unwrap()
.then((res: TokenResponseType) => {
console.log(res, '์ฟผ๋ฆฌ res');
setTokenInStorage(token);
})
.catch(() => toast.error('ํ† ํฐ ์—๋Ÿฌ ๋ฐœ์ƒ'));

 

์ด๋ ‡๊ฒŒ ์ ์–ด์ฃผ๋ฉด ์„œ๋ฒ„ ์‘๋‹ต ์„ฑ๊ณต์‹œ response์— ๋ฐ”๋กœ ์‘๋‹ต payload ๋ฐ”๋””๊ฐ€ ๋‹ด๊ธฐ๊ณ , ์—๋Ÿฌ๋Š” ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ catch ๋ฌธ์—์„œ ์žกํžŒ๋‹ค. 

 

 

๊ณต์‹๋ฌธ์„œ ์ฐธ๊ณ 

https://redux-toolkit.js.org/rtk-query/api/fetchBaseQuery

https://redux-toolkit.js.org/rtk-query/usage/error-handling

'React' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

failed to resolve entry for package monorepo  (0) 2024.01.23
์žฌ๊ท€ํ•จ์ˆ˜ ๊ฒฐ๊ณผ๊ฐ’ useMemo๋กœ ์žฌ๋ Œ๋”๋ง ๋ฐฉ์ง€  (0) 2024.01.17
axios post ์š”์ฒญ์‹œ ํ—ค๋”, ํŒŒ๋ผ๋ฏธํ„ฐ ์•ˆ ๋ณด๋‚ด์ง€๋Š” ์˜ค๋ฅ˜  (0) 2023.11.01
onClick์‹œ onBlur์ด๋ฒคํŠธ๊ฐ€ ๋จผ์ € ์‹คํ–‰๋˜๋Š” ์ด์Šˆํ•ด๊ฒฐ mui textfield adornment  (0) 2023.10.26
react ์ด์ „ ํŽ˜์ด์ง€ ํ™•์ธ - ์–ด๋–ค ํŽ˜์ด์ง€์—์„œ ์ง„์ž…ํ•˜๋Š”์ง€ ํ™•์ธ  (0) 2023.10.20
  1. ๊ณต์‹๋ฌธ์„œ ์ฐธ๊ณ 
'React' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • failed to resolve entry for package monorepo
  • ์žฌ๊ท€ํ•จ์ˆ˜ ๊ฒฐ๊ณผ๊ฐ’ useMemo๋กœ ์žฌ๋ Œ๋”๋ง ๋ฐฉ์ง€
  • axios post ์š”์ฒญ์‹œ ํ—ค๋”, ํŒŒ๋ผ๋ฏธํ„ฐ ์•ˆ ๋ณด๋‚ด์ง€๋Š” ์˜ค๋ฅ˜
  • onClick์‹œ onBlur์ด๋ฒคํŠธ๊ฐ€ ๋จผ์ € ์‹คํ–‰๋˜๋Š” ์ด์Šˆํ•ด๊ฒฐ mui textfield adornment
hana1203
hana1203
hana1203
hana's lab ๐ŸŒฑ
hana1203
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (93)
    • React (38)
    • JavaScript (7)
    • Firebase (4)
    • Google Cloud Platform (4)
    • Flutter (3)
    • Python (7)
    • Computer Science (6)
    • Problem Solving (3)
    • Git (5)
    • ํšŒ๊ณ  (10)
    • Etc (3)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ

๊ณต์ง€์‚ฌํ•ญ

์ „์ฒด
์˜ค๋Š˜
์–ด์ œ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • ํ‹ฐ์Šคํ† ๋ฆฌ์ฑŒ๋ฆฐ์ง€
  • ์˜ค๋ธ”์™„

์ตœ๊ทผ ๊ธ€

์ตœ๊ทผ ๋Œ“๊ธ€

hELLO ยท Designed By ์ •์ƒ์šฐ.v4.2.2
hana1203
rtk query ์„œ๋ฒ„ response body payload ๊ฐ’ ๋ฐ”๋กœ ์•ˆ ๊ฐ€์ ธ์™€์ง€๋Š” ์ด์Šˆ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”

๋‹จ์ถ•ํ‚ค

๋‚ด ๋ธ”๋กœ๊ทธ

๋‚ด ๋ธ”๋กœ๊ทธ - ๊ด€๋ฆฌ์ž ํ™ˆ ์ „ํ™˜
Q
Q
์ƒˆ ๊ธ€ ์“ฐ๊ธฐ
W
W

๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€

๊ธ€ ์ˆ˜์ • (๊ถŒํ•œ ์žˆ๋Š” ๊ฒฝ์šฐ)
E
E
๋Œ“๊ธ€ ์˜์—ญ์œผ๋กœ ์ด๋™
C
C

๋ชจ๋“  ์˜์—ญ

์ด ํŽ˜์ด์ง€์˜ URL ๋ณต์‚ฌ
S
S
๋งจ ์œ„๋กœ ์ด๋™
T
T
ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ ์ด๋™
H
H
๋‹จ์ถ•ํ‚ค ์•ˆ๋‚ด
Shift + /
โ‡ง + /

* ๋‹จ์ถ•ํ‚ค๋Š” ํ•œ๊ธ€/์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ‹ฐ์Šคํ† ๋ฆฌ ๊ธฐ๋ณธ ๋„๋ฉ”์ธ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.