bootpress 7.1.2 → 8.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 +4 -0
- package/helpers/index.d.ts +1 -1
- package/index.js +66 -47
- package/package.json +1 -1
- /package/types/{index.ts → index.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -219,6 +219,10 @@ There must be only one element in an array schema which defines ````ArrayOf<Sche
|
|
|
219
219
|
### **asStrict(any, string | object | array)**
|
|
220
220
|
Same as 'as' method but doesn't try to parse different types instead throws error.
|
|
221
221
|
|
|
222
|
+
## v8.0.0 Release Notes:
|
|
223
|
+
- Added support for async service functions. (You don't need to await if you wrapped your service with Bootpress functions)
|
|
224
|
+
- Bugfix for falsy response values
|
|
225
|
+
- Simplified implementation
|
|
222
226
|
|
|
223
227
|
## v7.1.0 Release Notes:
|
|
224
228
|
- getOrThrow: Throws specified error when value is an empty array too.
|
package/helpers/index.d.ts
CHANGED
|
@@ -32,7 +32,7 @@ type TypedSchema<T> = {
|
|
|
32
32
|
|
|
33
33
|
export function getOrThrow<T, K extends NonNullable<T>, E extends HttpError>(data: T, error: E): K;
|
|
34
34
|
export function getOrElse<T, E>(data: T, defaultValue: E): E extends NonNullable<infer T> ? E : T | E;
|
|
35
|
-
export function schema<T extends JsSchema>(schema: T): T
|
|
35
|
+
export function schema<T extends JsSchema>(schema: T): TypedSchema<T>;
|
|
36
36
|
|
|
37
37
|
type ExtendedTypeMap = {
|
|
38
38
|
"string": string,
|
package/index.js
CHANGED
|
@@ -28,48 +28,57 @@ function RestService(service) {
|
|
|
28
28
|
...Object.getOwnPropertyDescriptors(service),
|
|
29
29
|
...Object.getOwnPropertyDescriptors(service.__proto__ || {})
|
|
30
30
|
};
|
|
31
|
-
const
|
|
31
|
+
const newService = {};
|
|
32
|
+
Object.entries(descriptors).filter(keyvalue => !protectedProperties.includes(keyvalue[0])).forEach(keyvalue => {
|
|
32
33
|
const propertyName = keyvalue[0];
|
|
33
34
|
const value = keyvalue[1].value;
|
|
34
35
|
if (typeof value == "function" && !propertyName.startsWith("#")) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
]
|
|
36
|
+
newService[propertyName] = ((...args) =>
|
|
37
|
+
(req, res) => {
|
|
38
|
+
try {
|
|
39
|
+
const result = service[propertyName](...args);
|
|
40
|
+
if (result == undefined) {
|
|
41
|
+
reply(res, 204, null);
|
|
42
|
+
}
|
|
43
|
+
else if (result instanceof Promise) {
|
|
44
|
+
result.then(r => {
|
|
45
|
+
reply(res, r.status ?? 200, r.data ?? r);
|
|
46
|
+
}).catch(e => {
|
|
47
|
+
reply(res, e.status ?? 500, e.message ?? e);
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
reply(res, result.status ?? 200, result.data ?? result)
|
|
52
|
+
}
|
|
53
|
+
} catch (e) {
|
|
54
|
+
reply(res, e.status ?? 500, e.message ?? e);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
57
|
} else {
|
|
58
|
-
|
|
58
|
+
newService[propertyName] = service[propertyName];
|
|
59
59
|
}
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
return service;
|
|
60
|
+
})
|
|
61
|
+
return newService;
|
|
63
62
|
}
|
|
64
63
|
|
|
65
64
|
function RestMethod(callback) {
|
|
66
65
|
return (req, res) => {
|
|
67
66
|
try {
|
|
68
67
|
const result = callback();
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
if (result == undefined) {
|
|
69
|
+
reply(res, 204, null);
|
|
70
|
+
}
|
|
71
|
+
else if (result instanceof Promise) {
|
|
72
|
+
result.then(r => {
|
|
73
|
+
reply(res, r.status ?? 200, r.data ?? r);
|
|
74
|
+
}).catch(e => {
|
|
75
|
+
reply(res, e.status ?? 500, e.message ?? e);
|
|
76
|
+
})
|
|
77
|
+
} else {
|
|
78
|
+
reply(res, result.status ?? 200, result.data ?? result);
|
|
79
|
+
}
|
|
71
80
|
} catch (e) {
|
|
72
|
-
reply(res, e.status
|
|
81
|
+
reply(res, e.status ?? 500, e.message ?? e)
|
|
73
82
|
}
|
|
74
83
|
}
|
|
75
84
|
}
|
|
@@ -82,10 +91,20 @@ function Restify(target, key, desc) {
|
|
|
82
91
|
return (req, res) => {
|
|
83
92
|
try {
|
|
84
93
|
const result = oldFunc(...args);
|
|
85
|
-
|
|
86
|
-
|
|
94
|
+
if (result == undefined) {
|
|
95
|
+
reply(res, 204, null);
|
|
96
|
+
}
|
|
97
|
+
else if (result instanceof Promise) {
|
|
98
|
+
result.then(r => {
|
|
99
|
+
reply(res, r.status ?? 200, r.data ?? r);
|
|
100
|
+
}).catch(e => {
|
|
101
|
+
reply(res, e.status ?? 500, e.message ?? e);
|
|
102
|
+
})
|
|
103
|
+
} else {
|
|
104
|
+
reply(res, result.status ?? 200, result.data ?? result);
|
|
105
|
+
}
|
|
87
106
|
} catch (e) {
|
|
88
|
-
reply(res, e.status
|
|
107
|
+
reply(res, e.status ?? 500, e.message ?? e);
|
|
89
108
|
}
|
|
90
109
|
}
|
|
91
110
|
}).bind(target)
|
|
@@ -204,25 +223,25 @@ function PassBodyAs(type) {
|
|
|
204
223
|
}
|
|
205
224
|
|
|
206
225
|
function PassBody(actualHandler) {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
226
|
+
return (...args) => {
|
|
227
|
+
if (isRequstHandlerArgs(args)) {
|
|
228
|
+
const req = args.at(-3); const res = args.at(-2);
|
|
229
|
+
try {
|
|
230
|
+
return actualHandler(req.body)(req, res);
|
|
231
|
+
} catch (e) {
|
|
232
|
+
reply(res, e.status || 500, e.message || e);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
} else {
|
|
236
|
+
return (req, res) => {
|
|
210
237
|
try {
|
|
211
|
-
return actualHandler(req.body)(req, res);
|
|
238
|
+
return actualHandler(...args, req.body)(req, res);
|
|
212
239
|
} catch (e) {
|
|
213
|
-
reply(res, e.status || 500, e.message || e)
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
} else {
|
|
217
|
-
return (req, res) => {
|
|
218
|
-
try {
|
|
219
|
-
return actualHandler(...args, req.body)(req, res);
|
|
220
|
-
} catch (e) {
|
|
221
|
-
reply(res, e.status || 500, e.message || e)
|
|
222
|
-
}
|
|
240
|
+
reply(res, e.status || 500, e.message || e)
|
|
223
241
|
}
|
|
224
242
|
}
|
|
225
243
|
}
|
|
244
|
+
}
|
|
226
245
|
}
|
|
227
246
|
|
|
228
247
|
function PassRequest(actualHandler) {
|
package/package.json
CHANGED
|
File without changes
|