MSW(Mock Service Worker) mock API ์ด๋ฏธ์ง€ ํŒŒ์ผ (๋ฐ”์ด๋„ˆ๋ฆฌ) ๋‹ค์šด๋กœ๋“œ

2023. 8. 24. 00:26ยท React

 

๋ฐ”์ด๋„ˆ๋ฆฌ ํƒ€์ž…์˜ ํŒŒ์ผ ์‘๋‹ต ๋ชจํ‚นํ•˜๊ธฐ

์ง€๊ธˆ ์‚ฌ๋‚ด ํ”„๋กœ์ ํŠธ๋Š” ๋ฐฑ์—”๋“œ api์™€ ํ”„๋ŸฐํŠธ ๊ฐœ๋ฐœ์„ ๋™์‹œ์— ๋ณ‘ํ–‰์ ์œผ๋กœ ์ง„ํ–‰ํ•˜๊ณ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์„œ๋ฒ„๋‹จ API๊ฐ€ ์™„์„ฑ๋˜๊ณ  ๋ฐฐํฌ๋˜๊ธฐ ์ด์ „์—๋Š” msw๋ผ๋Š” ์„œ๋ฒ„ API ๋ชจํ‚น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ์‹ค์ œ ๋ฐฑ์—”๋“œ api ์—†์ด๋„ ํ”„๋ŸฐํŠธ๋‹จ์—์„œ mock ์„œ๋ฒ„๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ๊ฐ€์งœ๋กœ ๋„คํŠธ์›Œํฌ ํ†ต์‹ ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. 

json ํ˜•์‹์œผ๋กœ ํ†ต์‹ ์„ ์ฃผ๊ณ ๋ฐ›๋Š” ๊ฑด ๊ณต์‹๋ฌธ์„œ์—๋„ ์ž˜ ๋‚˜์™€์žˆ์–ด์„œ Mocking์„ ํ–ˆ์—ˆ๋Š”๋ฐ, ์ด๋ฒˆ์— ๋ฐ”์ด๋„ˆ๋ฆฌ ํƒ€์ž…์˜ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ get ํ•ด์˜ค๋Š” ๋ถ€๋ถ„์—์„œ ๋ชฉ์„œ๋ฒ„๋ฅผ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ํฐ ๋‚œ๊ด€์ด ์žˆ์—ˆ๋‹ค.

 

์‘๋‹ต ์˜ˆ์‹œ

ํ”„๋ŸฐํŠธ์—์„œ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ get ํ•ด์˜ฌ๋•Œ ์„œ๋ฒ„์—์„œ ์ฃผ๋Š” ์‘๋‹ต์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ

์ฆ‰, ์„œ๋ฒ„์—์„œ๋Š” ์ด ๋ฉ€ํ‹ฐ๋ฏธ๋””์–ด ๋ฐ์ดํ„ฐ๋ฅผ 0๊ณผ 1 ๊ฐ’๋งŒ ์กด์žฌํ•˜๋Š” ์ด์ง„ ๋ฐ์ดํ„ฐ๋กœ ์‘๋‹ตํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. 

 

HTTP/1.1 200 OK
Content-Type: image/jpeg;charset=UTF-8
Accept-Ranges: bytes
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Frame-Options: DENY

 

 

์‹œ๋„ 

์ผ๋ฐ˜์ ์ธ json ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋Š” ์ง์ ‘ mock data๋ฅผ ๋„ฃ์–ด๋‘๊ณ , ์ด ๋ชฉ๋ฐ์ดํ„ฐ๋ฅผ ์‘๋‹ต์œผ๋กœ ์ฃผ๋‹ˆ๊นŒ.. 

const todoList = [
{ id: 'bzte_tfG3k2mh3o-Vf-zX', content: 'book a hotel'}, 
{ id: 'rAFHp_GrutBJzt_5r1Gej', content: 'issue a travel card'},
...
]

์ด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด๋ฏธ์ง€ํŒŒ์ผ๋„ ์„œ๋ฒ„์— ์žˆ๋Š”๊ฒƒ ์ฒ˜๋Ÿผ ๊ฐ€์งœ๋กœ ํ‘œํ˜„ํ•˜๋ ค๋ฉด...? 

 

 

