fauxy 0.0.1-alpha.2 → 0.0.1-alpha.4
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/Taskfile.yaml +6 -6
- package/docs/laravel-mix.md +54 -0
- package/package.json +1 -1
- package/readme.md +28 -30
- package/scripts/init.cjs +9 -4
- package/scripts/mockServiceWorker.js +349 -0
package/Taskfile.yaml
CHANGED
|
@@ -10,25 +10,25 @@ tasks:
|
|
|
10
10
|
build:
|
|
11
11
|
desc: "Build Docker image"
|
|
12
12
|
cmds:
|
|
13
|
-
- docker build --no-cache -t
|
|
13
|
+
- docker build --no-cache -t fauxy .
|
|
14
14
|
|
|
15
15
|
down:
|
|
16
16
|
desc: "Stop and remove playground container"
|
|
17
17
|
cmds:
|
|
18
|
-
- docker stop
|
|
19
|
-
- docker rm
|
|
18
|
+
- docker stop fauxy-app
|
|
19
|
+
- docker rm fauxy-app
|
|
20
20
|
|
|
21
21
|
up:
|
|
22
22
|
desc: "Start playground container"
|
|
23
23
|
cmds:
|
|
24
|
-
- docker start
|
|
24
|
+
- docker start fauxy-app || docker run -d --name fauxy-app -p 5173:5173 -v ${PWD}:/app fauxy
|
|
25
25
|
|
|
26
26
|
logs:
|
|
27
27
|
desc: "Follow logs from the playground container with tail output"
|
|
28
28
|
cmds:
|
|
29
|
-
- docker logs -f --tail 100
|
|
29
|
+
- docker logs -f --tail 100 fauxy-app
|
|
30
30
|
|
|
31
31
|
bash:
|
|
32
32
|
desc: "Open interactive shell session inside the running playground container"
|
|
33
33
|
cmds:
|
|
34
|
-
- docker exec -it
|
|
34
|
+
- docker exec -it fauxy-app sh
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Using Fauxy with Laravel Mix
|
|
2
|
+
|
|
3
|
+
This section demonstrates how to integrate Fauxy into a project using Laravel Mix, ensuring that all mock-related code
|
|
4
|
+
does not end up in production. This was particularly important for me, as supporting very old browser versions was
|
|
5
|
+
challenging.
|
|
6
|
+
|
|
7
|
+
> **Note:** This is just an example setup to demonstrate how Fauxy can be integrated with Laravel Mix.
|
|
8
|
+
> It is not meant to be a production-ready configuration, and some parts of the code are simplified for illustrative purposes.
|
|
9
|
+
> Adjust it according to your project's requirements and best practices.
|
|
10
|
+
|
|
11
|
+
First, create a file, for example, `resources/ts/mock.ts`, and initialize Fauxy there.
|
|
12
|
+
|
|
13
|
+
Next, add the following code to your `webpack.mix.js`:
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
const mockSrc = process.env.MIX_MOCK_API === 'true' ? ['resources/ts/mock.ts'] : [];
|
|
17
|
+
|
|
18
|
+
mix
|
|
19
|
+
.ts([
|
|
20
|
+
...mockSrc,
|
|
21
|
+
'resources/ts/vue.ts'
|
|
22
|
+
], 'public/js/vue.js')
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
In this example, I use Vue. Since the `mock.ts` and `vue.ts` files are added to the build independently, it’s important to
|
|
26
|
+
ensure that the code from `mock.ts` runs first. The simplest way to achieve this is:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
// mock.ts
|
|
30
|
+
import { mockApi } from 'fauxy'
|
|
31
|
+
import someHandlers from '@/mocks/handlers/some-handlers'
|
|
32
|
+
|
|
33
|
+
window.MOCK_SETUP = {
|
|
34
|
+
startMock: async () => {
|
|
35
|
+
return mockApi([...someHandlers])
|
|
36
|
+
},
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
// vue.ts
|
|
42
|
+
import Vue from 'vue'
|
|
43
|
+
|
|
44
|
+
const initApp = async () => {
|
|
45
|
+
if (window?.MOCK_SETUP?.startMock) {
|
|
46
|
+
await window.MOCK_SETUP?.startMock()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return new Vue({
|
|
50
|
+
el: '#app',
|
|
51
|
+
pinia,
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
```
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,32 +1,25 @@
|
|
|
1
1
|
# Fauxy
|
|
2
|
-
description
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
-
|
|
3
|
+
Fauxy is a lightweight wrapper around **faker.js** and **MSW** (Mock Service Worker), designed to simplify mocking data
|
|
4
|
+
and API requests for both front-end and back-end development. MSW is used as-is (imported directly from the package),
|
|
5
|
+
while Faker is wrapped with a special API that adds some convenience helpers and customizations. I created Fauxy
|
|
6
|
+
primarily for my own convenience, but I hope it can be useful to others as well.
|
|
6
7
|
|
|
7
|
-
##
|
|
8
|
+
## Features
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
#### Настройка окружения
|
|
15
|
-
1. Для добавления Mock API в Laravel Mix, необходимо добавить новый файл, например, `resources/ts/mock.ts` с содержимым:
|
|
16
|
-
```ts
|
|
17
|
-
import { mockApi } from 'fauxy'
|
|
10
|
+
- Simple API for generating mock data using a wrapped version of **faker.js** with additional helpers.
|
|
11
|
+
- Built-in **MSW** integration to mock API requests.
|
|
12
|
+
- Ready-to-use mock service worker file for front-end development.
|
|
13
|
+
- Lightweight and easy to set up.
|
|
18
14
|
|
|
19
|
-
|
|
20
|
-
```
|
|
15
|
+
## Installation
|
|
21
16
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"watch:msw": "MIX_MOCK_API=true mix watch",
|
|
26
|
-
},
|
|
17
|
+
```bash
|
|
18
|
+
npm install fauxy
|
|
19
|
+
npx fauxy init
|
|
27
20
|
```
|
|
28
21
|
|
|
29
|
-
|
|
22
|
+
To ensure MSW knows where to find the service worker, add the following section to your package.json:
|
|
30
23
|
```json
|
|
31
24
|
"msw": {
|
|
32
25
|
"workerDirectory": [
|
|
@@ -35,13 +28,18 @@ mockApi.start()
|
|
|
35
28
|
}
|
|
36
29
|
```
|
|
37
30
|
|
|
38
|
-
|
|
39
|
-
```
|
|
40
|
-
|
|
31
|
+
An example of starting the mock service worker:
|
|
32
|
+
```ts
|
|
33
|
+
import { mockApi } from 'fauxy'
|
|
34
|
+
|
|
35
|
+
mockApi.start()
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## How to use 
|
|
39
|
+
|
|
40
|
+
- [Laravel Mix](docs/laravel-mix.md)
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
], 'public/js/vue.js')
|
|
47
|
-
```
|
|
42
|
+
## Notes
|
|
43
|
+
- Fauxy uses MSW directly from its package, so you can access all MSW features as usual.
|
|
44
|
+
- Faker is only available via Fauxy’s wrapper, which adds convenience methods and some custom enhancements.
|
|
45
|
+
- For more advanced usage and configuration, check out the official MSW repository.
|
package/scripts/init.cjs
CHANGED
|
@@ -9,12 +9,17 @@ const colors = {
|
|
|
9
9
|
reset: '\x1b[0m'
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
const sourceSwPath = path.join(__dirname, '
|
|
12
|
+
const sourceSwPath = path.join(__dirname, './mockServiceWorker.js');
|
|
13
13
|
const targetSwPath = path.join(process.cwd(), 'public/mockServiceWorker.js');
|
|
14
14
|
|
|
15
|
+
console.warn(
|
|
16
|
+
'\x1b[33m⚠️ This package uses MSW (https://github.com/mswjs/msw). ' +
|
|
17
|
+
'For more details and advanced usage, please visit the official repository.\x1b[0m'
|
|
18
|
+
);
|
|
19
|
+
|
|
15
20
|
try {
|
|
16
21
|
if (!fs.existsSync(sourceSwPath)) {
|
|
17
|
-
console.error(colors.error + '❌
|
|
22
|
+
console.error(colors.error + '❌ mockServiceWorker.js not found' + colors.reset);
|
|
18
23
|
process.exit(0);
|
|
19
24
|
}
|
|
20
25
|
|
|
@@ -24,9 +29,9 @@ try {
|
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
fs.copyFileSync(sourceSwPath, targetSwPath);
|
|
27
|
-
console.log(colors.success + '✅
|
|
32
|
+
console.log(colors.success + '✅ Mock Service Worker copied to public/' + colors.reset);
|
|
28
33
|
|
|
29
34
|
} catch (error) {
|
|
30
|
-
console.error(colors.error + '❌
|
|
35
|
+
console.error(colors.error + '❌ Failed to copy Mock Service Worker:' + colors.reset);
|
|
31
36
|
console.error(colors.error + error.message + colors.reset);
|
|
32
37
|
}
|
|
@@ -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.12.0'
|
|
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
|
+
}
|