360-mock-server 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/README.md +29 -1
- package/bin/cli.js +65 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,6 +37,7 @@ Server runs at `http://localhost:3000`
|
|
|
37
37
|
| `360-mock init` | Create mock-data.json |
|
|
38
38
|
| `360-mock list` | Show available resources |
|
|
39
39
|
| `360-mock reset` | Clear all data |
|
|
40
|
+
| `360-mock req` | Make API requests |
|
|
40
41
|
|
|
41
42
|
### Options
|
|
42
43
|
|
|
@@ -49,7 +50,34 @@ Server runs at `http://localhost:3000`
|
|
|
49
50
|
|
|
50
51
|
---
|
|
51
52
|
|
|
52
|
-
## API
|
|
53
|
+
## Making API Requests (Easy Way!)
|
|
54
|
+
|
|
55
|
+
Use the built-in `req` command instead of curl:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
# CREATE
|
|
59
|
+
360-mock req POST /users '{"name": "Ali", "email": "ali@test.com"}'
|
|
60
|
+
360-mock req POST /products '{"name": "Laptop", "price": 999}'
|
|
61
|
+
360-mock req POST /orders '{"product": "Laptop", "qty": 2}'
|
|
62
|
+
|
|
63
|
+
# READ ALL
|
|
64
|
+
360-mock req GET /users
|
|
65
|
+
360-mock req GET /products
|
|
66
|
+
|
|
67
|
+
# READ ONE
|
|
68
|
+
360-mock req GET /users/1234567890
|
|
69
|
+
|
|
70
|
+
# UPDATE
|
|
71
|
+
360-mock req PUT /users/1234567890 '{"name": "Ali Khan"}'
|
|
72
|
+
360-mock req PATCH /users/1234567890 '{"email": "ali.khan@test.com"}'
|
|
73
|
+
|
|
74
|
+
# DELETE
|
|
75
|
+
360-mock req DELETE /users/1234567890
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## API Endpoints
|
|
53
81
|
|
|
54
82
|
### Any endpoint works automatically!
|
|
55
83
|
|
package/bin/cli.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const { program } = require("commander");
|
|
4
4
|
const path = require("path");
|
|
5
5
|
const fs = require("fs");
|
|
6
|
+
const http = require("http");
|
|
6
7
|
|
|
7
8
|
const packageJson = require("../package.json");
|
|
8
9
|
|
|
@@ -85,4 +86,68 @@ program
|
|
|
85
86
|
}
|
|
86
87
|
});
|
|
87
88
|
|
|
89
|
+
// API Request command
|
|
90
|
+
program
|
|
91
|
+
.command("req <method> <endpoint> [data]")
|
|
92
|
+
.alias("request")
|
|
93
|
+
.description("Make API request (GET, POST, PUT, PATCH, DELETE)")
|
|
94
|
+
.option("-p, --port <port>", "Server port", "3000")
|
|
95
|
+
.action((method, endpoint, data, options) => {
|
|
96
|
+
const upperMethod = method.toUpperCase();
|
|
97
|
+
const path = endpoint.startsWith("/") ? endpoint : "/" + endpoint;
|
|
98
|
+
|
|
99
|
+
let jsonData = null;
|
|
100
|
+
if (data) {
|
|
101
|
+
try {
|
|
102
|
+
jsonData = JSON.parse(data);
|
|
103
|
+
} catch (e) {
|
|
104
|
+
console.log(`ā Invalid JSON: ${data}`);
|
|
105
|
+
process.exit(1);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const reqOptions = {
|
|
110
|
+
hostname: "localhost",
|
|
111
|
+
port: options.port,
|
|
112
|
+
path: path,
|
|
113
|
+
method: upperMethod,
|
|
114
|
+
headers: {
|
|
115
|
+
"Content-Type": "application/json",
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
console.log(`\nš¤ ${upperMethod} ${path}`);
|
|
120
|
+
if (jsonData) {
|
|
121
|
+
console.log(` Body: ${JSON.stringify(jsonData, null, 2)}`);
|
|
122
|
+
}
|
|
123
|
+
console.log("");
|
|
124
|
+
|
|
125
|
+
const req = http.request(reqOptions, (res) => {
|
|
126
|
+
let body = "";
|
|
127
|
+
res.on("data", (chunk) => { body += chunk; });
|
|
128
|
+
res.on("end", () => {
|
|
129
|
+
const statusEmoji = res.statusCode >= 200 && res.statusCode < 300 ? "ā
" : "ā";
|
|
130
|
+
console.log(`${statusEmoji} Status: ${res.statusCode}`);
|
|
131
|
+
console.log(`š„ Response:`);
|
|
132
|
+
try {
|
|
133
|
+
const json = JSON.parse(body);
|
|
134
|
+
console.log(JSON.stringify(json, null, 2));
|
|
135
|
+
} catch {
|
|
136
|
+
console.log(body);
|
|
137
|
+
}
|
|
138
|
+
console.log("");
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
req.on("error", (error) => {
|
|
143
|
+
console.error(`ā Error: ${error.message}`);
|
|
144
|
+
console.log("\nš” Make sure the server is running: 360-mock start\n");
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
if (jsonData) {
|
|
148
|
+
req.write(JSON.stringify(jsonData));
|
|
149
|
+
}
|
|
150
|
+
req.end();
|
|
151
|
+
});
|
|
152
|
+
|
|
88
153
|
program.parse();
|