๋‚ด ๋กœ์ปฌ ์ปดํ“จํ„ฐ์— ์žˆ๋Š” ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ src > assets ํด๋” ์•ˆ์— ๋„ฃ์–ด๋‘๊ณ , ๋ชจํ‚น ์ฝ”๋“œ๋ฅผ ์“ฐ๋Š” handler ํŒŒ์ผ์—์„œ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ import ํ•ด์™€์„œ ๋ฐ”์ด๋„ˆ๋ฆฌ ํƒ€์ž…์œผ๋กœ ์‘๋‹ต์„ ํ•ด์ค€๋‹ค.

๋ผ๋Š” ํ”„๋กœ์„ธ์Šค ์ด๊ฒ ์ง€?๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ  ์‹ค์ œ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ๋ฐ”์ด๋„ˆ๋ฆฌ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•ด๋ณด๋ ค๊ณ  ํ–ˆ์œผ๋‚˜.. ์‰ฝ๊ฒŒ ๋˜์ง€ ์•Š์•˜๋‹ค. 

 

๋”ฐ๋ผ์„œ ์•„๋ž˜์˜ ํ”„๋กœ์„ธ์Šค๋กœ mock ์ด๋ฏธ์ง€ ํŒŒ์ผ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์—ˆ๊ณ , ๋ฐ”์ด๋„ˆ๋ฆฌ ํƒ€์ž…์œผ๋กœ ์‘๋‹ต์„ ์ฃผ๋„๋ก ๋ชจํ‚นํ•˜์˜€๋‹ค.

 

  1. ๋กœ์ปฌ pc์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ์ด๋ฏธ์ง€๊ฐ€ ์•„๋‹Œ base64 ํ˜•์‹์˜ ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ค๊ธฐ
    • base64 ํŒŒ์ผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฒ•
      • ๊ตฌ๊ธ€๋ง์œผ๋กœ ์ฐพ์€ ์•„๋ฌด ์ด๋ฏธ์ง€ ๋งํฌ๋ฅผ base64 ์ธ์ฝ”๋”ฉ ์‚ฌ์ดํŠธ๋ฅผ ์ด์šฉํ•ด์„œ base64๋กœ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค.
    •  base64๋ž€?
      • 0๊ณผ 1๋กœ ์ด๋ค„์ง„ ๋ฐ”์ด๋„ˆ๋ฆฌ ํ˜•์‹์œผ๋กœ ์ธ์ฝ”๋”ฉ ๋˜์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ…์ŠคํŠธ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•œ ๊ฒƒ
  2. ํ…์ŠคํŠธํ˜•์‹์˜ base64 ๋ฐ์ดํ„ฐ๋ฅผ Binary ๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ
    • js์˜ ๋‚ด์žฅ ํ•จ์ˆ˜์ธ atob()๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ด์ง„๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜์‹œํ‚จ๋‹ค.
    • atob(encodedData) ํ•จ์ˆ˜๋Š” Base64 (en-US) ์ธ์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๋””์ฝ”๋”ฉํ•œ๋‹ค.
  3. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ Binary ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋„๋ก ArrayBuffer ๊ฐ์ฒด ๋งŒ๋“ค์–ด์ฃผ๊ธฐ
    • ArrayBuffer๋ž€?
      • ๋ฐ”์ดํŠธ๋กœ ๊ตฌ์„ฑ๋œ ๋ฐฐ์—ด
      • Js์—์„œ ์›์‹œ๋ฐ์ดํ„ฐ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„
    • new ArrayBuffer(16) //16๋ฐ”์ดํŠธ ํฌ๊ธฐ์˜ ๋ฒ„ํผ๋ฅผ ์ƒ์„ฑ
    • ์—ฐ์†์œผ๋กœ ๊ณ ์ •๋œ ํฌ๊ธฐ์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„(16๋ฐ”์ดํŠธ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„)์— ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
    • ์ดˆ๊ธฐ๊ฐ’์€ 0์œผ๋กœ ์ฑ„์›Œ์ ธ์žˆ์Œ
  4. ArrayBuffer์— ์ €์žฅ๋œ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ์— ์ง์ ‘ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด์ธ Unit8Array๋ฅผ ์ƒ์„ฑ
    • ArrayBuffer๋Š” ๋ฐ”์ดํŠธ๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ๋ฐฐ์—ด๋กœ "๋ฐ”์ดํŠธ ๋ฐฐ์—ด"์ด๋ผ๊ณ ๋„ ๋ถ€๋ฆ„
    • ArrayBuffer์— ๋‹ด๊ธด ์ •๋ณด๋ฅผ ์ง์ ‘ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅ
    • ๋”ฐ๋ผ์„œ ArrayBufferView ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ๋ฒ„ํผ๋ฅผ ํŠน์ • ํ˜•์‹์œผ๋กœ ๋‚˜ํƒ€๋ƒ„์œผ๋กœ์จ ๋ฒ„ํผ์˜ ๋‚ด์šฉ์„ ์ฝ๊ณ  ์“ฐ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅ
      • ArrayBufferView 
        • TypedArray, DataView 2์ข…๋ฅ˜์˜ ๊ฐ์ฒด๊ฐ€ ์žˆ์Œ
        • TypedArray (ํ˜•์‹ํ™” ๋ฐฐ์—ด)
          • Unit8Array, Uint16Array, Float32Array ๋“ฑ์ด ์žˆ์Œ
          • Unit8Array
            • 8๋น„ํŠธ = 1๋ฐ”์ดํŠธ๋กœ ๊ฐ ๋ฐ”์ดํŠธ ๋ณ„๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ view ๊ฐ์ฒด
  5. ArrayBuffer -> Blob(Binary Large Object) ํ˜•์‹์œผ๋กœ ๋ฐ”๊พธ๊ธฐ
    • Blob?
      • ์ด๋ฏธ์ง€, ์˜ค๋””์˜ค, ๋น„๋””์˜ค ๊ฐ™์€ ๋ฉ€ํ‹ฐ๋ฏธ๋””์–ด ํŒŒ์ผ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์ €์žฅํ•œ ๊ฒƒ
      • base64๊ฐ€ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ํ…์ŠคํŠธ๋กœ ์ €์žฅํ•œ๋‹ค๋ฉด Blob์€ ๊ฐ์ฒด ํ˜•์‹์œผ๋กœ ์ €์žฅํ•œ๋‹ค.
    • ๋งŒ๋“  ArrayBuffer๋ฅผ ์ธ์ˆ˜๋กœ ๋„ฃ์–ด์ฃผ์–ด new Blob(ArrayBuffer) ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ฌ

 

