@shadow-library/fastify 1.5.0 → 1.6.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 +64 -7
- package/cjs/decorators/api-operation.decorator.d.ts +22 -0
- package/cjs/decorators/api-operation.decorator.js +13 -0
- package/cjs/decorators/index.d.ts +1 -0
- package/cjs/decorators/index.js +1 -0
- package/cjs/decorators/transform.decorator.d.ts +3 -0
- package/cjs/interfaces/server-metadata.interface.d.ts +2 -1
- package/cjs/module/data-transformers.js +1 -0
- package/cjs/module/fastify-router.js +5 -3
- package/esm/decorators/api-operation.decorator.d.ts +22 -0
- package/esm/decorators/api-operation.decorator.js +10 -0
- package/esm/decorators/index.d.ts +1 -0
- package/esm/decorators/index.js +1 -0
- package/esm/decorators/transform.decorator.d.ts +3 -0
- package/esm/interfaces/server-metadata.interface.d.ts +2 -1
- package/esm/module/data-transformers.js +1 -0
- package/esm/module/fastify-router.js +5 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -952,6 +952,62 @@ class CreateProductDto {
|
|
|
952
952
|
}
|
|
953
953
|
```
|
|
954
954
|
|
|
955
|
+
## API Documentation Metadata
|
|
956
|
+
|
|
957
|
+
The `@ApiOperation` decorator allows you to add OpenAPI/Swagger metadata to your route handlers. This metadata is integrated into the Fastify route schema and can be consumed by documentation generators like Swagger UI.
|
|
958
|
+
|
|
959
|
+
```typescript
|
|
960
|
+
@ApiOperation({
|
|
961
|
+
summary: string; // Short description of the operation
|
|
962
|
+
description?: string; // Detailed description
|
|
963
|
+
tags?: string[]; // Operation tags for grouping
|
|
964
|
+
deprecated?: boolean; // Mark if endpoint is deprecated
|
|
965
|
+
externalDocs?: { // Link to external documentation
|
|
966
|
+
url: string;
|
|
967
|
+
description?: string;
|
|
968
|
+
};
|
|
969
|
+
security?: Record<string, []>; // Security requirements
|
|
970
|
+
})
|
|
971
|
+
```
|
|
972
|
+
|
|
973
|
+
### Example Usage
|
|
974
|
+
|
|
975
|
+
```typescript
|
|
976
|
+
import { HttpController, Get, Post, Body, ApiOperation } from '@shadow-library/fastify';
|
|
977
|
+
|
|
978
|
+
@HttpController('/api/users')
|
|
979
|
+
export class UserController {
|
|
980
|
+
@Get()
|
|
981
|
+
@ApiOperation({
|
|
982
|
+
summary: 'List all users',
|
|
983
|
+
description: 'Retrieve a paginated list of all users in the system',
|
|
984
|
+
tags: ['users'],
|
|
985
|
+
})
|
|
986
|
+
async getUsers() {
|
|
987
|
+
return [];
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
@Post()
|
|
991
|
+
@ApiOperation({
|
|
992
|
+
summary: 'Create a new user',
|
|
993
|
+
tags: ['users'],
|
|
994
|
+
security: { bearerAuth: [] },
|
|
995
|
+
})
|
|
996
|
+
async createUser(@Body() userData: CreateUserDto) {
|
|
997
|
+
return { id: 1, ...userData };
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
@Get('/:id')
|
|
1001
|
+
@ApiOperation({
|
|
1002
|
+
summary: 'Get user by ID',
|
|
1003
|
+
deprecated: false,
|
|
1004
|
+
})
|
|
1005
|
+
async getUserById() {
|
|
1006
|
+
return { id: 1, name: 'John' };
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
```
|
|
1010
|
+
|
|
955
1011
|
## Data Transformation
|
|
956
1012
|
|
|
957
1013
|
The `@Transform` decorator enables automatic data transformation at two key points in the request-response lifecycle:
|
|
@@ -1036,13 +1092,14 @@ export class UserController {
|
|
|
1036
1092
|
|
|
1037
1093
|
The following transformers are available out of the box:
|
|
1038
1094
|
|
|
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
|
|
1095
|
+
| Transformer | Input Type | Output Type | Description |
|
|
1096
|
+
| ----------------- | ---------- | ----------- | ------------------------------------------------------------------------------------ |
|
|
1097
|
+
| `email:normalize` | `string` | `string` | Trims whitespace and converts to lowercase |
|
|
1098
|
+
| `string:trim` | `string` | `string` | Removes leading and trailing whitespace |
|
|
1099
|
+
| `int:parse` | `string` | `number` | Parses string to integer (base 10) |
|
|
1100
|
+
| `float:parse` | `string` | `number` | Parses string to floating-point number |
|
|
1101
|
+
| `bigint:parse` | `string` | `bigint` | Parses string to BigInt |
|
|
1102
|
+
| `strip:null` | `any` | `any` | Returns undefined when the field is null which will remove the field from the object |
|
|
1046
1103
|
|
|
1047
1104
|
### Request Transformation Examples
|
|
1048
1105
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing user defined packages
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Defining types
|
|
6
|
+
*/
|
|
7
|
+
export interface ApiOperationMetadata {
|
|
8
|
+
summary?: string;
|
|
9
|
+
description?: string;
|
|
10
|
+
tags?: string[];
|
|
11
|
+
deprecated?: boolean;
|
|
12
|
+
externalDocs?: {
|
|
13
|
+
url: string;
|
|
14
|
+
description?: string;
|
|
15
|
+
};
|
|
16
|
+
security?: Record<string, string[]>;
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Declaring the constants
|
|
21
|
+
*/
|
|
22
|
+
export declare function ApiOperation(options: ApiOperationMetadata): ClassDecorator & MethodDecorator;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApiOperation = ApiOperation;
|
|
4
|
+
/**
|
|
5
|
+
* Importing npm packages
|
|
6
|
+
*/
|
|
7
|
+
const app_1 = require("@shadow-library/app");
|
|
8
|
+
/**
|
|
9
|
+
* Declaring the constants
|
|
10
|
+
*/
|
|
11
|
+
function ApiOperation(options) {
|
|
12
|
+
return (0, app_1.Route)({ operation: options });
|
|
13
|
+
}
|
package/cjs/decorators/index.js
CHANGED
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./api-operation.decorator.js"), exports);
|
|
17
18
|
__exportStar(require("./http-controller.decorator.js"), exports);
|
|
18
19
|
__exportStar(require("./http-input.decorator.js"), exports);
|
|
19
20
|
__exportStar(require("./http-output.decorator.js"), exports);
|
|
@@ -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
|
*/
|
|
@@ -8,7 +8,7 @@ import { FastifyInstance, RouteShorthandOptions } from 'fastify';
|
|
|
8
8
|
* Importing user defined packages
|
|
9
9
|
*/
|
|
10
10
|
import { HTTP_CONTROLLER_TYPE } from '../constants.js';
|
|
11
|
-
import { HttpMethod, RouteInputSchemas } from '../decorators/index.js';
|
|
11
|
+
import { ApiOperationMetadata, HttpMethod, RouteInputSchemas } from '../decorators/index.js';
|
|
12
12
|
/**
|
|
13
13
|
* Defining types
|
|
14
14
|
*/
|
|
@@ -20,6 +20,7 @@ declare module '@shadow-library/app' {
|
|
|
20
20
|
schemas?: RouteInputSchemas & {
|
|
21
21
|
response?: Record<number | string, JSONSchema | SchemaClass>;
|
|
22
22
|
};
|
|
23
|
+
operation?: ApiOperationMetadata;
|
|
23
24
|
rawBody?: boolean;
|
|
24
25
|
silentValidation?: boolean;
|
|
25
26
|
status?: number;
|
|
@@ -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() {
|
|
@@ -157,6 +157,8 @@ let FastifyRouter = class FastifyRouter extends app_1.Router {
|
|
|
157
157
|
const versionPrefix = this.config.prefixVersioning ? `/v${version}` : '';
|
|
158
158
|
const path = this.joinPaths(this.config.routePrefix, versionPrefix, metadata.path, route.metadata.path);
|
|
159
159
|
const parsedController = { ...route, instance, metatype };
|
|
160
|
+
if (metadata.operation || route.metadata.operation)
|
|
161
|
+
route.metadata.operation = Object.assign({}, metadata.operation, route.metadata.operation);
|
|
160
162
|
parsedController.metadata.path = path;
|
|
161
163
|
parsedControllers.routes.push(parsedController);
|
|
162
164
|
}
|
|
@@ -311,7 +313,7 @@ let FastifyRouter = class FastifyRouter extends app_1.Router {
|
|
|
311
313
|
}
|
|
312
314
|
}
|
|
313
315
|
const responseSchemas = { ...defaultResponseSchemas };
|
|
314
|
-
routeOptions.schema = { response: responseSchemas };
|
|
316
|
+
routeOptions.schema = { ...metadata.operation, response: responseSchemas };
|
|
315
317
|
routeOptions.attachValidation = metadata.silentValidation ?? false;
|
|
316
318
|
const { body: bodySchema, params: paramsSchema, query: querySchema, response: responseSchema } = metadata.schemas ?? {};
|
|
317
319
|
const isMaskEnabled = this.config.maskSensitiveData ?? true;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing user defined packages
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Defining types
|
|
6
|
+
*/
|
|
7
|
+
export interface ApiOperationMetadata {
|
|
8
|
+
summary?: string;
|
|
9
|
+
description?: string;
|
|
10
|
+
tags?: string[];
|
|
11
|
+
deprecated?: boolean;
|
|
12
|
+
externalDocs?: {
|
|
13
|
+
url: string;
|
|
14
|
+
description?: string;
|
|
15
|
+
};
|
|
16
|
+
security?: Record<string, string[]>;
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Declaring the constants
|
|
21
|
+
*/
|
|
22
|
+
export declare function ApiOperation(options: ApiOperationMetadata): ClassDecorator & MethodDecorator;
|
package/esm/decorators/index.js
CHANGED
|
@@ -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
|
*/
|
|
@@ -8,7 +8,7 @@ import { FastifyInstance, RouteShorthandOptions } from 'fastify';
|
|
|
8
8
|
* Importing user defined packages
|
|
9
9
|
*/
|
|
10
10
|
import { HTTP_CONTROLLER_TYPE } from '../constants.js';
|
|
11
|
-
import { HttpMethod, RouteInputSchemas } from '../decorators/index.js';
|
|
11
|
+
import { ApiOperationMetadata, HttpMethod, RouteInputSchemas } from '../decorators/index.js';
|
|
12
12
|
/**
|
|
13
13
|
* Defining types
|
|
14
14
|
*/
|
|
@@ -20,6 +20,7 @@ declare module '@shadow-library/app' {
|
|
|
20
20
|
schemas?: RouteInputSchemas & {
|
|
21
21
|
response?: Record<number | string, JSONSchema | SchemaClass>;
|
|
22
22
|
};
|
|
23
|
+
operation?: ApiOperationMetadata;
|
|
23
24
|
rawBody?: boolean;
|
|
24
25
|
silentValidation?: boolean;
|
|
25
26
|
status?: number;
|
|
@@ -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() {
|
|
@@ -151,6 +151,8 @@ let FastifyRouter = class FastifyRouter extends Router {
|
|
|
151
151
|
const versionPrefix = this.config.prefixVersioning ? `/v${version}` : '';
|
|
152
152
|
const path = this.joinPaths(this.config.routePrefix, versionPrefix, metadata.path, route.metadata.path);
|
|
153
153
|
const parsedController = { ...route, instance, metatype };
|
|
154
|
+
if (metadata.operation || route.metadata.operation)
|
|
155
|
+
route.metadata.operation = Object.assign({}, metadata.operation, route.metadata.operation);
|
|
154
156
|
parsedController.metadata.path = path;
|
|
155
157
|
parsedControllers.routes.push(parsedController);
|
|
156
158
|
}
|
|
@@ -305,7 +307,7 @@ let FastifyRouter = class FastifyRouter extends Router {
|
|
|
305
307
|
}
|
|
306
308
|
}
|
|
307
309
|
const responseSchemas = { ...defaultResponseSchemas };
|
|
308
|
-
routeOptions.schema = { response: responseSchemas };
|
|
310
|
+
routeOptions.schema = { ...metadata.operation, response: responseSchemas };
|
|
309
311
|
routeOptions.attachValidation = metadata.silentValidation ?? false;
|
|
310
312
|
const { body: bodySchema, params: paramsSchema, query: querySchema, response: responseSchema } = metadata.schemas ?? {};
|
|
311
313
|
const isMaskEnabled = this.config.maskSensitiveData ?? true;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shadow-library/fastify",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.6.0",
|
|
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
|
},
|