bootpress 6.0.0 → 6.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/helpers/index.d.ts +2 -0
- package/helpers/index.js +22 -34
- package/index.d.ts +4 -2
- package/index.js +40 -9
- package/package.json +33 -33
package/helpers/index.d.ts
CHANGED
package/helpers/index.js
CHANGED
|
@@ -34,9 +34,9 @@ function asBoolean(o, errorMessage = undefined, errorStatus = 400) {
|
|
|
34
34
|
return validBooleanStrings.get(lowercased);
|
|
35
35
|
} else if (typeof o === "boolean") {
|
|
36
36
|
return o;
|
|
37
|
-
} else if (typeof o === "number"){
|
|
38
|
-
if(o === 1) return true;
|
|
39
|
-
if(o === 0) return false;
|
|
37
|
+
} else if (typeof o === "number") {
|
|
38
|
+
if (o === 1) return true;
|
|
39
|
+
if (o === 0) return false;
|
|
40
40
|
}
|
|
41
41
|
throw new HttpError(errorStatus, errorMessage);
|
|
42
42
|
}
|
|
@@ -90,7 +90,7 @@ function asSchema(o, schema) {
|
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
const expectedType = schemaKeyValues[i][1];
|
|
93
|
-
const errorMessage = o[key] == null ? `Value of ${key} should have been a ${expectedType} but it's null` : `Value of ${key} should have been a ${expectedType} but it's a ${typeof o[key]}`;
|
|
93
|
+
// const errorMessage = o[key] == null ? `Value of ${key} should have been a ${expectedType} but it's null` : `Value of ${key} should have been a ${expectedType} but it's a ${typeof o[key]}`;
|
|
94
94
|
|
|
95
95
|
if (typeof expectedType === "object") {
|
|
96
96
|
if (Array.isArray(expectedType)) {
|
|
@@ -103,20 +103,7 @@ function asSchema(o, schema) {
|
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
105
|
else if (typeof expectedType === "string") {
|
|
106
|
-
|
|
107
|
-
const elementType = expectedType.replace("[]", "");
|
|
108
|
-
for (let j = 0; j < o[key].length; j++) {
|
|
109
|
-
if (typeof o[key][j] !== elementType) {
|
|
110
|
-
throw new HttpError(400, `Each element of ${key} should have been a ${elementType} but a ${typeof o[key][j]} is present (${o[key][j]})`);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
result[key] = o[key];
|
|
114
|
-
}
|
|
115
|
-
else if (typeof o[key] === expectedType) {
|
|
116
|
-
result[key] = o[key];
|
|
117
|
-
} else {
|
|
118
|
-
throw new HttpError(400, errorMessage);
|
|
119
|
-
}
|
|
106
|
+
result[key] = as(o[key], expectedType, key);
|
|
120
107
|
}
|
|
121
108
|
else {
|
|
122
109
|
throw new HttpError(500, `Type of a schema key should be a primitive type or another schema`);
|
|
@@ -133,9 +120,9 @@ function schema(schema) {
|
|
|
133
120
|
function asArrayOf(o, elementType) {
|
|
134
121
|
if (Array.isArray(o)) {
|
|
135
122
|
for (let i = 0; i < o.length; i++) {
|
|
136
|
-
if(elementType === "integer"){
|
|
123
|
+
if (elementType === "integer") {
|
|
137
124
|
asInteger(o[i]);
|
|
138
|
-
}else if(typeof o[i] != elementType){
|
|
125
|
+
} else if (typeof o[i] != elementType) {
|
|
139
126
|
throw new HttpError(400, `Each element in array should have been a ${elementType} but ${o[i]} is present with type ${typeof o[i]}`);
|
|
140
127
|
}
|
|
141
128
|
}
|
|
@@ -145,49 +132,50 @@ function asArrayOf(o, elementType) {
|
|
|
145
132
|
}
|
|
146
133
|
}
|
|
147
134
|
|
|
148
|
-
function as(o, type) {
|
|
135
|
+
function as(o, type, namedErrorVariable = o) {
|
|
149
136
|
if (typeof type == "string") {
|
|
150
137
|
if (type.endsWith("[]")) {
|
|
151
138
|
// array check
|
|
152
139
|
const elementType = type.replace("[]", "");
|
|
153
140
|
return asArrayOf(o, elementType);
|
|
154
|
-
} else {
|
|
155
|
-
if(type.endsWith("?") && o == null){
|
|
141
|
+
} else { // non array types:
|
|
142
|
+
if (type.endsWith("?") && o == null) {
|
|
156
143
|
return null;
|
|
157
|
-
}else if(type.endsWith("?") && o != null
|
|
144
|
+
} else if (type.endsWith("?") && o != null) {
|
|
158
145
|
const actualType = type.replace("?", "");
|
|
159
146
|
return as(o, actualType);
|
|
160
147
|
}
|
|
161
148
|
// primitive check
|
|
162
149
|
switch (type) {
|
|
163
150
|
case "string":
|
|
164
|
-
return asString(o);
|
|
151
|
+
return asString(o, `Type of ${namedErrorVariable} should have been a string but it's ${o == null ? "null" : typeof o}`);
|
|
165
152
|
case "number":
|
|
166
|
-
return asNumber(o);
|
|
153
|
+
return asNumber(o, `Type of ${namedErrorVariable} should have been a number but it's ${o == null ? "null" : typeof o}`);
|
|
167
154
|
case "boolean":
|
|
168
|
-
return asBoolean(o);
|
|
155
|
+
return asBoolean(o, `Type of ${namedErrorVariable} should have been a boolean but it's ${o == null ? "null" : typeof o}`);
|
|
169
156
|
case "integer":
|
|
170
|
-
return asInteger(o);
|
|
157
|
+
return asInteger(o, `Type of ${namedErrorVariable} should have been an integer but it's ${o == null ? "null" : typeof o}`);
|
|
171
158
|
default:
|
|
172
159
|
throw new HttpError(500, `Unsupported type ${type}`);
|
|
173
160
|
}
|
|
174
161
|
}
|
|
175
162
|
} else if (typeof type == "object" && type != null) {
|
|
176
163
|
if (Array.isArray(type)) {
|
|
177
|
-
if(type.length > 1){
|
|
164
|
+
if (type.length > 1) {
|
|
178
165
|
throw new HttpError(500, `You can define only one schema for types ArrayOf<Schema>`);
|
|
179
|
-
}else if (type.length < 1){
|
|
166
|
+
} else if (type.length < 1) {
|
|
180
167
|
throw new HttpError(500, `You must define a schema for types ArrayOf<Schema>`);
|
|
181
168
|
}
|
|
182
169
|
// array schema validation
|
|
183
|
-
if(!Array.isArray(o)){
|
|
170
|
+
if (!Array.isArray(o)) {
|
|
184
171
|
throw new HttpError(400, `Provided value should have been an array. (${JSON.stringify(o)})`)
|
|
185
172
|
}
|
|
186
173
|
const providedSchema = type[0];
|
|
187
|
-
|
|
188
|
-
|
|
174
|
+
let result = [];
|
|
175
|
+
for (let i = 0; i < o.length; i++) {
|
|
176
|
+
result.push(asSchema(o[i], providedSchema));
|
|
189
177
|
}
|
|
190
|
-
return
|
|
178
|
+
return result;
|
|
191
179
|
} else {
|
|
192
180
|
// schema validation
|
|
193
181
|
return asSchema(o, type);
|
package/index.d.ts
CHANGED
|
@@ -17,13 +17,14 @@ declare function Restify(target: any, key: string, desc: PropertyDescriptor): Pr
|
|
|
17
17
|
|
|
18
18
|
declare function PassBody(serviceFunction: RequestHandler | RequsetHandlerWithArgs): RequestHandler
|
|
19
19
|
declare function PassBodyAs(type: ValidTypeKeys | JsSchema | ArraySchema ): (serviceFunction: RequestHandler | RequsetHandlerWithArgs) => RequestHandler
|
|
20
|
-
declare function PassRequest(serviceFunction: RequestHandler | RequsetHandlerWithArgs): RequestHandler
|
|
21
20
|
declare function PassAllParams(serviceFunction: RequestHandler | RequsetHandlerWithArgs): RequestHandler
|
|
22
21
|
declare function PassAllQueries(serviceFunction: RequestHandler | RequsetHandlerWithArgs): RequestHandler
|
|
23
22
|
declare function PassAllCookies(serviceFunction: RequestHandler | RequsetHandlerWithArgs): RequestHandler
|
|
24
23
|
declare function PassParams(...paramNames: string[]): (serviceFunction: RequestHandler | RequsetHandlerWithArgs) => RequestHandler
|
|
25
24
|
declare function PassQueries(...queryNames: string[]): (serviceFunction: RequestHandler | RequsetHandlerWithArgs) => RequestHandler
|
|
26
25
|
declare function PassCookies(...cookieNames: string[]): (serviceFunction: RequestHandler | RequsetHandlerWithArgs) => RequestHandler
|
|
26
|
+
declare function PassRequest(serviceFunction: RequestHandler | RequsetHandlerWithArgs): RequestHandler
|
|
27
|
+
declare function PassResponse(serviceFunction: RequestHandler | RequsetHandlerWithArgs): RequestHandler
|
|
27
28
|
|
|
28
29
|
export {
|
|
29
30
|
RestService,
|
|
@@ -37,5 +38,6 @@ export {
|
|
|
37
38
|
PassAllCookies,
|
|
38
39
|
PassBody,
|
|
39
40
|
PassBodyAs,
|
|
40
|
-
PassRequest
|
|
41
|
+
PassRequest,
|
|
42
|
+
PassResponse
|
|
41
43
|
}
|
package/index.js
CHANGED
|
@@ -8,6 +8,14 @@ const protectedProperties = [
|
|
|
8
8
|
"toLocaleString"
|
|
9
9
|
]
|
|
10
10
|
|
|
11
|
+
function reply(res, status, data) {
|
|
12
|
+
if (typeof data == "object") {
|
|
13
|
+
res.status(status).json(data);
|
|
14
|
+
} else {
|
|
15
|
+
res.status(status).send(data);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
11
19
|
function RestService(service) {
|
|
12
20
|
if (typeof service == "function") {
|
|
13
21
|
try {
|
|
@@ -36,9 +44,9 @@ function RestService(service) {
|
|
|
36
44
|
} else if (result === null) {
|
|
37
45
|
throw new HttpError(200, "Your method is executed but it returned null. At least a value is expected to be returned.");
|
|
38
46
|
}
|
|
39
|
-
res
|
|
47
|
+
reply(res, result.status || 200, result.data || result)
|
|
40
48
|
} catch (e) {
|
|
41
|
-
res
|
|
49
|
+
reply(res, e.status || 500, e.message || e);
|
|
42
50
|
}
|
|
43
51
|
}),
|
|
44
52
|
configurable: keyvalue[1].configurable,
|
|
@@ -58,10 +66,10 @@ function RestMethod(callback) {
|
|
|
58
66
|
return (req, res) => {
|
|
59
67
|
try {
|
|
60
68
|
const result = callback();
|
|
61
|
-
res
|
|
69
|
+
reply(res, result.status || 200, result.data || result);
|
|
62
70
|
return result;
|
|
63
71
|
} catch (e) {
|
|
64
|
-
res
|
|
72
|
+
reply(res, e.status || 500, e.message || e)
|
|
65
73
|
}
|
|
66
74
|
}
|
|
67
75
|
}
|
|
@@ -74,10 +82,10 @@ function Restify(target, key, desc) {
|
|
|
74
82
|
return (req, res) => {
|
|
75
83
|
try {
|
|
76
84
|
const result = oldFunc(...args);
|
|
77
|
-
res
|
|
85
|
+
reply(res, result.status || 200, result.data || result);
|
|
78
86
|
return result;
|
|
79
87
|
} catch (e) {
|
|
80
|
-
res
|
|
88
|
+
reply(res, e.status || 500, e.message || e);
|
|
81
89
|
}
|
|
82
90
|
}
|
|
83
91
|
}).bind(target)
|
|
@@ -163,9 +171,20 @@ function PassBodyAs(type) {
|
|
|
163
171
|
return (...args) => {
|
|
164
172
|
if (isRequstHandlerArgs(args)) {
|
|
165
173
|
const req = args.at(-3); const res = args.at(-2);
|
|
166
|
-
|
|
174
|
+
try {
|
|
175
|
+
return actualHandler(as(req.body, type))(req, res);
|
|
176
|
+
} catch (e) {
|
|
177
|
+
reply(res, e.status || 500, e.message || e);
|
|
178
|
+
}
|
|
179
|
+
|
|
167
180
|
} else {
|
|
168
|
-
return (req, res) =>
|
|
181
|
+
return (req, res) => {
|
|
182
|
+
try{
|
|
183
|
+
return actualHandler(...args, as(req.body, type))(req, res);
|
|
184
|
+
}catch(e){
|
|
185
|
+
reply(res, e.status || 500, e.message || e)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
169
188
|
}
|
|
170
189
|
}
|
|
171
190
|
}
|
|
@@ -182,6 +201,17 @@ function PassRequest(actualHandler) {
|
|
|
182
201
|
}
|
|
183
202
|
}
|
|
184
203
|
|
|
204
|
+
function PassResponse(actualHandler) {
|
|
205
|
+
return (...args) => {
|
|
206
|
+
if (isRequstHandlerArgs(args)) {
|
|
207
|
+
const req = args.at(-3); const res = args.at(-2);
|
|
208
|
+
return actualHandler(res)(req, res);
|
|
209
|
+
} else {
|
|
210
|
+
return (req, res) => actualHandler(...args, res)(req, res)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
185
215
|
function PassAllCookies(actualHandler) {
|
|
186
216
|
return (...args) => {
|
|
187
217
|
if (isRequstHandlerArgs(args)) {
|
|
@@ -219,5 +249,6 @@ module.exports = {
|
|
|
219
249
|
PassCookies,
|
|
220
250
|
PassBody,
|
|
221
251
|
PassBodyAs,
|
|
222
|
-
PassRequest
|
|
252
|
+
PassRequest,
|
|
253
|
+
PassResponse
|
|
223
254
|
}
|
package/package.json
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "bootpress",
|
|
3
|
-
"version": "6.0.
|
|
4
|
-
"description": "REST service methods for express",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"repository": {
|
|
7
|
-
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/ufukbakan/bootpress.git"
|
|
9
|
-
},
|
|
10
|
-
"keywords": [
|
|
11
|
-
"bootpress",
|
|
12
|
-
"express",
|
|
13
|
-
"rest",
|
|
14
|
-
"service",
|
|
15
|
-
"methods",
|
|
16
|
-
"spring",
|
|
17
|
-
"boot",
|
|
18
|
-
"like"
|
|
19
|
-
],
|
|
20
|
-
"author": "Ufuk Bakan",
|
|
21
|
-
"license": "MIT",
|
|
22
|
-
"bugs": {
|
|
23
|
-
"url": "https://github.com/ufukbakan/bootpress/issues"
|
|
24
|
-
},
|
|
25
|
-
"homepage": "https://github.com/ufukbakan/bootpress#readme",
|
|
26
|
-
"dependencies": {
|
|
27
|
-
"express": "^4.18.2"
|
|
28
|
-
},
|
|
29
|
-
"devDependencies": {
|
|
30
|
-
"@types/express": "^4.17.17",
|
|
31
|
-
"typescript": "^4.9.5"
|
|
32
|
-
}
|
|
33
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "bootpress",
|
|
3
|
+
"version": "6.0.2",
|
|
4
|
+
"description": "REST service methods for express",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/ufukbakan/bootpress.git"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"bootpress",
|
|
12
|
+
"express",
|
|
13
|
+
"rest",
|
|
14
|
+
"service",
|
|
15
|
+
"methods",
|
|
16
|
+
"spring",
|
|
17
|
+
"boot",
|
|
18
|
+
"like"
|
|
19
|
+
],
|
|
20
|
+
"author": "Ufuk Bakan",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/ufukbakan/bootpress/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/ufukbakan/bootpress#readme",
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"express": "^4.18.2"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/express": "^4.17.17",
|
|
31
|
+
"typescript": "^4.9.5"
|
|
32
|
+
}
|
|
33
|
+
}
|