msw handler ์ „์ฒด ์ฝ”๋“œ

const base64mockImg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LHJJFbiR3p2TyiMtXr0g411rjncci7/APvtJi8lo2jBvsZy4erjJ6sl4ppXRDKROSSzXr0ieXO0rM8lYj6ViUn9W5e1bGKONOO1LfvE9vh7g0KII6qCZn+u2jtjn94J8QZA7Y/8w6y+E/5+msCrGc7H8/EtrHdl6TKt4lzB2xf5jL+K+Ip2xf4M9rk+E3D6Vb2FLmrs6Op1qTsHiTFirn5ew+koeLcQyMrK+J+DsRj+LlU6P1nY+ZWSGZbFPqCpEtlFyREJpOmbKvOFWJYC2pS4uWOfQ/CTuV+ZxEPjlayQPWUlXE3rJXybQA3T5T1kKNEOSZ6Hi54RtAcw31G5Z4mYpDIp7joD6Gec4vFPw751PqdGWFHGvLJKVWuew5VJ/wASuSZZGja5XEd0kWcvbXfpuQPD2YLWvbfUmY7MzOLZ1gWrh+YKt9xWessOD08RwrOZMDLKHuvlGQoUrbPN/DXHLYZPynWv5g8Y4t1rxkI5j8z69vQSKjZdzKRgZFXu5TtDu4dSa2LYWUWI/ENb/wDM486bTijsxVaYNGew0UuKn+1jsSxxuLjzAt6lT251/wDcyNmLxWu5lXhuU6js4UdR9tyXg4XFMi5a7cPIoQnq9ibA/YzLfSzXg1nkwyXLPRMK4ZSEr8wX83+PvH/K+khcIpp4biCmtmdieZnYaJP29JLOSN95q4JSjjSkYWaMXNuPg8zDL7RxWX2kEMYYYzUaOFE9WT2jilPaVwc+8IO3vDRJZqyewjitX/av7SqFje8MWN7w6sVotlNX9q/tHF8j+xf2lQtje8MWN7yNWTsi4XyP+Nf2jimj/jX9pSi1/eGLX94XEakXatSO1a/tHVurHZVH6SiFr+8IWv7wOBOxfjJUdgIYyxM95r+8XzX95GhOxofjB7zvix7zP+c/vO85/eR20TuX5y/rBOZ9ZQG1/eIbn957to9uXxzfrB+N+soTa/vE81/eT2yNz//Z';
const mockImg = atob(base64mockImg.split(',')[1]); // data:image/jpeg;base64 ๋–ผ์–ด๋‚ด์„œ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋กœ ๋””์ฝ”๋”ฉ
export const siteHandler = [
//์‚ฌ์ง„ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ
rest.get(`photos/:photoId/download`, (req, res, ctx) => {
const arrayBuffer = new ArrayBuffer(mockImg.length);
const view = new Uint8Array(arrayBuffer);
for (let i = 0; i < mockImg.length; i++) {
view[i] = mockImg.charCodeAt(i) & 0xff;
}
return res(
ctx.status(200),
ctx.set('Content-Type', 'image/jpeg'),
ctx.body(new Blob([arrayBuffer])),
);
}),
];

 

