-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [LINKER-73] msw Mock Server 구축 + ky 인터페이스 개선 (#31)
- Loading branch information
Showing
27 changed files
with
707 additions
and
38 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file added
BIN
+5.99 KB
.yarn/cache/@mswjs-http-middleware-npm-0.9.2-dc94ec99bc-f5e8293269.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,4 @@ | ||
import ky from 'ky'; | ||
import kyInstance, { createKyApis, prefix } from './kyInstance'; | ||
|
||
const kyInstance = ky.create({ | ||
retry: 0, | ||
credentials: 'include', | ||
hooks: { | ||
beforeRequest: [ | ||
(request) => { | ||
// TODO(@useonglee): 토큰 로직 추가하기 | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
export default kyInstance; | ||
export const ky = createKyApis(kyInstance); | ||
export const API_URL = prefix; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* eslint-disable require-await */ | ||
import ky, { KyInstance, Options } from 'ky'; | ||
|
||
const kyInstance = ky.create({ | ||
retry: 0, | ||
credentials: 'include', | ||
timeout: 15_000, | ||
hooks: { | ||
beforeRequest: [ | ||
(request) => { | ||
// TODO(@useonglee): 토큰 로직 추가하기 | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
export const prefix = | ||
process.env.NEXT_PUBLIC_MSW_MOCK === 'enabled' | ||
? 'http://localhost:8000' | ||
: `${process.env.NEXT_PUBLIC_API_URL}/api`; | ||
|
||
export const createKyApis = (instance: KyInstance) => ({ | ||
get: async <T = unknown>(url: string, options?: Options) => { | ||
return instance | ||
.get(`${prefix}${url}`, options) | ||
.json<{ data: T }>() | ||
.then((data) => data.data); | ||
}, | ||
post: async <T = unknown>(url: string, options?: Options) => { | ||
return instance | ||
.post(`${prefix}${url}`, options) | ||
.json<{ data: T }>() | ||
.then((data) => data.data); | ||
}, | ||
put: async <T = unknown>(url: string, options?: Options) => { | ||
return instance | ||
.put(`${prefix}${url}`, options) | ||
.json<{ data: T }>() | ||
.then((data) => data.data); | ||
}, | ||
delete: async <T = unknown>(url: string, options?: Options) => { | ||
return instance | ||
.delete(`${prefix}${url}`, options) | ||
.json<{ data: T }>() | ||
.then((data) => data.data); | ||
}, | ||
}); | ||
|
||
export default kyInstance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es6", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"noEmit": true, | ||
"noImplicitAny": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "react-jsx", | ||
"incremental": true, | ||
"plugins": [ | ||
{ | ||
"name": "next" | ||
} | ||
], | ||
"baseUrl": ".", | ||
"paths": { | ||
"@*": ["./src/*"] | ||
} | ||
}, | ||
"include": [ | ||
"next-env.d.ts", | ||
"./src/**/*.tsx", | ||
"./src/**/*.ts", | ||
"./jest.config.ts", | ||
".next/types/**/*.ts" | ||
], | ||
"exclude": ["node_modules"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
NEXT_PUBLIC_API_URL=https://api.im-linker.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
NEXT_PUBLIC_API_URL=https://api.im-linker.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"jsc": { | ||
"parser": { | ||
"syntax": "typescript", | ||
"jsx": false, | ||
"dynamicImport": false, | ||
"privateMethod": false, | ||
"functionBind": false, | ||
"exportDefaultFrom": false, | ||
"exportNamespaceFrom": false, | ||
"decorators": false, | ||
"decoratorsBeforeExport": false, | ||
"topLevelAwait": false, | ||
"importMeta": false | ||
}, | ||
"transform": null, | ||
"target": "es5", | ||
"loose": false, | ||
"externalHelpers": false, | ||
"keepClassNames": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
import { http, HttpResponse } from 'msw'; | ||
import { cdnHandler } from './mocks/cdn'; | ||
import { feedHandlers } from './mocks/feed'; | ||
|
||
export const handlers = [ | ||
http.get('/api/test', () => { | ||
return HttpResponse.json({ result: true }, { status: 200 }); | ||
}), | ||
]; | ||
export const handlers = [...cdnHandler, ...feedHandlers]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* eslint-disable no-console */ | ||
import { createMiddleware } from '@mswjs/http-middleware'; | ||
import cors from 'cors'; | ||
import express from 'express'; | ||
import next from 'next'; | ||
|
||
import { handlers } from './handlers'; | ||
|
||
const app = express(); | ||
const port = 8000; | ||
|
||
const nextApp = next({ dev: true, port }); | ||
|
||
nextApp.prepare().then(() => { | ||
app.use( | ||
cors({ | ||
origin: 'http://localhost:3000', | ||
optionsSuccessStatus: 200, | ||
credentials: true, | ||
}), | ||
); | ||
app.use(express.json()); | ||
app.use(createMiddleware(...handlers)); | ||
|
||
app.listen(port, () => console.log(`Server listening on port: ${port}`)); | ||
}); | ||
|
||
export default nextApp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { API_URL } from '@linker/ky'; | ||
import { DefaultBodyType, http, HttpResponse, PathParams, ResponseResolver } from 'msw'; | ||
import { HttpRequestResolverExtras } from 'msw/lib/core/handlers/HttpHandler'; | ||
|
||
type Resolver = ResponseResolver<HttpRequestResolverExtras<PathParams>> | DefaultBodyType; | ||
|
||
type MockApi = Record<keyof typeof http, (endpoint: string, resolver: Resolver) => HttpResponse>; | ||
|
||
const methods = Object.keys(http); | ||
|
||
export const mockApi: MockApi = new Proxy({} as MockApi, { | ||
get(_target, key: keyof typeof http, _receiver) { | ||
if (!methods.includes(key)) { | ||
throw new Error('invalid method'); | ||
} | ||
|
||
return (endpoint: string, resolver: Resolver) => { | ||
const url = `${API_URL}${endpoint}`; | ||
|
||
return http[key as keyof typeof http](url, (info) => { | ||
if (typeof resolver !== 'function') { | ||
return HttpResponse.json({ result: info }); | ||
} | ||
|
||
return resolver(info); | ||
}); | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { http, HttpResponse } from 'msw'; | ||
|
||
export const cdnHandler = [ | ||
http.get('https://static.im-linker.com/*', () => { | ||
return HttpResponse.json({ result: '' }, { status: 200 }); | ||
}), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* eslint-disable max-len */ | ||
import { API_URL } from '@linker/ky'; | ||
import { http, HttpResponse } from 'msw'; | ||
|
||
export const feedHandlers = [ | ||
http.get(`${API_URL}/v1/my`, () => { | ||
return HttpResponse.json({ data: my }, { status: 200 }); | ||
}), | ||
|
||
http.get(`${API_URL}/v1/schedules/upcoming/recommendation`, () => { | ||
return HttpResponse.json({ data: upcomingSchedules }, { status: 200 }); | ||
}), | ||
]; | ||
|
||
const my = { | ||
name: '김태준', | ||
profileImgUrl: | ||
'https://postfiles.pstatic.net/MjAyMjA5MTdfMTE1/MDAxNjYzMzc3MDc1MTA2.bToArUww9E15OT_Mmt5mz7xAkuK98KGBbeI_dsJeaDAg.WJAhfo5kHehNQKWLEWKURBlZ7m_GZVZ9hoCBM2b_lL0g.JPEG.drusty97/IMG_0339.jpg?type=w966', | ||
job: 'Json 상하차 담당', | ||
association: 'Yapp23기 Web1팀', | ||
email: '[email protected]', | ||
tags: [ | ||
{ | ||
id: 1, | ||
name: '스포츠', | ||
}, | ||
{ | ||
id: 2, | ||
name: '게임', | ||
}, | ||
], | ||
contactsNum: 0, | ||
scheduleNum: 0, | ||
}; | ||
|
||
const upcomingSchedules = { | ||
scheduleId: '49b258bc-7a6e-4ce5-9ce0-901abcb38485', | ||
title: '일정 1', | ||
profileImgUrl: | ||
'https://postfiles.pstatic.net/MjAyMjA5MTdfMTE1/MDAxNjYzMzc3MDc1MTA2.bToArUww9E15OT_Mmt5mz7xAkuK98KGBbeI_dsJeaDAg.WJAhfo5kHehNQKWLEWKURBlZ7m_GZVZ9hoCBM2b_lL0g.JPEG.drusty97/IMG_0339.jpg?type=w966', | ||
startTs: '2024-01-13 17:55:04', | ||
endTs: '2024-01-13 18:55:04', | ||
recommendations: [ | ||
{ | ||
tag: { | ||
id: 1, | ||
name: '스포츠', | ||
}, | ||
contents: [ | ||
{ | ||
id: 1, | ||
title: '스포츠 뉴스', | ||
newsProvider: '연합뉴스', | ||
thumbnailUrl: 'https://r.yna.co.kr/global/home/v01/img/yonhapnews_logo_600x600_kr01.jpg', | ||
}, | ||
], | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.