jgloo 1.5.2 → 1.6.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/CHANGELOG.md +10 -0
- package/README.md +24 -10
- package/jgloo.js +7 -1
- package/package.json +2 -2
- package/server.js +6 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
<a name="1.6.0"></a>
|
|
2
|
+
|
|
3
|
+
## [1.6.0](https://github.com/zosma180/jgloo/compare/1.5.2...1.6.0) (2022-02-20)
|
|
4
|
+
|
|
5
|
+
### Features
|
|
6
|
+
|
|
7
|
+
- Added the optional "delay" property to the configuration
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
1
11
|
<a name="1.5.2"></a>
|
|
2
12
|
|
|
3
13
|
## [1.5.2](https://github.com/zosma180/jgloo/compare/1.5.1...1.5.2) (2021-02-21)
|
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ This project is based on the Node framework Express. The highlights are:
|
|
|
27
27
|
## Installation
|
|
28
28
|
|
|
29
29
|
```bash
|
|
30
|
-
npm i -
|
|
30
|
+
npm i -D jgloo
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
After the installation create a folder "mock" in your project root (you can use another path and folder name if you want).
|
|
@@ -45,6 +45,7 @@ Now you are ready to [create your first API](#create-a-simple-api).
|
|
|
45
45
|
- [Where data are stored](#where-data-are-stored)
|
|
46
46
|
- [Expose the static files](#expose-the-static-files)
|
|
47
47
|
- [Handle requests with file uploads](#handle-requests-with-file-uploads)
|
|
48
|
+
- [Simulate network delay](#simulate-network-delay)
|
|
48
49
|
- [Run the server](#run-the-server)
|
|
49
50
|
|
|
50
51
|
---
|
|
@@ -53,7 +54,7 @@ Now you are ready to [create your first API](#create-a-simple-api).
|
|
|
53
54
|
|
|
54
55
|
To setup your first API create a new file "hello.js" in the "api" folder. The name of the file does not matter. Then insert the following snippet:
|
|
55
56
|
|
|
56
|
-
```
|
|
57
|
+
```javascript
|
|
57
58
|
module.exports = {
|
|
58
59
|
path: '/hello',
|
|
59
60
|
method: 'get',
|
|
@@ -72,7 +73,7 @@ You are ready to [run the server](#run-the-server) now.
|
|
|
72
73
|
|
|
73
74
|
To setup a ReST API, you have to create a new file in the "api" folder with the name you prefer and the following snippet:
|
|
74
75
|
|
|
75
|
-
```
|
|
76
|
+
```javascript
|
|
76
77
|
module.exports = {
|
|
77
78
|
path: '/user',
|
|
78
79
|
method: 'resource',
|
|
@@ -90,7 +91,7 @@ With these few rows of code will be created 6 routes:
|
|
|
90
91
|
|
|
91
92
|
If you want to skip any of the previous routes, you can add the "not" property:
|
|
92
93
|
|
|
93
|
-
```
|
|
94
|
+
```javascript
|
|
94
95
|
module.exports = {
|
|
95
96
|
path: '/user',
|
|
96
97
|
method: 'resource',
|
|
@@ -106,7 +107,7 @@ The available values of the "not" property are ['LIST', 'READ', 'CREATE', 'UPDAT
|
|
|
106
107
|
If you need to control the logic of your resources, you can create a custom API that read and/or write the data in the JSON database.
|
|
107
108
|
To achieve it create a new file in the "api" folder with the name you prefer and the following snippet:
|
|
108
109
|
|
|
109
|
-
```
|
|
110
|
+
```javascript
|
|
110
111
|
const { getResource, setResource } = require('jgloo');
|
|
111
112
|
|
|
112
113
|
module.exports = {
|
|
@@ -139,7 +140,7 @@ module.exports = {
|
|
|
139
140
|
To add a middleware, you have to create a folder "middlewares" in your chosen root path (e.g. "mock/middlewares").
|
|
140
141
|
Then create a new file inside with the name you prefer and the following sample snippet:
|
|
141
142
|
|
|
142
|
-
```
|
|
143
|
+
```javascript
|
|
143
144
|
module.exports = (req, res, next) => {
|
|
144
145
|
const isAuthorized = req.get('Authorization') === 'my-token';
|
|
145
146
|
isAuthorized ? next() : res.sendStatus(401);
|
|
@@ -156,7 +157,7 @@ The resources are stored in JSON files placed in the subfolder "db" of your chos
|
|
|
156
157
|
The [default ReST API](#create-a-default-rest-api) store the JSON file with the name generated by resource path replacing the slashes with the minus sign (e.g. "/auth/user" will be stored as "auth-user.json").
|
|
157
158
|
If you want to specify the file name of the resources, you can set it as the "name" property of the API:
|
|
158
159
|
|
|
159
|
-
```
|
|
160
|
+
```javascript
|
|
160
161
|
module.exports = {
|
|
161
162
|
path: '/my/long/path',
|
|
162
163
|
method: 'resource',
|
|
@@ -183,24 +184,37 @@ It's recommended to add the `static` folder in the `.gitignore` file.
|
|
|
183
184
|
|
|
184
185
|
---
|
|
185
186
|
|
|
187
|
+
### Simulate network delay
|
|
188
|
+
|
|
189
|
+
If you want to simulate a network delay, you can add the `delay` property to your API configuration:
|
|
190
|
+
|
|
191
|
+
```javascript
|
|
192
|
+
module.exports = {
|
|
193
|
+
...
|
|
194
|
+
delay: 3 // Seconds
|
|
195
|
+
};
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
186
200
|
### Run the server
|
|
187
201
|
|
|
188
202
|
To run the server execute the following command in your project root:
|
|
189
203
|
|
|
190
204
|
```shell
|
|
191
|
-
jgloo
|
|
205
|
+
npx jgloo
|
|
192
206
|
```
|
|
193
207
|
|
|
194
208
|
The full optional parameters are:
|
|
195
209
|
|
|
196
210
|
```shell
|
|
197
|
-
jgloo -f [FOLDER] -p [PORT] -s [STATIC_URL]
|
|
211
|
+
npx jgloo -f [FOLDER] -p [PORT] -s [STATIC_URL]
|
|
198
212
|
```
|
|
199
213
|
|
|
200
214
|
For example:
|
|
201
215
|
|
|
202
216
|
```shell
|
|
203
|
-
jgloo -f 'mock' -p 3000 -s 'static'
|
|
217
|
+
npx jgloo -f 'mock' -p 3000 -s 'static'
|
|
204
218
|
```
|
|
205
219
|
|
|
206
220
|
- "**-f**" or "**--folder**": the folder where your mocks are placed. It's optional, by default it's the folder "mock".
|
package/jgloo.js
CHANGED
|
@@ -20,5 +20,11 @@ module.exports = {
|
|
|
20
20
|
if (!existsSync(db)) { mkdirSync(db); }
|
|
21
21
|
const path = `${db}/${name}.json`;
|
|
22
22
|
writeFileSync(path, JSON.stringify(value), 'utf8');
|
|
23
|
-
}
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
getDelayMiddleware: (delay) => {
|
|
26
|
+
return (_, __, next) => {
|
|
27
|
+
setTimeout(() => next(), delay * 1000);
|
|
28
|
+
};
|
|
29
|
+
},
|
|
24
30
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jgloo",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "The coldest mock server.",
|
|
5
|
-
"homepage": "https://github.com/zosma180/jgloo",
|
|
5
|
+
"homepage": "https://github.com/zosma180/jgloo#readme",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/zosma180/jgloo.git"
|
package/server.js
CHANGED
|
@@ -6,6 +6,7 @@ const bodyParser = require('body-parser');
|
|
|
6
6
|
const multer = require('multer');
|
|
7
7
|
|
|
8
8
|
const { configureResource } = require(path.join(__dirname, 'rest'));
|
|
9
|
+
const { getDelayMiddleware } = require(path.join(__dirname, 'jgloo'));
|
|
9
10
|
const app = express();
|
|
10
11
|
const params = process.argv.slice(2);
|
|
11
12
|
const root = params[0];
|
|
@@ -57,6 +58,11 @@ if (!api.length) {
|
|
|
57
58
|
api.forEach((file) => {
|
|
58
59
|
const config = require(path.join(apiPath, file));
|
|
59
60
|
|
|
61
|
+
// Add the API delay, if it is provided
|
|
62
|
+
if (config.delay) {
|
|
63
|
+
app.use(config.path, getDelayMiddleware(config.delay));
|
|
64
|
+
}
|
|
65
|
+
|
|
60
66
|
if (config.method === 'resource') {
|
|
61
67
|
// ReST resource
|
|
62
68
|
configureResource(app, config);
|