mock ์ด๋ฏธ์ง€๋ฅผ ์‘๋‹ต์œผ๋กœ ์ฃผ๊ธฐ์œ„ํ•ด ์œ„ ํ”„๋กœ์„ธ์Šค๋Œ€๋กœ ์ฐจ๋ก€๋Œ€๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ , ์‘๋‹ต ๋ฐ”๋””์— arrayBuffer๋ฅผ ๋‹ด์€ Blob ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด, ์„ฑ๊ณต์ ์œผ๋กœ response๊ฐ€ ๋“ค์–ด์˜จ๋‹ค.

 

 

response body์— ๋‹ด๊ฒจ์˜ค๋Š” ๋ชจ์Šต ์ฝ˜์†”๋กœ ํ™•์ธ

 

 

์ปดํฌ๋„ŒํŠธ ์ฝ”๋“œ 

export const downloadSiteImageFile = async (photoId: string) => {
try {
const { data }: { data: Blob } = await axios.get(
`http://localhost:5173/photos/${photoId}/download`,
{ responseType: 'blob' },
);
return data;
} catch (err) {
console.log(err);
}
};
export const PhotoCard = () => {
const [imgUrlFromBlob, setImgUrlFromBlob] = useState<string>();
useEffect(() => {
if (photoData.id) {
downloadSiteImageFile(photoData.id)
.then((res) => {
console.log(res, 'axios res');
if (res) {
const urlFromBlob = URL.createObjectURL(res);
setImgUrlFromBlob(urlFromBlob);
}
})
.catch((err) => console.log(err));
}
return () => {
if (imgUrlFromBlob) {
URL.revokeObjectURL(imgUrlFromBlob);
} //๋ธŒ๋ผ์šฐ์ €์˜ ๋ฉ”๋ชจ๋ฆฌ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด URL ๊ฐ์ฒด ํ•ด์ œ
};
}, []);
return (
<CardMedia
sx={{
width: 160,
height: 160,
border: 'solid 1px',
}}
image={imgUrlFromBlob}>
</CardMedia>
);
}

 

์‹ค์ œ Blob ๊ฐ์ฒด๋ฅผ ํ”„๋ŸฐํŠธ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด์„œ ์ด๋ฏธ์ง€ tag์—  URL.createObjectURL ์„ ์‚ฌ์šฉํ•ด์„œ url ์„ ์ƒ์„ฑํ•œ๋‹ค. 

 

