@shadow-library/fastify 0.0.4 → 0.0.6
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/cjs/classes/default-error-handler.js +3 -9
- package/cjs/constants.d.ts +1 -0
- package/cjs/constants.js +2 -1
- package/cjs/module/fastify-router.d.ts +2 -2
- package/cjs/module/fastify-router.js +4 -5
- package/cjs/module/fastify.module.js +3 -2
- package/cjs/services/context.service.d.ts +1 -1
- package/cjs/services/context.service.js +5 -5
- package/esm/classes/default-error-handler.js +3 -9
- package/esm/constants.d.ts +1 -0
- package/esm/constants.js +1 -0
- package/esm/module/fastify-router.d.ts +2 -2
- package/esm/module/fastify-router.js +6 -7
- package/esm/module/fastify.module.js +3 -2
- package/esm/services/context.service.d.ts +1 -1
- package/esm/services/context.service.js +4 -4
- package/package.json +7 -7
|
@@ -2,22 +2,16 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DefaultErrorHandler = void 0;
|
|
4
4
|
const common_1 = require("@shadow-library/common");
|
|
5
|
+
const constants_1 = require("../constants.js");
|
|
5
6
|
const server_error_1 = require("../server.error.js");
|
|
6
7
|
const unexpectedError = new server_error_1.ServerError(server_error_1.ServerErrorCode.S001);
|
|
7
8
|
const validationError = new server_error_1.ServerError(server_error_1.ServerErrorCode.S003);
|
|
8
9
|
class DefaultErrorHandler {
|
|
9
|
-
logger = common_1.Logger.getLogger(DefaultErrorHandler
|
|
10
|
+
logger = common_1.Logger.getLogger(constants_1.NAMESPACE, 'DefaultErrorHandler');
|
|
10
11
|
parseFastifyError(err) {
|
|
11
12
|
if (err.statusCode === 500)
|
|
12
13
|
return { statusCode: 500, error: unexpectedError.toObject() };
|
|
13
|
-
return {
|
|
14
|
-
statusCode: err.statusCode,
|
|
15
|
-
error: {
|
|
16
|
-
code: err.code,
|
|
17
|
-
type: common_1.ErrorType.CLIENT_ERROR,
|
|
18
|
-
message: err.message,
|
|
19
|
-
},
|
|
20
|
-
};
|
|
14
|
+
return { statusCode: err.statusCode, error: { code: err.code, type: common_1.ErrorType.CLIENT_ERROR, message: err.message } };
|
|
21
15
|
}
|
|
22
16
|
handle(err, _req, res) {
|
|
23
17
|
this.logger.warn('Handling error', err);
|
package/cjs/constants.d.ts
CHANGED
package/cjs/constants.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HTTP_CONTROLLER_INPUTS = exports.HTTP_CONTROLLER_TYPE = exports.FASTIFY_INSTANCE = exports.FASTIFY_CONFIG = exports.PARAMTYPES_METADATA = void 0;
|
|
3
|
+
exports.HTTP_CONTROLLER_INPUTS = exports.HTTP_CONTROLLER_TYPE = exports.FASTIFY_INSTANCE = exports.FASTIFY_CONFIG = exports.PARAMTYPES_METADATA = exports.NAMESPACE = void 0;
|
|
4
|
+
exports.NAMESPACE = '@shadow-library/fastify';
|
|
4
5
|
exports.PARAMTYPES_METADATA = 'design:paramtypes';
|
|
5
6
|
exports.FASTIFY_CONFIG = Symbol('fastify-config');
|
|
6
7
|
exports.FASTIFY_INSTANCE = Symbol('fastify-instance');
|
|
@@ -3,7 +3,7 @@ import { type FastifyInstance } from 'fastify';
|
|
|
3
3
|
import { Chain as MockRequestChain, InjectOptions as MockRequestOptions, Response as MockResponse } from 'light-my-request';
|
|
4
4
|
import { JsonObject } from 'type-fest';
|
|
5
5
|
import { HttpRequest, HttpResponse, ServerMetadata } from '../interfaces/index.js';
|
|
6
|
-
import {
|
|
6
|
+
import { ContextService } from '../services/index.js';
|
|
7
7
|
import { type FastifyConfig } from './fastify-module.interface.js';
|
|
8
8
|
declare module 'fastify' {
|
|
9
9
|
interface FastifyRequest {
|
|
@@ -40,7 +40,7 @@ export declare class FastifyRouter extends Router {
|
|
|
40
40
|
private readonly instance;
|
|
41
41
|
private readonly context;
|
|
42
42
|
private readonly logger;
|
|
43
|
-
constructor(config: FastifyConfig, instance: FastifyInstance, context:
|
|
43
|
+
constructor(config: FastifyConfig, instance: FastifyInstance, context: ContextService);
|
|
44
44
|
getInstance(): FastifyInstance;
|
|
45
45
|
private registerRawBody;
|
|
46
46
|
private getRequestLogger;
|
|
@@ -14,7 +14,6 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
14
14
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
15
15
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
16
16
|
};
|
|
17
|
-
var FastifyRouter_1;
|
|
18
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
18
|
exports.FastifyRouter = void 0;
|
|
20
19
|
const assert_1 = __importDefault(require("assert"));
|
|
@@ -25,11 +24,11 @@ const constants_1 = require("../constants.js");
|
|
|
25
24
|
const decorators_1 = require("../decorators/index.js");
|
|
26
25
|
const services_1 = require("../services/index.js");
|
|
27
26
|
const httpMethods = Object.values(decorators_1.HttpMethod).filter(m => m !== decorators_1.HttpMethod.ALL);
|
|
28
|
-
let FastifyRouter =
|
|
27
|
+
let FastifyRouter = class FastifyRouter extends app_1.Router {
|
|
29
28
|
config;
|
|
30
29
|
instance;
|
|
31
30
|
context;
|
|
32
|
-
logger = common_1.Logger.getLogger(
|
|
31
|
+
logger = common_1.Logger.getLogger(constants_1.NAMESPACE, 'FastifyRouter');
|
|
33
32
|
constructor(config, instance, context) {
|
|
34
33
|
super();
|
|
35
34
|
this.config = config;
|
|
@@ -209,9 +208,9 @@ let FastifyRouter = FastifyRouter_1 = class FastifyRouter extends app_1.Router {
|
|
|
209
208
|
}
|
|
210
209
|
};
|
|
211
210
|
exports.FastifyRouter = FastifyRouter;
|
|
212
|
-
exports.FastifyRouter = FastifyRouter =
|
|
211
|
+
exports.FastifyRouter = FastifyRouter = __decorate([
|
|
213
212
|
(0, app_1.Injectable)(),
|
|
214
213
|
__param(0, (0, app_1.Inject)(constants_1.FASTIFY_CONFIG)),
|
|
215
214
|
__param(1, (0, app_1.Inject)(constants_1.FASTIFY_INSTANCE)),
|
|
216
|
-
__metadata("design:paramtypes", [Object, Object, services_1.
|
|
215
|
+
__metadata("design:paramtypes", [Object, Object, services_1.ContextService])
|
|
217
216
|
], FastifyRouter);
|
|
@@ -7,6 +7,7 @@ const common_1 = require("@shadow-library/common");
|
|
|
7
7
|
const uuid_1 = require("uuid");
|
|
8
8
|
const classes_1 = require("../classes/index.js");
|
|
9
9
|
const constants_1 = require("../constants.js");
|
|
10
|
+
const services_1 = require("../services/index.js");
|
|
10
11
|
const error_response_dto_1 = require("./error-response.dto.js");
|
|
11
12
|
const fastify_router_1 = require("./fastify-router.js");
|
|
12
13
|
const fastify_utils_1 = require("./fastify.utils.js");
|
|
@@ -30,11 +31,11 @@ class FastifyModule {
|
|
|
30
31
|
}
|
|
31
32
|
static forRootAsync(options) {
|
|
32
33
|
const imports = options.imports ?? [];
|
|
33
|
-
const providers = [{ token: app_1.Router, useClass: fastify_router_1.FastifyRouter }];
|
|
34
|
+
const providers = [{ token: app_1.Router, useClass: fastify_router_1.FastifyRouter }, services_1.ContextService];
|
|
34
35
|
providers.push({ token: constants_1.FASTIFY_CONFIG, useFactory: options.useFactory, inject: options.inject });
|
|
35
36
|
const fastifyFactory = (config) => (0, fastify_utils_1.createFastifyInstance)(config, options.fastifyFactory);
|
|
36
37
|
providers.push({ token: constants_1.FASTIFY_INSTANCE, useFactory: fastifyFactory, inject: [constants_1.FASTIFY_CONFIG] });
|
|
37
|
-
(0, app_1.Module)({ imports, providers, exports: [app_1.Router] })(FastifyModule);
|
|
38
|
+
(0, app_1.Module)({ imports, providers, exports: [app_1.Router, services_1.ContextService] })(FastifyModule);
|
|
38
39
|
return FastifyModule;
|
|
39
40
|
}
|
|
40
41
|
}
|
|
@@ -6,14 +6,14 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
6
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
7
|
};
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.
|
|
9
|
+
exports.ContextService = void 0;
|
|
10
10
|
const async_hooks_1 = require("async_hooks");
|
|
11
11
|
const app_1 = require("@shadow-library/app");
|
|
12
12
|
const common_1 = require("@shadow-library/common");
|
|
13
13
|
const REQUEST = Symbol('request');
|
|
14
14
|
const RESPONSE = Symbol('response');
|
|
15
15
|
const RID = Symbol('rid');
|
|
16
|
-
let
|
|
16
|
+
let ContextService = class ContextService {
|
|
17
17
|
storage = new async_hooks_1.AsyncLocalStorage();
|
|
18
18
|
init() {
|
|
19
19
|
return async (req, res) => {
|
|
@@ -51,7 +51,7 @@ let Context = class Context {
|
|
|
51
51
|
return this.get(RID, true);
|
|
52
52
|
}
|
|
53
53
|
};
|
|
54
|
-
exports.
|
|
55
|
-
exports.
|
|
54
|
+
exports.ContextService = ContextService;
|
|
55
|
+
exports.ContextService = ContextService = __decorate([
|
|
56
56
|
(0, app_1.Injectable)()
|
|
57
|
-
],
|
|
57
|
+
], ContextService);
|
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
import { AppError, ErrorType, Logger, ValidationError } from '@shadow-library/common';
|
|
2
|
+
import { NAMESPACE } from '../constants.js';
|
|
2
3
|
import { ServerError, ServerErrorCode } from '../server.error.js';
|
|
3
4
|
const unexpectedError = new ServerError(ServerErrorCode.S001);
|
|
4
5
|
const validationError = new ServerError(ServerErrorCode.S003);
|
|
5
6
|
export class DefaultErrorHandler {
|
|
6
|
-
logger = Logger.getLogger(DefaultErrorHandler
|
|
7
|
+
logger = Logger.getLogger(NAMESPACE, 'DefaultErrorHandler');
|
|
7
8
|
parseFastifyError(err) {
|
|
8
9
|
if (err.statusCode === 500)
|
|
9
10
|
return { statusCode: 500, error: unexpectedError.toObject() };
|
|
10
|
-
return {
|
|
11
|
-
statusCode: err.statusCode,
|
|
12
|
-
error: {
|
|
13
|
-
code: err.code,
|
|
14
|
-
type: ErrorType.CLIENT_ERROR,
|
|
15
|
-
message: err.message,
|
|
16
|
-
},
|
|
17
|
-
};
|
|
11
|
+
return { statusCode: err.statusCode, error: { code: err.code, type: ErrorType.CLIENT_ERROR, message: err.message } };
|
|
18
12
|
}
|
|
19
13
|
handle(err, _req, res) {
|
|
20
14
|
this.logger.warn('Handling error', err);
|
package/esm/constants.d.ts
CHANGED
package/esm/constants.js
CHANGED
|
@@ -3,7 +3,7 @@ import { type FastifyInstance } from 'fastify';
|
|
|
3
3
|
import { Chain as MockRequestChain, InjectOptions as MockRequestOptions, Response as MockResponse } from 'light-my-request';
|
|
4
4
|
import { JsonObject } from 'type-fest';
|
|
5
5
|
import { HttpRequest, HttpResponse, ServerMetadata } from '../interfaces/index.js';
|
|
6
|
-
import {
|
|
6
|
+
import { ContextService } from '../services/index.js';
|
|
7
7
|
import { type FastifyConfig } from './fastify-module.interface.js';
|
|
8
8
|
declare module 'fastify' {
|
|
9
9
|
interface FastifyRequest {
|
|
@@ -40,7 +40,7 @@ export declare class FastifyRouter extends Router {
|
|
|
40
40
|
private readonly instance;
|
|
41
41
|
private readonly context;
|
|
42
42
|
private readonly logger;
|
|
43
|
-
constructor(config: FastifyConfig, instance: FastifyInstance, context:
|
|
43
|
+
constructor(config: FastifyConfig, instance: FastifyInstance, context: ContextService);
|
|
44
44
|
getInstance(): FastifyInstance;
|
|
45
45
|
private registerRawBody;
|
|
46
46
|
private getRequestLogger;
|
|
@@ -10,20 +10,19 @@ 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
|
-
var FastifyRouter_1;
|
|
14
13
|
import assert from 'assert';
|
|
15
14
|
import { Inject, Injectable, Router } from '@shadow-library/app';
|
|
16
15
|
import { InternalError, Logger, utils } from '@shadow-library/common';
|
|
17
16
|
import merge from 'deepmerge';
|
|
18
|
-
import { FASTIFY_CONFIG, FASTIFY_INSTANCE, HTTP_CONTROLLER_INPUTS, HTTP_CONTROLLER_TYPE } from '../constants.js';
|
|
17
|
+
import { FASTIFY_CONFIG, FASTIFY_INSTANCE, HTTP_CONTROLLER_INPUTS, HTTP_CONTROLLER_TYPE, NAMESPACE } from '../constants.js';
|
|
19
18
|
import { HttpMethod } from '../decorators/index.js';
|
|
20
|
-
import {
|
|
19
|
+
import { ContextService } from '../services/index.js';
|
|
21
20
|
const httpMethods = Object.values(HttpMethod).filter(m => m !== HttpMethod.ALL);
|
|
22
|
-
let FastifyRouter =
|
|
21
|
+
let FastifyRouter = class FastifyRouter extends Router {
|
|
23
22
|
config;
|
|
24
23
|
instance;
|
|
25
24
|
context;
|
|
26
|
-
logger = Logger.getLogger(
|
|
25
|
+
logger = Logger.getLogger(NAMESPACE, 'FastifyRouter');
|
|
27
26
|
constructor(config, instance, context) {
|
|
28
27
|
super();
|
|
29
28
|
this.config = config;
|
|
@@ -202,10 +201,10 @@ let FastifyRouter = FastifyRouter_1 = class FastifyRouter extends Router {
|
|
|
202
201
|
return options ? this.instance.inject(options) : this.instance.inject();
|
|
203
202
|
}
|
|
204
203
|
};
|
|
205
|
-
FastifyRouter =
|
|
204
|
+
FastifyRouter = __decorate([
|
|
206
205
|
Injectable(),
|
|
207
206
|
__param(0, Inject(FASTIFY_CONFIG)),
|
|
208
207
|
__param(1, Inject(FASTIFY_INSTANCE)),
|
|
209
|
-
__metadata("design:paramtypes", [Object, Object,
|
|
208
|
+
__metadata("design:paramtypes", [Object, Object, ContextService])
|
|
210
209
|
], FastifyRouter);
|
|
211
210
|
export { FastifyRouter };
|
|
@@ -4,6 +4,7 @@ import { utils } from '@shadow-library/common';
|
|
|
4
4
|
import { v4 as uuid } from 'uuid';
|
|
5
5
|
import { DefaultErrorHandler } from '../classes/index.js';
|
|
6
6
|
import { FASTIFY_CONFIG, FASTIFY_INSTANCE } from '../constants.js';
|
|
7
|
+
import { ContextService } from '../services/index.js';
|
|
7
8
|
import { ErrorResponseDto } from './error-response.dto.js';
|
|
8
9
|
import { FastifyRouter } from './fastify-router.js';
|
|
9
10
|
import { createFastifyInstance } from './fastify.utils.js';
|
|
@@ -27,11 +28,11 @@ export class FastifyModule {
|
|
|
27
28
|
}
|
|
28
29
|
static forRootAsync(options) {
|
|
29
30
|
const imports = options.imports ?? [];
|
|
30
|
-
const providers = [{ token: Router, useClass: FastifyRouter }];
|
|
31
|
+
const providers = [{ token: Router, useClass: FastifyRouter }, ContextService];
|
|
31
32
|
providers.push({ token: FASTIFY_CONFIG, useFactory: options.useFactory, inject: options.inject });
|
|
32
33
|
const fastifyFactory = (config) => createFastifyInstance(config, options.fastifyFactory);
|
|
33
34
|
providers.push({ token: FASTIFY_INSTANCE, useFactory: fastifyFactory, inject: [FASTIFY_CONFIG] });
|
|
34
|
-
Module({ imports, providers, exports: [Router] })(FastifyModule);
|
|
35
|
+
Module({ imports, providers, exports: [Router, ContextService] })(FastifyModule);
|
|
35
36
|
return FastifyModule;
|
|
36
37
|
}
|
|
37
38
|
}
|
|
@@ -10,7 +10,7 @@ import { InternalError } from '@shadow-library/common';
|
|
|
10
10
|
const REQUEST = Symbol('request');
|
|
11
11
|
const RESPONSE = Symbol('response');
|
|
12
12
|
const RID = Symbol('rid');
|
|
13
|
-
let
|
|
13
|
+
let ContextService = class ContextService {
|
|
14
14
|
storage = new AsyncLocalStorage();
|
|
15
15
|
init() {
|
|
16
16
|
return async (req, res) => {
|
|
@@ -48,7 +48,7 @@ let Context = class Context {
|
|
|
48
48
|
return this.get(RID, true);
|
|
49
49
|
}
|
|
50
50
|
};
|
|
51
|
-
|
|
51
|
+
ContextService = __decorate([
|
|
52
52
|
Injectable()
|
|
53
|
-
],
|
|
54
|
-
export {
|
|
53
|
+
], ContextService);
|
|
54
|
+
export { ContextService };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shadow-library/fastify",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.6",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "A Fastify wrapper featuring decorator-based routing, middleware and error handling",
|
|
7
7
|
"repository": {
|
|
@@ -16,18 +16,18 @@
|
|
|
16
16
|
"homepage": "https://github.com/shadow-library/fastify#readme",
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@fastify/middie": "^9.0.3",
|
|
19
|
-
"@shadow-library/class-schema": "^0.0.5",
|
|
20
19
|
"ajv": "^8.17.1",
|
|
21
20
|
"deepmerge": "^4.3.1",
|
|
22
|
-
"fastify": "^5.
|
|
21
|
+
"fastify": "^5.3.3",
|
|
23
22
|
"jest": "^29.7.0",
|
|
24
|
-
"type-fest": "^4.
|
|
25
|
-
"uuid": "^11.0
|
|
23
|
+
"type-fest": "^4.41.0",
|
|
24
|
+
"uuid": "^11.1.0"
|
|
26
25
|
},
|
|
27
26
|
"peerDependencies": {
|
|
28
27
|
"@fastify/view": "^10.0.0",
|
|
29
|
-
"@shadow-library/app": "^1.0.
|
|
30
|
-
"@shadow-library/
|
|
28
|
+
"@shadow-library/app": "^1.0.7",
|
|
29
|
+
"@shadow-library/class-schema": "^0.0.5",
|
|
30
|
+
"@shadow-library/common": "^1.0.13",
|
|
31
31
|
"reflect-metadata": "^0.2.2"
|
|
32
32
|
},
|
|
33
33
|
"peerDependenciesMeta": {
|