bootpress 7.1.0 → 7.1.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/README.md +94 -2
- package/helpers/index.d.ts +1 -1
- package/index.d.ts +5 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,10 +4,16 @@
|
|
|
4
4
|
<p align=center>Express but Spring Boot like</p>
|
|
5
5
|
|
|
6
6
|
## Quick Start
|
|
7
|
-
Recommended tool: [create-bootpress-app](https://www.npmjs.com/package/create-bootpress-app)
|
|
7
|
+
Recommended tool: **[create-bootpress-app](https://www.npmjs.com/package/create-bootpress-app)**
|
|
8
|
+
___
|
|
8
9
|
|
|
9
10
|
## Methods
|
|
10
|
-
### **<u>RestService</u>**: Converts all methods to Express RequestHandlers
|
|
11
|
+
### **<u>RestService</u>**: Converts all methods to Express RequestHandlers.
|
|
12
|
+
Http Response Status will inherit status field of returned value by method or 200 by default.
|
|
13
|
+
|
|
14
|
+
Http Response Body will be mapped to 'data' field or value itself by default.
|
|
15
|
+
|
|
16
|
+
If you want to explicitly specify a field named 'data' or 'status' it's recommended to encapsulate your value with HttpResponse class.
|
|
11
17
|
#### Basic usage:
|
|
12
18
|
```ts
|
|
13
19
|
import express from "express";
|
|
@@ -128,6 +134,92 @@ const LogService = new LogServiceImpl();
|
|
|
128
134
|
|
|
129
135
|
app.get("/logs", LogService.findAll() as RequestHandler)
|
|
130
136
|
```
|
|
137
|
+
|
|
138
|
+
### **Argument Passers**
|
|
139
|
+
|
|
140
|
+
```PassBody(serviceFunction)``` -> Passes body to service function without any validation
|
|
141
|
+
|
|
142
|
+
```ParseBodyAs(type)(serviceFunction)``` -> Parses body to specified type then passes it to service function
|
|
143
|
+
|
|
144
|
+
```PassBodyAs(type)(serviceFunction)``` -> Validates body with provided type and passes it to service function
|
|
145
|
+
|
|
146
|
+
```PassAllParams(serviceFunction)``` -> Passes all path parameters to service function as a Record<string, string> (pure js object that contains key-value pairs)
|
|
147
|
+
|
|
148
|
+
```PassAllQueries(serviceFunction)``` -> Passes query to service function as Record<string, string>
|
|
149
|
+
|
|
150
|
+
```PassAllCookies(serviceFunction)``` -> Passes cookies to service function as Record<string, string>
|
|
151
|
+
|
|
152
|
+
```PassParams(...paramNames)(serviceFunction)``` -> Passes specified parameters as arguments to service function
|
|
153
|
+
|
|
154
|
+
```PassQueries(...queryNames)(serviceFunction)``` -> Passes specified queries as arguments to service function
|
|
155
|
+
|
|
156
|
+
```PassCookies(...cookieNames)(serviceFunction)``` -> Passes specified cookies as arguments to service function
|
|
157
|
+
|
|
158
|
+
```PassRequest(serviceFunction)``` -> Passes express request object to service function
|
|
159
|
+
|
|
160
|
+
```PassResponse(serviceFunction)``` -> Passes express response object to service function
|
|
161
|
+
|
|
162
|
+
### Chaining argument passers:
|
|
163
|
+
Argument passers can be chained by passing result of one as serviceFunction parameter to other. e.g.:
|
|
164
|
+
```ts
|
|
165
|
+
// in rest service class:
|
|
166
|
+
function serviceFunction(cookies, body){
|
|
167
|
+
...
|
|
168
|
+
}
|
|
169
|
+
// in router:
|
|
170
|
+
router.post("/", PassAllCookies( PassBodyAs(yourSchema)(restService.serviceFunction) ));
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
## Helper Methods
|
|
175
|
+
|
|
176
|
+
### **getOrThrow(value, httpError)**
|
|
177
|
+
Returns the value back if it's not null, undefined or empty array.
|
|
178
|
+
### **getOrElse(value, defaultValue)**
|
|
179
|
+
Returns the value if it's not null or undefined otherwise returns the default value.
|
|
180
|
+
### **schema(object)**
|
|
181
|
+
Helps you to define a JS Schema.
|
|
182
|
+
### **as(any, string | object | array)**
|
|
183
|
+
Tries to parse any value to provided type.
|
|
184
|
+
|
|
185
|
+
If type of provided type is string then it's a primitive key and valid values are:
|
|
186
|
+
```
|
|
187
|
+
"string"
|
|
188
|
+
"string[]"
|
|
189
|
+
"boolean"
|
|
190
|
+
"boolean[]"
|
|
191
|
+
"number"
|
|
192
|
+
"number[]"
|
|
193
|
+
"integer"
|
|
194
|
+
"integer[]"
|
|
195
|
+
"string?"
|
|
196
|
+
"string[]?"
|
|
197
|
+
"boolean?"
|
|
198
|
+
"boolean[]?"
|
|
199
|
+
"number?"
|
|
200
|
+
"number[]?"
|
|
201
|
+
"integer?"
|
|
202
|
+
"integer[]?"
|
|
203
|
+
```
|
|
204
|
+
If typeof provided type is object then it's a JS Schema and structure must follow:
|
|
205
|
+
```
|
|
206
|
+
{
|
|
207
|
+
"property": string | object | array // Nullable primitives not allowed here instead use question mark end of the property key
|
|
208
|
+
"nullableProperty?": string | object | array
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
If typeof provided type is an array the structure must follow:
|
|
213
|
+
```
|
|
214
|
+
[
|
|
215
|
+
yourJsSchemaObject
|
|
216
|
+
]
|
|
217
|
+
```
|
|
218
|
+
There must be only one element in an array schema which defines ````ArrayOf<Schema>````
|
|
219
|
+
### **asStrict(any, string | object | array)**
|
|
220
|
+
Same as 'as' method but doesn't try to parse different types instead throws error.
|
|
221
|
+
|
|
222
|
+
|
|
131
223
|
## v7.1.0 Release Notes:
|
|
132
224
|
- getOrThrow: Throws specified error when value is an empty array too.
|
|
133
225
|
## v7.0.0 Release Notes:
|
package/helpers/index.d.ts
CHANGED
|
@@ -30,7 +30,7 @@ type TypedSchema<T> = {
|
|
|
30
30
|
: (T[key] extends ValidTypeKeys ? ValOf<T[key]> : T[key] extends ArraySchema ? TypedSchema<T[key][0]>[] : TypedSchema<T[key]>)
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
export function getOrThrow<T, E extends HttpError>(data: T, error: E):
|
|
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
35
|
export function schema<T extends JsSchema>(schema: T): T;
|
|
36
36
|
|
package/index.d.ts
CHANGED
|
@@ -4,16 +4,17 @@ import { ArraySchema, JsSchema, ValidTypeKeys } from "./helpers"
|
|
|
4
4
|
type RequestHandler = (req: Request, res: Response) => void
|
|
5
5
|
type RequsetHandlerWithArgs = (...args: any[]) => RequestHandler
|
|
6
6
|
|
|
7
|
-
type RestedService<T extends Record<
|
|
7
|
+
type RestedService<T extends Record<PropertyKey, any>> = { [K in keyof T]:
|
|
8
8
|
T[K] extends Function
|
|
9
9
|
? (...args: Parameters<T[K]>) => RequestHandler
|
|
10
10
|
: T[K]
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
type InstanceOrClass<T extends Record<PropertyKey, any>> = T | (new () => T);
|
|
14
|
+
|
|
15
|
+
declare function RestService<T extends Record<PropertyKey, any>>(service: InstanceOrClass<T>): RestedService<T>;
|
|
15
16
|
declare function RestMethod<T>(callback: () => T): RequestHandler;
|
|
16
|
-
declare function Restify(target: any, key:
|
|
17
|
+
declare function Restify(target: any, key: PropertyKey, desc: PropertyDescriptor): PropertyDescriptor;
|
|
17
18
|
|
|
18
19
|
declare function PassBody(serviceFunction: RequestHandler | RequsetHandlerWithArgs): RequestHandler
|
|
19
20
|
declare function ParseBodyAs(type: ValidTypeKeys | JsSchema | ArraySchema ): (serviceFunction: RequestHandler | RequsetHandlerWithArgs) => RequestHandler
|