@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
|
@@ -7,7 +7,19 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
|
+
/**
|
|
11
|
+
* Importing npm packages
|
|
12
|
+
*/
|
|
10
13
|
import { Field, Schema } from '@shadow-library/class-schema';
|
|
14
|
+
/**
|
|
15
|
+
* Importing user defined packages
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Defining types
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Declaring the constants
|
|
22
|
+
*/
|
|
11
23
|
let ErrorFieldDto = class ErrorFieldDto {
|
|
12
24
|
field;
|
|
13
25
|
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>;
|
|
@@ -10,6 +10,9 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
11
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* Importing npm packages
|
|
15
|
+
*/
|
|
13
16
|
import assert from 'node:assert';
|
|
14
17
|
import { Inject, Injectable, Router } from '@shadow-library/app';
|
|
15
18
|
import { ClassSchema, TransformerFactory } from '@shadow-library/class-schema';
|
|
@@ -17,9 +20,15 @@ import { InternalError, Logger, utils } from '@shadow-library/common';
|
|
|
17
20
|
import merge from 'deepmerge';
|
|
18
21
|
import findMyWay from 'find-my-way';
|
|
19
22
|
import stringify from 'json-stable-stringify';
|
|
23
|
+
/**
|
|
24
|
+
* Importing user defined packages
|
|
25
|
+
*/
|
|
20
26
|
import { FASTIFY_CONFIG, FASTIFY_INSTANCE, HTTP_CONTROLLER_INPUTS, HTTP_CONTROLLER_TYPE, NAMESPACE } from '../constants.js';
|
|
21
27
|
import { HttpMethod } from '../decorators/index.js';
|
|
22
28
|
import { ContextService } from '../services/index.js';
|
|
29
|
+
/**
|
|
30
|
+
* Declaring the constants
|
|
31
|
+
*/
|
|
23
32
|
const httpMethods = Object.values(HttpMethod).filter(m => m !== HttpMethod.ALL);
|
|
24
33
|
const DEFAULT_ARTIFACTS = { transforms: {} };
|
|
25
34
|
let FastifyRouter = class FastifyRouter extends Router {
|
|
@@ -84,7 +93,7 @@ let FastifyRouter = class FastifyRouter extends Router {
|
|
|
84
93
|
metadata.reqIp = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
|
|
85
94
|
metadata.resLen = res.getHeader('content-length');
|
|
86
95
|
const resTime = process.hrtime(startTime);
|
|
87
|
-
metadata.timeTaken = (resTime[0] * 1e3 + resTime[1] * 1e-6).toFixed(3);
|
|
96
|
+
metadata.timeTaken = (resTime[0] * 1e3 + resTime[1] * 1e-6).toFixed(3); // Converting time to milliseconds
|
|
88
97
|
if (req.body)
|
|
89
98
|
metadata.body = transforms.maskBody ? transforms.maskBody(structuredClone(req.body)) : req.body;
|
|
90
99
|
if (req.query)
|
|
@@ -146,10 +155,12 @@ let FastifyRouter = class FastifyRouter extends Router {
|
|
|
146
155
|
const query = request.query;
|
|
147
156
|
const body = request.body;
|
|
148
157
|
const context = { request, response, params, query, body };
|
|
158
|
+
/** Setting the status code and headers */
|
|
149
159
|
response.status(statusCode);
|
|
150
160
|
for (const [key, value] of Object.entries(metadata.headers ?? {})) {
|
|
151
161
|
response.header(key, typeof value === 'function' ? value() : value);
|
|
152
162
|
}
|
|
163
|
+
/** Handling the actual route and serializing the output */
|
|
153
164
|
const args = argsOrder.map(arg => arg && context[arg]);
|
|
154
165
|
const data = await route.handler(...args);
|
|
155
166
|
if (metadata.redirect)
|
|
@@ -170,11 +181,13 @@ let FastifyRouter = class FastifyRouter extends Router {
|
|
|
170
181
|
async getMiddlewareHandler(middleware, metadata) {
|
|
171
182
|
if (!middleware.metadata.generates)
|
|
172
183
|
return middleware.handler.bind(middleware.instance);
|
|
184
|
+
/** Generating the cache key and getting the cached middleware */
|
|
173
185
|
const genCacheKey = 'cacheKey' in middleware.instance && typeof middleware.instance.cacheKey === 'function' ? middleware.instance.cacheKey : stringify;
|
|
174
186
|
const cacheKey = genCacheKey(metadata);
|
|
175
187
|
const cachedMiddleware = this.cachedDynamicMiddlewares.get(cacheKey);
|
|
176
188
|
if (cachedMiddleware)
|
|
177
189
|
return cachedMiddleware;
|
|
190
|
+
/** Generating the middleware handler */
|
|
178
191
|
const handler = await middleware.handler.apply(middleware.instance, [metadata]);
|
|
179
192
|
this.cachedDynamicMiddlewares.set(cacheKey, handler);
|
|
180
193
|
return handler;
|
|
@@ -200,6 +213,7 @@ let FastifyRouter = class FastifyRouter extends Router {
|
|
|
200
213
|
routeOptions.url = metadata.path;
|
|
201
214
|
routeOptions.method = metadata.method === HttpMethod.ALL ? httpMethods : [metadata.method];
|
|
202
215
|
routeOptions.handler = this.generateRouteHandler(route);
|
|
216
|
+
/** Applying middlewares */
|
|
203
217
|
for (const middleware of middlewares) {
|
|
204
218
|
const name = middleware.metatype.name;
|
|
205
219
|
const { type } = middleware.metadata;
|
|
@@ -260,6 +274,11 @@ let FastifyRouter = class FastifyRouter extends Router {
|
|
|
260
274
|
await this.instance.close();
|
|
261
275
|
this.logger.info('server stopped');
|
|
262
276
|
}
|
|
277
|
+
/**
|
|
278
|
+
* Creates a new child context derived from the current one to load route-specific data
|
|
279
|
+
* during SSR. Automatically reuses middleware results from the parent context to avoid
|
|
280
|
+
* redundant execution and ensures correct context isolation for nested route data fetching.
|
|
281
|
+
*/
|
|
263
282
|
async resolveChildRoute(url) {
|
|
264
283
|
if (!this.childRouter)
|
|
265
284
|
throw new InternalError('Child routes are not enabled');
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { Class } from 'type-fest';
|
|
2
2
|
import { FastifyModuleAsyncOptions, FastifyModuleOptions } from './fastify-module.interface.js';
|
|
3
|
+
/**
|
|
4
|
+
* Defining types
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Declaring the constants
|
|
8
|
+
*/
|
|
3
9
|
export declare class FastifyModule {
|
|
4
10
|
private static getDefaultConfig;
|
|
5
11
|
static forRoot(options: FastifyModuleOptions): Class<FastifyModule>;
|
|
@@ -1,13 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing npm packages
|
|
3
|
+
*/
|
|
1
4
|
import { Module, Router } from '@shadow-library/app';
|
|
2
5
|
import { ClassSchema } from '@shadow-library/class-schema';
|
|
3
6
|
import { Config, utils } from '@shadow-library/common';
|
|
4
7
|
import { v4 as uuid } from 'uuid';
|
|
8
|
+
/**
|
|
9
|
+
* Importing user defined packages
|
|
10
|
+
*/
|
|
5
11
|
import { DefaultErrorHandler } from '../classes/index.js';
|
|
6
12
|
import { FASTIFY_CONFIG, FASTIFY_INSTANCE } from '../constants.js';
|
|
7
13
|
import { ContextService } from '../services/index.js';
|
|
8
14
|
import { ErrorResponseDto } from './error-response.dto.js';
|
|
9
15
|
import { FastifyRouter } from './fastify-router.js';
|
|
10
16
|
import { createFastifyInstance } from './fastify.utils.js';
|
|
17
|
+
/**
|
|
18
|
+
* Defining types
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Declaring the constants
|
|
22
|
+
*/
|
|
11
23
|
export class FastifyModule {
|
|
12
24
|
static getDefaultConfig() {
|
|
13
25
|
const errorResponseSchema = ClassSchema.generate(ErrorResponseDto);
|
|
@@ -1,8 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing npm packages
|
|
3
|
+
*/
|
|
1
4
|
import assert from 'node:assert';
|
|
2
5
|
import { ValidationError, throwError, utils } from '@shadow-library/common';
|
|
3
6
|
import Ajv from 'ajv';
|
|
4
7
|
import { fastify } from 'fastify';
|
|
8
|
+
/**
|
|
9
|
+
* Importing user defined packages
|
|
10
|
+
*/
|
|
5
11
|
import { ServerError, ServerErrorCode } from '../server.error.js';
|
|
12
|
+
/**
|
|
13
|
+
* Defining types
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Declaring the constants
|
|
17
|
+
*/
|
|
6
18
|
const keywords = ['x-fastify'];
|
|
7
19
|
const allowedHttpParts = ['body', 'params', 'querystring'];
|
|
8
20
|
const strictValidator = new Ajv({ allErrors: true, useDefaults: true, removeAdditional: true, strict: true, keywords });
|
|
@@ -29,12 +41,13 @@ export function compileValidator(routeSchema) {
|
|
|
29
41
|
return (data) => {
|
|
30
42
|
validate(data);
|
|
31
43
|
for (const error of validate.errors ?? []) {
|
|
44
|
+
/** Since this schema is for querystring there won't be any nested objects so we are directly accessing the path */
|
|
32
45
|
const path = error.instancePath.substring(1);
|
|
33
46
|
const defaultValue = routeSchema.schema.properties?.[path]?.default;
|
|
34
47
|
if (defaultValue !== undefined)
|
|
35
48
|
data[path] = defaultValue;
|
|
36
49
|
else
|
|
37
|
-
delete data[path];
|
|
50
|
+
delete data[path]; // eslint-disable-line @typescript-eslint/no-dynamic-delete
|
|
38
51
|
}
|
|
39
52
|
return { value: data };
|
|
40
53
|
};
|
package/esm/server.error.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing npm packages
|
|
3
|
+
*/
|
|
1
4
|
import { AppError, ErrorCode, ErrorType } from '@shadow-library/common';
|
|
2
5
|
export declare class ServerError extends AppError<ServerErrorCode> {
|
|
3
6
|
getStatusCode(): number;
|
|
@@ -6,13 +9,25 @@ export declare class ServerErrorCode extends ErrorCode {
|
|
|
6
9
|
private readonly statusCode;
|
|
7
10
|
protected constructor(code: string, type: ErrorType, msg: string, statusCode?: number);
|
|
8
11
|
getStatusCode(): number;
|
|
12
|
+
/*!
|
|
13
|
+
* List of all server related errors
|
|
14
|
+
*/
|
|
15
|
+
/** An unexpected server error occurred while processing the request */
|
|
9
16
|
static readonly S001: ServerErrorCode;
|
|
17
|
+
/** The requested endpoint does not exist */
|
|
10
18
|
static readonly S002: ServerErrorCode;
|
|
19
|
+
/** The provided input data is invalid or does not meet validation requirements */
|
|
11
20
|
static readonly S003: ServerErrorCode;
|
|
21
|
+
/** Authentication credentials are required to access this resource */
|
|
12
22
|
static readonly S004: ServerErrorCode;
|
|
23
|
+
/** Access denied due to insufficient permissions to perform this operation */
|
|
13
24
|
static readonly S005: ServerErrorCode;
|
|
25
|
+
/** The request is malformed or contains invalid parameters */
|
|
14
26
|
static readonly S006: ServerErrorCode;
|
|
27
|
+
/** Rate limit exceeded due to too many requests sent in a given time frame */
|
|
15
28
|
static readonly S007: ServerErrorCode;
|
|
29
|
+
/** Resource conflict as the requested operation conflicts with existing data */
|
|
16
30
|
static readonly S008: ServerErrorCode;
|
|
31
|
+
/** The requested resource could not be found */
|
|
17
32
|
static readonly S009: ServerErrorCode;
|
|
18
33
|
}
|
package/esm/server.error.js
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing npm packages
|
|
3
|
+
*/
|
|
1
4
|
import { AppError, ErrorCode, ErrorType } from '@shadow-library/common';
|
|
5
|
+
/**
|
|
6
|
+
* Importing user defined packages
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Defining types
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Declaring the constants
|
|
13
|
+
*/
|
|
2
14
|
const ERROR_STATUS_CODES = {
|
|
3
15
|
[ErrorType.CLIENT_ERROR]: 400,
|
|
4
16
|
[ErrorType.HTTP_ERROR]: 400,
|
|
@@ -23,13 +35,25 @@ export class ServerErrorCode extends ErrorCode {
|
|
|
23
35
|
getStatusCode() {
|
|
24
36
|
return this.statusCode;
|
|
25
37
|
}
|
|
38
|
+
/*!
|
|
39
|
+
* List of all server related errors
|
|
40
|
+
*/
|
|
41
|
+
/** An unexpected server error occurred while processing the request */
|
|
26
42
|
static S001 = new ServerErrorCode('S001', ErrorType.SERVER_ERROR, 'An unexpected server error occurred while processing the request');
|
|
43
|
+
/** The requested endpoint does not exist */
|
|
27
44
|
static S002 = new ServerErrorCode('S002', ErrorType.NOT_FOUND, 'The requested endpoint does not exist');
|
|
45
|
+
/** The provided input data is invalid or does not meet validation requirements */
|
|
28
46
|
static S003 = new ServerErrorCode('S003', ErrorType.VALIDATION_ERROR, 'The provided input data is invalid or does not meet validation requirements');
|
|
47
|
+
/** Authentication credentials are required to access this resource */
|
|
29
48
|
static S004 = new ServerErrorCode('S004', ErrorType.UNAUTHENTICATED, 'Authentication credentials are required to access this resource');
|
|
49
|
+
/** Access denied due to insufficient permissions to perform this operation */
|
|
30
50
|
static S005 = new ServerErrorCode('S005', ErrorType.UNAUTHORIZED, 'Access denied due to insufficient permissions to perform this operation');
|
|
51
|
+
/** The request is malformed or contains invalid parameters */
|
|
31
52
|
static S006 = new ServerErrorCode('S006', ErrorType.CLIENT_ERROR, 'The request is malformed or contains invalid parameters');
|
|
53
|
+
/** Rate limit exceeded due to too many requests sent in a given time frame */
|
|
32
54
|
static S007 = new ServerErrorCode('S007', ErrorType.CLIENT_ERROR, 'Rate limit exceeded due to too many requests sent in a given time frame', 429);
|
|
55
|
+
/** Resource conflict as the requested operation conflicts with existing data */
|
|
33
56
|
static S008 = new ServerErrorCode('S008', ErrorType.CONFLICT, 'Resource conflict as the requested operation conflicts with existing data');
|
|
57
|
+
/** The requested resource could not be found */
|
|
34
58
|
static S009 = new ServerErrorCode('S009', ErrorType.NOT_FOUND, 'The requested resource could not be found');
|
|
35
59
|
}
|
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import { onRequestHookHandler } from 'fastify';
|
|
2
|
+
/**
|
|
3
|
+
* Importing user defined packages
|
|
4
|
+
*/
|
|
2
5
|
import { HttpRequest, HttpResponse } from '../interfaces/index.js';
|
|
6
|
+
/**
|
|
7
|
+
* Defining types
|
|
8
|
+
*/
|
|
3
9
|
type Key = string | symbol;
|
|
4
|
-
export
|
|
10
|
+
export interface ContextExtension {
|
|
11
|
+
}
|
|
12
|
+
export declare class ContextService implements ContextExtension {
|
|
5
13
|
static readonly name = "ContextService";
|
|
6
14
|
private readonly storage;
|
|
7
15
|
init(): onRequestHookHandler;
|
|
@@ -21,5 +29,6 @@ export declare class ContextService {
|
|
|
21
29
|
getResponse(throwOnMissing: false): HttpResponse | null;
|
|
22
30
|
getRID(): string;
|
|
23
31
|
getRID(throwOnMissing: false): string | null;
|
|
32
|
+
extend<T extends ContextExtension = ContextExtension>(extension: T & ThisType<this & T>): this;
|
|
24
33
|
}
|
|
25
34
|
export {};
|
|
@@ -4,9 +4,15 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
4
4
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
6
|
};
|
|
7
|
+
/**
|
|
8
|
+
* Importing npm packages
|
|
9
|
+
*/
|
|
7
10
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
8
11
|
import { Injectable } from '@shadow-library/app';
|
|
9
12
|
import { InternalError } from '@shadow-library/common';
|
|
13
|
+
/**
|
|
14
|
+
* Declaring the constants
|
|
15
|
+
*/
|
|
10
16
|
const REQUEST = Symbol('request');
|
|
11
17
|
const RESPONSE = Symbol('response');
|
|
12
18
|
const RID = Symbol('rid');
|
|
@@ -89,6 +95,9 @@ let ContextService = class ContextService {
|
|
|
89
95
|
getRID(throwOnMissing = true) {
|
|
90
96
|
return this.get(RID, throwOnMissing);
|
|
91
97
|
}
|
|
98
|
+
extend(extension) {
|
|
99
|
+
return Object.assign(this, extension);
|
|
100
|
+
}
|
|
92
101
|
};
|
|
93
102
|
ContextService = __decorate([
|
|
94
103
|
Injectable()
|
package/package.json
CHANGED