bootpress 8.0.1 → 9.0.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 +19 -10
- package/index.d.ts +19 -16
- package/index.js +15 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ If you want to explicitly specify a field named 'data' or 'status' it's recommen
|
|
|
17
17
|
#### Basic usage:
|
|
18
18
|
```ts
|
|
19
19
|
import express from "express";
|
|
20
|
-
import { HttpError,
|
|
20
|
+
import { HttpError, PassParam, RestService } from "bootpress";
|
|
21
21
|
import { as, getOrThrow } from "bootpress/helpers";
|
|
22
22
|
|
|
23
23
|
const app = express();
|
|
@@ -28,7 +28,7 @@ const UserServiceImpl = {
|
|
|
28
28
|
findAllUsers(): number[] {
|
|
29
29
|
return this.users;
|
|
30
30
|
},
|
|
31
|
-
findUserById(idInParams: string) {
|
|
31
|
+
findUserById(idInParams: string | undefined) {
|
|
32
32
|
const id = as(idInParams, "integer");
|
|
33
33
|
return getOrThrow(this.users.find(user => user === id), new HttpError(404, "Not Found"));
|
|
34
34
|
}
|
|
@@ -37,12 +37,12 @@ const UserServiceImpl = {
|
|
|
37
37
|
const UserService = RestService(UserServiceImpl);
|
|
38
38
|
|
|
39
39
|
app.get("/users", UserService.findAllUsers());
|
|
40
|
-
app.get("/users/:id",
|
|
40
|
+
app.get("/users/:id", PassParam("id")(UserService.findUserById));
|
|
41
41
|
```
|
|
42
42
|
|
|
43
43
|
#### Advanced usage:
|
|
44
44
|
```ts
|
|
45
|
-
import { HttpError, HttpResponse, PassBody,
|
|
45
|
+
import { HttpError, HttpResponse, PassBody, PassParam, PassQuery, RestService } from "bootpress";
|
|
46
46
|
import { as, asStrict, getOrThrow } from "bootpress/helpers";
|
|
47
47
|
|
|
48
48
|
class PostServiceImpl {
|
|
@@ -60,7 +60,7 @@ class PostServiceImpl {
|
|
|
60
60
|
this.posts.push(casted.id);
|
|
61
61
|
return new HttpResponse(201, casted.id);
|
|
62
62
|
}
|
|
63
|
-
delete(deleteInQuery: string, idInQuery: string) {
|
|
63
|
+
delete(deleteInQuery: string | undefined, idInQuery: string | undefined) {
|
|
64
64
|
const idx = deleteInQuery === "yes" ? this.posts.indexOf(as(idInQuery, "integer")) : -1;
|
|
65
65
|
if (idx > -1) {
|
|
66
66
|
this.#logDeleted(idInQuery);
|
|
@@ -84,8 +84,8 @@ const PostService = RestService(PostServiceImpl);
|
|
|
84
84
|
|
|
85
85
|
app.get("/posts", PostService.findAll())
|
|
86
86
|
app.post("/posts", PassBody(PostService.add));
|
|
87
|
-
app.delete("/posts",
|
|
88
|
-
app.get("/posts/:id",
|
|
87
|
+
app.delete("/posts", PassQuery("delete")(PassQuery("id")(PostService.delete)));
|
|
88
|
+
app.get("/posts/:id", PassParam("id")(PostService.findById));
|
|
89
89
|
```
|
|
90
90
|
|
|
91
91
|
### **<u>RestMethod</u>**: Converts single method to RequestHandler
|
|
@@ -149,11 +149,11 @@ app.get("/logs", LogService.findAll() as RequestHandler)
|
|
|
149
149
|
|
|
150
150
|
```PassAllCookies(serviceFunction)``` -> Passes cookies to service function as Record<string, string>
|
|
151
151
|
|
|
152
|
-
```
|
|
152
|
+
```PassParam(pathParam)(serviceFunction)``` -> Passes specified parameter as arguments to service function
|
|
153
153
|
|
|
154
|
-
```
|
|
154
|
+
```PassQuery(searchQueryName)(serviceFunction)``` -> Passes specified query as arguments to service function
|
|
155
155
|
|
|
156
|
-
```
|
|
156
|
+
```PassCookie(cookieName)(serviceFunction)``` -> Passes specified cookie as arguments to service function
|
|
157
157
|
|
|
158
158
|
```PassRequest(serviceFunction)``` -> Passes express request object to service function
|
|
159
159
|
|
|
@@ -219,6 +219,15 @@ 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
|
+
## v9.0.0 Release Notes:
|
|
223
|
+
- New Feature:
|
|
224
|
+
- Type checking for each argument while passing arguments to service methods
|
|
225
|
+
- Deprecated:
|
|
226
|
+
- PassQueries, PassCookies, PassParams
|
|
227
|
+
- Please use PassAllQueries, PassAllCookies or PassAllParams
|
|
228
|
+
- Added:
|
|
229
|
+
- PassQuery, PassCookie, PassParam
|
|
230
|
+
|
|
222
231
|
## v8.0.0 Release Notes:
|
|
223
232
|
- Added support for async service functions. (You don't need to await if you wrapped your service with Bootpress functions)
|
|
224
233
|
- Bugfix for falsy response values
|
package/index.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { Response, Request } from "express"
|
|
2
|
-
import { ArraySchema, JsSchema, ValidTypeKeys } from "./helpers"
|
|
2
|
+
import { ArraySchema, JsSchema, ValidTypeKeys, TypedSchema } from "./helpers"
|
|
3
3
|
|
|
4
4
|
type RequestHandler = (req: Request, res: Response) => void
|
|
5
|
-
type RequsetHandlerWithArgs = (...args:
|
|
5
|
+
type RequsetHandlerWithArgs = <A extends any[]>(...args: A) => RequestHandler
|
|
6
|
+
type RequsetHandlerWithFirstArg<T> = (arg1: T, ...rest: any[]) => RequestHandler
|
|
7
|
+
type NonEmptyArray = readonly [any, ...any[]];
|
|
8
|
+
type ShiftRequestHandler<F extends (arg1: any, ...rest: any[]) => RequestHandler> = F extends (arg1: infer T, ...rest: infer U) => RequestHandler ? U extends NonEmptyArray ? (...rest: U) => RequestHandler : RequestHandler : RequestHandler
|
|
6
9
|
|
|
7
10
|
type RestedService<T extends Record<PropertyKey, any>> = { [K in keyof T]:
|
|
8
11
|
T[K] extends Function
|
|
@@ -16,27 +19,27 @@ declare function RestService<T extends Record<PropertyKey, any>>(service: Instan
|
|
|
16
19
|
declare function RestMethod<T>(callback: () => T): RequestHandler;
|
|
17
20
|
declare function Restify(target: any, key: PropertyKey, desc: PropertyDescriptor): PropertyDescriptor;
|
|
18
21
|
|
|
19
|
-
declare function PassBody(serviceFunction:
|
|
20
|
-
declare function ParseBodyAs
|
|
21
|
-
declare function PassBodyAs
|
|
22
|
-
declare function PassAllParams(serviceFunction:
|
|
23
|
-
declare function PassAllQueries(serviceFunction:
|
|
24
|
-
declare function PassAllCookies(serviceFunction:
|
|
25
|
-
declare function
|
|
26
|
-
declare function
|
|
27
|
-
declare function
|
|
28
|
-
declare function PassRequest(serviceFunction:
|
|
29
|
-
declare function PassResponse(serviceFunction:
|
|
22
|
+
declare function PassBody<F extends RequsetHandlerWithFirstArg<any>>(serviceFunction: F): ShiftRequestHandler<F>
|
|
23
|
+
declare function ParseBodyAs<T extends ValidTypeKeys | JsSchema | TypedSchema<any> | ArraySchema, F extends RequsetHandlerWithFirstArg<T>>(type: T): (serviceFunction: F) => ShiftRequestHandler<F>
|
|
24
|
+
declare function PassBodyAs<T extends ValidTypeKeys | JsSchema | TypedSchema<any> | ArraySchema, F extends RequsetHandlerWithFirstArg<T>>(type: T): (serviceFunction: F) => ShiftRequestHandler<F>
|
|
25
|
+
declare function PassAllParams<F extends RequsetHandlerWithFirstArg<string[]>>(serviceFunction: F): ShiftRequestHandler<F>
|
|
26
|
+
declare function PassAllQueries<F extends RequsetHandlerWithFirstArg<string[]>>(serviceFunction: F): ShiftRequestHandler<F>
|
|
27
|
+
declare function PassAllCookies<F extends RequsetHandlerWithFirstArg<string[]>>(serviceFunction: F): ShiftRequestHandler<F>
|
|
28
|
+
declare function PassParam<F extends RequsetHandlerWithFirstArg<string | undefined>>(paramName: string): (serviceFunction: F) => ShiftRequestHandler<F>
|
|
29
|
+
declare function PassQuery<F extends RequsetHandlerWithFirstArg<string | undefined>>(queryName: string): (serviceFunction: F) => ShiftRequestHandler<F>
|
|
30
|
+
declare function PassCookie<F extends RequsetHandlerWithFirstArg<string | undefined>>(cookieName: string): (serviceFunction: F) => ShiftRequestHandler<F>
|
|
31
|
+
declare function PassRequest<F extends RequsetHandlerWithFirstArg<Request>>(serviceFunction: F): ShiftRequestHandler<F>
|
|
32
|
+
declare function PassResponse<F extends RequsetHandlerWithFirstArg<Response>>(serviceFunction: F): ShiftRequestHandler<F>
|
|
30
33
|
|
|
31
34
|
export {
|
|
32
35
|
RestService,
|
|
33
36
|
RestMethod,
|
|
34
37
|
Restify,
|
|
35
|
-
|
|
38
|
+
PassParam,
|
|
36
39
|
PassAllParams,
|
|
37
|
-
|
|
40
|
+
PassQuery,
|
|
38
41
|
PassAllQueries,
|
|
39
|
-
|
|
42
|
+
PassCookie,
|
|
40
43
|
PassAllCookies,
|
|
41
44
|
PassBody,
|
|
42
45
|
PassBodyAs,
|
package/index.js
CHANGED
|
@@ -124,29 +124,29 @@ function isRequstHandlerArgs(args) {
|
|
|
124
124
|
return isResponse(last2) && isRequest(last3);
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
-
function
|
|
127
|
+
function PassParam(paramName) {
|
|
128
128
|
return (actualHandler) => {
|
|
129
129
|
return (...args) => {
|
|
130
|
+
const paramToPass = req.params[paramName];
|
|
130
131
|
if (isRequstHandlerArgs(args)) {
|
|
131
132
|
const req = args.at(-3); const res = args.at(-2);
|
|
132
|
-
|
|
133
|
-
return actualHandler(...paramsToPass)(req, res);
|
|
133
|
+
return actualHandler(paramToPass)(req, res);
|
|
134
134
|
} else {
|
|
135
|
-
return (req, res) =>
|
|
135
|
+
return (req, res) => actualHandler(...args, paramToPass)(req, res);
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
-
function
|
|
141
|
+
function PassQuery(searchQuery) {
|
|
142
142
|
return (actualHandler) => {
|
|
143
143
|
return (...args) => {
|
|
144
|
+
const paramToPass = req.query[searchQuery];
|
|
144
145
|
if (isRequstHandlerArgs(args)) {
|
|
145
146
|
const req = args.at(-3); const res = args.at(-2);
|
|
146
|
-
|
|
147
|
-
return actualHandler(...paramsToPass)(req, res);
|
|
147
|
+
return actualHandler(paramToPass)(req, res);
|
|
148
148
|
} else {
|
|
149
|
-
return (req, res) =>
|
|
149
|
+
return (req, res) => actualHandler(...args, paramToPass)(req, res);
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
152
|
}
|
|
@@ -277,15 +277,15 @@ function PassAllCookies(actualHandler) {
|
|
|
277
277
|
}
|
|
278
278
|
}
|
|
279
279
|
|
|
280
|
-
function
|
|
280
|
+
function PassCookie(cookieName) {
|
|
281
281
|
return (actualHandler) => {
|
|
282
282
|
return (...args) => {
|
|
283
|
+
const cookie = req.cookies[cookieName];
|
|
283
284
|
if (isRequstHandlerArgs(args)) {
|
|
284
285
|
const req = args.at(-3); const res = args.at(-2);
|
|
285
|
-
|
|
286
|
-
return actualHandler(...paramsToPass)(req, res);
|
|
286
|
+
return actualHandler(cookie)(req, res);
|
|
287
287
|
} else {
|
|
288
|
-
return (req, res) =>
|
|
288
|
+
return (req, res) => actualHandler(...args, cookie)(req, res);
|
|
289
289
|
}
|
|
290
290
|
}
|
|
291
291
|
}
|
|
@@ -295,12 +295,12 @@ module.exports = {
|
|
|
295
295
|
RestService,
|
|
296
296
|
RestMethod,
|
|
297
297
|
Restify,
|
|
298
|
-
|
|
298
|
+
PassParam,
|
|
299
299
|
PassAllParams,
|
|
300
|
-
|
|
300
|
+
PassQuery,
|
|
301
301
|
PassAllQueries,
|
|
302
302
|
PassAllCookies,
|
|
303
|
-
|
|
303
|
+
PassCookie,
|
|
304
304
|
PassBody,
|
|
305
305
|
PassBodyAs,
|
|
306
306
|
ParseBodyAs,
|