bootpress 10.0.3 → 11.0.0-rc.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/index.d.ts +35 -33
- package/index.js +84 -237
- package/package.json +39 -38
package/index.d.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import { Request, Response } from "express";
|
|
2
|
-
import { ArraySchema, ErrorTemplateConfiguration,
|
|
3
|
-
|
|
4
|
-
type RequestHandler =
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
: RequestHandler
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
import { ArraySchema, ErrorTemplateConfiguration, ExtendedTypeKeys, JsSchema, TypedSchema } from "./helpers";
|
|
3
|
+
|
|
4
|
+
type RequestHandler = {
|
|
5
|
+
(req: Request, res: Response): void
|
|
6
|
+
(req: Request, res: Response, next: NextFunction): void
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
type FunctionWithArgs<T = any> = (...args: any[]) => T;
|
|
10
|
+
|
|
12
11
|
|
|
13
12
|
type RestedService<T extends Record<PropertyKey, any>> = {
|
|
14
13
|
[K in keyof T]:
|
|
@@ -19,31 +18,34 @@ type RestedService<T extends Record<PropertyKey, any>> = {
|
|
|
19
18
|
|
|
20
19
|
type InstanceOrClass<T extends Record<PropertyKey, any>> = T | (new () => T);
|
|
21
20
|
|
|
22
|
-
type TypeValueOf<S extends ExtendedTypeKeys | JsSchema | TypedSchema<JsSchema> | ArraySchema> =
|
|
23
|
-
S extends TypedSchema<JsSchema> ? S
|
|
24
|
-
: S extends ExtendedTypeKeys ? ExtValOf<S>
|
|
25
|
-
: S extends JsSchema ? TypedSchema<S>
|
|
26
|
-
: S extends ArraySchema ? TypedArraySchema<S>
|
|
27
|
-
: never;
|
|
28
|
-
|
|
29
21
|
declare function RestService<T extends Record<PropertyKey, any>>(service: InstanceOrClass<T>): RestedService<T>;
|
|
30
|
-
declare function RestMethod<T extends
|
|
22
|
+
declare function RestMethod<T extends FunctionWithArgs>(callback: T): (...args: Parameters<T>) => RequestHandler;
|
|
31
23
|
declare function Restify(target: any, key: PropertyKey, desc: PropertyDescriptor): PropertyDescriptor;
|
|
32
24
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
25
|
+
type BodyArgGetter = {
|
|
26
|
+
(type: ExtendedTypeKeys | JsSchema | TypedSchema<JsSchema> | ArraySchema): NeedsBuilder;
|
|
27
|
+
(type: ExtendedTypeKeys | JsSchema | TypedSchema<JsSchema> | ArraySchema, config: ErrorTemplateConfiguration): NeedsBuilder;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type NeedsBuilder = {
|
|
31
|
+
param: (name: string) => NeedsBuilder;
|
|
32
|
+
params: () => NeedsBuilder;
|
|
33
|
+
query: (name: string) => NeedsBuilder;
|
|
34
|
+
queries: () => NeedsBuilder;
|
|
35
|
+
body: (() => NeedsBuilder) | BodyArgGetter;
|
|
36
|
+
parseBody: BodyArgGetter;
|
|
37
|
+
cookie: (name: string) => NeedsBuilder;
|
|
38
|
+
cookies: () => NeedsBuilder;
|
|
39
|
+
header: (name: string) => NeedsBuilder;
|
|
40
|
+
headers: () => NeedsBuilder;
|
|
41
|
+
to: (handler: FunctionWithArgs) => RequestHandler;
|
|
42
|
+
}
|
|
43
|
+
declare function Needs(): NeedsBuilder;
|
|
44
44
|
|
|
45
45
|
export {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
RestMethod,
|
|
47
|
+
RestService,
|
|
48
|
+
Restify,
|
|
49
|
+
Needs
|
|
49
50
|
};
|
|
51
|
+
|
package/index.js
CHANGED
|
@@ -35,7 +35,7 @@ function RestService(service) {
|
|
|
35
35
|
const value = keyvalue[1].value;
|
|
36
36
|
if (typeof value == "function" && !propertyName.startsWith("#")) {
|
|
37
37
|
newService[propertyName] = ((...args) =>
|
|
38
|
-
(
|
|
38
|
+
(_req, res) => {
|
|
39
39
|
try {
|
|
40
40
|
const result = service[propertyName](...args);
|
|
41
41
|
if (result == undefined) {
|
|
@@ -71,34 +71,33 @@ function RestService(service) {
|
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
function RestMethod(callback) {
|
|
74
|
-
if (callback.length < 1) {
|
|
75
|
-
return handle(callback());
|
|
76
|
-
}
|
|
77
74
|
return (...args) => handle(callback(...args));
|
|
78
75
|
function handle(result) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
result
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
76
|
+
return (_req, res) => {
|
|
77
|
+
try {
|
|
78
|
+
if (result == undefined) {
|
|
79
|
+
reply(res, 204, null);
|
|
80
|
+
} else if (result instanceof Promise) {
|
|
81
|
+
result.then((r) => {
|
|
82
|
+
if (r == undefined) {
|
|
83
|
+
reply(res, 204, undefined);
|
|
84
|
+
} else {
|
|
85
|
+
reply(res, r.status ?? 200, r.data ?? r);
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
.catch((e) => {
|
|
89
|
+
const status = e.status ?? 500;
|
|
90
|
+
status === 500 && log.error(e.stack);
|
|
91
|
+
reply(res, status, e.message ?? e);
|
|
92
|
+
});
|
|
93
|
+
} else {
|
|
94
|
+
reply(res, result.status ?? 200, result.data ?? result);
|
|
95
|
+
}
|
|
96
|
+
} catch (e) {
|
|
97
|
+
const status = e.status ?? 500;
|
|
98
|
+
status === 500 && log.error(e.stack);
|
|
99
|
+
reply(res, status, e.message ?? e);
|
|
97
100
|
}
|
|
98
|
-
} catch (e) {
|
|
99
|
-
const status = e.status ?? 500;
|
|
100
|
-
status === 500 && log.error(e.stack);
|
|
101
|
-
reply(res, status, e.message ?? e);
|
|
102
101
|
}
|
|
103
102
|
}
|
|
104
103
|
}
|
|
@@ -139,228 +138,76 @@ function Restify(target, key, desc) {
|
|
|
139
138
|
}
|
|
140
139
|
}
|
|
141
140
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
const paramToPass = req.params[paramName];
|
|
166
|
-
actualHandler(...args, paramToPass)(req, res);
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function PassQuery(searchQuery) {
|
|
174
|
-
return (actualHandler) => {
|
|
175
|
-
return (...args) => {
|
|
176
|
-
if (isRequstHandlerArgs(args)) {
|
|
177
|
-
const req = args.at(-3);
|
|
178
|
-
const res = args.at(-2);
|
|
179
|
-
const paramToPass = req.query[searchQuery];
|
|
180
|
-
return actualHandler(paramToPass)(req, res);
|
|
181
|
-
} else {
|
|
182
|
-
return (req, res) => {
|
|
183
|
-
const paramToPass = req.query[searchQuery];
|
|
184
|
-
actualHandler(...args, paramToPass)(req, res);
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
function PassAllParams(actualHandler) {
|
|
192
|
-
return (...args) => {
|
|
193
|
-
if (isRequstHandlerArgs(args)) {
|
|
194
|
-
const req = args.at(-3);
|
|
195
|
-
const res = args.at(-2);
|
|
196
|
-
return actualHandler(req.params)(req, res);
|
|
197
|
-
} else {
|
|
198
|
-
return (req, res) => actualHandler(...args, req.params)(req, res);
|
|
199
|
-
}
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function PassAllQueries(actualHandler) {
|
|
204
|
-
return (...args) => {
|
|
205
|
-
if (isRequstHandlerArgs(args)) {
|
|
206
|
-
const req = args.at(-3);
|
|
207
|
-
const res = args.at(-2);
|
|
208
|
-
return actualHandler(req.query)(req, res);
|
|
209
|
-
} else {
|
|
210
|
-
return (req, res) => actualHandler(...args, req.query)(req, res);
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function ParseBodyAs(type, config = { messageTemplate: "Malformed Request Body\n{0}" }) {
|
|
216
|
-
return (actualHandler) => {
|
|
217
|
-
return (...args) => {
|
|
218
|
-
if (isRequstHandlerArgs(args)) {
|
|
219
|
-
const req = args.at(-3);
|
|
220
|
-
const res = args.at(-2);
|
|
221
|
-
try {
|
|
222
|
-
return actualHandler(as(req.body, type, config))(req, res);
|
|
223
|
-
} catch (e) {
|
|
224
|
-
const status = e.status ?? 500;
|
|
225
|
-
status === 500 && log.error(e.stack);
|
|
226
|
-
reply(res, status, e.message || e);
|
|
227
|
-
}
|
|
228
|
-
} else {
|
|
229
|
-
return (req, res) => {
|
|
230
|
-
try {
|
|
231
|
-
return actualHandler(...args, as(req.body, type, config))(req, res);
|
|
232
|
-
} catch (e) {
|
|
233
|
-
const status = e.status ?? 500;
|
|
234
|
-
status === 500 && log.error(e.stack);
|
|
235
|
-
reply(res, status, e.message || e);
|
|
236
|
-
}
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
};
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function PassBodyAs(type, config = { messageTemplate: "Malformed Request Body\n{0}" }) {
|
|
244
|
-
return (actualHandler) => {
|
|
245
|
-
return (...args) => {
|
|
246
|
-
if (isRequstHandlerArgs(args)) {
|
|
247
|
-
const req = args.at(-3);
|
|
248
|
-
const res = args.at(-2);
|
|
249
|
-
try {
|
|
250
|
-
return actualHandler(asStrict(req.body, type, config))(req, res);
|
|
251
|
-
} catch (e) {
|
|
252
|
-
const status = e.status ?? 500;
|
|
253
|
-
status === 500 && log.error(e.stack);
|
|
254
|
-
reply(res, status, e.message || e);
|
|
255
|
-
}
|
|
141
|
+
function Needs() {
|
|
142
|
+
const argGetters = [];
|
|
143
|
+
|
|
144
|
+
const needs = {
|
|
145
|
+
param(name) {
|
|
146
|
+
argGetters.push((req) => req.params[name]);
|
|
147
|
+
return needs; // Return the object itself
|
|
148
|
+
},
|
|
149
|
+
params() {
|
|
150
|
+
argGetters.push((req) => req.params);
|
|
151
|
+
return needs;
|
|
152
|
+
},
|
|
153
|
+
query(name) {
|
|
154
|
+
argGetters.push((req) => req.query[name]);
|
|
155
|
+
return needs;
|
|
156
|
+
},
|
|
157
|
+
queries() {
|
|
158
|
+
argGetters.push((req) => req.query);
|
|
159
|
+
return needs;
|
|
160
|
+
},
|
|
161
|
+
body(type, config) {
|
|
162
|
+
if (type) {
|
|
163
|
+
argGetters.push((req) => asStrict(req.body || {}, type, config || { messageTemplate: "Malformed Request Body\n{0}" }));
|
|
256
164
|
} else {
|
|
257
|
-
|
|
258
|
-
try {
|
|
259
|
-
return actualHandler(...args, asStrict(req.body, type, config))(req, res);
|
|
260
|
-
} catch (e) {
|
|
261
|
-
const status = e.status ?? 500;
|
|
262
|
-
status === 500 && log.error(e.stack);
|
|
263
|
-
reply(res, status, e.message || e);
|
|
264
|
-
}
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
};
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
function PassBody(actualHandler) {
|
|
272
|
-
return (...args) => {
|
|
273
|
-
if (isRequstHandlerArgs(args)) {
|
|
274
|
-
const req = args.at(-3);
|
|
275
|
-
const res = args.at(-2);
|
|
276
|
-
try {
|
|
277
|
-
return actualHandler(req.body)(req, res);
|
|
278
|
-
} catch (e) {
|
|
279
|
-
const status = e.status ?? 500;
|
|
280
|
-
status === 500 && log.error(e.stack);
|
|
281
|
-
reply(res, status, e.message || e);
|
|
165
|
+
argGetters.push((req) => req.body || {});
|
|
282
166
|
}
|
|
283
|
-
|
|
167
|
+
return needs;
|
|
168
|
+
},
|
|
169
|
+
parseBody(type, config) {
|
|
170
|
+
argGetters.push((req) => as(req.body || {}, type, config || { messageTemplate: "Malformed Request Body\n{0}" }));
|
|
171
|
+
return needs;
|
|
172
|
+
},
|
|
173
|
+
cookie(name) {
|
|
174
|
+
argGetters.push((req) => req.cookies[name]);
|
|
175
|
+
return needs;
|
|
176
|
+
},
|
|
177
|
+
cookies() {
|
|
178
|
+
argGetters.push((req) => req.cookies);
|
|
179
|
+
return needs;
|
|
180
|
+
},
|
|
181
|
+
header(name) {
|
|
182
|
+
argGetters.push((req) => req.headers[name]);
|
|
183
|
+
return needs;
|
|
184
|
+
},
|
|
185
|
+
headers() {
|
|
186
|
+
argGetters.push((req) => req.headers);
|
|
187
|
+
return needs;
|
|
188
|
+
},
|
|
189
|
+
request() {
|
|
190
|
+
argGetters.push((req) => req)
|
|
191
|
+
return needs;
|
|
192
|
+
},
|
|
193
|
+
response() {
|
|
194
|
+
argGetters.push((_, res) => res)
|
|
195
|
+
return needs;
|
|
196
|
+
},
|
|
197
|
+
to(callback) {
|
|
284
198
|
return (req, res) => {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
} catch (e) {
|
|
288
|
-
const status = e.status ?? 500;
|
|
289
|
-
status === 500 && log.error(e.stack);
|
|
290
|
-
reply(res, status, e.message || e);
|
|
291
|
-
}
|
|
199
|
+
const args = argGetters.map((getter) => getter(req, res));
|
|
200
|
+
return RestMethod(callback)(...args)(req, res);
|
|
292
201
|
};
|
|
293
202
|
}
|
|
294
203
|
};
|
|
295
|
-
}
|
|
296
204
|
|
|
297
|
-
|
|
298
|
-
return (...args) => {
|
|
299
|
-
if (isRequstHandlerArgs(args)) {
|
|
300
|
-
const req = args.at(-3);
|
|
301
|
-
const res = args.at(-2);
|
|
302
|
-
return actualHandler(req)(req, res);
|
|
303
|
-
} else {
|
|
304
|
-
return (req, res) => actualHandler(...args, req)(req, res);
|
|
305
|
-
}
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
function PassResponse(actualHandler) {
|
|
310
|
-
return (...args) => {
|
|
311
|
-
if (isRequstHandlerArgs(args)) {
|
|
312
|
-
const req = args.at(-3);
|
|
313
|
-
const res = args.at(-2);
|
|
314
|
-
return actualHandler(res)(req, res);
|
|
315
|
-
} else {
|
|
316
|
-
return (req, res) => actualHandler(...args, res)(req, res);
|
|
317
|
-
}
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
function PassAllCookies(actualHandler) {
|
|
322
|
-
return (...args) => {
|
|
323
|
-
if (isRequstHandlerArgs(args)) {
|
|
324
|
-
const req = args.at(-3);
|
|
325
|
-
const res = args.at(-2);
|
|
326
|
-
return actualHandler(req.cookies)(req, res);
|
|
327
|
-
} else {
|
|
328
|
-
return (req, res) => actualHandler(...args, req.cookies)(req, res);
|
|
329
|
-
}
|
|
330
|
-
};
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
function PassCookie(cookieName) {
|
|
334
|
-
return (actualHandler) => {
|
|
335
|
-
return (...args) => {
|
|
336
|
-
if (isRequstHandlerArgs(args)) {
|
|
337
|
-
const req = args.at(-3);
|
|
338
|
-
const res = args.at(-2);
|
|
339
|
-
const cookie = req.cookies[cookieName];
|
|
340
|
-
return actualHandler(cookie)(req, res);
|
|
341
|
-
} else {
|
|
342
|
-
return (req, res) => {
|
|
343
|
-
const cookie = req.cookies[cookieName];
|
|
344
|
-
actualHandler(...args, cookie)(req, res);
|
|
345
|
-
};
|
|
346
|
-
}
|
|
347
|
-
};
|
|
348
|
-
};
|
|
205
|
+
return needs;
|
|
349
206
|
}
|
|
350
207
|
|
|
351
208
|
module.exports = {
|
|
352
209
|
RestService,
|
|
353
210
|
RestMethod,
|
|
354
211
|
Restify,
|
|
355
|
-
|
|
356
|
-
PassAllParams,
|
|
357
|
-
PassQuery,
|
|
358
|
-
PassAllQueries,
|
|
359
|
-
PassAllCookies,
|
|
360
|
-
PassCookie,
|
|
361
|
-
PassBody,
|
|
362
|
-
PassBodyAs,
|
|
363
|
-
ParseBodyAs,
|
|
364
|
-
PassRequest,
|
|
365
|
-
PassResponse,
|
|
212
|
+
Needs,
|
|
366
213
|
};
|
package/package.json
CHANGED
|
@@ -1,38 +1,39 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "bootpress",
|
|
3
|
-
"version": "
|
|
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
|
-
"js",
|
|
14
|
-
"javascript",
|
|
15
|
-
"backend framework",
|
|
16
|
-
"backend",
|
|
17
|
-
"rest",
|
|
18
|
-
"service",
|
|
19
|
-
"methods",
|
|
20
|
-
"spring",
|
|
21
|
-
"boot",
|
|
22
|
-
"like"
|
|
23
|
-
],
|
|
24
|
-
"author": "Ufuk Bakan",
|
|
25
|
-
"license": "MIT",
|
|
26
|
-
"bugs": {
|
|
27
|
-
"url": "https://github.com/ufukbakan/bootpress/issues"
|
|
28
|
-
},
|
|
29
|
-
"homepage": "https://github.com/ufukbakan/bootpress#readme",
|
|
30
|
-
"dependencies": {
|
|
31
|
-
"express": "^4.18.2"
|
|
32
|
-
},
|
|
33
|
-
"devDependencies": {
|
|
34
|
-
"@types/express": "^4.17.17",
|
|
35
|
-
"eslint": "^8.55.0",
|
|
36
|
-
"typescript": "^4.9.5"
|
|
37
|
-
|
|
38
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "bootpress",
|
|
3
|
+
"version": "11.0.0-rc.1",
|
|
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
|
+
"js",
|
|
14
|
+
"javascript",
|
|
15
|
+
"backend framework",
|
|
16
|
+
"backend",
|
|
17
|
+
"rest",
|
|
18
|
+
"service",
|
|
19
|
+
"methods",
|
|
20
|
+
"spring",
|
|
21
|
+
"boot",
|
|
22
|
+
"like"
|
|
23
|
+
],
|
|
24
|
+
"author": "Ufuk Bakan",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/ufukbakan/bootpress/issues"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/ufukbakan/bootpress#readme",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"express": "^4.18.2"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/express": "^4.17.17",
|
|
35
|
+
"eslint": "^8.55.0",
|
|
36
|
+
"typescript": "^4.9.5",
|
|
37
|
+
"vitest": "^4.0.18"
|
|
38
|
+
}
|
|
39
|
+
}
|