equipped 5.0.0-beta.9 → 5.0.0-rc.2
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/CHANGELOG.md +156 -0
- package/lib/bull/index.js +33 -44
- package/lib/cache/cache.d.ts +1 -0
- package/lib/cache/types/redis-cache.d.ts +1 -0
- package/lib/cache/types/redis-cache.js +8 -0
- package/lib/db/_instance.d.ts +2 -2
- package/lib/db/_instance.js +9 -25
- package/lib/db/mongoose/changes.d.ts +1 -26
- package/lib/db/mongoose/changes.js +7 -20
- package/lib/db/mongoose/index.d.ts +2 -27
- package/lib/db/mongoose/index.js +8 -24
- package/lib/db/mongoose/query.d.ts +0 -25
- package/lib/errors/customError.js +2 -0
- package/lib/errors/types/accessTokenExpired.js +1 -1
- package/lib/errors/types/badRequestError.js +1 -1
- package/lib/errors/types/notAuthenticatedError.js +1 -1
- package/lib/errors/types/notAuthorizedError.js +1 -1
- package/lib/errors/types/notFoundError.js +1 -1
- package/lib/errors/types/refreshTokenMisusedError.js +1 -1
- package/lib/errors/types/validationError.js +1 -1
- package/lib/events/index.js +1 -3
- package/lib/events/kafka.d.ts +1 -1
- package/lib/events/kafka.js +19 -25
- package/lib/events/rabbit.d.ts +1 -1
- package/lib/events/rabbit.js +9 -22
- package/lib/instance/index.d.ts +2 -1
- package/lib/instance/index.js +28 -41
- package/lib/instance/settings.d.ts +2 -2
- package/lib/instance/settings.js +4 -4
- package/lib/listeners/emitter.d.ts +6 -3
- package/lib/listeners/emitter.js +97 -109
- package/lib/scripts/json-schema.d.ts +2 -1
- package/lib/scripts/json-schema.js +29 -22
- package/lib/server/impls/base.d.ts +18 -7
- package/lib/server/impls/base.js +177 -80
- package/lib/server/impls/express.d.ts +0 -1
- package/lib/server/impls/express.js +16 -34
- package/lib/server/impls/fastify.d.ts +2 -4
- package/lib/server/impls/fastify.js +21 -39
- package/lib/server/middlewares/errorHandler.d.ts +2 -2
- package/lib/server/middlewares/notFoundHandler.d.ts +2 -2
- package/lib/server/middlewares/parseAuthUser.d.ts +2 -2
- package/lib/server/middlewares/requireAuthUser.d.ts +2 -2
- package/lib/server/middlewares/requireAuthUser.js +2 -2
- package/lib/server/middlewares/requireRefreshUser.d.ts +2 -2
- package/lib/server/middlewares/requireRefreshUser.js +2 -2
- package/lib/server/requests.d.ts +10 -7
- package/lib/server/requests.js +27 -21
- package/lib/server/routes.d.ts +4 -2
- package/lib/server/routes.js +33 -33
- package/lib/server/types.d.ts +19 -9
- package/lib/server/types.js +3 -0
- package/lib/storage/index.d.ts +0 -1
- package/lib/structure/baseEntity.js +7 -9
- package/lib/types/index.d.ts +2 -2
- package/lib/utils/auth.d.ts +5 -5
- package/lib/utils/authUser.d.ts +3 -2
- package/lib/utils/media.d.ts +0 -1
- package/lib/utils/utils.d.ts +1 -0
- package/lib/utils/utils.js +9 -0
- package/lib/validations/index.d.ts +79 -85
- package/lib/validations/index.js +4 -4
- package/package.json +24 -25
|
@@ -4,9 +4,9 @@ exports.AccessTokenExpired = void 0;
|
|
|
4
4
|
const server_1 = require("../../server");
|
|
5
5
|
const customError_1 = require("../customError");
|
|
6
6
|
class AccessTokenExpired extends customError_1.CustomError {
|
|
7
|
+
statusCode = server_1.StatusCodes.AccessTokenExpired;
|
|
7
8
|
constructor(message = 'Access token expired') {
|
|
8
9
|
super(message, [{ message }]);
|
|
9
|
-
this.statusCode = server_1.StatusCodes.AccessTokenExpired;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
exports.AccessTokenExpired = AccessTokenExpired;
|
|
@@ -4,9 +4,9 @@ exports.BadRequestError = void 0;
|
|
|
4
4
|
const server_1 = require("../../server");
|
|
5
5
|
const customError_1 = require("../customError");
|
|
6
6
|
class BadRequestError extends customError_1.CustomError {
|
|
7
|
+
statusCode = server_1.StatusCodes.BadRequest;
|
|
7
8
|
constructor(message) {
|
|
8
9
|
super(message, [{ message }]);
|
|
9
|
-
this.statusCode = server_1.StatusCodes.BadRequest;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
exports.BadRequestError = BadRequestError;
|
|
@@ -4,9 +4,9 @@ exports.NotAuthenticatedError = void 0;
|
|
|
4
4
|
const server_1 = require("../../server");
|
|
5
5
|
const customError_1 = require("../customError");
|
|
6
6
|
class NotAuthenticatedError extends customError_1.CustomError {
|
|
7
|
+
statusCode = server_1.StatusCodes.NotAuthenticated;
|
|
7
8
|
constructor(message = 'Not authenticated') {
|
|
8
9
|
super(message, [{ message }]);
|
|
9
|
-
this.statusCode = server_1.StatusCodes.NotAuthenticated;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
exports.NotAuthenticatedError = NotAuthenticatedError;
|
|
@@ -4,9 +4,9 @@ exports.NotAuthorizedError = void 0;
|
|
|
4
4
|
const server_1 = require("../../server");
|
|
5
5
|
const customError_1 = require("../customError");
|
|
6
6
|
class NotAuthorizedError extends customError_1.CustomError {
|
|
7
|
+
statusCode = server_1.StatusCodes.NotAuthorized;
|
|
7
8
|
constructor(message = 'Not authorized') {
|
|
8
9
|
super(message, [{ message }]);
|
|
9
|
-
this.statusCode = server_1.StatusCodes.NotAuthorized;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
exports.NotAuthorizedError = NotAuthorizedError;
|
|
@@ -4,9 +4,9 @@ exports.NotFoundError = void 0;
|
|
|
4
4
|
const server_1 = require("../../server");
|
|
5
5
|
const customError_1 = require("../customError");
|
|
6
6
|
class NotFoundError extends customError_1.CustomError {
|
|
7
|
+
statusCode = server_1.StatusCodes.NotFound;
|
|
7
8
|
constructor(message = 'Not found') {
|
|
8
9
|
super(message, [{ message }]);
|
|
9
|
-
this.statusCode = server_1.StatusCodes.NotFound;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
exports.NotFoundError = NotFoundError;
|
|
@@ -4,9 +4,9 @@ exports.RefreshTokenMisusedError = void 0;
|
|
|
4
4
|
const server_1 = require("../../server");
|
|
5
5
|
const customError_1 = require("../customError");
|
|
6
6
|
class RefreshTokenMisusedError extends customError_1.CustomError {
|
|
7
|
+
statusCode = server_1.StatusCodes.NotAuthenticated;
|
|
7
8
|
constructor(message = 'Refresh token misused') {
|
|
8
9
|
super(message, [{ message }]);
|
|
9
|
-
this.statusCode = server_1.StatusCodes.NotAuthenticated;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
exports.RefreshTokenMisusedError = RefreshTokenMisusedError;
|
|
@@ -4,9 +4,9 @@ exports.ValidationError = void 0;
|
|
|
4
4
|
const server_1 = require("../../server");
|
|
5
5
|
const customError_1 = require("../customError");
|
|
6
6
|
class ValidationError extends customError_1.CustomError {
|
|
7
|
+
statusCode = server_1.StatusCodes.ValidationError;
|
|
7
8
|
constructor(errors) {
|
|
8
9
|
super('Invalid request parameters', errors.map((e) => ({ field: e.field, message: e.messages.join('\n') })));
|
|
9
|
-
this.statusCode = server_1.StatusCodes.ValidationError;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
exports.ValidationError = ValidationError;
|
package/lib/events/index.js
CHANGED
|
@@ -2,9 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DefaultSubscribeOptions = exports.EventBus = void 0;
|
|
4
4
|
class EventBus {
|
|
5
|
-
|
|
6
|
-
this._subscribers = [];
|
|
7
|
-
}
|
|
5
|
+
_subscribers = [];
|
|
8
6
|
async startSubscribers() {
|
|
9
7
|
return Promise.all(this._subscribers.map((sub) => sub()));
|
|
10
8
|
}
|
package/lib/events/kafka.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export declare class KafkaEventBus extends EventBus {
|
|
|
3
3
|
#private;
|
|
4
4
|
constructor();
|
|
5
5
|
createPublisher<Event extends Events[keyof Events]>(topic: Event['topic']): {
|
|
6
|
-
publish: (data: Event[
|
|
6
|
+
publish: (data: Event["data"]) => Promise<boolean>;
|
|
7
7
|
};
|
|
8
8
|
createSubscriber<Event extends Events[keyof Events]>(topic: Event['topic'], onMessage: (data: Event['data']) => Promise<void>, options?: Partial<SubscribeOptions>): {
|
|
9
9
|
subscribe: () => Promise<void>;
|
package/lib/events/kafka.js
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
-
};
|
|
7
|
-
var _KafkaEventBus_instances, _KafkaEventBus_client, _KafkaEventBus_createTopic, _KafkaEventBus_deleteGroup;
|
|
8
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
3
|
exports.KafkaEventBus = void 0;
|
|
10
4
|
const kafkajs_1 = require("kafkajs");
|
|
@@ -14,19 +8,18 @@ const instance_1 = require("../instance");
|
|
|
14
8
|
const json_1 = require("../utils/json");
|
|
15
9
|
const utils_1 = require("../utils/utils");
|
|
16
10
|
class KafkaEventBus extends _1.EventBus {
|
|
11
|
+
#client = new kafkajs_1.Kafka({
|
|
12
|
+
clientId: instance_1.Instance.get().settings.eventColumnName,
|
|
13
|
+
brokers: instance_1.Instance.get().settings.kafkaURIs,
|
|
14
|
+
logLevel: kafkajs_1.logLevel.NOTHING
|
|
15
|
+
});
|
|
17
16
|
constructor() {
|
|
18
17
|
super();
|
|
19
|
-
_KafkaEventBus_instances.add(this);
|
|
20
|
-
_KafkaEventBus_client.set(this, new kafkajs_1.Kafka({
|
|
21
|
-
clientId: instance_1.Instance.get().settings.eventColumnName,
|
|
22
|
-
brokers: instance_1.Instance.get().settings.kafkaURIs,
|
|
23
|
-
logLevel: kafkajs_1.logLevel.NOTHING
|
|
24
|
-
}));
|
|
25
18
|
}
|
|
26
19
|
createPublisher(topic) {
|
|
27
20
|
const publish = async (data) => {
|
|
28
21
|
try {
|
|
29
|
-
const producer =
|
|
22
|
+
const producer = this.#client.producer();
|
|
30
23
|
await producer.connect();
|
|
31
24
|
await producer.send({
|
|
32
25
|
topic,
|
|
@@ -47,11 +40,11 @@ class KafkaEventBus extends _1.EventBus {
|
|
|
47
40
|
if (started)
|
|
48
41
|
return;
|
|
49
42
|
started = true;
|
|
50
|
-
await
|
|
43
|
+
await this.#createTopic(topic);
|
|
51
44
|
const groupId = options.fanout
|
|
52
45
|
? `${instance_1.Instance.get().settings.appId}-fanout-${utils_1.Random.string(10)}`
|
|
53
46
|
: `${instance_1.Instance.get().settings.appId}-${topic}`;
|
|
54
|
-
const consumer =
|
|
47
|
+
const consumer = this.#client.consumer({ groupId });
|
|
55
48
|
await consumer.connect();
|
|
56
49
|
await consumer.subscribe({ topic });
|
|
57
50
|
await consumer.run({
|
|
@@ -66,20 +59,21 @@ class KafkaEventBus extends _1.EventBus {
|
|
|
66
59
|
if (options.fanout)
|
|
67
60
|
(0, exit_1.addWaitBeforeExit)(async () => {
|
|
68
61
|
await consumer.disconnect();
|
|
69
|
-
await
|
|
62
|
+
await this.#deleteGroup(groupId);
|
|
70
63
|
});
|
|
71
64
|
};
|
|
72
65
|
this._subscribers.push(subscribe);
|
|
73
66
|
return { subscribe };
|
|
74
67
|
}
|
|
68
|
+
async #createTopic(topic) {
|
|
69
|
+
const admin = this.#client.admin();
|
|
70
|
+
await admin.createTopics({
|
|
71
|
+
topics: [{ topic, numPartitions: 5 }],
|
|
72
|
+
}).catch();
|
|
73
|
+
}
|
|
74
|
+
async #deleteGroup(groupId) {
|
|
75
|
+
const admin = this.#client.admin();
|
|
76
|
+
await admin.deleteGroups([groupId]).catch();
|
|
77
|
+
}
|
|
75
78
|
}
|
|
76
79
|
exports.KafkaEventBus = KafkaEventBus;
|
|
77
|
-
_KafkaEventBus_client = new WeakMap(), _KafkaEventBus_instances = new WeakSet(), _KafkaEventBus_createTopic = async function _KafkaEventBus_createTopic(topic) {
|
|
78
|
-
const admin = __classPrivateFieldGet(this, _KafkaEventBus_client, "f").admin();
|
|
79
|
-
await admin.createTopics({
|
|
80
|
-
topics: [{ topic, numPartitions: 5 }],
|
|
81
|
-
}).catch();
|
|
82
|
-
}, _KafkaEventBus_deleteGroup = async function _KafkaEventBus_deleteGroup(groupId) {
|
|
83
|
-
const admin = __classPrivateFieldGet(this, _KafkaEventBus_client, "f").admin();
|
|
84
|
-
await admin.deleteGroups([groupId]).catch();
|
|
85
|
-
};
|
package/lib/events/rabbit.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export declare class RabbitEventBus extends EventBus {
|
|
|
3
3
|
#private;
|
|
4
4
|
constructor();
|
|
5
5
|
createPublisher<Event extends Events[keyof Events]>(topic: Event['topic']): {
|
|
6
|
-
publish: (data: Event[
|
|
6
|
+
publish: (data: Event["data"]) => Promise<boolean>;
|
|
7
7
|
};
|
|
8
8
|
createSubscriber<Event extends Events[keyof Events]>(topic: Event['topic'], onMessage: (data: Event['data']) => Promise<void>, options?: Partial<SubscribeOptions>): {
|
|
9
9
|
subscribe: () => Promise<void>;
|
package/lib/events/rabbit.js
CHANGED
|
@@ -1,44 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
8
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
11
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
|
-
};
|
|
13
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
4
|
};
|
|
16
|
-
var _RabbitEventBus_client, _RabbitEventBus_columnName;
|
|
17
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
6
|
exports.RabbitEventBus = void 0;
|
|
19
7
|
const amqp_connection_manager_1 = __importDefault(require("amqp-connection-manager"));
|
|
20
|
-
const exit_1 = require("
|
|
8
|
+
const exit_1 = require("../exit");
|
|
21
9
|
const _1 = require(".");
|
|
22
10
|
const instance_1 = require("../instance");
|
|
23
11
|
const json_1 = require("../utils/json");
|
|
24
12
|
const utils_1 = require("../utils/utils");
|
|
25
13
|
class RabbitEventBus extends _1.EventBus {
|
|
14
|
+
#client;
|
|
15
|
+
#columnName = instance_1.Instance.get().settings.eventColumnName;
|
|
26
16
|
constructor() {
|
|
27
17
|
super();
|
|
28
|
-
|
|
29
|
-
_RabbitEventBus_columnName.set(this, instance_1.Instance.get().settings.eventColumnName);
|
|
30
|
-
__classPrivateFieldSet(this, _RabbitEventBus_client, amqp_connection_manager_1.default.connect([instance_1.Instance.get().settings.rabbitURI])
|
|
18
|
+
this.#client = amqp_connection_manager_1.default.connect([instance_1.Instance.get().settings.rabbitURI])
|
|
31
19
|
.createChannel({
|
|
32
20
|
json: false,
|
|
33
21
|
setup: async (channel) => {
|
|
34
|
-
await channel.assertExchange(
|
|
22
|
+
await channel.assertExchange(this.#columnName, 'direct', { durable: true });
|
|
35
23
|
await channel.prefetch(1);
|
|
36
24
|
},
|
|
37
|
-
})
|
|
25
|
+
});
|
|
38
26
|
}
|
|
39
27
|
createPublisher(topic) {
|
|
40
28
|
const publish = async (data) => {
|
|
41
|
-
return await
|
|
29
|
+
return await this.#client.publish(this.#columnName, topic, JSON.stringify(data), { persistent: true });
|
|
42
30
|
};
|
|
43
31
|
return { publish };
|
|
44
32
|
}
|
|
@@ -49,12 +37,12 @@ class RabbitEventBus extends _1.EventBus {
|
|
|
49
37
|
if (started)
|
|
50
38
|
return;
|
|
51
39
|
started = true;
|
|
52
|
-
await
|
|
40
|
+
await this.#client.addSetup(async (channel) => {
|
|
53
41
|
const queueName = options.fanout
|
|
54
42
|
? `${instance_1.Instance.get().settings.appId}-fanout-${utils_1.Random.string(10)}`
|
|
55
43
|
: `${instance_1.Instance.get().settings.appId}-${topic}`;
|
|
56
44
|
const { queue } = await channel.assertQueue(queueName, { durable: !options.fanout, exclusive: options.fanout });
|
|
57
|
-
await channel.bindQueue(queue,
|
|
45
|
+
await channel.bindQueue(queue, this.#columnName, topic);
|
|
58
46
|
channel.consume(queue, async (msg) => {
|
|
59
47
|
(0, exit_1.addWaitBeforeExit)((async () => {
|
|
60
48
|
if (!msg)
|
|
@@ -75,4 +63,3 @@ class RabbitEventBus extends _1.EventBus {
|
|
|
75
63
|
}
|
|
76
64
|
}
|
|
77
65
|
exports.RabbitEventBus = RabbitEventBus;
|
|
78
|
-
_RabbitEventBus_client = new WeakMap(), _RabbitEventBus_columnName = new WeakMap();
|
package/lib/instance/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import pino from 'pino';
|
|
2
2
|
import { BullJob } from '../bull';
|
|
3
3
|
import { Cache } from '../cache/cache';
|
|
4
|
+
import { RedisCache } from '../cache/types/redis-cache';
|
|
4
5
|
import { MongoDb } from '../db/mongoose';
|
|
5
6
|
import { EventBus } from '../events/';
|
|
6
7
|
import { Server } from '../server';
|
|
@@ -10,7 +11,7 @@ export declare class Instance {
|
|
|
10
11
|
private constructor();
|
|
11
12
|
get logger(): pino.Logger<any>;
|
|
12
13
|
get job(): BullJob;
|
|
13
|
-
get cache(): Cache;
|
|
14
|
+
get cache(): Cache | RedisCache;
|
|
14
15
|
get eventBus(): EventBus;
|
|
15
16
|
get server(): Server<any, any>;
|
|
16
17
|
get dbs(): {
|
package/lib/instance/index.js
CHANGED
|
@@ -1,19 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
8
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
11
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
|
-
};
|
|
13
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
4
|
};
|
|
16
|
-
var _a, _Instance_initialized, _Instance_instance, _Instance_settings, _Instance_logger, _Instance_job, _Instance_cache, _Instance_eventBus, _Instance_server, _Instance_dbs;
|
|
17
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
6
|
exports.Instance = void 0;
|
|
19
7
|
const pino_1 = __importDefault(require("pino"));
|
|
@@ -25,43 +13,45 @@ const exit_1 = require("../exit");
|
|
|
25
13
|
const server_1 = require("../server");
|
|
26
14
|
const settings_1 = require("./settings");
|
|
27
15
|
class Instance {
|
|
16
|
+
static #initialized = false;
|
|
17
|
+
static #instance;
|
|
18
|
+
#settings = { ...settings_1.defaulInstanceSetting };
|
|
19
|
+
#logger = null;
|
|
20
|
+
#job = null;
|
|
21
|
+
#cache = null;
|
|
22
|
+
#eventBus = null;
|
|
23
|
+
#server = null;
|
|
24
|
+
#dbs = null;
|
|
28
25
|
constructor() {
|
|
29
|
-
_Instance_settings.set(this, { ...settings_1.defaulInstanceSetting });
|
|
30
|
-
_Instance_logger.set(this, null);
|
|
31
|
-
_Instance_job.set(this, null);
|
|
32
|
-
_Instance_cache.set(this, null);
|
|
33
|
-
_Instance_eventBus.set(this, null);
|
|
34
|
-
_Instance_server.set(this, null);
|
|
35
|
-
_Instance_dbs.set(this, null);
|
|
36
26
|
}
|
|
37
27
|
get logger() {
|
|
38
|
-
return
|
|
28
|
+
return this.#logger ||= Instance.createLogger();
|
|
39
29
|
}
|
|
40
30
|
get job() {
|
|
41
|
-
return
|
|
31
|
+
return this.#job ||= new bull_1.BullJob();
|
|
42
32
|
}
|
|
43
33
|
get cache() {
|
|
44
|
-
return
|
|
34
|
+
return this.#cache ||= new redis_cache_1.RedisCache();
|
|
45
35
|
}
|
|
46
36
|
get eventBus() {
|
|
47
|
-
return
|
|
37
|
+
return this.#eventBus ||= new kafka_1.KafkaEventBus();
|
|
48
38
|
}
|
|
49
39
|
get server() {
|
|
50
|
-
return
|
|
40
|
+
return this.#server ||= server_1.serverTypes[this.settings.server]();
|
|
51
41
|
}
|
|
52
42
|
get dbs() {
|
|
53
|
-
return
|
|
43
|
+
return this.#dbs ||= { mongo: new mongoose_1.MongoDb() };
|
|
54
44
|
}
|
|
55
45
|
get listener() {
|
|
56
46
|
return this.server.listener;
|
|
57
47
|
}
|
|
58
48
|
get settings() {
|
|
59
|
-
return
|
|
49
|
+
return this.#settings;
|
|
60
50
|
}
|
|
61
51
|
static createLogger() {
|
|
62
52
|
const defaultLogLevel = 'info';
|
|
63
53
|
return (0, pino_1.default)({
|
|
64
|
-
level:
|
|
54
|
+
level: Instance.#initialized ? Instance.get().settings?.logLevel ?? defaultLogLevel : defaultLogLevel,
|
|
65
55
|
serializers: {
|
|
66
56
|
err: pino_1.default.stdSerializers.err,
|
|
67
57
|
req: pino_1.default.stdSerializers.req,
|
|
@@ -70,30 +60,30 @@ class Instance {
|
|
|
70
60
|
});
|
|
71
61
|
}
|
|
72
62
|
static initialize(settings) {
|
|
73
|
-
|
|
74
|
-
const instanceSettings =
|
|
63
|
+
Instance.#initialized = true;
|
|
64
|
+
const instanceSettings = Instance.get().settings;
|
|
75
65
|
Object.entries(settings).forEach(([key, value]) => {
|
|
76
66
|
instanceSettings[key] = value;
|
|
77
67
|
});
|
|
78
68
|
}
|
|
79
69
|
static get() {
|
|
80
|
-
if (!
|
|
70
|
+
if (!this.#initialized)
|
|
81
71
|
return (0, exit_1.exit)('Has not been initialized. Make sure initialize is called before you get an instance');
|
|
82
|
-
if (!
|
|
83
|
-
|
|
84
|
-
return
|
|
72
|
+
if (!Instance.#instance)
|
|
73
|
+
Instance.#instance = new Instance();
|
|
74
|
+
return Instance.#instance;
|
|
85
75
|
}
|
|
86
76
|
async startConnections() {
|
|
87
77
|
try {
|
|
88
|
-
await
|
|
89
|
-
await
|
|
90
|
-
await Promise.all(Object.values(
|
|
78
|
+
await Instance.get().cache.start();
|
|
79
|
+
await Instance.get().listener.start();
|
|
80
|
+
await Promise.all(Object.values(Instance.get().dbs).map(async (db) => {
|
|
91
81
|
await db.start();
|
|
92
82
|
await db.startAllDbChanges();
|
|
93
83
|
(0, exit_1.addWaitBeforeExit)(db.close);
|
|
94
84
|
}));
|
|
95
|
-
await
|
|
96
|
-
(0, exit_1.addWaitBeforeExit)(
|
|
85
|
+
await Instance.get().eventBus.startSubscribers();
|
|
86
|
+
(0, exit_1.addWaitBeforeExit)(Instance.get().cache.close);
|
|
97
87
|
}
|
|
98
88
|
catch (error) {
|
|
99
89
|
(0, exit_1.exit)(`Error starting connections: ${error}`);
|
|
@@ -101,6 +91,3 @@ class Instance {
|
|
|
101
91
|
}
|
|
102
92
|
}
|
|
103
93
|
exports.Instance = Instance;
|
|
104
|
-
_a = Instance, _Instance_settings = new WeakMap(), _Instance_logger = new WeakMap(), _Instance_job = new WeakMap(), _Instance_cache = new WeakMap(), _Instance_eventBus = new WeakMap(), _Instance_server = new WeakMap(), _Instance_dbs = new WeakMap();
|
|
105
|
-
_Instance_initialized = { value: false };
|
|
106
|
-
_Instance_instance = { value: void 0 };
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Level } from 'pino';
|
|
2
2
|
import type { ServerTypes } from '../server';
|
|
3
3
|
export type Settings = {
|
|
4
|
-
isDev: boolean;
|
|
5
4
|
accessTokenKey: string;
|
|
6
5
|
accessTokenTTL: number;
|
|
7
6
|
refreshTokenKey: string;
|
|
@@ -26,7 +25,8 @@ export type Settings = {
|
|
|
26
25
|
paginationDefaultLimit: number;
|
|
27
26
|
server: ServerTypes;
|
|
28
27
|
openapiDocsVersion: string;
|
|
29
|
-
|
|
28
|
+
openapiDocsBaseUrl: string[];
|
|
29
|
+
openapiDocsPath: string;
|
|
30
30
|
logLevel: Level;
|
|
31
31
|
logRequests: boolean;
|
|
32
32
|
requestSchemaValidation: boolean;
|
package/lib/instance/settings.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.defaulInstanceSetting = void 0;
|
|
4
4
|
exports.defaulInstanceSetting = {
|
|
5
|
-
isDev: false,
|
|
6
5
|
accessTokenKey: 'accessTokenKey',
|
|
7
6
|
accessTokenTTL: 60 * 60,
|
|
8
7
|
refreshTokenKey: 'refreshTokenKey',
|
|
@@ -18,16 +17,17 @@ exports.defaulInstanceSetting = {
|
|
|
18
17
|
maxFileUploadSizeInMb: 500,
|
|
19
18
|
useRateLimit: false,
|
|
20
19
|
rateLimitPeriodInMs: 60 * 60 * 1000,
|
|
21
|
-
rateLimit:
|
|
20
|
+
rateLimit: 5000,
|
|
22
21
|
useSlowDown: false,
|
|
23
22
|
slowDownPeriodInMs: 10 * 60 * 1000,
|
|
24
|
-
slowDownAfter:
|
|
23
|
+
slowDownAfter: 2000,
|
|
25
24
|
slowDownDelayInMs: 500,
|
|
26
25
|
hashSaltRounds: 10,
|
|
27
26
|
paginationDefaultLimit: 100,
|
|
28
27
|
server: 'express',
|
|
29
28
|
openapiDocsVersion: '1.0.0',
|
|
30
|
-
|
|
29
|
+
openapiDocsBaseUrl: ['/'],
|
|
30
|
+
openapiDocsPath: '/__docs',
|
|
31
31
|
logLevel: 'info',
|
|
32
32
|
logRequests: true,
|
|
33
33
|
requestSchemaValidation: false,
|
|
@@ -13,9 +13,12 @@ export declare class Listener {
|
|
|
13
13
|
#private;
|
|
14
14
|
constructor(socket: io.Server, callers: SocketCallers);
|
|
15
15
|
start(): Promise<void>;
|
|
16
|
-
created(channels: string[], data:
|
|
17
|
-
updated(channels: string[],
|
|
18
|
-
|
|
16
|
+
created<T extends BaseEntity<any, any>>(channels: string[], data: T): Promise<void>;
|
|
17
|
+
updated<T extends BaseEntity<any, any>>(channels: string[], { after, before }: {
|
|
18
|
+
after: T;
|
|
19
|
+
before: T;
|
|
20
|
+
}): Promise<void>;
|
|
21
|
+
deleted<T extends BaseEntity<any, any>>(channels: string[], data: T): Promise<void>;
|
|
19
22
|
set callers(callers: SocketCallers);
|
|
20
23
|
register(channel: string, onJoin?: OnJoinFn): this;
|
|
21
24
|
}
|