@safagayret/bemirror 1.0.0 → 2.1.0
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/README.md +17 -8
- package/package.json +2 -4
- package/server.js +15 -6
- package/endpoints.json +0 -122
package/README.md
CHANGED
|
@@ -25,11 +25,11 @@ Make sure you have Node.js installed (v18+ recommended).
|
|
|
25
25
|
Install `bemirror` as a development dependency:
|
|
26
26
|
|
|
27
27
|
```bash
|
|
28
|
-
npm install --save-dev bemirror
|
|
28
|
+
npm install --save-dev @safagayret/bemirror
|
|
29
29
|
# or
|
|
30
|
-
pnpm add -D bemirror
|
|
30
|
+
pnpm add -D @safagayret/bemirror
|
|
31
31
|
# or
|
|
32
|
-
yarn add -D bemirror
|
|
32
|
+
yarn add -D @safagayret/bemirror
|
|
33
33
|
```
|
|
34
34
|
|
|
35
35
|
### Run the server
|
|
@@ -37,7 +37,7 @@ yarn add -D bemirror
|
|
|
37
37
|
Start the mock server directly with `npx`:
|
|
38
38
|
|
|
39
39
|
```bash
|
|
40
|
-
npx bemirror
|
|
40
|
+
npx @safagayret/bemirror
|
|
41
41
|
```
|
|
42
42
|
|
|
43
43
|
Or add it to your project scripts:
|
|
@@ -45,7 +45,7 @@ Or add it to your project scripts:
|
|
|
45
45
|
```json
|
|
46
46
|
{
|
|
47
47
|
"scripts": {
|
|
48
|
-
"bemirror": "bemirror"
|
|
48
|
+
"bemirror": "@safagayret/bemirror"
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
```
|
|
@@ -53,18 +53,27 @@ Or add it to your project scripts:
|
|
|
53
53
|
Then run:
|
|
54
54
|
|
|
55
55
|
```bash
|
|
56
|
-
npm run bemirror
|
|
56
|
+
npm run @safagayret/bemirror
|
|
57
57
|
```
|
|
58
58
|
|
|
59
59
|
You can also set a custom port:
|
|
60
60
|
|
|
61
61
|
```bash
|
|
62
|
-
npx bemirror --port=9000
|
|
62
|
+
npx @safagayret/bemirror --port=9000
|
|
63
63
|
```
|
|
64
64
|
|
|
65
65
|
Once started, open `http://localhost:8000` (or your configured port) to use the UI.
|
|
66
66
|
|
|
67
|
-
##
|
|
67
|
+
## � Data Storage
|
|
68
|
+
|
|
69
|
+
Your mock data is stored in JSON files in your project root directory:
|
|
70
|
+
|
|
71
|
+
- `endpoints.json`: Contains your project, entity, and endpoint configurations
|
|
72
|
+
- `variables.json`: Contains global variables for use in endpoints
|
|
73
|
+
|
|
74
|
+
These files are automatically created when you add data through the UI. You can commit them to your repository to share mock configurations with your team.
|
|
75
|
+
|
|
76
|
+
## �🛠 Usage
|
|
68
77
|
|
|
69
78
|
- Focus on the **Sidebar**: Create a **Project** first, then an **Entity**, and add your mapped **Endpoints**.
|
|
70
79
|
- **Edit your Endpoint:** In the main view, define routing paths, params, and payload rules.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@safagayret/bemirror",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Fake mirror your backend, fast",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
@@ -28,9 +28,7 @@
|
|
|
28
28
|
"server.js",
|
|
29
29
|
"bin/",
|
|
30
30
|
"public/",
|
|
31
|
-
"mocks/"
|
|
32
|
-
"endpoints.json",
|
|
33
|
-
"variables.json"
|
|
31
|
+
"mocks/"
|
|
34
32
|
],
|
|
35
33
|
"repository": {
|
|
36
34
|
"type": "git",
|
package/server.js
CHANGED
|
@@ -33,7 +33,10 @@ const loadJsonFile = (filePath, defaultValue) => {
|
|
|
33
33
|
return JSON.parse(fileContent)
|
|
34
34
|
}
|
|
35
35
|
} catch (err) {
|
|
36
|
-
console.warn(
|
|
36
|
+
console.warn(
|
|
37
|
+
`Failed to load ${path.basename(filePath)}, starting with default state:`,
|
|
38
|
+
err.message,
|
|
39
|
+
)
|
|
37
40
|
}
|
|
38
41
|
return defaultValue
|
|
39
42
|
}
|
|
@@ -47,8 +50,8 @@ const saveJsonFile = (filePath, data) => {
|
|
|
47
50
|
}
|
|
48
51
|
|
|
49
52
|
const createBemirrorApp = (options = {}) => {
|
|
50
|
-
const ENDPOINTS_FILE = options.endpointsFile || path.join(__dirname, 'endpoints.json')
|
|
51
|
-
const VARIABLES_FILE = options.variablesFile || path.join(__dirname, 'variables.json')
|
|
53
|
+
const ENDPOINTS_FILE = options.endpointsFile || path.join(__dirname, '../..', 'endpoints.json')
|
|
54
|
+
const VARIABLES_FILE = options.variablesFile || path.join(__dirname, '../..', 'variables.json')
|
|
52
55
|
const PUBLIC_DIR = options.publicDir || path.join(__dirname, 'public')
|
|
53
56
|
|
|
54
57
|
const app = express()
|
|
@@ -229,7 +232,9 @@ const createBemirrorApp = (options = {}) => {
|
|
|
229
232
|
if (incomingObj[key] === undefined) missingKeys.push(key)
|
|
230
233
|
})
|
|
231
234
|
if (missingKeys.length > 0)
|
|
232
|
-
return res
|
|
235
|
+
return res
|
|
236
|
+
.status(400)
|
|
237
|
+
.json({ error: 'Validation failed', missing_expected_keys: missingKeys })
|
|
233
238
|
} catch (e) {}
|
|
234
239
|
}
|
|
235
240
|
|
|
@@ -242,14 +247,18 @@ const createBemirrorApp = (options = {}) => {
|
|
|
242
247
|
if (incomingParams[key] === undefined) missingParams.push(key)
|
|
243
248
|
})
|
|
244
249
|
if (missingParams.length > 0)
|
|
245
|
-
return res
|
|
250
|
+
return res
|
|
251
|
+
.status(400)
|
|
252
|
+
.json({ error: 'Validation failed', missing_query_parameters: missingParams })
|
|
246
253
|
} catch (e) {}
|
|
247
254
|
}
|
|
248
255
|
|
|
249
256
|
let responseJson = {}
|
|
250
257
|
let isJson = true
|
|
251
258
|
try {
|
|
252
|
-
responseJson = processedEndpoint.responseBody
|
|
259
|
+
responseJson = processedEndpoint.responseBody
|
|
260
|
+
? JSON.parse(processedEndpoint.responseBody)
|
|
261
|
+
: {}
|
|
253
262
|
} catch (e) {
|
|
254
263
|
responseJson = processedEndpoint.responseBody
|
|
255
264
|
isJson = false
|
package/endpoints.json
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"projects": [
|
|
3
|
-
{
|
|
4
|
-
"id": "p-product-api",
|
|
5
|
-
"name": "Product API",
|
|
6
|
-
"entities": [
|
|
7
|
-
{
|
|
8
|
-
"id": "e-products",
|
|
9
|
-
"name": "Products",
|
|
10
|
-
"endpoints": [
|
|
11
|
-
{
|
|
12
|
-
"id": "ep-get-products",
|
|
13
|
-
"method": "GET",
|
|
14
|
-
"path": "/products",
|
|
15
|
-
"statusCode": 200,
|
|
16
|
-
"delay": 200,
|
|
17
|
-
"expectedParams": "",
|
|
18
|
-
"expectedPayload": "",
|
|
19
|
-
"authorization": {
|
|
20
|
-
"type": "Basic Auth",
|
|
21
|
-
"value": "{{username}}:{{password}}"
|
|
22
|
-
},
|
|
23
|
-
"headers": [
|
|
24
|
-
{
|
|
25
|
-
"key": "Content-Type",
|
|
26
|
-
"value": "application/json"
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
"key": "X-API-Key",
|
|
30
|
-
"value": "{{apiKey}}"
|
|
31
|
-
}
|
|
32
|
-
],
|
|
33
|
-
"responseBody": "{\n \"products\": [\n {\n \"id\": 1,\n \"name\": \"Laptop\",\n \"price\": 999.99,\n \"category\": \"Electronics\"\n },\n {\n \"id\": 2,\n \"name\": \"Book\",\n \"price\": 19.99,\n \"category\": \"Education\"\n }\n ]\n}"
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
"id": "ep-post-product",
|
|
37
|
-
"method": "POST",
|
|
38
|
-
"path": "/products",
|
|
39
|
-
"statusCode": 201,
|
|
40
|
-
"delay": 300,
|
|
41
|
-
"expectedParams": "",
|
|
42
|
-
"expectedPayload": "{\n \"name\": \"string\",\n \"price\": \"number\",\n \"category\": \"string\"\n}",
|
|
43
|
-
"authorization": {
|
|
44
|
-
"type": "Bearer Token",
|
|
45
|
-
"value": "{{token}}"
|
|
46
|
-
},
|
|
47
|
-
"headers": [
|
|
48
|
-
{
|
|
49
|
-
"key": "Content-Type",
|
|
50
|
-
"value": "application/json"
|
|
51
|
-
}
|
|
52
|
-
],
|
|
53
|
-
"responseBody": "{\n \"id\": 3,\n \"name\": \"New Product\",\n \"price\": 49.99,\n \"category\": \"Test\",\n \"createdAt\": \"2024-01-01T00:00:00Z\"\n}"
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
"id": "ep-put-product",
|
|
57
|
-
"method": "PUT",
|
|
58
|
-
"path": "/products/:id",
|
|
59
|
-
"statusCode": 200,
|
|
60
|
-
"delay": 250,
|
|
61
|
-
"expectedParams": "",
|
|
62
|
-
"expectedPayload": "{\n \"name\": \"string\",\n \"price\": \"number\",\n \"category\": \"string\"\n}",
|
|
63
|
-
"authorization": {
|
|
64
|
-
"type": "Bearer Token",
|
|
65
|
-
"value": "{{token}}"
|
|
66
|
-
},
|
|
67
|
-
"headers": [
|
|
68
|
-
{
|
|
69
|
-
"key": "Content-Type",
|
|
70
|
-
"value": "application/json"
|
|
71
|
-
}
|
|
72
|
-
],
|
|
73
|
-
"responseBody": "{\n \"id\": 1,\n \"name\": \"Updated Laptop\",\n \"price\": 1099.99,\n \"category\": \"Electronics\",\n \"updatedAt\": \"2024-01-01T00:00:00Z\"\n}"
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
"id": "ep-delete-product",
|
|
77
|
-
"method": "DELETE",
|
|
78
|
-
"path": "/products/:id",
|
|
79
|
-
"statusCode": 204,
|
|
80
|
-
"delay": 150,
|
|
81
|
-
"expectedParams": "",
|
|
82
|
-
"expectedPayload": "",
|
|
83
|
-
"authorization": {
|
|
84
|
-
"type": "Bearer Token",
|
|
85
|
-
"value": "{{token}}"
|
|
86
|
-
},
|
|
87
|
-
"headers": [],
|
|
88
|
-
"responseBody": ""
|
|
89
|
-
}
|
|
90
|
-
]
|
|
91
|
-
}
|
|
92
|
-
]
|
|
93
|
-
},
|
|
94
|
-
{
|
|
95
|
-
"id": "p-jiqoz2ox2",
|
|
96
|
-
"name": "lorem",
|
|
97
|
-
"entities": [
|
|
98
|
-
{
|
|
99
|
-
"id": "4r96tesww",
|
|
100
|
-
"name": "ipsum",
|
|
101
|
-
"endpoints": [
|
|
102
|
-
{
|
|
103
|
-
"id": "j4e6t3wui",
|
|
104
|
-
"method": "GET",
|
|
105
|
-
"path": "/dolor",
|
|
106
|
-
"statusCode": 200,
|
|
107
|
-
"delay": 0,
|
|
108
|
-
"expectedParams": "",
|
|
109
|
-
"expectedPayload": "",
|
|
110
|
-
"responseBody": "{\"success\": true}",
|
|
111
|
-
"authorization": {
|
|
112
|
-
"type": "None",
|
|
113
|
-
"value": ""
|
|
114
|
-
},
|
|
115
|
-
"headers": []
|
|
116
|
-
}
|
|
117
|
-
]
|
|
118
|
-
}
|
|
119
|
-
]
|
|
120
|
-
}
|
|
121
|
-
]
|
|
122
|
-
}
|