bootpress 9.0.1 → 9.1.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/README.md +22 -10
- package/helpers/index.d.ts +1 -0
- package/index.d.ts +27 -16
- package/index.js +15 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -61,13 +61,17 @@ class PostServiceImpl {
|
|
|
61
61
|
return new HttpResponse(201, casted.id);
|
|
62
62
|
}
|
|
63
63
|
delete(deleteInQuery: string | undefined, idInQuery: string | undefined) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
this
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
64
|
+
if (deleteInQuery === "yes") {
|
|
65
|
+
const id = as(idInQuery, "integer");
|
|
66
|
+
const index = this.posts.indexOf(id);
|
|
67
|
+
if (index > -1) {
|
|
68
|
+
this.#logDeleted(idInQuery!);
|
|
69
|
+
return this.posts.splice(index, 1);
|
|
70
|
+
} else {
|
|
71
|
+
throw new HttpError(404, "Post is not found")
|
|
72
|
+
}
|
|
70
73
|
}
|
|
74
|
+
throw new HttpError(400, "Bad Request");
|
|
71
75
|
}
|
|
72
76
|
// use private methods to
|
|
73
77
|
#logDeleted(id: number | string) {
|
|
@@ -219,7 +223,15 @@ There must be only one element in an array schema which defines ````ArrayOf<Sche
|
|
|
219
223
|
### **asStrict(any, string | object | array)**
|
|
220
224
|
Same as 'as' method but doesn't try to parse different types instead throws error.
|
|
221
225
|
|
|
222
|
-
|
|
226
|
+
# Release Notes
|
|
227
|
+
|
|
228
|
+
## v9.0.2:
|
|
229
|
+
- Added support for null/undefined returning async functions
|
|
230
|
+
|
|
231
|
+
## v9.0.1:
|
|
232
|
+
- Fixed errors in argument passers
|
|
233
|
+
|
|
234
|
+
## v9.0.0:
|
|
223
235
|
- New Feature:
|
|
224
236
|
- Type checking for each argument while passing arguments to service methods
|
|
225
237
|
- Deprecated:
|
|
@@ -228,14 +240,14 @@ Same as 'as' method but doesn't try to parse different types instead throws erro
|
|
|
228
240
|
- Added:
|
|
229
241
|
- PassQuery, PassCookie, PassParam
|
|
230
242
|
|
|
231
|
-
## v8.0.0
|
|
243
|
+
## v8.0.0:
|
|
232
244
|
- Added support for async service functions. (You don't need to await if you wrapped your service with Bootpress functions)
|
|
233
245
|
- Bugfix for falsy response values
|
|
234
246
|
- Simplified implementation
|
|
235
247
|
|
|
236
|
-
## v7.1.0
|
|
248
|
+
## v7.1.0:
|
|
237
249
|
- getOrThrow: Throws specified error when value is an empty array too.
|
|
238
|
-
## v7.0.0
|
|
250
|
+
## v7.0.0:
|
|
239
251
|
|
|
240
252
|
### Deprecated helper methods:
|
|
241
253
|
- asSchema
|
package/helpers/index.d.ts
CHANGED
package/index.d.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { Response, Request } from "express"
|
|
2
|
-
import { ArraySchema, JsSchema, ValidTypeKeys, TypedSchema } from "./helpers"
|
|
2
|
+
import { ArraySchema, JsSchema, ValidTypeKeys, TypedSchema, ValOf, TypedArraySchema } from "./helpers"
|
|
3
3
|
|
|
4
4
|
type RequestHandler = (req: Request, res: Response) => void
|
|
5
|
-
type
|
|
6
|
-
type
|
|
5
|
+
type ArrayWithLastElement<L> = [...rest: any[], lastArg: L];
|
|
6
|
+
type RequsetHandlerWithLastArg<T, F extends (...args: any) => RequestHandler = (...args: any[]) => RequestHandler, P extends ArrayWithLastElement<T> = Parameters<F>> = (...args: P) => RequestHandler;
|
|
7
7
|
type NonEmptyArray = readonly [any, ...any[]];
|
|
8
|
-
type ShiftRequestHandler<F extends
|
|
8
|
+
type ShiftRequestHandler<F extends RequsetHandlerWithLastArg<any> | RequestHandler> =
|
|
9
|
+
F extends (arg1: infer T, ...rest: infer U) => RequestHandler ?
|
|
10
|
+
U extends NonEmptyArray ? (...rest: U) => RequestHandler : RequestHandler
|
|
11
|
+
: RequestHandler
|
|
9
12
|
|
|
10
|
-
type RestedService<T extends Record<PropertyKey, any>> = {
|
|
13
|
+
type RestedService<T extends Record<PropertyKey, any>> = {
|
|
14
|
+
[K in keyof T]:
|
|
11
15
|
T[K] extends Function
|
|
12
16
|
? (...args: Parameters<T[K]>) => RequestHandler
|
|
13
17
|
: T[K]
|
|
@@ -15,21 +19,28 @@ type RestedService<T extends Record<PropertyKey, any>> = { [K in keyof T]:
|
|
|
15
19
|
|
|
16
20
|
type InstanceOrClass<T extends Record<PropertyKey, any>> = T | (new () => T);
|
|
17
21
|
|
|
22
|
+
type TypeValueOf<S extends ValidTypeKeys | JsSchema | TypedSchema<any> | ArraySchema> =
|
|
23
|
+
S extends ValidTypeKeys ? ValOf<S>
|
|
24
|
+
: S extends JsSchema ? TypedSchema<S>
|
|
25
|
+
: S extends TypedSchema<any> ? S
|
|
26
|
+
: S extends ArraySchema ? TypedArraySchema<S>
|
|
27
|
+
: never;
|
|
28
|
+
|
|
18
29
|
declare function RestService<T extends Record<PropertyKey, any>>(service: InstanceOrClass<T>): RestedService<T>;
|
|
19
30
|
declare function RestMethod<T>(callback: () => T): RequestHandler;
|
|
20
31
|
declare function Restify(target: any, key: PropertyKey, desc: PropertyDescriptor): PropertyDescriptor;
|
|
21
32
|
|
|
22
|
-
declare function PassBody<F extends
|
|
23
|
-
declare function ParseBodyAs<
|
|
24
|
-
declare function PassBodyAs<
|
|
25
|
-
declare function PassAllParams<F extends
|
|
26
|
-
declare function PassAllQueries<F extends
|
|
27
|
-
declare function PassAllCookies<F extends
|
|
28
|
-
declare function PassParam<F extends
|
|
29
|
-
declare function PassQuery<F extends
|
|
30
|
-
declare function PassCookie<F extends
|
|
31
|
-
declare function PassRequest<F extends
|
|
32
|
-
declare function PassResponse<F extends
|
|
33
|
+
declare function PassBody<F extends RequsetHandlerWithLastArg<any>>(serviceFunction: F): ShiftRequestHandler<F>
|
|
34
|
+
declare function ParseBodyAs<S extends ValidTypeKeys | JsSchema | TypedSchema<any> | ArraySchema, T = TypeValueOf<S>>(type: S): <F extends RequsetHandlerWithLastArg<T>>(serviceFunction: F) => ShiftRequestHandler<F>
|
|
35
|
+
declare function PassBodyAs<S extends ValidTypeKeys | JsSchema | TypedSchema<any> | ArraySchema, T = TypeValueOf<S>>(type: S): <F extends RequsetHandlerWithLastArg<T>>(serviceFunction: F) => ShiftRequestHandler<F>
|
|
36
|
+
declare function PassAllParams<F extends RequsetHandlerWithLastArg<string[]>>(serviceFunction: F): ShiftRequestHandler<F>
|
|
37
|
+
declare function PassAllQueries<F extends RequsetHandlerWithLastArg<string[]>>(serviceFunction: F): ShiftRequestHandler<F>
|
|
38
|
+
declare function PassAllCookies<F extends RequsetHandlerWithLastArg<string[]>>(serviceFunction: F): ShiftRequestHandler<F>
|
|
39
|
+
declare function PassParam(paramName: string): <F extends RequsetHandlerWithLastArg<string | undefined>>(serviceFunction: F) => ShiftRequestHandler<F>
|
|
40
|
+
declare function PassQuery(queryName: string): <F extends RequsetHandlerWithLastArg<string | undefined>>(serviceFunction: F) => ShiftRequestHandler<F>
|
|
41
|
+
declare function PassCookie(cookieName: string): <F extends RequsetHandlerWithLastArg<string | undefined>>(serviceFunction: F) => ShiftRequestHandler<F>
|
|
42
|
+
declare function PassRequest<F extends RequsetHandlerWithLastArg<Request>>(serviceFunction: F): ShiftRequestHandler<F>
|
|
43
|
+
declare function PassResponse<F extends RequsetHandlerWithLastArg<Response>>(serviceFunction: F): ShiftRequestHandler<F>
|
|
33
44
|
|
|
34
45
|
export {
|
|
35
46
|
RestService,
|
package/index.js
CHANGED
|
@@ -42,7 +42,11 @@ function RestService(service) {
|
|
|
42
42
|
}
|
|
43
43
|
else if (result instanceof Promise) {
|
|
44
44
|
result.then(r => {
|
|
45
|
-
|
|
45
|
+
if (r == undefined) {
|
|
46
|
+
reply(res, 204, null);
|
|
47
|
+
} else {
|
|
48
|
+
reply(res, r.status ?? 200, r.data ?? r);
|
|
49
|
+
}
|
|
46
50
|
}).catch(e => {
|
|
47
51
|
reply(res, e.status ?? 500, e.message ?? e);
|
|
48
52
|
})
|
|
@@ -70,7 +74,11 @@ function RestMethod(callback) {
|
|
|
70
74
|
}
|
|
71
75
|
else if (result instanceof Promise) {
|
|
72
76
|
result.then(r => {
|
|
73
|
-
|
|
77
|
+
if (r == undefined) {
|
|
78
|
+
reply(res, 204, undefined);
|
|
79
|
+
} else {
|
|
80
|
+
reply(res, r.status ?? 200, r.data ?? r);
|
|
81
|
+
}
|
|
74
82
|
}).catch(e => {
|
|
75
83
|
reply(res, e.status ?? 500, e.message ?? e);
|
|
76
84
|
})
|
|
@@ -96,7 +104,11 @@ function Restify(target, key, desc) {
|
|
|
96
104
|
}
|
|
97
105
|
else if (result instanceof Promise) {
|
|
98
106
|
result.then(r => {
|
|
99
|
-
|
|
107
|
+
if (r == undefined) {
|
|
108
|
+
reply(res, 204, null);
|
|
109
|
+
} else {
|
|
110
|
+
reply(res, r.status ?? 200, r.data ?? r);
|
|
111
|
+
}
|
|
100
112
|
}).catch(e => {
|
|
101
113
|
reply(res, e.status ?? 500, e.message ?? e);
|
|
102
114
|
})
|