fauxy 0.0.1-alpha.3 → 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 +8 -3
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
|
@@ -12,9 +12,14 @@ const colors = {
|
|
|
12
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
|
}
|