@shadow-library/fastify 1.5.1 → 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 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:
@@ -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
+ }
@@ -1,3 +1,4 @@
1
+ export * from './api-operation.decorator.js';
1
2
  export * from './http-controller.decorator.js';
2
3
  export * from './http-input.decorator.js';
3
4
  export * from './http-output.decorator.js';
@@ -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);
@@ -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;
@@ -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;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Importing npm packages
3
+ */
4
+ import { Route } from '@shadow-library/app';
5
+ /**
6
+ * Declaring the constants
7
+ */
8
+ export function ApiOperation(options) {
9
+ return Route({ operation: options });
10
+ }
@@ -1,3 +1,4 @@
1
+ export * from './api-operation.decorator.js';
1
2
  export * from './http-controller.decorator.js';
2
3
  export * from './http-input.decorator.js';
3
4
  export * from './http-output.decorator.js';
@@ -1,3 +1,4 @@
1
+ export * from './api-operation.decorator.js';
1
2
  export * from './http-controller.decorator.js';
2
3
  export * from './http-input.decorator.js';
3
4
  export * from './http-output.decorator.js';
@@ -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;
@@ -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.5.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": {