360-mock-server 1.2.2 β 1.2.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/README.md +5 -5
- package/bin/cli.js +4 -4
- package/lib/server.js +62 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -41,7 +41,7 @@ Thatβs it! π
|
|
|
41
41
|
The mock server will start at:
|
|
42
42
|
|
|
43
43
|
```
|
|
44
|
-
http://localhost:
|
|
44
|
+
http://localhost:5050
|
|
45
45
|
```
|
|
46
46
|
|
|
47
47
|
|
|
@@ -83,7 +83,7 @@ Once the server starts, you can interact with it directly from the terminal:
|
|
|
83
83
|
**Base URL**
|
|
84
84
|
|
|
85
85
|
```
|
|
86
|
-
http://localhost:
|
|
86
|
+
http://localhost:5050
|
|
87
87
|
```
|
|
88
88
|
|
|
89
89
|
### Example Endpoints
|
|
@@ -158,7 +158,7 @@ GET /faker/fields
|
|
|
158
158
|
<summary><strong>Fetch API</strong></summary>
|
|
159
159
|
|
|
160
160
|
```javascript
|
|
161
|
-
const API = 'http://localhost:
|
|
161
|
+
const API = 'http://localhost:5050';
|
|
162
162
|
|
|
163
163
|
// CREATE
|
|
164
164
|
await fetch(`${API}/users`, {
|
|
@@ -189,7 +189,7 @@ await fetch(`${API}/users/123`, { method: 'DELETE' });
|
|
|
189
189
|
```javascript
|
|
190
190
|
import axios from 'axios';
|
|
191
191
|
|
|
192
|
-
const API = 'http://localhost:
|
|
192
|
+
const API = 'http://localhost:5050';
|
|
193
193
|
|
|
194
194
|
// CREATE
|
|
195
195
|
await axios.post(`${API}/users`, {
|
|
@@ -216,7 +216,7 @@ await axios.delete(`${API}/users/123`);
|
|
|
216
216
|
|
|
217
217
|
| Option | Description | Default |
|
|
218
218
|
| -------- | -------------- | ---------------- |
|
|
219
|
-
| `--port` | Server port | `
|
|
219
|
+
| `--port` | Server port | `5050` |
|
|
220
220
|
| `--file` | Data file name | `mock-data.json` |
|
|
221
221
|
|
|
222
222
|
### Examples
|
package/bin/cli.js
CHANGED
|
@@ -9,7 +9,7 @@ const readline = require("readline");
|
|
|
9
9
|
const packageJson = require("../package.json");
|
|
10
10
|
|
|
11
11
|
// Helper function for making HTTP requests
|
|
12
|
-
function makeRequest(method, endpoint, data, port = "
|
|
12
|
+
function makeRequest(method, endpoint, data, port = "5050") {
|
|
13
13
|
return new Promise((resolve) => {
|
|
14
14
|
const upperMethod = method.toUpperCase();
|
|
15
15
|
const urlPath = endpoint.startsWith("/") ? endpoint : "/" + endpoint;
|
|
@@ -166,7 +166,7 @@ program
|
|
|
166
166
|
program
|
|
167
167
|
.command("run", { isDefault: true })
|
|
168
168
|
.description("Start server + interactive mode (recommended)")
|
|
169
|
-
.option("-p, --port <port>", "Port number", "
|
|
169
|
+
.option("-p, --port <port>", "Port number", "5050")
|
|
170
170
|
.option("-f, --file <filename>", "Data file name", "mock-data.json")
|
|
171
171
|
.action((options) => {
|
|
172
172
|
const dataFile = path.join(process.cwd(), options.file);
|
|
@@ -192,7 +192,7 @@ program
|
|
|
192
192
|
program
|
|
193
193
|
.command("start")
|
|
194
194
|
.description("Start the mock server only (no interactive mode)")
|
|
195
|
-
.option("-p, --port <port>", "Port number", "
|
|
195
|
+
.option("-p, --port <port>", "Port number", "5050")
|
|
196
196
|
.option("-f, --file <filename>", "Data file name", "mock-data.json")
|
|
197
197
|
.action((options) => {
|
|
198
198
|
const dataFile = path.join(process.cwd(), options.file);
|
|
@@ -266,7 +266,7 @@ program
|
|
|
266
266
|
program
|
|
267
267
|
.command("req <method> <endpoint> [data]")
|
|
268
268
|
.description("Make single API request")
|
|
269
|
-
.option("-p, --port <port>", "Server port", "
|
|
269
|
+
.option("-p, --port <port>", "Server port", "5050")
|
|
270
270
|
.action(async (method, endpoint, data, options) => {
|
|
271
271
|
await makeRequest(method, endpoint, data, options.port);
|
|
272
272
|
});
|
package/lib/server.js
CHANGED
|
@@ -5,7 +5,7 @@ const cors = require("cors");
|
|
|
5
5
|
const { faker } = require("@faker-js/faker");
|
|
6
6
|
|
|
7
7
|
const app = express();
|
|
8
|
-
const PORT = process.env.PORT ||
|
|
8
|
+
const PORT = process.env.PORT || 5050;
|
|
9
9
|
const DATA_FILE = process.env.DATA_FILE || path.join(process.cwd(), "mock-data.json");
|
|
10
10
|
|
|
11
11
|
app.use(cors());
|
|
@@ -154,7 +154,31 @@ app.get("/", (req, res) => {
|
|
|
154
154
|
"PUT /:resource/:id": "Replace item",
|
|
155
155
|
"PATCH /:resource/:id": "Update item",
|
|
156
156
|
"DELETE /:resource/:id": "Delete item",
|
|
157
|
+
"DELETE /:resource": "Delete all items in resource",
|
|
157
158
|
"GET /faker/fields": "List available faker fields"
|
|
159
|
+
},
|
|
160
|
+
pagination: {
|
|
161
|
+
description: "Use query parameters for pagination",
|
|
162
|
+
parameters: {
|
|
163
|
+
"_limit": "Number of items per page (e.g., ?_limit=10)",
|
|
164
|
+
"_page": "Page number (default: 1, e.g., ?_page=2)",
|
|
165
|
+
"_sort": "Sort by field (e.g., ?_sort=name)",
|
|
166
|
+
"_order": "Sort order: 'asc' or 'desc' (e.g., ?_order=desc)"
|
|
167
|
+
},
|
|
168
|
+
example: "GET /users?_limit=10&_page=2&_sort=name&_order=asc",
|
|
169
|
+
response: {
|
|
170
|
+
data: "Array of items",
|
|
171
|
+
pagination: {
|
|
172
|
+
page: "Current page number",
|
|
173
|
+
limit: "Items per page",
|
|
174
|
+
totalItems: "Total number of items",
|
|
175
|
+
totalPages: "Total number of pages",
|
|
176
|
+
hasNextPage: "Boolean - if next page exists",
|
|
177
|
+
hasPrevPage: "Boolean - if previous page exists",
|
|
178
|
+
nextPage: "Next page number or null",
|
|
179
|
+
prevPage: "Previous page number or null"
|
|
180
|
+
}
|
|
181
|
+
}
|
|
158
182
|
}
|
|
159
183
|
});
|
|
160
184
|
});
|
|
@@ -168,6 +192,8 @@ app.get("/:resource", (req, res) => {
|
|
|
168
192
|
}
|
|
169
193
|
let items = [...data[resource]];
|
|
170
194
|
const queryParams = req.query;
|
|
195
|
+
|
|
196
|
+
// Apply filters (exclude pagination and sorting params)
|
|
171
197
|
if (Object.keys(queryParams).length > 0) {
|
|
172
198
|
items = items.filter(item => {
|
|
173
199
|
return Object.entries(queryParams).every(([key, value]) => {
|
|
@@ -178,6 +204,11 @@ app.get("/:resource", (req, res) => {
|
|
|
178
204
|
});
|
|
179
205
|
});
|
|
180
206
|
}
|
|
207
|
+
|
|
208
|
+
// Store total count after filtering but before pagination
|
|
209
|
+
const totalItems = items.length;
|
|
210
|
+
|
|
211
|
+
// Apply sorting
|
|
181
212
|
if (queryParams._sort) {
|
|
182
213
|
const sortField = queryParams._sort;
|
|
183
214
|
const order = queryParams._order === "desc" ? -1 : 1;
|
|
@@ -187,12 +218,36 @@ app.get("/:resource", (req, res) => {
|
|
|
187
218
|
return 0;
|
|
188
219
|
});
|
|
189
220
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
221
|
+
|
|
222
|
+
// Apply pagination
|
|
223
|
+
const limit = parseInt(queryParams._limit) || null;
|
|
224
|
+
const page = parseInt(queryParams._page) || 1;
|
|
225
|
+
|
|
226
|
+
if (limit) {
|
|
193
227
|
const start = (page - 1) * limit;
|
|
194
|
-
|
|
228
|
+
const end = start + limit;
|
|
229
|
+
const paginatedItems = items.slice(start, end);
|
|
230
|
+
const totalPages = Math.ceil(totalItems / limit);
|
|
231
|
+
|
|
232
|
+
console.log(` β
Returning page ${page}/${totalPages} (${paginatedItems.length} items)`);
|
|
233
|
+
|
|
234
|
+
// Return paginated response with metadata
|
|
235
|
+
return res.json({
|
|
236
|
+
data: paginatedItems,
|
|
237
|
+
pagination: {
|
|
238
|
+
page: page,
|
|
239
|
+
limit: limit,
|
|
240
|
+
totalItems: totalItems,
|
|
241
|
+
totalPages: totalPages,
|
|
242
|
+
hasNextPage: page < totalPages,
|
|
243
|
+
hasPrevPage: page > 1,
|
|
244
|
+
nextPage: page < totalPages ? page + 1 : null,
|
|
245
|
+
prevPage: page > 1 ? page - 1 : null
|
|
246
|
+
}
|
|
247
|
+
});
|
|
195
248
|
}
|
|
249
|
+
|
|
250
|
+
// No pagination - return all items
|
|
196
251
|
console.log(` β
Returning ${items.length} ${resource}`);
|
|
197
252
|
res.json(items);
|
|
198
253
|
});
|
|
@@ -378,8 +433,8 @@ app.use((err, req, res, next) => {
|
|
|
378
433
|
res.status(500).json({ error: "Internal server error" });
|
|
379
434
|
});
|
|
380
435
|
|
|
381
|
-
app.listen(PORT, () => {
|
|
382
|
-
console.log(`\nπ Mock API Server running on
|
|
436
|
+
app.listen(PORT, '0.0.0.0', () => {
|
|
437
|
+
console.log(`\nπ Mock API Server running on http://0.0.0.0:${PORT}`);
|
|
383
438
|
console.log(`π Data file: ${DATA_FILE}\n`);
|
|
384
439
|
});
|
|
385
440
|
|