@shadow-library/fastify 0.0.9 → 0.0.11
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 +58 -0
- package/cjs/classes/default-error-handler.d.ts +6 -0
- package/cjs/classes/default-error-handler.js +9 -0
- package/cjs/constants.d.ts +12 -0
- package/cjs/constants.js +12 -0
- package/cjs/decorators/http-controller.decorator.d.ts +6 -0
- package/cjs/decorators/http-controller.decorator.js +12 -0
- package/cjs/decorators/http-input.decorator.d.ts +6 -0
- package/cjs/decorators/http-input.decorator.js +12 -0
- package/cjs/decorators/http-output.decorator.d.ts +9 -0
- package/cjs/decorators/http-output.decorator.js +6 -0
- package/cjs/decorators/http-route.decorator.d.ts +9 -0
- package/cjs/decorators/http-route.decorator.js +12 -0
- package/cjs/decorators/middleware.decorator.d.ts +11 -0
- package/cjs/decorators/middleware.decorator.js +9 -0
- package/cjs/decorators/sensitive.decorator.d.ts +9 -0
- package/cjs/decorators/sensitive.decorator.js +3 -0
- package/cjs/index.d.ts +6 -0
- package/cjs/index.js +6 -0
- package/cjs/interfaces/error-handler.interface.d.ts +12 -0
- package/cjs/interfaces/error-handler.interface.js +3 -0
- package/cjs/interfaces/middleware.interface.d.ts +9 -0
- package/cjs/interfaces/route-handler.interface.d.ts +12 -0
- package/cjs/interfaces/server-metadata.interface.d.ts +9 -0
- package/cjs/module/error-response.dto.d.ts +9 -0
- package/cjs/module/error-response.dto.js +12 -0
- package/cjs/module/fastify-module.interface.d.ts +55 -0
- package/cjs/module/fastify-router.d.ts +11 -0
- package/cjs/module/fastify-router.js +20 -1
- package/cjs/module/fastify.module.d.ts +6 -0
- package/cjs/module/fastify.module.js +12 -0
- package/cjs/module/fastify.utils.js +14 -1
- package/cjs/server.error.d.ts +15 -0
- package/cjs/server.error.js +24 -0
- package/cjs/services/context.service.d.ts +10 -1
- package/cjs/services/context.service.js +9 -0
- package/esm/classes/default-error-handler.d.ts +6 -0
- package/esm/classes/default-error-handler.js +9 -0
- package/esm/constants.d.ts +12 -0
- package/esm/constants.js +12 -0
- package/esm/decorators/http-controller.decorator.d.ts +6 -0
- package/esm/decorators/http-controller.decorator.js +12 -0
- package/esm/decorators/http-input.decorator.d.ts +6 -0
- package/esm/decorators/http-input.decorator.js +12 -0
- package/esm/decorators/http-output.decorator.d.ts +9 -0
- package/esm/decorators/http-output.decorator.js +6 -0
- package/esm/decorators/http-route.decorator.d.ts +9 -0
- package/esm/decorators/http-route.decorator.js +12 -0
- package/esm/decorators/middleware.decorator.d.ts +11 -0
- package/esm/decorators/middleware.decorator.js +9 -0
- package/esm/decorators/sensitive.decorator.d.ts +9 -0
- package/esm/decorators/sensitive.decorator.js +3 -0
- package/esm/index.d.ts +6 -0
- package/esm/index.js +6 -0
- package/esm/interfaces/error-handler.interface.d.ts +12 -0
- package/esm/interfaces/error-handler.interface.js +3 -0
- package/esm/interfaces/middleware.interface.d.ts +9 -0
- package/esm/interfaces/route-handler.interface.d.ts +12 -0
- package/esm/interfaces/server-metadata.interface.d.ts +9 -0
- package/esm/module/error-response.dto.d.ts +9 -0
- package/esm/module/error-response.dto.js +12 -0
- package/esm/module/fastify-module.interface.d.ts +55 -0
- package/esm/module/fastify-router.d.ts +11 -0
- package/esm/module/fastify-router.js +20 -1
- package/esm/module/fastify.module.d.ts +6 -0
- package/esm/module/fastify.module.js +12 -0
- package/esm/module/fastify.utils.js +14 -1
- package/esm/server.error.d.ts +15 -0
- package/esm/server.error.js +24 -0
- package/esm/services/context.service.d.ts +10 -1
- package/esm/services/context.service.js +9 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -695,6 +695,64 @@ export class DataController {
|
|
|
695
695
|
}
|
|
696
696
|
```
|
|
697
697
|
|
|
698
|
+
### Extending Context Service
|
|
699
|
+
|
|
700
|
+
The `ContextService` can be extended with custom methods to add application-specific functionality while maintaining type safety and method chaining capabilities.
|
|
701
|
+
|
|
702
|
+
```typescript
|
|
703
|
+
import { contextService } from '@shadow-library/fastify';
|
|
704
|
+
|
|
705
|
+
declare module '@shadow-library/fastify' {
|
|
706
|
+
export interface ContextExtension {
|
|
707
|
+
setUserRole(role: string): ContextService;
|
|
708
|
+
getUserRole(): string;
|
|
709
|
+
setCurrentUserId(userId: string): ContextService;
|
|
710
|
+
getCurrentUserId(): string;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// Extend the context service with custom methods
|
|
715
|
+
contextService.extend({
|
|
716
|
+
setUserRole(role: string) {
|
|
717
|
+
return this.set('user-role', role);
|
|
718
|
+
},
|
|
719
|
+
getUserRole() {
|
|
720
|
+
return this.get<string>('user-role', false);
|
|
721
|
+
},
|
|
722
|
+
setCurrentUserId(userId: string) {
|
|
723
|
+
return this.set('current-user-id', userId);
|
|
724
|
+
},
|
|
725
|
+
getCurrentUserId() {
|
|
726
|
+
return this.get<string>('current-user-id', false);
|
|
727
|
+
},
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
// Use in controllers with method chaining
|
|
731
|
+
@HttpController('/api')
|
|
732
|
+
export class UserController {
|
|
733
|
+
constructor(private readonly contextService: ContextService) {}
|
|
734
|
+
|
|
735
|
+
@Post('/login')
|
|
736
|
+
async login(@Body() loginDto: LoginDto) {
|
|
737
|
+
const user = await this.authService.validateUser(loginDto);
|
|
738
|
+
|
|
739
|
+
// Chain extended methods
|
|
740
|
+
this.contextService.setCurrentUserId(user.id).setUserRole(user.role);
|
|
741
|
+
|
|
742
|
+
return { message: 'Login successful' };
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
@Get('/profile')
|
|
746
|
+
getProfile() {
|
|
747
|
+
return {
|
|
748
|
+
userId: this.contextService.getCurrentUserId(),
|
|
749
|
+
role: this.contextService.getUserRole(),
|
|
750
|
+
requestId: this.contextService.getRID(),
|
|
751
|
+
};
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
```
|
|
755
|
+
|
|
698
756
|
## Examples
|
|
699
757
|
|
|
700
758
|
Check out the [examples](./examples) directory for complete working examples:
|
|
@@ -1,6 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing npm packages
|
|
3
|
+
*/
|
|
1
4
|
import { AppErrorObject } from '@shadow-library/common';
|
|
2
5
|
import { FastifyError } from 'fastify';
|
|
3
6
|
import { ErrorHandler, HttpRequest, HttpResponse } from '../interfaces/index.js';
|
|
7
|
+
/**
|
|
8
|
+
* Defining types
|
|
9
|
+
*/
|
|
4
10
|
export interface ParsedFastifyError {
|
|
5
11
|
statusCode: number;
|
|
6
12
|
error: AppErrorObject;
|
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DefaultErrorHandler = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Importing npm packages
|
|
6
|
+
*/
|
|
4
7
|
const common_1 = require("@shadow-library/common");
|
|
8
|
+
/**
|
|
9
|
+
* Importing user defined packages
|
|
10
|
+
*/
|
|
5
11
|
const constants_1 = require("../constants.js");
|
|
6
12
|
const server_error_1 = require("../server.error.js");
|
|
13
|
+
/**
|
|
14
|
+
* Declaring the constants
|
|
15
|
+
*/
|
|
7
16
|
const unexpectedError = new server_error_1.ServerError(server_error_1.ServerErrorCode.S001);
|
|
8
17
|
const validationError = new server_error_1.ServerError(server_error_1.ServerErrorCode.S003);
|
|
9
18
|
const invalidRequestError = new server_error_1.ServerError(server_error_1.ServerErrorCode.S006);
|
package/cjs/constants.d.ts
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing npm packages
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Importing user defined packages
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Defining types
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Declaring the constants
|
|
12
|
+
*/
|
|
1
13
|
export declare const NAMESPACE = "@shadow-library/fastify";
|
|
2
14
|
export declare const PARAMTYPES_METADATA = "design:paramtypes";
|
|
3
15
|
export declare const FASTIFY_CONFIG: unique symbol;
|
package/cjs/constants.js
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Importing npm packages
|
|
4
|
+
*/
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.HTTP_CONTROLLER_INPUTS = exports.HTTP_CONTROLLER_TYPE = exports.FASTIFY_INSTANCE = exports.FASTIFY_CONFIG = exports.PARAMTYPES_METADATA = exports.NAMESPACE = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Importing user defined packages
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Defining types
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Declaring the constants
|
|
15
|
+
*/
|
|
4
16
|
exports.NAMESPACE = '@shadow-library/fastify';
|
|
5
17
|
exports.PARAMTYPES_METADATA = 'design:paramtypes';
|
|
6
18
|
exports.FASTIFY_CONFIG = Symbol('fastify-config');
|
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.HttpController = HttpController;
|
|
4
|
+
/**
|
|
5
|
+
* Importing npm packages
|
|
6
|
+
*/
|
|
4
7
|
const app_1 = require("@shadow-library/app");
|
|
8
|
+
/**
|
|
9
|
+
* Importing user defined packages
|
|
10
|
+
*/
|
|
5
11
|
const constants_1 = require("../constants.js");
|
|
12
|
+
/**
|
|
13
|
+
* Defining types
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Declaring the constants
|
|
17
|
+
*/
|
|
6
18
|
function HttpController(path = '') {
|
|
7
19
|
if (path.charAt(0) !== '/')
|
|
8
20
|
path = `/${path}`;
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { JSONSchema } from '@shadow-library/class-schema';
|
|
2
2
|
import { Class } from 'type-fest';
|
|
3
|
+
/**
|
|
4
|
+
* Defining types
|
|
5
|
+
*/
|
|
3
6
|
export declare enum RouteInputType {
|
|
4
7
|
BODY = "body",
|
|
5
8
|
PARAMS = "params",
|
|
@@ -8,6 +11,9 @@ export declare enum RouteInputType {
|
|
|
8
11
|
RESPONSE = "response"
|
|
9
12
|
}
|
|
10
13
|
export type RouteInputSchemas = Partial<Record<'body' | 'params' | 'query', JSONSchema | Class<unknown>>>;
|
|
14
|
+
/**
|
|
15
|
+
* Declaring the constants
|
|
16
|
+
*/
|
|
11
17
|
export declare function HttpInput(type: RouteInputType, schema?: JSONSchema): ParameterDecorator;
|
|
12
18
|
export declare const Body: (schema?: JSONSchema) => ParameterDecorator;
|
|
13
19
|
export declare const Params: (schema?: JSONSchema) => ParameterDecorator;
|
|
@@ -5,9 +5,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Res = exports.Response = exports.Req = exports.Request = exports.Query = exports.Params = exports.Body = exports.RouteInputType = void 0;
|
|
7
7
|
exports.HttpInput = HttpInput;
|
|
8
|
+
/**
|
|
9
|
+
* Importing npm packages
|
|
10
|
+
*/
|
|
8
11
|
const node_assert_1 = __importDefault(require("node:assert"));
|
|
9
12
|
const app_1 = require("@shadow-library/app");
|
|
13
|
+
/**
|
|
14
|
+
* Importing user defined packages
|
|
15
|
+
*/
|
|
10
16
|
const constants_1 = require("../constants.js");
|
|
17
|
+
/**
|
|
18
|
+
* Defining types
|
|
19
|
+
*/
|
|
11
20
|
var RouteInputType;
|
|
12
21
|
(function (RouteInputType) {
|
|
13
22
|
RouteInputType["BODY"] = "body";
|
|
@@ -16,6 +25,9 @@ var RouteInputType;
|
|
|
16
25
|
RouteInputType["REQUEST"] = "request";
|
|
17
26
|
RouteInputType["RESPONSE"] = "response";
|
|
18
27
|
})(RouteInputType || (exports.RouteInputType = RouteInputType = {}));
|
|
28
|
+
/**
|
|
29
|
+
* Declaring the constants
|
|
30
|
+
*/
|
|
19
31
|
function HttpInput(type, schema) {
|
|
20
32
|
return (target, propertyKey, index) => {
|
|
21
33
|
(0, node_assert_1.default)(propertyKey, 'Cannot apply decorator to a constructor parameter');
|
|
@@ -1,5 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing npm packages
|
|
3
|
+
*/
|
|
1
4
|
import { JSONSchema, SchemaClass } from '@shadow-library/class-schema';
|
|
2
5
|
import { JsonObject } from 'type-fest';
|
|
6
|
+
/**
|
|
7
|
+
* Importing user defined packages
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Defining types
|
|
11
|
+
*/
|
|
3
12
|
export interface DynamicRender<T extends JsonObject> {
|
|
4
13
|
template: string;
|
|
5
14
|
data: T;
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Importing npm packages
|
|
4
|
+
*/
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.Render = exports.Redirect = exports.Header = exports.HttpStatus = void 0;
|
|
4
7
|
exports.RespondFor = RespondFor;
|
|
5
8
|
const app_1 = require("@shadow-library/app");
|
|
6
9
|
const class_schema_1 = require("@shadow-library/class-schema");
|
|
10
|
+
/**
|
|
11
|
+
* Declaring the constants
|
|
12
|
+
*/
|
|
7
13
|
const isClass = (schema) => schema.toString().startsWith('class ');
|
|
8
14
|
const HttpStatus = (status) => (0, app_1.Route)({ status });
|
|
9
15
|
exports.HttpStatus = HttpStatus;
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing user defined packages
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Defining types
|
|
6
|
+
*/
|
|
1
7
|
export declare enum HttpMethod {
|
|
2
8
|
GET = "GET",
|
|
3
9
|
POST = "POST",
|
|
@@ -12,6 +18,9 @@ export interface RouteOptions {
|
|
|
12
18
|
method: HttpMethod;
|
|
13
19
|
path?: string;
|
|
14
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Declaring the constants
|
|
23
|
+
*/
|
|
15
24
|
export declare function HttpRoute(options: RouteOptions): MethodDecorator;
|
|
16
25
|
export declare const Get: (path?: string) => MethodDecorator;
|
|
17
26
|
export declare const Post: (path?: string) => MethodDecorator;
|
|
@@ -2,7 +2,16 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.All = exports.Head = exports.Options = exports.Patch = exports.Delete = exports.Put = exports.Post = exports.Get = exports.HttpMethod = void 0;
|
|
4
4
|
exports.HttpRoute = HttpRoute;
|
|
5
|
+
/**
|
|
6
|
+
* Importing npm packages
|
|
7
|
+
*/
|
|
5
8
|
const app_1 = require("@shadow-library/app");
|
|
9
|
+
/**
|
|
10
|
+
* Importing user defined packages
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Defining types
|
|
14
|
+
*/
|
|
6
15
|
var HttpMethod;
|
|
7
16
|
(function (HttpMethod) {
|
|
8
17
|
HttpMethod["GET"] = "GET";
|
|
@@ -14,6 +23,9 @@ var HttpMethod;
|
|
|
14
23
|
HttpMethod["HEAD"] = "HEAD";
|
|
15
24
|
HttpMethod["ALL"] = "ALL";
|
|
16
25
|
})(HttpMethod || (exports.HttpMethod = HttpMethod = {}));
|
|
26
|
+
/**
|
|
27
|
+
* Declaring the constants
|
|
28
|
+
*/
|
|
17
29
|
function HttpRoute(options) {
|
|
18
30
|
if (options.path && options.path.charAt(0) !== '/')
|
|
19
31
|
options.path = `/${options.path}`;
|
|
@@ -1,7 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing user defined packages
|
|
3
|
+
*/
|
|
1
4
|
import { HTTP_CONTROLLER_TYPE } from '../constants.js';
|
|
5
|
+
/**
|
|
6
|
+
* Defining types
|
|
7
|
+
*/
|
|
2
8
|
export type MiddlewareType = 'onRequest' | 'preParsing' | 'preValidation' | 'preHandler' | 'preSerialization' | 'onSend' | 'onResponse' | 'onError';
|
|
3
9
|
export interface MiddlewareOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Denotes when to execute the middleware. default value is `preHandler`.
|
|
12
|
+
* see https://fastify.dev/docs/latest/Reference/Lifecycle for more information
|
|
13
|
+
*/
|
|
4
14
|
type: MiddlewareType;
|
|
15
|
+
/** Denotes the execution order, the higher value gets executed first */
|
|
5
16
|
weight: number;
|
|
6
17
|
}
|
|
7
18
|
export interface MiddlewareMetadata extends MiddlewareOptions {
|
|
@@ -4,9 +4,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Middleware = Middleware;
|
|
7
|
+
/**
|
|
8
|
+
* Importing npm packages
|
|
9
|
+
*/
|
|
7
10
|
const node_assert_1 = __importDefault(require("node:assert"));
|
|
8
11
|
const app_1 = require("@shadow-library/app");
|
|
12
|
+
/**
|
|
13
|
+
* Importing user defined packages
|
|
14
|
+
*/
|
|
9
15
|
const constants_1 = require("../constants.js");
|
|
16
|
+
/**
|
|
17
|
+
* Declaring the constants
|
|
18
|
+
*/
|
|
10
19
|
const propertyKeys = ['generate', 'use'];
|
|
11
20
|
function Middleware(options = {}) {
|
|
12
21
|
if (!options.type)
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import { MaskOptions } from '@shadow-library/common';
|
|
2
|
+
/**
|
|
3
|
+
* Importing user defined packages
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Defining types
|
|
7
|
+
*/
|
|
2
8
|
export type SensitiveDataType = 'secret' | 'email' | 'number' | 'words';
|
|
9
|
+
/**
|
|
10
|
+
* Declaring the constants
|
|
11
|
+
*/
|
|
3
12
|
export declare function Sensitive(type?: SensitiveDataType): PropertyDecorator;
|
|
4
13
|
export declare function Sensitive(maskOptions: MaskOptions): PropertyDecorator;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Sensitive = Sensitive;
|
|
4
|
+
/**
|
|
5
|
+
* Importing npm packages
|
|
6
|
+
*/
|
|
4
7
|
const class_schema_1 = require("@shadow-library/class-schema");
|
|
5
8
|
function Sensitive(typeOrMaskOptions = 'secret') {
|
|
6
9
|
return (target, propertyKey) => {
|
package/cjs/index.d.ts
CHANGED
package/cjs/index.js
CHANGED
|
@@ -14,7 +14,13 @@ 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
|
+
/**
|
|
18
|
+
* Importing npm packages
|
|
19
|
+
*/
|
|
17
20
|
require("reflect-metadata");
|
|
21
|
+
/**
|
|
22
|
+
* exporting modules
|
|
23
|
+
*/
|
|
18
24
|
__exportStar(require("./classes/index.js"), exports);
|
|
19
25
|
__exportStar(require("./decorators/index.js"), exports);
|
|
20
26
|
__exportStar(require("./interfaces/index.js"), exports);
|
|
@@ -1,4 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing npm packages
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Importing user defined packages
|
|
6
|
+
*/
|
|
1
7
|
import { HttpRequest, HttpResponse } from './route-handler.interface.js';
|
|
8
|
+
/**
|
|
9
|
+
* Defining types
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Declaring the constants
|
|
13
|
+
*/
|
|
2
14
|
export interface ErrorHandler {
|
|
3
15
|
handle(err: Error, req: HttpRequest, res: HttpResponse): any | Promise<any>;
|
|
4
16
|
}
|
|
@@ -1,5 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing npm packages
|
|
3
|
+
*/
|
|
1
4
|
import { RouteMetadata } from '@shadow-library/app';
|
|
5
|
+
/**
|
|
6
|
+
* Importing user defined packages
|
|
7
|
+
*/
|
|
2
8
|
import { HttpCallback, HttpRequest, HttpResponse, RouteHandler } from './route-handler.interface.js';
|
|
9
|
+
/**
|
|
10
|
+
* Defining types
|
|
11
|
+
*/
|
|
3
12
|
export interface MiddlewareGenerator {
|
|
4
13
|
cacheKey?: (metadata: RouteMetadata) => string;
|
|
5
14
|
generate(metadata: RouteMetadata): RouteHandler | undefined | Promise<RouteHandler | undefined>;
|
|
@@ -1,5 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing npm packages
|
|
3
|
+
*/
|
|
1
4
|
import { SyncValue } from '@shadow-library/common';
|
|
2
5
|
import { DoneFuncWithErrOrRes, FastifyReply, FastifyRequest } from 'fastify';
|
|
6
|
+
/**
|
|
7
|
+
* Importing user defined packages
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Defining types
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Declaring the constants
|
|
14
|
+
*/
|
|
3
15
|
export type HttpRequest = FastifyRequest;
|
|
4
16
|
export type HttpResponse = FastifyReply;
|
|
5
17
|
export type HttpCallback = DoneFuncWithErrOrRes;
|
|
@@ -1,8 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing npm packages
|
|
3
|
+
*/
|
|
1
4
|
import { RouteMetadata } from '@shadow-library/app';
|
|
2
5
|
import { JSONSchema } from '@shadow-library/class-schema';
|
|
3
6
|
import { RouteShorthandOptions } from 'fastify';
|
|
7
|
+
/**
|
|
8
|
+
* Importing user defined packages
|
|
9
|
+
*/
|
|
4
10
|
import { HTTP_CONTROLLER_TYPE } from '../constants.js';
|
|
5
11
|
import { HttpMethod, RouteInputSchemas } from '../decorators/index.js';
|
|
12
|
+
/**
|
|
13
|
+
* Defining types
|
|
14
|
+
*/
|
|
6
15
|
declare module '@shadow-library/app' {
|
|
7
16
|
interface RouteMetadata extends Omit<RouteShorthandOptions, 'config'> {
|
|
8
17
|
method?: HttpMethod;
|
|
@@ -10,7 +10,19 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.ErrorResponseDto = exports.ErrorFieldDto = void 0;
|
|
13
|
+
/**
|
|
14
|
+
* Importing npm packages
|
|
15
|
+
*/
|
|
13
16
|
const class_schema_1 = require("@shadow-library/class-schema");
|
|
17
|
+
/**
|
|
18
|
+
* Importing user defined packages
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Defining types
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* Declaring the constants
|
|
25
|
+
*/
|
|
14
26
|
let ErrorFieldDto = class ErrorFieldDto {
|
|
15
27
|
field;
|
|
16
28
|
msg;
|
|
@@ -1,24 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing npm packages
|
|
3
|
+
*/
|
|
1
4
|
import { FactoryProvider, ModuleMetadata } from '@shadow-library/app';
|
|
2
5
|
import { JSONSchema } from '@shadow-library/class-schema';
|
|
3
6
|
import { FastifyInstance, FastifyServerOptions } from 'fastify';
|
|
4
7
|
import { Promisable } from 'type-fest';
|
|
8
|
+
/**
|
|
9
|
+
* Importing user defined packages
|
|
10
|
+
*/
|
|
5
11
|
import { ErrorHandler } from '../interfaces/index.js';
|
|
12
|
+
/**
|
|
13
|
+
* Defining types
|
|
14
|
+
*/
|
|
6
15
|
export interface FastifyConfig extends FastifyServerOptions {
|
|
16
|
+
/**
|
|
17
|
+
* The host on which the Fastify instance is to be started
|
|
18
|
+
* @default '127.0.0.1'
|
|
19
|
+
*/
|
|
7
20
|
host: string;
|
|
21
|
+
/**
|
|
22
|
+
* The port on which the Fastify instance is to be started
|
|
23
|
+
* @default 8080
|
|
24
|
+
*/
|
|
8
25
|
port: number;
|
|
26
|
+
/**
|
|
27
|
+
* The error handler to be used to handle errors thrown by the Fastify instance
|
|
28
|
+
* @default DefaultErrorHandler
|
|
29
|
+
*/
|
|
9
30
|
errorHandler: ErrorHandler;
|
|
31
|
+
/**
|
|
32
|
+
* The schema to be used to validate the response of the Fastify instance
|
|
33
|
+
* @default { '4xx': errorResponseSchema, '5xx': errorResponseSchema }
|
|
34
|
+
*/
|
|
10
35
|
responseSchema?: Record<string | number, JSONSchema>;
|
|
36
|
+
/**
|
|
37
|
+
* Enables internal execution of child routes (e.g., for SSR data fetching) without making actual HTTP requests.
|
|
38
|
+
* Useful for loading data while reusing middleware logic and shared context.
|
|
39
|
+
* @default false
|
|
40
|
+
*/
|
|
11
41
|
enableChildRoutes?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Masks fields marked as sensitive in API inputs (body, query, and URL params) when written to logs.
|
|
44
|
+
* @default true
|
|
45
|
+
*/
|
|
12
46
|
maskSensitiveData?: boolean;
|
|
13
47
|
}
|
|
14
48
|
export interface FastifyModuleOptions extends Partial<FastifyConfig> {
|
|
49
|
+
/**
|
|
50
|
+
* The list of modules whose controllers are to be registered in the Fastify instance
|
|
51
|
+
*/
|
|
15
52
|
imports?: ModuleMetadata['imports'];
|
|
53
|
+
/**
|
|
54
|
+
* Factory function to modify the Fastify instance before it is used to register the controllers
|
|
55
|
+
*/
|
|
16
56
|
fastifyFactory?: (instance: FastifyInstance) => Promisable<FastifyInstance>;
|
|
57
|
+
/**
|
|
58
|
+
* The list of controllers to be registered in the Fastify instance
|
|
59
|
+
*/
|
|
17
60
|
controllers?: ModuleMetadata['controllers'];
|
|
61
|
+
/**
|
|
62
|
+
* The list of providers to be registered in the Fastify instance
|
|
63
|
+
*/
|
|
18
64
|
providers?: ModuleMetadata['providers'];
|
|
65
|
+
/**
|
|
66
|
+
* The list of providers to be exported by the Fastify module
|
|
67
|
+
*/
|
|
19
68
|
exports?: ModuleMetadata['exports'];
|
|
20
69
|
}
|
|
21
70
|
export interface FastifyModuleAsyncOptions extends Pick<FastifyModuleOptions, 'imports' | 'controllers' | 'providers' | 'exports' | 'fastifyFactory'> {
|
|
71
|
+
/**
|
|
72
|
+
* Factory function to create the FastifyModuleOptions
|
|
73
|
+
*/
|
|
22
74
|
useFactory: (...args: any[]) => Promisable<FastifyConfig>;
|
|
75
|
+
/**
|
|
76
|
+
* The list of providers to be injected into the factory function
|
|
77
|
+
*/
|
|
23
78
|
inject?: FactoryProvider['inject'];
|
|
24
79
|
}
|
|
@@ -6,6 +6,9 @@ import { HttpMethod } from '../decorators/index.js';
|
|
|
6
6
|
import { HttpRequest, HttpResponse, ServerMetadata } from '../interfaces/index.js';
|
|
7
7
|
import { ContextService } from '../services/index.js';
|
|
8
8
|
import { type FastifyConfig } from './fastify-module.interface.js';
|
|
9
|
+
/**
|
|
10
|
+
* Defining types
|
|
11
|
+
*/
|
|
9
12
|
declare module 'fastify' {
|
|
10
13
|
interface FastifyRequest {
|
|
11
14
|
rawBody?: Buffer;
|
|
@@ -23,7 +26,9 @@ export interface RequestContext {
|
|
|
23
26
|
body: JsonObject;
|
|
24
27
|
}
|
|
25
28
|
export interface RequestMetadata {
|
|
29
|
+
/** request id */
|
|
26
30
|
rid?: string;
|
|
31
|
+
/** Service request id */
|
|
27
32
|
srid?: string;
|
|
28
33
|
method?: string;
|
|
29
34
|
url?: string;
|
|
@@ -31,6 +36,7 @@ export interface RequestMetadata {
|
|
|
31
36
|
reqLen?: string;
|
|
32
37
|
reqIp?: string;
|
|
33
38
|
resLen?: string;
|
|
39
|
+
/** Time taken to process the request */
|
|
34
40
|
timeTaken?: string;
|
|
35
41
|
body?: any;
|
|
36
42
|
query?: object;
|
|
@@ -71,6 +77,11 @@ export declare class FastifyRouter extends Router {
|
|
|
71
77
|
register(controllers: ControllerRouteMetadata[]): Promise<void>;
|
|
72
78
|
start(): Promise<void>;
|
|
73
79
|
stop(): Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* Creates a new child context derived from the current one to load route-specific data
|
|
82
|
+
* during SSR. Automatically reuses middleware results from the parent context to avoid
|
|
83
|
+
* redundant execution and ensures correct context isolation for nested route data fetching.
|
|
84
|
+
*/
|
|
74
85
|
resolveChildRoute<T extends JsonValue = JsonObject>(url: string): Promise<T>;
|
|
75
86
|
mockRequest(): MockRequestChain;
|
|
76
87
|
mockRequest(options: MockRequestOptions): Promise<MockResponse>;
|