@farmzone/fz-template-react 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/create.js +10 -4
- package/package.json +8 -2
- package/template/.env.example +5 -0
- package/template/eslint.config.js +4 -1
- package/template/index.css +15 -2
- package/template/index.html +1 -1
- package/template/package.json +55 -41
- package/template/public/favicon.ico +0 -0
- package/template/public/mockServiceWorker.js +349 -0
- package/template/src/app/App.tsx +2 -0
- package/template/src/app/api/api.ts +178 -0
- package/template/src/app/api/queries.ts +321 -0
- package/template/src/app/api/queryKey.ts +7 -0
- package/template/src/app/api/token.ts +7 -0
- package/template/src/app/layout/Layout.tsx +33 -16
- package/template/src/app/layout/ListContents.tsx +9 -0
- package/template/src/app/layout/ListHeader.tsx +41 -0
- package/template/src/app/layout/MultiTabNav.tsx +101 -0
- package/template/src/app/layout/Sidebar.tsx +33 -53
- package/template/src/app/layout/UserInfo.tsx +94 -0
- package/template/src/app/layout/menu.ts +46 -21
- package/template/src/app/layout/tabSwitchStore.ts +11 -0
- package/template/src/app/router/Router.tsx +54 -28
- package/template/src/app/store/index.ts +26 -0
- package/template/src/index.tsx +21 -12
- package/template/src/mocks/browser.ts +17 -0
- package/template/src/mocks/handlers.ts +43 -0
- package/template/src/mocks/scenarios.ts +57 -0
- package/template/src/pages/dashboard/index.tsx +541 -8
- package/template/src/pages/error/Error.tsx +29 -17
- package/template/src/pages/error/NotFound.tsx +27 -17
- package/template/src/pages/login/index.tsx +317 -0
- package/template/src/pages/post/PostFormModal.tsx +128 -0
- package/template/src/pages/post/detail/index.tsx +548 -0
- package/template/src/pages/post/index.tsx +267 -0
- package/template/src/pages/sample/SampleFormModal.tsx +77 -0
- package/template/src/pages/sample/detail/index.tsx +424 -0
- package/template/src/pages/sample/index.tsx +269 -0
- package/template/src/pages/system/log/index.tsx +173 -0
- package/template/src/pages/user/config/columns.tsx +109 -0
- package/template/src/pages/user/config/schema.ts +54 -0
- package/template/src/pages/user/index.tsx +641 -0
- package/template/src/shared/components/CommentInput.tsx +243 -0
- package/template/src/shared/components/FilePreviewCard.tsx +70 -0
- package/template/src/shared/config/text.ts +27 -0
- package/template/src/shared/config/type.ts +40 -0
- package/template/src/shared/utils/format.ts +11 -0
- package/template/src/types/auth.ts +10 -0
- package/template/src/types/comment.ts +33 -0
- package/template/src/types/common.ts +19 -0
- package/template/src/types/dashboard.ts +53 -0
- package/template/src/types/index.ts +16 -0
- package/template/src/types/log.ts +21 -0
- package/template/src/types/post.ts +32 -0
- package/template/src/types/sample.ts +28 -0
- package/template/src/types/user.ts +51 -0
- package/template/src/vite-env.d.ts +10 -0
- package/template/gitignore +0 -32
package/bin/create.js
CHANGED
|
@@ -60,7 +60,9 @@ async function main() {
|
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
if (!/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/.test(projectName)) {
|
|
63
|
-
console.error(
|
|
63
|
+
console.error(
|
|
64
|
+
"❌ 프로젝트 이름은 소문자, 숫자, 하이픈(-)만 사용 가능하며 하이픈으로 시작하거나 끝날 수 없습니다.",
|
|
65
|
+
);
|
|
64
66
|
process.exit(1);
|
|
65
67
|
}
|
|
66
68
|
|
|
@@ -84,15 +86,19 @@ async function main() {
|
|
|
84
86
|
if (existsSync(gitignoreSrc)) renameSync(gitignoreSrc, gitignoreDest);
|
|
85
87
|
|
|
86
88
|
// __PROJECT_NAME__ 치환
|
|
87
|
-
replaceInFile(
|
|
89
|
+
replaceInFile(
|
|
90
|
+
join(targetDir, "package.json"),
|
|
91
|
+
"__PROJECT_NAME__",
|
|
92
|
+
projectName,
|
|
93
|
+
);
|
|
88
94
|
replaceInFile(join(targetDir, "index.html"), "__PROJECT_NAME__", projectName);
|
|
89
95
|
|
|
90
96
|
console.log("✅ 프로젝트 생성 완료!\n");
|
|
91
97
|
console.log("──────────────────────────────");
|
|
92
98
|
console.log("다음 명령어로 시작하세요:\n");
|
|
93
99
|
console.log(` cd ${projectName}`);
|
|
94
|
-
console.log(`
|
|
95
|
-
console.log(`
|
|
100
|
+
console.log(` pnpm install`);
|
|
101
|
+
console.log(` pnpm run dev`);
|
|
96
102
|
console.log("──────────────────────────────\n");
|
|
97
103
|
}
|
|
98
104
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farmzone/fz-template-react",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Farmzone React 프로젝트 보일러플레이트 생성 CLI",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -10,7 +10,13 @@
|
|
|
10
10
|
"bin",
|
|
11
11
|
"template"
|
|
12
12
|
],
|
|
13
|
-
"keywords": [
|
|
13
|
+
"keywords": [
|
|
14
|
+
"farmzone",
|
|
15
|
+
"react",
|
|
16
|
+
"template",
|
|
17
|
+
"boilerplate",
|
|
18
|
+
"vite"
|
|
19
|
+
],
|
|
14
20
|
"license": "UNLICENSED",
|
|
15
21
|
"engines": {
|
|
16
22
|
"node": ">=18"
|
|
@@ -12,12 +12,15 @@ export default defineConfig([
|
|
|
12
12
|
extends: [
|
|
13
13
|
js.configs.recommended,
|
|
14
14
|
tseslint.configs.recommended,
|
|
15
|
-
reactHooks.configs
|
|
15
|
+
reactHooks.configs["recommended-latest"],
|
|
16
16
|
reactRefresh.configs.vite,
|
|
17
17
|
],
|
|
18
18
|
languageOptions: {
|
|
19
19
|
ecmaVersion: 2020,
|
|
20
20
|
globals: globals.browser,
|
|
21
21
|
},
|
|
22
|
+
rules: {
|
|
23
|
+
"@typescript-eslint/array-type": ["error", { default: "generic" }],
|
|
24
|
+
},
|
|
22
25
|
},
|
|
23
26
|
]);
|
package/template/index.css
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
@import "tailwindcss";
|
|
2
2
|
@import "tw-animate-css";
|
|
3
|
-
@import "@farmzone/fz-react-ui/
|
|
3
|
+
@import "@farmzone/fz-react-ui/tw.css";
|
|
4
4
|
|
|
5
5
|
@custom-variant dark (&:is(.dark *));
|
|
6
6
|
|
|
@@ -12,8 +12,21 @@
|
|
|
12
12
|
--color-sub-darkgray: #6b7280;
|
|
13
13
|
--color-sub-darkgray-2: #374151;
|
|
14
14
|
--color-basic-gray: #9ca3af;
|
|
15
|
+
--color-th: rgb(250 250 250);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@layer utilities {
|
|
19
|
+
.bg-th {
|
|
20
|
+
background-color: var(--color-th);
|
|
21
|
+
}
|
|
15
22
|
}
|
|
16
23
|
|
|
17
24
|
body {
|
|
18
|
-
font-family:
|
|
25
|
+
font-family:
|
|
26
|
+
"Pretendard Variable",
|
|
27
|
+
Pretendard,
|
|
28
|
+
-apple-system,
|
|
29
|
+
BlinkMacSystemFont,
|
|
30
|
+
system-ui,
|
|
31
|
+
sans-serif;
|
|
19
32
|
}
|
package/template/index.html
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
href="https://cdn.jsdelivr.net/gh/orioncactus/pretendard@v1.3.9/dist/web/static/pretendard-dynamic-subset.min.css"
|
|
11
11
|
/>
|
|
12
12
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
13
|
-
<title>
|
|
13
|
+
<title>fz-template-react</title>
|
|
14
14
|
</head>
|
|
15
15
|
<body>
|
|
16
16
|
<div id="root"></div>
|
package/template/package.json
CHANGED
|
@@ -1,41 +1,55 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "__PROJECT_NAME__",
|
|
3
|
-
"private": true,
|
|
4
|
-
"version": "
|
|
5
|
-
"type": "module",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"dev": "vite --host 0.0.0.0 --port
|
|
8
|
-
"build": "tsc -b && vite build",
|
|
9
|
-
"lint": "eslint .",
|
|
10
|
-
"preview": "vite preview"
|
|
11
|
-
},
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"@farmzone/fz-react-ui": "
|
|
14
|
-
"@hookform/resolvers": "^5.2.2",
|
|
15
|
-
"@tanstack/react-query": "^5.90.0",
|
|
16
|
-
"axios": "^1.13.0",
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"react
|
|
21
|
-
"react
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"eslint": "^9.0.0",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "__PROJECT_NAME__",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "1.0.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite --host 0.0.0.0 --port 5000",
|
|
8
|
+
"build": "tsc -b && vite build",
|
|
9
|
+
"lint": "eslint .",
|
|
10
|
+
"preview": "vite preview"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@farmzone/fz-react-ui": "^0.0.6",
|
|
14
|
+
"@hookform/resolvers": "^5.2.2",
|
|
15
|
+
"@tanstack/react-query": "^5.90.0",
|
|
16
|
+
"axios": "^1.13.0",
|
|
17
|
+
"dayjs": "^1.11.21",
|
|
18
|
+
"es-toolkit": "^1.47.0",
|
|
19
|
+
"js-cookie": "^3.0.8",
|
|
20
|
+
"lucide-react": "^1.16.0",
|
|
21
|
+
"react": "^19.0.0",
|
|
22
|
+
"react-daum-postcode": "^4.0.0",
|
|
23
|
+
"react-dom": "^19.0.0",
|
|
24
|
+
"react-hook-form": "^7.68.0",
|
|
25
|
+
"react-router": "^7.11.0",
|
|
26
|
+
"uuid": "^14.0.0",
|
|
27
|
+
"zod": "^3.25.0",
|
|
28
|
+
"zustand": "^5.0.14"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@eslint/js": "^9.0.0",
|
|
32
|
+
"@tailwindcss/vite": "^4.1.0",
|
|
33
|
+
"@types/js-cookie": "^3.0.6",
|
|
34
|
+
"@types/node": "^22.0.0",
|
|
35
|
+
"@types/react": "^19.0.0",
|
|
36
|
+
"@types/react-dom": "^19.0.0",
|
|
37
|
+
"@types/uuid": "^11.0.0",
|
|
38
|
+
"@vitejs/plugin-react": "^5.1.0",
|
|
39
|
+
"eslint": "^9.0.0",
|
|
40
|
+
"eslint-plugin-react-hooks": "^5.0.0",
|
|
41
|
+
"eslint-plugin-react-refresh": "^0.4.0",
|
|
42
|
+
"globals": "^16.0.0",
|
|
43
|
+
"msw": "^2.14.6",
|
|
44
|
+
"tailwindcss": "^4.0.0",
|
|
45
|
+
"tw-animate-css": "^1.0.0",
|
|
46
|
+
"typescript": "~5.9.0",
|
|
47
|
+
"typescript-eslint": "^8.0.0",
|
|
48
|
+
"vite": "^7.0.0"
|
|
49
|
+
},
|
|
50
|
+
"msw": {
|
|
51
|
+
"workerDirectory": [
|
|
52
|
+
"public"
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/* tslint:disable */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Mock Service Worker.
|
|
6
|
+
* @see https://github.com/mswjs/msw
|
|
7
|
+
* - Please do NOT modify this file.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const PACKAGE_VERSION = '2.14.6'
|
|
11
|
+
const INTEGRITY_CHECKSUM = '4db4a41e972cec1b64cc569c66952d82'
|
|
12
|
+
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
|
|
13
|
+
const activeClientIds = new Set()
|
|
14
|
+
|
|
15
|
+
addEventListener('install', function () {
|
|
16
|
+
self.skipWaiting()
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
addEventListener('activate', function (event) {
|
|
20
|
+
event.waitUntil(self.clients.claim())
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
addEventListener('message', async function (event) {
|
|
24
|
+
const clientId = Reflect.get(event.source || {}, 'id')
|
|
25
|
+
|
|
26
|
+
if (!clientId || !self.clients) {
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const client = await self.clients.get(clientId)
|
|
31
|
+
|
|
32
|
+
if (!client) {
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const allClients = await self.clients.matchAll({
|
|
37
|
+
type: 'window',
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
switch (event.data) {
|
|
41
|
+
case 'KEEPALIVE_REQUEST': {
|
|
42
|
+
sendToClient(client, {
|
|
43
|
+
type: 'KEEPALIVE_RESPONSE',
|
|
44
|
+
})
|
|
45
|
+
break
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
case 'INTEGRITY_CHECK_REQUEST': {
|
|
49
|
+
sendToClient(client, {
|
|
50
|
+
type: 'INTEGRITY_CHECK_RESPONSE',
|
|
51
|
+
payload: {
|
|
52
|
+
packageVersion: PACKAGE_VERSION,
|
|
53
|
+
checksum: INTEGRITY_CHECKSUM,
|
|
54
|
+
},
|
|
55
|
+
})
|
|
56
|
+
break
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
case 'MOCK_ACTIVATE': {
|
|
60
|
+
activeClientIds.add(clientId)
|
|
61
|
+
|
|
62
|
+
sendToClient(client, {
|
|
63
|
+
type: 'MOCKING_ENABLED',
|
|
64
|
+
payload: {
|
|
65
|
+
client: {
|
|
66
|
+
id: client.id,
|
|
67
|
+
frameType: client.frameType,
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
break
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
case 'CLIENT_CLOSED': {
|
|
75
|
+
activeClientIds.delete(clientId)
|
|
76
|
+
|
|
77
|
+
const remainingClients = allClients.filter((client) => {
|
|
78
|
+
return client.id !== clientId
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
// Unregister itself when there are no more clients
|
|
82
|
+
if (remainingClients.length === 0) {
|
|
83
|
+
self.registration.unregister()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
break
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
addEventListener('fetch', function (event) {
|
|
92
|
+
const requestInterceptedAt = Date.now()
|
|
93
|
+
|
|
94
|
+
// Bypass navigation requests.
|
|
95
|
+
if (event.request.mode === 'navigate') {
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Opening the DevTools triggers the "only-if-cached" request
|
|
100
|
+
// that cannot be handled by the worker. Bypass such requests.
|
|
101
|
+
if (
|
|
102
|
+
event.request.cache === 'only-if-cached' &&
|
|
103
|
+
event.request.mode !== 'same-origin'
|
|
104
|
+
) {
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Bypass all requests when there are no active clients.
|
|
109
|
+
// Prevents the self-unregistered worked from handling requests
|
|
110
|
+
// after it's been terminated (still remains active until the next reload).
|
|
111
|
+
if (activeClientIds.size === 0) {
|
|
112
|
+
return
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const requestId = crypto.randomUUID()
|
|
116
|
+
event.respondWith(handleRequest(event, requestId, requestInterceptedAt))
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @param {FetchEvent} event
|
|
121
|
+
* @param {string} requestId
|
|
122
|
+
* @param {number} requestInterceptedAt
|
|
123
|
+
*/
|
|
124
|
+
async function handleRequest(event, requestId, requestInterceptedAt) {
|
|
125
|
+
const client = await resolveMainClient(event)
|
|
126
|
+
const requestCloneForEvents = event.request.clone()
|
|
127
|
+
const response = await getResponse(
|
|
128
|
+
event,
|
|
129
|
+
client,
|
|
130
|
+
requestId,
|
|
131
|
+
requestInterceptedAt,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
// Send back the response clone for the "response:*" life-cycle events.
|
|
135
|
+
// Ensure MSW is active and ready to handle the message, otherwise
|
|
136
|
+
// this message will pend indefinitely.
|
|
137
|
+
if (client && activeClientIds.has(client.id)) {
|
|
138
|
+
const serializedRequest = await serializeRequest(requestCloneForEvents)
|
|
139
|
+
|
|
140
|
+
// Clone the response so both the client and the library could consume it.
|
|
141
|
+
const responseClone = response.clone()
|
|
142
|
+
|
|
143
|
+
sendToClient(
|
|
144
|
+
client,
|
|
145
|
+
{
|
|
146
|
+
type: 'RESPONSE',
|
|
147
|
+
payload: {
|
|
148
|
+
isMockedResponse: IS_MOCKED_RESPONSE in response,
|
|
149
|
+
request: {
|
|
150
|
+
id: requestId,
|
|
151
|
+
...serializedRequest,
|
|
152
|
+
},
|
|
153
|
+
response: {
|
|
154
|
+
type: responseClone.type,
|
|
155
|
+
status: responseClone.status,
|
|
156
|
+
statusText: responseClone.statusText,
|
|
157
|
+
headers: Object.fromEntries(responseClone.headers.entries()),
|
|
158
|
+
body: responseClone.body,
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
responseClone.body ? [serializedRequest.body, responseClone.body] : [],
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return response
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Resolve the main client for the given event.
|
|
171
|
+
* Client that issues a request doesn't necessarily equal the client
|
|
172
|
+
* that registered the worker. It's with the latter the worker should
|
|
173
|
+
* communicate with during the response resolving phase.
|
|
174
|
+
* @param {FetchEvent} event
|
|
175
|
+
* @returns {Promise<Client | undefined>}
|
|
176
|
+
*/
|
|
177
|
+
async function resolveMainClient(event) {
|
|
178
|
+
const client = await self.clients.get(event.clientId)
|
|
179
|
+
|
|
180
|
+
if (activeClientIds.has(event.clientId)) {
|
|
181
|
+
return client
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (client?.frameType === 'top-level') {
|
|
185
|
+
return client
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const allClients = await self.clients.matchAll({
|
|
189
|
+
type: 'window',
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
return allClients
|
|
193
|
+
.filter((client) => {
|
|
194
|
+
// Get only those clients that are currently visible.
|
|
195
|
+
return client.visibilityState === 'visible'
|
|
196
|
+
})
|
|
197
|
+
.find((client) => {
|
|
198
|
+
// Find the client ID that's recorded in the
|
|
199
|
+
// set of clients that have registered the worker.
|
|
200
|
+
return activeClientIds.has(client.id)
|
|
201
|
+
})
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* @param {FetchEvent} event
|
|
206
|
+
* @param {Client | undefined} client
|
|
207
|
+
* @param {string} requestId
|
|
208
|
+
* @param {number} requestInterceptedAt
|
|
209
|
+
* @returns {Promise<Response>}
|
|
210
|
+
*/
|
|
211
|
+
async function getResponse(event, client, requestId, requestInterceptedAt) {
|
|
212
|
+
// Clone the request because it might've been already used
|
|
213
|
+
// (i.e. its body has been read and sent to the client).
|
|
214
|
+
const requestClone = event.request.clone()
|
|
215
|
+
|
|
216
|
+
function passthrough() {
|
|
217
|
+
// Cast the request headers to a new Headers instance
|
|
218
|
+
// so the headers can be manipulated with.
|
|
219
|
+
const headers = new Headers(requestClone.headers)
|
|
220
|
+
|
|
221
|
+
// Remove the "accept" header value that marked this request as passthrough.
|
|
222
|
+
// This prevents request alteration and also keeps it compliant with the
|
|
223
|
+
// user-defined CORS policies.
|
|
224
|
+
const acceptHeader = headers.get('accept')
|
|
225
|
+
if (acceptHeader) {
|
|
226
|
+
const values = acceptHeader.split(',').map((value) => value.trim())
|
|
227
|
+
const filteredValues = values.filter(
|
|
228
|
+
(value) => value !== 'msw/passthrough',
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
if (filteredValues.length > 0) {
|
|
232
|
+
headers.set('accept', filteredValues.join(', '))
|
|
233
|
+
} else {
|
|
234
|
+
headers.delete('accept')
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return fetch(requestClone, { headers })
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Bypass mocking when the client is not active.
|
|
242
|
+
if (!client) {
|
|
243
|
+
return passthrough()
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Bypass initial page load requests (i.e. static assets).
|
|
247
|
+
// The absence of the immediate/parent client in the map of the active clients
|
|
248
|
+
// means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet
|
|
249
|
+
// and is not ready to handle requests.
|
|
250
|
+
if (!activeClientIds.has(client.id)) {
|
|
251
|
+
return passthrough()
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Notify the client that a request has been intercepted.
|
|
255
|
+
const serializedRequest = await serializeRequest(event.request)
|
|
256
|
+
const clientMessage = await sendToClient(
|
|
257
|
+
client,
|
|
258
|
+
{
|
|
259
|
+
type: 'REQUEST',
|
|
260
|
+
payload: {
|
|
261
|
+
id: requestId,
|
|
262
|
+
interceptedAt: requestInterceptedAt,
|
|
263
|
+
...serializedRequest,
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
[serializedRequest.body],
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
switch (clientMessage.type) {
|
|
270
|
+
case 'MOCK_RESPONSE': {
|
|
271
|
+
return respondWithMock(clientMessage.data)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
case 'PASSTHROUGH': {
|
|
275
|
+
return passthrough()
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return passthrough()
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* @param {Client} client
|
|
284
|
+
* @param {any} message
|
|
285
|
+
* @param {Array<Transferable>} transferrables
|
|
286
|
+
* @returns {Promise<any>}
|
|
287
|
+
*/
|
|
288
|
+
function sendToClient(client, message, transferrables = []) {
|
|
289
|
+
return new Promise((resolve, reject) => {
|
|
290
|
+
const channel = new MessageChannel()
|
|
291
|
+
|
|
292
|
+
channel.port1.onmessage = (event) => {
|
|
293
|
+
if (event.data && event.data.error) {
|
|
294
|
+
return reject(event.data.error)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
resolve(event.data)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
client.postMessage(message, [
|
|
301
|
+
channel.port2,
|
|
302
|
+
...transferrables.filter(Boolean),
|
|
303
|
+
])
|
|
304
|
+
})
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* @param {Response} response
|
|
309
|
+
* @returns {Response}
|
|
310
|
+
*/
|
|
311
|
+
function respondWithMock(response) {
|
|
312
|
+
// Setting response status code to 0 is a no-op.
|
|
313
|
+
// However, when responding with a "Response.error()", the produced Response
|
|
314
|
+
// instance will have status code set to 0. Since it's not possible to create
|
|
315
|
+
// a Response instance with status code 0, handle that use-case separately.
|
|
316
|
+
if (response.status === 0) {
|
|
317
|
+
return Response.error()
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const mockedResponse = new Response(response.body, response)
|
|
321
|
+
|
|
322
|
+
Reflect.defineProperty(mockedResponse, IS_MOCKED_RESPONSE, {
|
|
323
|
+
value: true,
|
|
324
|
+
enumerable: true,
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
return mockedResponse
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* @param {Request} request
|
|
332
|
+
*/
|
|
333
|
+
async function serializeRequest(request) {
|
|
334
|
+
return {
|
|
335
|
+
url: request.url,
|
|
336
|
+
mode: request.mode,
|
|
337
|
+
method: request.method,
|
|
338
|
+
headers: Object.fromEntries(request.headers.entries()),
|
|
339
|
+
cache: request.cache,
|
|
340
|
+
credentials: request.credentials,
|
|
341
|
+
destination: request.destination,
|
|
342
|
+
integrity: request.integrity,
|
|
343
|
+
redirect: request.redirect,
|
|
344
|
+
referrer: request.referrer,
|
|
345
|
+
referrerPolicy: request.referrerPolicy,
|
|
346
|
+
body: await request.arrayBuffer(),
|
|
347
|
+
keepalive: request.keepalive,
|
|
348
|
+
}
|
|
349
|
+
}
|
package/template/src/app/App.tsx
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
2
|
+
import { ToastProvider } from "@farmzone/fz-react-ui";
|
|
2
3
|
|
|
3
4
|
import Router from "@/app/router/Router";
|
|
4
5
|
|
|
@@ -18,6 +19,7 @@ const queryClient = new QueryClient({
|
|
|
18
19
|
export default function App() {
|
|
19
20
|
return (
|
|
20
21
|
<QueryClientProvider client={queryClient}>
|
|
22
|
+
<ToastProvider />
|
|
21
23
|
<Router />
|
|
22
24
|
</QueryClientProvider>
|
|
23
25
|
);
|