bootpress 10.0.0 → 10.0.2
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 +7 -0
- package/index.d.ts +1 -1
- package/index.js +113 -91
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
<img src="https://raw.githubusercontent.com/ufukbakan/bootpress/main/bootpress.svg" width=500 alt="bootpress">
|
|
3
3
|
</h1>
|
|
4
4
|
<p align=center>Express but Spring Boot like</p>
|
|
5
|
+
<center>
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/bootpress)
|
|
8
|
+

|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
</center>
|
|
5
12
|
|
|
6
13
|
## Quick Start
|
|
7
14
|
Recommended tool: **[create-bootpress-app](https://www.npmjs.com/package/create-bootpress-app)**
|
package/index.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ type TypeValueOf<S extends ExtendedTypeKeys | JsSchema | TypedSchema<JsSchema> |
|
|
|
27
27
|
: never;
|
|
28
28
|
|
|
29
29
|
declare function RestService<T extends Record<PropertyKey, any>>(service: InstanceOrClass<T>): RestedService<T>;
|
|
30
|
-
declare function RestMethod<T>(callback:
|
|
30
|
+
declare function RestMethod<T extends Function>(callback: T): (args: Parameters<T>) => RequestHandler;
|
|
31
31
|
declare function Restify(target: any, key: PropertyKey, desc: PropertyDescriptor): PropertyDescriptor;
|
|
32
32
|
|
|
33
33
|
declare function PassBody<F extends RequsetHandlerWithLastArg<any>>(serviceFunction: F): ShiftRequestHandler<F>
|
package/index.js
CHANGED
|
@@ -24,76 +24,81 @@ function RestService(service) {
|
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
const descriptors = {
|
|
27
|
-
...Object.getOwnPropertyDescriptors(service),
|
|
28
|
-
...Object.getOwnPropertyDescriptors(service
|
|
27
|
+
...Object.getOwnPropertyDescriptors(service.__proto__ || {}),
|
|
28
|
+
...Object.getOwnPropertyDescriptors(service)
|
|
29
29
|
};
|
|
30
30
|
const newService = {};
|
|
31
|
-
Object.entries(descriptors)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
31
|
+
Object.entries(descriptors)
|
|
32
|
+
.filter((keyvalue) => !protectedProperties.includes(keyvalue[0]))
|
|
33
|
+
.forEach((keyvalue) => {
|
|
34
|
+
const propertyName = keyvalue[0];
|
|
35
|
+
const value = keyvalue[1].value;
|
|
36
|
+
if (typeof value == "function" && !propertyName.startsWith("#")) {
|
|
37
|
+
newService[propertyName] = ((...args) =>
|
|
38
|
+
(req, res) => {
|
|
39
|
+
try {
|
|
40
|
+
const result = service[propertyName](...args);
|
|
41
|
+
if (result == undefined) {
|
|
42
|
+
reply(res, 204, null);
|
|
43
|
+
}
|
|
44
|
+
else if (result instanceof Promise) {
|
|
45
|
+
result.then(r => {
|
|
46
|
+
if (r == undefined) {
|
|
47
|
+
reply(res, 204, null);
|
|
48
|
+
} else {
|
|
49
|
+
reply(res, r.status ?? 200, r.data ?? r);
|
|
50
|
+
}
|
|
51
|
+
}).catch(e => {
|
|
52
|
+
const status = e.status ?? 500;
|
|
53
|
+
status === 500 && log.error(e.stack);
|
|
54
|
+
reply(res, status, e.message ?? e);
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
reply(res, result.status ?? 200, result.data ?? result)
|
|
59
|
+
}
|
|
60
|
+
} catch (e) {
|
|
61
|
+
const status = e.status ?? 500;
|
|
62
|
+
status === 500 && log.error(e.stack);
|
|
63
|
+
reply(res, status, e.message ?? e);
|
|
57
64
|
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
});
|
|
64
|
-
} else {
|
|
65
|
-
newService[propertyName] = service[propertyName];
|
|
66
|
-
}
|
|
67
|
-
})
|
|
65
|
+
});
|
|
66
|
+
} else {
|
|
67
|
+
newService[propertyName] = service[propertyName];
|
|
68
|
+
}
|
|
69
|
+
})
|
|
68
70
|
return newService;
|
|
69
71
|
}
|
|
70
72
|
|
|
71
73
|
function RestMethod(callback) {
|
|
72
|
-
|
|
74
|
+
if (callback.length < 1) {
|
|
75
|
+
return handle(callback());
|
|
76
|
+
}
|
|
77
|
+
return (...args) => handle(callback(...args));
|
|
78
|
+
function handle(result) {
|
|
73
79
|
try {
|
|
74
|
-
const result = callback();
|
|
75
80
|
if (result == undefined) {
|
|
76
81
|
reply(res, 204, null);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
result.then(r => {
|
|
82
|
+
} else if (result instanceof Promise) {
|
|
83
|
+
result.then((r) => {
|
|
80
84
|
if (r == undefined) {
|
|
81
85
|
reply(res, 204, undefined);
|
|
82
86
|
} else {
|
|
83
87
|
reply(res, r.status ?? 200, r.data ?? r);
|
|
84
88
|
}
|
|
85
|
-
}).catch(e => {
|
|
86
|
-
const status = e.status ?? 500;
|
|
87
|
-
status === 500 && log.error(e.stack);
|
|
88
|
-
reply(res, status, e.message ?? e);
|
|
89
89
|
})
|
|
90
|
+
.catch((e) => {
|
|
91
|
+
const status = e.status ?? 500;
|
|
92
|
+
status === 500 && log.error(e.stack);
|
|
93
|
+
reply(res, status, e.message ?? e);
|
|
94
|
+
});
|
|
90
95
|
} else {
|
|
91
96
|
reply(res, result.status ?? 200, result.data ?? result);
|
|
92
97
|
}
|
|
93
98
|
} catch (e) {
|
|
94
99
|
const status = e.status ?? 500;
|
|
95
100
|
status === 500 && log.error(e.stack);
|
|
96
|
-
reply(res, status, e.message ?? e)
|
|
101
|
+
reply(res, status, e.message ?? e);
|
|
97
102
|
}
|
|
98
103
|
}
|
|
99
104
|
}
|
|
@@ -151,57 +156,68 @@ function PassParam(paramName) {
|
|
|
151
156
|
return (actualHandler) => {
|
|
152
157
|
return (...args) => {
|
|
153
158
|
if (isRequstHandlerArgs(args)) {
|
|
154
|
-
const req = args.at(-3);
|
|
159
|
+
const req = args.at(-3);
|
|
160
|
+
const res = args.at(-2);
|
|
155
161
|
const paramToPass = req.params[paramName];
|
|
156
162
|
return actualHandler(paramToPass)(req, res);
|
|
157
163
|
} else {
|
|
158
|
-
return (req, res) => {
|
|
164
|
+
return (req, res) => {
|
|
165
|
+
const paramToPass = req.params[paramName];
|
|
166
|
+
actualHandler(...args, paramToPass)(req, res);
|
|
167
|
+
};
|
|
159
168
|
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
169
|
+
};
|
|
170
|
+
};
|
|
162
171
|
}
|
|
163
172
|
|
|
164
173
|
function PassQuery(searchQuery) {
|
|
165
174
|
return (actualHandler) => {
|
|
166
175
|
return (...args) => {
|
|
167
176
|
if (isRequstHandlerArgs(args)) {
|
|
168
|
-
const req = args.at(-3);
|
|
177
|
+
const req = args.at(-3);
|
|
178
|
+
const res = args.at(-2);
|
|
169
179
|
const paramToPass = req.query[searchQuery];
|
|
170
180
|
return actualHandler(paramToPass)(req, res);
|
|
171
181
|
} else {
|
|
172
|
-
return (req, res) => {
|
|
182
|
+
return (req, res) => {
|
|
183
|
+
const paramToPass = req.query[searchQuery];
|
|
184
|
+
actualHandler(...args, paramToPass)(req, res);
|
|
185
|
+
};
|
|
173
186
|
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
187
|
+
};
|
|
188
|
+
};
|
|
176
189
|
}
|
|
177
190
|
|
|
178
191
|
function PassAllParams(actualHandler) {
|
|
179
192
|
return (...args) => {
|
|
180
193
|
if (isRequstHandlerArgs(args)) {
|
|
181
|
-
const req = args.at(-3);
|
|
194
|
+
const req = args.at(-3);
|
|
195
|
+
const res = args.at(-2);
|
|
182
196
|
return actualHandler(req.params)(req, res);
|
|
183
197
|
} else {
|
|
184
198
|
return (req, res) => actualHandler(...args, req.params)(req, res);
|
|
185
199
|
}
|
|
186
|
-
}
|
|
200
|
+
};
|
|
187
201
|
}
|
|
188
202
|
|
|
189
203
|
function PassAllQueries(actualHandler) {
|
|
190
204
|
return (...args) => {
|
|
191
205
|
if (isRequstHandlerArgs(args)) {
|
|
192
|
-
const req = args.at(-3);
|
|
206
|
+
const req = args.at(-3);
|
|
207
|
+
const res = args.at(-2);
|
|
193
208
|
return actualHandler(req.query)(req, res);
|
|
194
209
|
} else {
|
|
195
210
|
return (req, res) => actualHandler(...args, req.query)(req, res);
|
|
196
211
|
}
|
|
197
|
-
}
|
|
212
|
+
};
|
|
198
213
|
}
|
|
199
214
|
|
|
200
215
|
function ParseBodyAs(type, config = { messageTemplate: "Malformed Request Body\n{0}" }) {
|
|
201
216
|
return (actualHandler) => {
|
|
202
217
|
return (...args) => {
|
|
203
218
|
if (isRequstHandlerArgs(args)) {
|
|
204
|
-
const req = args.at(-3);
|
|
219
|
+
const req = args.at(-3);
|
|
220
|
+
const res = args.at(-2);
|
|
205
221
|
try {
|
|
206
222
|
return actualHandler(as(req.body, type, config))(req, res);
|
|
207
223
|
} catch (e) {
|
|
@@ -209,7 +225,6 @@ function ParseBodyAs(type, config = { messageTemplate: "Malformed Request Body\n
|
|
|
209
225
|
status === 500 && log.error(e.stack);
|
|
210
226
|
reply(res, status, e.message || e);
|
|
211
227
|
}
|
|
212
|
-
|
|
213
228
|
} else {
|
|
214
229
|
return (req, res) => {
|
|
215
230
|
try {
|
|
@@ -217,19 +232,20 @@ function ParseBodyAs(type, config = { messageTemplate: "Malformed Request Body\n
|
|
|
217
232
|
} catch (e) {
|
|
218
233
|
const status = e.status ?? 500;
|
|
219
234
|
status === 500 && log.error(e.stack);
|
|
220
|
-
reply(res, status, e.message || e)
|
|
235
|
+
reply(res, status, e.message || e);
|
|
221
236
|
}
|
|
222
|
-
}
|
|
237
|
+
};
|
|
223
238
|
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
239
|
+
};
|
|
240
|
+
};
|
|
226
241
|
}
|
|
227
242
|
|
|
228
243
|
function PassBodyAs(type, config = { messageTemplate: "Malformed Request Body\n{0}" }) {
|
|
229
244
|
return (actualHandler) => {
|
|
230
245
|
return (...args) => {
|
|
231
246
|
if (isRequstHandlerArgs(args)) {
|
|
232
|
-
const req = args.at(-3);
|
|
247
|
+
const req = args.at(-3);
|
|
248
|
+
const res = args.at(-2);
|
|
233
249
|
try {
|
|
234
250
|
return actualHandler(asStrict(req.body, type, config))(req, res);
|
|
235
251
|
} catch (e) {
|
|
@@ -237,7 +253,6 @@ function PassBodyAs(type, config = { messageTemplate: "Malformed Request Body\n{
|
|
|
237
253
|
status === 500 && log.error(e.stack);
|
|
238
254
|
reply(res, status, e.message || e);
|
|
239
255
|
}
|
|
240
|
-
|
|
241
256
|
} else {
|
|
242
257
|
return (req, res) => {
|
|
243
258
|
try {
|
|
@@ -245,18 +260,19 @@ function PassBodyAs(type, config = { messageTemplate: "Malformed Request Body\n{
|
|
|
245
260
|
} catch (e) {
|
|
246
261
|
const status = e.status ?? 500;
|
|
247
262
|
status === 500 && log.error(e.stack);
|
|
248
|
-
reply(res, status, e.message || e)
|
|
263
|
+
reply(res, status, e.message || e);
|
|
249
264
|
}
|
|
250
|
-
}
|
|
265
|
+
};
|
|
251
266
|
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
267
|
+
};
|
|
268
|
+
};
|
|
254
269
|
}
|
|
255
270
|
|
|
256
271
|
function PassBody(actualHandler) {
|
|
257
272
|
return (...args) => {
|
|
258
273
|
if (isRequstHandlerArgs(args)) {
|
|
259
|
-
const req = args.at(-3);
|
|
274
|
+
const req = args.at(-3);
|
|
275
|
+
const res = args.at(-2);
|
|
260
276
|
try {
|
|
261
277
|
return actualHandler(req.body)(req, res);
|
|
262
278
|
} catch (e) {
|
|
@@ -264,7 +280,6 @@ function PassBody(actualHandler) {
|
|
|
264
280
|
status === 500 && log.error(e.stack);
|
|
265
281
|
reply(res, status, e.message || e);
|
|
266
282
|
}
|
|
267
|
-
|
|
268
283
|
} else {
|
|
269
284
|
return (req, res) => {
|
|
270
285
|
try {
|
|
@@ -272,58 +287,65 @@ function PassBody(actualHandler) {
|
|
|
272
287
|
} catch (e) {
|
|
273
288
|
const status = e.status ?? 500;
|
|
274
289
|
status === 500 && log.error(e.stack);
|
|
275
|
-
reply(res, status, e.message || e)
|
|
290
|
+
reply(res, status, e.message || e);
|
|
276
291
|
}
|
|
277
|
-
}
|
|
292
|
+
};
|
|
278
293
|
}
|
|
279
|
-
}
|
|
294
|
+
};
|
|
280
295
|
}
|
|
281
296
|
|
|
282
297
|
function PassRequest(actualHandler) {
|
|
283
298
|
return (...args) => {
|
|
284
299
|
if (isRequstHandlerArgs(args)) {
|
|
285
|
-
const req = args.at(-3);
|
|
300
|
+
const req = args.at(-3);
|
|
301
|
+
const res = args.at(-2);
|
|
286
302
|
return actualHandler(req)(req, res);
|
|
287
303
|
} else {
|
|
288
|
-
return (req, res) => actualHandler(...args, req)(req, res)
|
|
304
|
+
return (req, res) => actualHandler(...args, req)(req, res);
|
|
289
305
|
}
|
|
290
|
-
}
|
|
306
|
+
};
|
|
291
307
|
}
|
|
292
308
|
|
|
293
309
|
function PassResponse(actualHandler) {
|
|
294
310
|
return (...args) => {
|
|
295
311
|
if (isRequstHandlerArgs(args)) {
|
|
296
|
-
const req = args.at(-3);
|
|
312
|
+
const req = args.at(-3);
|
|
313
|
+
const res = args.at(-2);
|
|
297
314
|
return actualHandler(res)(req, res);
|
|
298
315
|
} else {
|
|
299
|
-
return (req, res) => actualHandler(...args, res)(req, res)
|
|
316
|
+
return (req, res) => actualHandler(...args, res)(req, res);
|
|
300
317
|
}
|
|
301
|
-
}
|
|
318
|
+
};
|
|
302
319
|
}
|
|
303
320
|
|
|
304
321
|
function PassAllCookies(actualHandler) {
|
|
305
322
|
return (...args) => {
|
|
306
323
|
if (isRequstHandlerArgs(args)) {
|
|
307
|
-
const req = args.at(-3);
|
|
324
|
+
const req = args.at(-3);
|
|
325
|
+
const res = args.at(-2);
|
|
308
326
|
return actualHandler(req.cookies)(req, res);
|
|
309
327
|
} else {
|
|
310
328
|
return (req, res) => actualHandler(...args, req.cookies)(req, res);
|
|
311
329
|
}
|
|
312
|
-
}
|
|
330
|
+
};
|
|
313
331
|
}
|
|
314
332
|
|
|
315
333
|
function PassCookie(cookieName) {
|
|
316
334
|
return (actualHandler) => {
|
|
317
335
|
return (...args) => {
|
|
318
336
|
if (isRequstHandlerArgs(args)) {
|
|
319
|
-
const req = args.at(-3);
|
|
337
|
+
const req = args.at(-3);
|
|
338
|
+
const res = args.at(-2);
|
|
320
339
|
const cookie = req.cookies[cookieName];
|
|
321
340
|
return actualHandler(cookie)(req, res);
|
|
322
341
|
} else {
|
|
323
|
-
return (req, res) => {
|
|
342
|
+
return (req, res) => {
|
|
343
|
+
const cookie = req.cookies[cookieName];
|
|
344
|
+
actualHandler(...args, cookie)(req, res);
|
|
345
|
+
};
|
|
324
346
|
}
|
|
325
|
-
}
|
|
326
|
-
}
|
|
347
|
+
};
|
|
348
|
+
};
|
|
327
349
|
}
|
|
328
350
|
|
|
329
351
|
module.exports = {
|
|
@@ -340,5 +362,5 @@ module.exports = {
|
|
|
340
362
|
PassBodyAs,
|
|
341
363
|
ParseBodyAs,
|
|
342
364
|
PassRequest,
|
|
343
|
-
PassResponse
|
|
344
|
-
}
|
|
365
|
+
PassResponse,
|
|
366
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bootpress",
|
|
3
|
-
"version": "10.0.
|
|
3
|
+
"version": "10.0.2",
|
|
4
4
|
"description": "REST service methods for express",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -10,6 +10,10 @@
|
|
|
10
10
|
"keywords": [
|
|
11
11
|
"bootpress",
|
|
12
12
|
"express",
|
|
13
|
+
"js",
|
|
14
|
+
"javascript",
|
|
15
|
+
"backend framework",
|
|
16
|
+
"backend",
|
|
13
17
|
"rest",
|
|
14
18
|
"service",
|
|
15
19
|
"methods",
|