@shadow-library/fastify 1.5.0 → 1.5.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/README.md +8 -7
- package/cjs/decorators/transform.decorator.d.ts +3 -0
- package/cjs/module/data-transformers.js +1 -0
- package/cjs/module/fastify-router.js +2 -2
- package/esm/decorators/transform.decorator.d.ts +3 -0
- package/esm/module/data-transformers.js +1 -0
- package/esm/module/fastify-router.js +2 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1036,13 +1036,14 @@ export class UserController {
|
|
|
1036
1036
|
|
|
1037
1037
|
The following transformers are available out of the box:
|
|
1038
1038
|
|
|
1039
|
-
| Transformer | Input Type | Output Type | Description
|
|
1040
|
-
| ----------------- | ---------- | ----------- |
|
|
1041
|
-
| `email:normalize` | `string` | `string` | Trims whitespace and converts to lowercase
|
|
1042
|
-
| `string:trim` | `string` | `string` | Removes leading and trailing whitespace
|
|
1043
|
-
| `int:parse` | `string` | `number` | Parses string to integer (base 10)
|
|
1044
|
-
| `float:parse` | `string` | `number` | Parses string to floating-point number
|
|
1045
|
-
| `bigint:parse` | `string` | `bigint` | Parses string to BigInt
|
|
1039
|
+
| Transformer | Input Type | Output Type | Description |
|
|
1040
|
+
| ----------------- | ---------- | ----------- | ------------------------------------------------------------------------------------ |
|
|
1041
|
+
| `email:normalize` | `string` | `string` | Trims whitespace and converts to lowercase |
|
|
1042
|
+
| `string:trim` | `string` | `string` | Removes leading and trailing whitespace |
|
|
1043
|
+
| `int:parse` | `string` | `number` | Parses string to integer (base 10) |
|
|
1044
|
+
| `float:parse` | `string` | `number` | Parses string to floating-point number |
|
|
1045
|
+
| `bigint:parse` | `string` | `bigint` | Parses string to BigInt |
|
|
1046
|
+
| `strip:null` | `any` | `any` | Returns undefined when the field is null which will remove the field from the object |
|
|
1046
1047
|
|
|
1047
1048
|
### Request Transformation Examples
|
|
1048
1049
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Importing npm packages
|
|
3
3
|
*/
|
|
4
|
+
import { TransformerContext } from '@shadow-library/class-schema';
|
|
4
5
|
/**
|
|
5
6
|
* Importing user defined packages
|
|
6
7
|
*/
|
|
@@ -19,8 +20,10 @@ export interface InbuiltTransformers {
|
|
|
19
20
|
'int:parse': (value: string) => number;
|
|
20
21
|
'float:parse': (value: string) => number;
|
|
21
22
|
'bigint:parse': (value: string) => bigint;
|
|
23
|
+
'strip:null': (value: any) => any;
|
|
22
24
|
}
|
|
23
25
|
export type TransformTypes = keyof CustomTransformers | keyof InbuiltTransformers;
|
|
26
|
+
export type TransformerFn = (value: any, ctx: TransformerContext) => any;
|
|
24
27
|
/**
|
|
25
28
|
* Declaring the constants
|
|
26
29
|
*/
|
|
@@ -107,11 +107,11 @@ let FastifyRouter = class FastifyRouter extends app_1.Router {
|
|
|
107
107
|
return '****';
|
|
108
108
|
}
|
|
109
109
|
generateDataTransformer(type) {
|
|
110
|
-
return (value, schema) => {
|
|
110
|
+
return (value, schema, ctx) => {
|
|
111
111
|
const transformType = schema['x-fastify']?.transform?.[type];
|
|
112
112
|
const transformer = this.transformers[transformType];
|
|
113
113
|
(0, node_assert_1.default)(transformer, `transformer '${transformType}' not found`);
|
|
114
|
-
return transformer(value);
|
|
114
|
+
return transformer(value, ctx);
|
|
115
115
|
};
|
|
116
116
|
}
|
|
117
117
|
getRequestLogger() {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Importing npm packages
|
|
3
3
|
*/
|
|
4
|
+
import { TransformerContext } from '@shadow-library/class-schema';
|
|
4
5
|
/**
|
|
5
6
|
* Importing user defined packages
|
|
6
7
|
*/
|
|
@@ -19,8 +20,10 @@ export interface InbuiltTransformers {
|
|
|
19
20
|
'int:parse': (value: string) => number;
|
|
20
21
|
'float:parse': (value: string) => number;
|
|
21
22
|
'bigint:parse': (value: string) => bigint;
|
|
23
|
+
'strip:null': (value: any) => any;
|
|
22
24
|
}
|
|
23
25
|
export type TransformTypes = keyof CustomTransformers | keyof InbuiltTransformers;
|
|
26
|
+
export type TransformerFn = (value: any, ctx: TransformerContext) => any;
|
|
24
27
|
/**
|
|
25
28
|
* Declaring the constants
|
|
26
29
|
*/
|
|
@@ -101,11 +101,11 @@ let FastifyRouter = class FastifyRouter extends Router {
|
|
|
101
101
|
return '****';
|
|
102
102
|
}
|
|
103
103
|
generateDataTransformer(type) {
|
|
104
|
-
return (value, schema) => {
|
|
104
|
+
return (value, schema, ctx) => {
|
|
105
105
|
const transformType = schema['x-fastify']?.transform?.[type];
|
|
106
106
|
const transformer = this.transformers[transformType];
|
|
107
107
|
assert(transformer, `transformer '${transformType}' not found`);
|
|
108
|
-
return transformer(value);
|
|
108
|
+
return transformer(value, ctx);
|
|
109
109
|
};
|
|
110
110
|
}
|
|
111
111
|
getRequestLogger() {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shadow-library/fastify",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.5.
|
|
4
|
+
"version": "1.5.1",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "A Fastify wrapper featuring decorator-based routing, middleware and error handling",
|
|
7
7
|
"repository": {
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"@fastify/view": "^10.0.0",
|
|
30
30
|
"@shadow-library/app": "^1.0.11",
|
|
31
|
-
"@shadow-library/class-schema": "^0.
|
|
31
|
+
"@shadow-library/class-schema": "^0.1.3",
|
|
32
32
|
"@shadow-library/common": "^1.0.22",
|
|
33
33
|
"reflect-metadata": "^0.2.2"
|
|
34
34
|
},
|