์ปดํฌ๋„ŒํŠธ ์ฝ”๋“œ์—์„œ ์‚ฌ์ง„ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œํ•ด์˜จ ์‘๋‹ต response๋ฅผ ์ฐ์–ด๋ณด๋ฉด Blob ๊ฐ์ฒด๊ฐ€ ์ž˜ ๋“ค์–ด์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Reference 

https://www.daleseo.com/mock-service-worker/

https://mswjs.io/docs/recipes/binary-response-type

https://github.com/mswjs/msw/issues/461

https://inpa.tistory.com/m/entry/JS-%F0%9F%93%9A-Base64-Blob-ArrayBuffer-File-%EB%8B%A4%EB%A3%A8%EA%B8%B0-%EC%A0%95%EB%A7%90-%EC%9D%B4%ED%95%B4%ED%95%98%EA%B8%B0-%EC%89%BD%EA%B2%8C-%EC%84%A4%EB%AA%85  

https://curryyou.tistory.com/441 

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

Vite localhost ๋กœ์ปฌ ๊ฐœ๋ฐœ ์„œ๋ฒ„ ๋ชจ๋ฐ”์ผ ์ ‘์†  (0) 2023.10.05
flex item ๋์— ์ •๋ ฌํ•˜๊ธฐ positioning flex item to the end  (0) 2023.10.04
MVVM ๋””์ž์ธ ํŒจํ„ด์œผ๋กœ react todo app ๊ตฌํ˜„  (0) 2023.06.20
redux toolkit ๋ฐฐ์—ด ๊ฐ์ฒด ์š”์†Œ ์ˆ˜์ •ํ•˜๊ธฐ | [Immer] An immer producer returned a new value *and* modified its draft ์—๋Ÿฌ ํ•ด๊ฒฐ | ์–ธ์ œ return ์‚ฌ์šฉ?  (0) 2023.06.19
d3 shape - line ์ฐจํŠธ ๋ฐ์ดํ„ฐ ์—†์„ ๋•Œ ๋ผ์ธ ๊ทธ๋ž˜ํ”„ ์•ˆ๋ณด์ž„ ํ•ด๊ฒฐ  (0) 2023.06.12
'React' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • Vite localhost ๋กœ์ปฌ ๊ฐœ๋ฐœ ์„œ๋ฒ„ ๋ชจ๋ฐ”์ผ ์ ‘์†
  • flex item ๋์— ์ •๋ ฌํ•˜๊ธฐ positioning flex item to the end
  • MVVM ๋””์ž์ธ ํŒจํ„ด์œผ๋กœ react todo app ๊ตฌํ˜„
  • redux toolkit ๋ฐฐ์—ด ๊ฐ์ฒด ์š”์†Œ ์ˆ˜์ •ํ•˜๊ธฐ | [Immer] An immer producer returned a new value *and* modified its draft ์—๋Ÿฌ ํ•ด๊ฒฐ | ์–ธ์ œ return ์‚ฌ์šฉ?
hana1203
hana1203
hana's lab ๐ŸŒฑhana1203 ๋‹˜์˜ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
hana1203
hana's lab ๐ŸŒฑ
hana1203
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (95)
    • React (38)
    • JavaScript (7)
    • Firebase (4)
    • Google Cloud Platform (6)
    • Flutter (3)
    • Python (7)
    • Computer Science (6)
    • Problem Solving (3)
    • Git (5)
    • ํšŒ๊ณ  (10)
    • Etc (3)

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

  • ํ™ˆ

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

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

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

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

์ตœ๊ทผ ๊ธ€

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

hELLO ยท Designed By ์ •์ƒ์šฐ.v4.2.2
hana1203
MSW(Mock Service Worker) mock API ์ด๋ฏธ์ง€ ํŒŒ์ผ (๋ฐ”์ด๋„ˆ๋ฆฌ) ๋‹ค์šด๋กœ๋“œ
์ƒ๋‹จ์œผ๋กœ

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

๋‹จ์ถ•ํ‚ค

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

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

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

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

๋ชจ๋“  ์˜์—ญ

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

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