equipped 4.4.6 → 5.0.0-beta-1
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 +25 -0
- package/lib/db/_instance.d.ts +3 -6
- package/lib/db/mongoose/changes.d.ts +2 -1
- package/lib/db/mongoose/index.d.ts +2 -1
- package/lib/db/mongoose/index.js +3 -1
- package/lib/db/mongoose/query.d.ts +1 -0
- package/lib/db/query.d.ts +12 -11
- package/lib/errors/customError.d.ts +0 -1
- package/lib/errors/customError.js +0 -1
- package/lib/errors/index.d.ts +0 -2
- package/lib/errors/index.js +0 -2
- package/lib/errors/types/refreshTokenMisusedError.d.ts +1 -1
- package/lib/errors/types/refreshTokenMisusedError.js +1 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/instance/index.d.ts +5 -4
- package/lib/instance/index.js +17 -4
- package/lib/instance/settings.d.ts +8 -0
- package/lib/instance/settings.js +7 -1
- package/lib/listeners/emitter.d.ts +3 -3
- package/lib/scripts/index.d.ts +1 -0
- package/lib/scripts/index.js +17 -0
- package/lib/scripts/json-schema.d.ts +2 -0
- package/lib/scripts/json-schema.js +75 -0
- package/lib/server/impls/base.d.ts +37 -0
- package/lib/server/impls/base.js +120 -0
- package/lib/server/impls/express.d.ts +16 -0
- package/lib/server/impls/express.js +183 -0
- package/lib/server/impls/fastify.d.ts +23 -0
- package/lib/server/impls/fastify.js +182 -0
- package/lib/server/index.d.ts +7 -6
- package/lib/server/index.js +10 -8
- package/lib/server/middlewares/errorHandler.d.ts +4 -1
- package/lib/server/middlewares/errorHandler.js +10 -11
- package/lib/server/middlewares/index.d.ts +1 -0
- package/lib/server/middlewares/index.js +1 -0
- package/lib/server/middlewares/notFoundHandler.d.ts +4 -1
- package/lib/server/middlewares/notFoundHandler.js +3 -3
- package/lib/server/middlewares/parseAuthUser.d.ts +4 -1
- package/lib/server/middlewares/parseAuthUser.js +2 -2
- package/lib/server/middlewares/requireAuthUser.d.ts +4 -2
- package/lib/server/middlewares/requireAuthUser.js +8 -3
- package/lib/server/middlewares/requireRefreshUser.d.ts +4 -2
- package/lib/server/middlewares/requireRefreshUser.js +7 -4
- package/lib/server/requests.d.ts +50 -0
- package/lib/server/requests.js +60 -0
- package/lib/server/routes.d.ts +6 -23
- package/lib/server/routes.js +39 -49
- package/lib/server/types.d.ts +85 -0
- package/lib/server/types.js +33 -0
- package/lib/structure/baseEntity.d.ts +4 -5
- package/lib/structure/baseEntity.js +20 -12
- package/lib/types/index.d.ts +20 -0
- package/lib/types/index.js +2 -0
- package/package.json +36 -22
- package/lib/errors/types/accountNotVerifiedError.d.ts +0 -5
- package/lib/errors/types/accountNotVerifiedError.js +0 -12
- package/lib/errors/types/invalidToken.d.ts +0 -5
- package/lib/errors/types/invalidToken.js +0 -12
- package/lib/logger/index.d.ts +0 -13
- package/lib/logger/index.js +0 -41
- package/lib/server/app.d.ts +0 -12
- package/lib/server/app.js +0 -121
- package/lib/server/controllers/index.d.ts +0 -10
- package/lib/server/controllers/index.js +0 -46
- package/lib/server/controllers/request.d.ts +0 -40
- package/lib/server/controllers/request.js +0 -85
- package/lib/server/controllers/response.d.ts +0 -14
- package/lib/server/controllers/response.js +0 -16
- package/lib/server/statusCodes.d.ts +0 -16
- package/lib/server/statusCodes.js +0 -17
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [5.0.0-beta-1](https://github.com/kevinand11/equipped/compare/v4.4.6...v5.0.0-beta-1) (2024-05-29)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* add request type details on Request class ([d6c3752](https://github.com/kevinand11/equipped/commit/d6c37529e7eeac5a3c2ac20e0a205de49cb0465e))
|
|
11
|
+
* error handler per route ([0eccc21](https://github.com/kevinand11/equipped/commit/0eccc2129c559353efc65ae7ddf6bd0368e4031e))
|
|
12
|
+
* express server swagger ([95c811b](https://github.com/kevinand11/equipped/commit/95c811ba7a1086116c4327c37e1723b08b85059d))
|
|
13
|
+
* fastify swagger and custom schema validation error ([17b8710](https://github.com/kevinand11/equipped/commit/17b8710324c5c515daa8928d7028ef0150a6c30d))
|
|
14
|
+
* hide routes from schema ([304df23](https://github.com/kevinand11/equipped/commit/304df23a413f13cbd77f2eeeae00032d57dcea2c))
|
|
15
|
+
* new JsonValue and Ignored types ([ba73af4](https://github.com/kevinand11/equipped/commit/ba73af440f54549d10a974fd530c35ac912ec4dd))
|
|
16
|
+
* regustere schemas in server and reference by key ([7298bb0](https://github.com/kevinand11/equipped/commit/7298bb0ae6a908325bbef3cd308e2a7787147387))
|
|
17
|
+
* route securities ([a7b91a1](https://github.com/kevinand11/equipped/commit/a7b91a17acf15cc9013fe971b0adf2b96cd129da))
|
|
18
|
+
* schema for fastify ([990529c](https://github.com/kevinand11/equipped/commit/990529ce777d5fcfb6f4c11d634ecdc31aa2ea81))
|
|
19
|
+
* script to generate schemas ([2736ffa](https://github.com/kevinand11/equipped/commit/2736ffa68da13fb0b3ef96809a06be74754257ba))
|
|
20
|
+
* settings to toggle whether to schema validate ([6f64313](https://github.com/kevinand11/equipped/commit/6f643133632963cc5418259d3f97a9cf172bcdaf))
|
|
21
|
+
* throw ValidationError for failed schema validation in express app ([be30a5e](https://github.com/kevinand11/equipped/commit/be30a5e1ff7fe7016bf6effa9566a3a48acafd0e))
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Bug Fixes
|
|
25
|
+
|
|
26
|
+
* catch errors in json schema ([02f4b0b](https://github.com/kevinand11/equipped/commit/02f4b0bb4c8eaa3fc0e1a25b14e2f531101d3d08))
|
|
27
|
+
* fix mongo createCollection command ([f0bfbfa](https://github.com/kevinand11/equipped/commit/f0bfbfa945d48741fbc56b6766483f12b35e0f7c))
|
|
28
|
+
* remove recursive type for QueryWhereBlock to prevent recursive errors in schema gen ([31e3af6](https://github.com/kevinand11/equipped/commit/31e3af64e6ead4b67e5a787df3c0b39671f8e39b))
|
|
29
|
+
|
|
5
30
|
### [4.4.6](https://github.com/kevinand11/equipped/compare/v4.4.5...v4.4.6) (2024-04-07)
|
|
6
31
|
|
|
7
32
|
### [4.4.5](https://github.com/kevinand11/equipped/compare/v4.4.4...v4.4.5) (2024-04-06)
|
package/lib/db/_instance.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { BaseEntity } from '../structure';
|
|
2
|
+
import { DeepPartial } from '../types';
|
|
2
3
|
import { DebeziumSetup } from './debezium';
|
|
3
4
|
import { QueryParams, QueryResults } from './query';
|
|
4
5
|
export declare abstract class Db {
|
|
5
6
|
#private;
|
|
6
|
-
abstract change<Model, Entity extends BaseEntity
|
|
7
|
+
abstract change<Model, Entity extends BaseEntity>(collection: any, callbacks: DbChangeCallbacks<Model, Entity>, mapper: (model: Model | null) => Entity | null): DbChange<Model, Entity>;
|
|
7
8
|
abstract query<Model>(collection: any, params: QueryParams): Promise<QueryResults<Model>>;
|
|
8
9
|
protected _addToDbChanges(dbChange: DbChange<any, any>): this;
|
|
9
10
|
startAllDbChanges(): Promise<void>;
|
|
10
11
|
abstract start(): Promise<void>;
|
|
11
12
|
abstract close(): Promise<void>;
|
|
12
13
|
}
|
|
13
|
-
export declare abstract class DbChange<Model, Entity extends BaseEntity
|
|
14
|
+
export declare abstract class DbChange<Model, Entity extends BaseEntity> {
|
|
14
15
|
#private;
|
|
15
16
|
constructor(callbacks: DbChangeCallbacks<Model, Entity>, mapper: (model: Model | null) => Entity | null);
|
|
16
17
|
abstract start(): Promise<void>;
|
|
@@ -18,9 +19,6 @@ export declare abstract class DbChange<Model, Entity extends BaseEntity<any>> {
|
|
|
18
19
|
get mapper(): (model: Model | null) => Entity | null;
|
|
19
20
|
protected _setup(key: string, data: DebeziumSetup): Promise<boolean>;
|
|
20
21
|
}
|
|
21
|
-
type DeepPartial<T> = {
|
|
22
|
-
[P in keyof T]?: DeepPartial<T[P]>;
|
|
23
|
-
};
|
|
24
22
|
export type DbChangeCallbacks<Model, Entity> = {
|
|
25
23
|
created?: (data: {
|
|
26
24
|
before: null;
|
|
@@ -36,4 +34,3 @@ export type DbChangeCallbacks<Model, Entity> = {
|
|
|
36
34
|
after: null;
|
|
37
35
|
}) => Promise<void>;
|
|
38
36
|
};
|
|
39
|
-
export {};
|
|
@@ -22,10 +22,11 @@
|
|
|
22
22
|
/// <reference types="mongoose/types/validation" />
|
|
23
23
|
/// <reference types="mongoose/types/virtuals" />
|
|
24
24
|
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
+
/// <reference types="mongoose/types/inferrawdoctype" />
|
|
25
26
|
import mongoose from 'mongoose';
|
|
26
27
|
import { BaseEntity } from '../../structure';
|
|
27
28
|
import { DbChange, DbChangeCallbacks } from '../_instance';
|
|
28
|
-
export declare class MongoDbChange<Model, Entity extends BaseEntity
|
|
29
|
+
export declare class MongoDbChange<Model, Entity extends BaseEntity> extends DbChange<Model, Entity> {
|
|
29
30
|
#private;
|
|
30
31
|
constructor(model: mongoose.Model<Model>, callbacks: DbChangeCallbacks<Model, Entity>, mapper: (model: Model | null) => Entity | null);
|
|
31
32
|
start(): Promise<void>;
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
/// <reference types="mongoose/types/validation" />
|
|
23
23
|
/// <reference types="mongoose/types/virtuals" />
|
|
24
24
|
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
+
/// <reference types="mongoose/types/inferrawdoctype" />
|
|
25
26
|
import mongoose from 'mongoose';
|
|
26
27
|
import { BaseEntity } from '../../structure';
|
|
27
28
|
import { QueryParams, QueryResults } from '../query';
|
|
@@ -32,7 +33,7 @@ export declare class MongoDb extends Db {
|
|
|
32
33
|
get Schema(): typeof mongoose.Schema;
|
|
33
34
|
get Id(): mongoose.Types.ObjectId;
|
|
34
35
|
use(dbName?: string): mongoose.Connection;
|
|
35
|
-
change<Model, Entity extends BaseEntity
|
|
36
|
+
change<Model, Entity extends BaseEntity>(model: mongoose.Model<Model>, callbacks: DbChangeCallbacks<Model, Entity>, mapper: (model: Model | null) => Entity | null): MongoDbChange<Model, Entity>;
|
|
36
37
|
query<Model>(model: mongoose.Model<Model>, params: QueryParams): Promise<QueryResults<Model>>;
|
|
37
38
|
start(): Promise<void>;
|
|
38
39
|
close(): Promise<void>;
|
package/lib/db/mongoose/index.js
CHANGED
|
@@ -58,7 +58,9 @@ class MongoDb extends _instance_1.Db {
|
|
|
58
58
|
await Promise.all([mongoose_1.default.connection, ...__classPrivateFieldGet(this, _MongoDb_instances, "a", _MongoDb_connections_get)].map((conn) => {
|
|
59
59
|
return Object.values(conn.models)
|
|
60
60
|
.map(async (model) => {
|
|
61
|
-
await conn.db.
|
|
61
|
+
await conn.db.createCollection(model.collection.name, {
|
|
62
|
+
changeStreamPreAndPostImages: { enabled: true }
|
|
63
|
+
});
|
|
62
64
|
});
|
|
63
65
|
}).flat());
|
|
64
66
|
}
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
/// <reference types="mongoose/types/validation" />
|
|
23
23
|
/// <reference types="mongoose/types/virtuals" />
|
|
24
24
|
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
+
/// <reference types="mongoose/types/inferrawdoctype" />
|
|
25
26
|
import mongoose from 'mongoose';
|
|
26
27
|
import { QueryParams, QueryResults } from '../query';
|
|
27
28
|
export declare const parseMongodbQueryParams: <Model>(model: mongoose.Model<Model>, params: QueryParams) => Promise<QueryResults<Model>>;
|
package/lib/db/query.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Paths } from '../types';
|
|
1
2
|
export type QueryResults<Model> = {
|
|
2
3
|
pages: {
|
|
3
4
|
start: number;
|
|
@@ -28,23 +29,23 @@ export declare enum Conditions {
|
|
|
28
29
|
nin = "nin",
|
|
29
30
|
exists = "exists"
|
|
30
31
|
}
|
|
31
|
-
export type QueryWhere = {
|
|
32
|
-
field: string
|
|
33
|
-
value:
|
|
32
|
+
export type QueryWhere<T> = {
|
|
33
|
+
field: Paths<T, string>;
|
|
34
|
+
value: unknown;
|
|
34
35
|
condition?: Conditions;
|
|
35
36
|
};
|
|
36
|
-
export type QueryWhereBlock = {
|
|
37
|
+
export type QueryWhereBlock<T> = {
|
|
37
38
|
condition: QueryKeys;
|
|
38
|
-
value:
|
|
39
|
+
value: QueryWhere<T>[];
|
|
39
40
|
};
|
|
40
|
-
export type QueryWhereClause = QueryWhere | QueryWhereBlock
|
|
41
|
-
export type QueryParams = {
|
|
42
|
-
where?: QueryWhereClause[];
|
|
43
|
-
auth?: QueryWhereClause[];
|
|
41
|
+
export type QueryWhereClause<T> = QueryWhere<T> | QueryWhereBlock<T>;
|
|
42
|
+
export type QueryParams<T = unknown> = {
|
|
43
|
+
where?: QueryWhereClause<T>[];
|
|
44
|
+
auth?: QueryWhereClause<T>[];
|
|
44
45
|
whereType?: QueryKeys;
|
|
45
46
|
authType?: QueryKeys;
|
|
46
47
|
sort?: {
|
|
47
|
-
field: string
|
|
48
|
+
field: Paths<T, string>;
|
|
48
49
|
desc?: boolean;
|
|
49
50
|
}[];
|
|
50
51
|
limit?: number;
|
|
@@ -52,6 +53,6 @@ export type QueryParams = {
|
|
|
52
53
|
page?: number;
|
|
53
54
|
search?: {
|
|
54
55
|
value: string;
|
|
55
|
-
fields: string[];
|
|
56
|
+
fields: Paths<T, string>[];
|
|
56
57
|
};
|
|
57
58
|
};
|
package/lib/errors/index.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
export * from './customError';
|
|
2
2
|
export * from './types/accessTokenExpired';
|
|
3
|
-
export * from './types/accountNotVerifiedError';
|
|
4
3
|
export * from './types/badRequestError';
|
|
5
|
-
export * from './types/invalidToken';
|
|
6
4
|
export * from './types/notAuthenticatedError';
|
|
7
5
|
export * from './types/notAuthorizedError';
|
|
8
6
|
export * from './types/notFoundError';
|
package/lib/errors/index.js
CHANGED
|
@@ -16,9 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./customError"), exports);
|
|
18
18
|
__exportStar(require("./types/accessTokenExpired"), exports);
|
|
19
|
-
__exportStar(require("./types/accountNotVerifiedError"), exports);
|
|
20
19
|
__exportStar(require("./types/badRequestError"), exports);
|
|
21
|
-
__exportStar(require("./types/invalidToken"), exports);
|
|
22
20
|
__exportStar(require("./types/notAuthenticatedError"), exports);
|
|
23
21
|
__exportStar(require("./types/notAuthorizedError"), exports);
|
|
24
22
|
__exportStar(require("./types/notFoundError"), exports);
|
|
@@ -6,7 +6,7 @@ const customError_1 = require("../customError");
|
|
|
6
6
|
class RefreshTokenMisusedError extends customError_1.CustomError {
|
|
7
7
|
constructor(message = 'Refresh token misused') {
|
|
8
8
|
super(message, [{ message }]);
|
|
9
|
-
this.statusCode = server_1.StatusCodes.
|
|
9
|
+
this.statusCode = server_1.StatusCodes.NotAuthenticated;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
exports.RefreshTokenMisusedError = RefreshTokenMisusedError;
|
package/lib/index.d.ts
CHANGED
|
@@ -7,9 +7,11 @@ export { Events } from './events/';
|
|
|
7
7
|
export * from './exit';
|
|
8
8
|
export * from './instance';
|
|
9
9
|
export * from './listeners';
|
|
10
|
+
export * from './scripts';
|
|
10
11
|
export * from './server';
|
|
11
12
|
export * from './storage';
|
|
12
13
|
export * from './structure';
|
|
14
|
+
export * from './types';
|
|
13
15
|
export * from './utils/auth';
|
|
14
16
|
export * from './utils/authUser';
|
|
15
17
|
export * from './utils/retry';
|
package/lib/index.js
CHANGED
|
@@ -28,9 +28,11 @@ __exportStar(require("./errors"), exports);
|
|
|
28
28
|
__exportStar(require("./exit"), exports);
|
|
29
29
|
__exportStar(require("./instance"), exports);
|
|
30
30
|
__exportStar(require("./listeners"), exports);
|
|
31
|
+
__exportStar(require("./scripts"), exports);
|
|
31
32
|
__exportStar(require("./server"), exports);
|
|
32
33
|
__exportStar(require("./storage"), exports);
|
|
33
34
|
__exportStar(require("./structure"), exports);
|
|
35
|
+
__exportStar(require("./types"), exports);
|
|
34
36
|
__exportStar(require("./utils/auth"), exports);
|
|
35
37
|
__exportStar(require("./utils/authUser"), exports);
|
|
36
38
|
__exportStar(require("./utils/retry"), exports);
|
package/lib/instance/index.d.ts
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
|
+
import pino from 'pino';
|
|
1
2
|
import { BullJob } from '../bull';
|
|
2
3
|
import { Cache } from '../cache/cache';
|
|
3
4
|
import { MongoDb } from '../db/mongoose';
|
|
4
5
|
import { EventBus } from '../events/';
|
|
5
|
-
import {
|
|
6
|
-
import { Server } from '../server/app';
|
|
6
|
+
import { Server } from '../server';
|
|
7
7
|
import { Settings } from './settings';
|
|
8
8
|
export declare class Instance {
|
|
9
9
|
#private;
|
|
10
10
|
private constructor();
|
|
11
|
-
get logger(): Logger
|
|
11
|
+
get logger(): pino.Logger<any>;
|
|
12
12
|
get job(): BullJob;
|
|
13
13
|
get cache(): Cache;
|
|
14
14
|
get eventBus(): EventBus;
|
|
15
|
-
get server(): Server
|
|
15
|
+
get server(): Server<any, any>;
|
|
16
16
|
get dbs(): {
|
|
17
17
|
mongo: MongoDb;
|
|
18
18
|
};
|
|
19
19
|
get listener(): import("..").Listener;
|
|
20
20
|
get settings(): Settings;
|
|
21
|
+
static createLogger(): import("pino").Logger<any>;
|
|
21
22
|
static initialize(settings: Partial<Settings>): void;
|
|
22
23
|
static get(): Instance;
|
|
23
24
|
startConnections(): Promise<void>;
|
package/lib/instance/index.js
CHANGED
|
@@ -10,16 +10,19 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
10
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
11
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
12
|
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
13
16
|
var _a, _Instance_initialized, _Instance_instance, _Instance_settings, _Instance_logger, _Instance_job, _Instance_cache, _Instance_eventBus, _Instance_server, _Instance_dbs;
|
|
14
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
18
|
exports.Instance = void 0;
|
|
19
|
+
const pino_1 = __importDefault(require("pino"));
|
|
16
20
|
const bull_1 = require("../bull");
|
|
17
21
|
const redis_cache_1 = require("../cache/types/redis-cache");
|
|
18
22
|
const mongoose_1 = require("../db/mongoose");
|
|
19
23
|
const kafka_1 = require("../events/kafka");
|
|
20
24
|
const exit_1 = require("../exit");
|
|
21
|
-
const
|
|
22
|
-
const app_1 = require("../server/app");
|
|
25
|
+
const server_1 = require("../server");
|
|
23
26
|
const settings_1 = require("./settings");
|
|
24
27
|
class Instance {
|
|
25
28
|
constructor() {
|
|
@@ -32,7 +35,7 @@ class Instance {
|
|
|
32
35
|
_Instance_dbs.set(this, null);
|
|
33
36
|
}
|
|
34
37
|
get logger() {
|
|
35
|
-
return __classPrivateFieldSet(this, _Instance_logger, __classPrivateFieldGet(this, _Instance_logger, "f") ||
|
|
38
|
+
return __classPrivateFieldSet(this, _Instance_logger, __classPrivateFieldGet(this, _Instance_logger, "f") || _a.createLogger(), "f");
|
|
36
39
|
}
|
|
37
40
|
get job() {
|
|
38
41
|
return __classPrivateFieldSet(this, _Instance_job, __classPrivateFieldGet(this, _Instance_job, "f") || new bull_1.BullJob(), "f");
|
|
@@ -44,7 +47,7 @@ class Instance {
|
|
|
44
47
|
return __classPrivateFieldSet(this, _Instance_eventBus, __classPrivateFieldGet(this, _Instance_eventBus, "f") || new kafka_1.KafkaEventBus(), "f");
|
|
45
48
|
}
|
|
46
49
|
get server() {
|
|
47
|
-
return __classPrivateFieldSet(this, _Instance_server, __classPrivateFieldGet(this, _Instance_server, "f") ||
|
|
50
|
+
return __classPrivateFieldSet(this, _Instance_server, __classPrivateFieldGet(this, _Instance_server, "f") || server_1.serverTypes[this.settings.server](), "f");
|
|
48
51
|
}
|
|
49
52
|
get dbs() {
|
|
50
53
|
return __classPrivateFieldSet(this, _Instance_dbs, __classPrivateFieldGet(this, _Instance_dbs, "f") || { mongo: new mongoose_1.MongoDb() }, "f");
|
|
@@ -55,6 +58,16 @@ class Instance {
|
|
|
55
58
|
get settings() {
|
|
56
59
|
return __classPrivateFieldGet(this, _Instance_settings, "f");
|
|
57
60
|
}
|
|
61
|
+
static createLogger() {
|
|
62
|
+
return (0, pino_1.default)({
|
|
63
|
+
level: _a.get()?.settings?.logLevel ?? 'info',
|
|
64
|
+
serializers: {
|
|
65
|
+
err: pino_1.default.stdSerializers.err,
|
|
66
|
+
req: pino_1.default.stdSerializers.req,
|
|
67
|
+
res: pino_1.default.stdSerializers.res,
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
}
|
|
58
71
|
static initialize(settings) {
|
|
59
72
|
__classPrivateFieldSet(_a, _a, true, "f", _Instance_initialized);
|
|
60
73
|
const instanceSettings = _a.get().settings;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Level } from 'pino';
|
|
2
|
+
import type { ServerTypes } from '../server';
|
|
1
3
|
export type Settings = {
|
|
2
4
|
isDev: boolean;
|
|
3
5
|
accessTokenKey: string;
|
|
@@ -22,5 +24,11 @@ export type Settings = {
|
|
|
22
24
|
slowDownDelayInMs: number;
|
|
23
25
|
hashSaltRounds: number;
|
|
24
26
|
paginationDefaultLimit: number;
|
|
27
|
+
server: ServerTypes;
|
|
28
|
+
openapiDocsVersion: string;
|
|
29
|
+
openapiDocsUrl: string;
|
|
30
|
+
logLevel: Level;
|
|
31
|
+
logRequests: boolean;
|
|
32
|
+
requestSchemaValidation: boolean;
|
|
25
33
|
};
|
|
26
34
|
export declare const defaulInstanceSetting: Settings;
|
package/lib/instance/settings.js
CHANGED
|
@@ -24,5 +24,11 @@ exports.defaulInstanceSetting = {
|
|
|
24
24
|
slowDownAfter: 1000,
|
|
25
25
|
slowDownDelayInMs: 500,
|
|
26
26
|
hashSaltRounds: 10,
|
|
27
|
-
paginationDefaultLimit: 100
|
|
27
|
+
paginationDefaultLimit: 100,
|
|
28
|
+
server: 'express',
|
|
29
|
+
openapiDocsVersion: '1.0.0',
|
|
30
|
+
openapiDocsUrl: '/__docs',
|
|
31
|
+
logLevel: 'info',
|
|
32
|
+
logRequests: true,
|
|
33
|
+
requestSchemaValidation: false,
|
|
28
34
|
};
|
|
@@ -13,9 +13,9 @@ 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: BaseEntity
|
|
17
|
-
updated(channels: string[], data: BaseEntity
|
|
18
|
-
deleted(channels: string[], data: BaseEntity
|
|
16
|
+
created(channels: string[], data: BaseEntity): Promise<void>;
|
|
17
|
+
updated(channels: string[], data: BaseEntity): Promise<void>;
|
|
18
|
+
deleted(channels: string[], data: BaseEntity): Promise<void>;
|
|
19
19
|
set callers(callers: SocketCallers);
|
|
20
20
|
register(channel: string, onJoin?: OnJoinFn): this;
|
|
21
21
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './json-schema';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./json-schema"), exports);
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.generateJSONSchema = void 0;
|
|
27
|
+
const ts_oas_1 = __importStar(require("ts-oas"));
|
|
28
|
+
const instance_1 = require("../instance");
|
|
29
|
+
function generateJSONSchema(patterns, paths) {
|
|
30
|
+
const tsProgram = (0, ts_oas_1.createProgram)(paths, { strictNullChecks: true });
|
|
31
|
+
const tsoas = new ts_oas_1.default(tsProgram, { ref: false, ignoreErrors: true });
|
|
32
|
+
const jsonSchema = tsoas.getSchemas(patterns);
|
|
33
|
+
return Object.entries(jsonSchema)
|
|
34
|
+
.map(([name, { properties: def }]) => {
|
|
35
|
+
try {
|
|
36
|
+
const key = def?.key?.enum?.at(0) ?? name;
|
|
37
|
+
if (!def || !key || !def.method)
|
|
38
|
+
throw new Error();
|
|
39
|
+
const response = def.responses.properties ?? def.responses.anyOf?.reduce((acc, cur) => {
|
|
40
|
+
if (cur.properties)
|
|
41
|
+
return { ...acc, ...cur.properties };
|
|
42
|
+
return acc;
|
|
43
|
+
}, {}) ?? undefined;
|
|
44
|
+
const schema = def
|
|
45
|
+
? {
|
|
46
|
+
body: def.body,
|
|
47
|
+
params: def.params,
|
|
48
|
+
querystring: def.query,
|
|
49
|
+
headers: def.headers,
|
|
50
|
+
response,
|
|
51
|
+
operationId: key,
|
|
52
|
+
summary: name,
|
|
53
|
+
}
|
|
54
|
+
: undefined;
|
|
55
|
+
return [key, schema];
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
instance_1.Instance.createLogger().warn(`Error parsing ${name}: ${err.message}. Skipping route`);
|
|
59
|
+
return [undefined, undefined];
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
.reduce((acc, [path, schema]) => {
|
|
63
|
+
if (path && schema)
|
|
64
|
+
acc[path] = stripEmptyObjects(schema);
|
|
65
|
+
return acc;
|
|
66
|
+
}, {});
|
|
67
|
+
}
|
|
68
|
+
exports.generateJSONSchema = generateJSONSchema;
|
|
69
|
+
function stripEmptyObjects(obj) {
|
|
70
|
+
return Object.entries(obj).reduce((acc, [key, value]) => {
|
|
71
|
+
if (!value || typeof value === 'object' && Object.keys(value).length === 0)
|
|
72
|
+
return acc;
|
|
73
|
+
return { ...acc, [key]: value };
|
|
74
|
+
}, {});
|
|
75
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node/http" />
|
|
3
|
+
/// <reference types="pino-http" />
|
|
4
|
+
import http from 'http';
|
|
5
|
+
import { OpenAPIV3_1 } from 'openapi-types';
|
|
6
|
+
import supertest from 'supertest';
|
|
7
|
+
import { FastifySchema } from 'fastify';
|
|
8
|
+
import { Listener } from '../../listeners';
|
|
9
|
+
import { Defined } from '../../types';
|
|
10
|
+
import { Request } from '../requests';
|
|
11
|
+
import { Router } from '../routes';
|
|
12
|
+
import { Route } from '../types';
|
|
13
|
+
export type FullRoute = Required<Omit<Route, 'schema' | 'tags' | 'security' | 'hideSchema' | 'onError' | 'onSetupHandler' | '__def'>> & {
|
|
14
|
+
schema: FastifySchema;
|
|
15
|
+
onError?: Route['onError'];
|
|
16
|
+
};
|
|
17
|
+
type Schemas = Record<string, Defined<Route['schema']>>;
|
|
18
|
+
export declare abstract class Server<Req = any, Res = any> {
|
|
19
|
+
#private;
|
|
20
|
+
protected server: http.Server;
|
|
21
|
+
protected staticPath: string;
|
|
22
|
+
protected settings: import("../../instance/settings").Settings;
|
|
23
|
+
protected baseOpenapiDoc: OpenAPIV3_1.Document;
|
|
24
|
+
protected abstract onLoad(): Promise<void>;
|
|
25
|
+
protected abstract startServer(port: number): Promise<boolean>;
|
|
26
|
+
protected abstract parse(req: Req, res: Res): Promise<Request>;
|
|
27
|
+
protected abstract registerRoute(route: FullRoute): void;
|
|
28
|
+
constructor(server: http.Server);
|
|
29
|
+
get listener(): Listener;
|
|
30
|
+
addRoutes(routes: Route[]): void;
|
|
31
|
+
addSchema(schemas: Schemas): void;
|
|
32
|
+
load(): Promise<void>;
|
|
33
|
+
register(router: Router | Router[]): void;
|
|
34
|
+
test(): import("supertest/lib/agent")<supertest.SuperTestStatic.Test>;
|
|
35
|
+
start(port: number): Promise<boolean>;
|
|
36
|
+
}
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,120 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
var _Server_instances, _Server_schemas, _Server_listener, _Server_regRoute;
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.Server = void 0;
|
|
19
|
+
const socket_io_1 = __importDefault(require("socket.io"));
|
|
20
|
+
const supertest_1 = __importDefault(require("supertest"));
|
|
21
|
+
const path_1 = __importDefault(require("path"));
|
|
22
|
+
const instance_1 = require("../../instance");
|
|
23
|
+
const listeners_1 = require("../../listeners");
|
|
24
|
+
const middlewares_1 = require("../middlewares");
|
|
25
|
+
const routes_1 = require("../routes");
|
|
26
|
+
class Server {
|
|
27
|
+
constructor(server) {
|
|
28
|
+
_Server_instances.add(this);
|
|
29
|
+
_Server_schemas.set(this, {});
|
|
30
|
+
_Server_listener.set(this, null);
|
|
31
|
+
this.staticPath = path_1.default.join(process.cwd(), 'public');
|
|
32
|
+
this.settings = instance_1.Instance.get().settings;
|
|
33
|
+
this.baseOpenapiDoc = {
|
|
34
|
+
openapi: '3.0.0',
|
|
35
|
+
info: { title: this.settings.appId, version: this.settings.openapiDocsVersion },
|
|
36
|
+
paths: {},
|
|
37
|
+
components: {
|
|
38
|
+
schemas: {},
|
|
39
|
+
securitySchemes: {
|
|
40
|
+
AccessToken: {
|
|
41
|
+
type: 'apiKey',
|
|
42
|
+
name: 'Access-Token',
|
|
43
|
+
in: 'header',
|
|
44
|
+
},
|
|
45
|
+
RefreshToken: {
|
|
46
|
+
type: 'apiKey',
|
|
47
|
+
name: 'Refresh-Token',
|
|
48
|
+
in: 'header',
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
this.server = server;
|
|
54
|
+
}
|
|
55
|
+
get listener() {
|
|
56
|
+
if (!__classPrivateFieldGet(this, _Server_listener, "f")) {
|
|
57
|
+
const socket = new socket_io_1.default.Server(this.server, { cors: { origin: '*' } });
|
|
58
|
+
__classPrivateFieldSet(this, _Server_listener, new listeners_1.Listener(socket, {
|
|
59
|
+
onConnect: async () => { },
|
|
60
|
+
onDisconnect: async () => { }
|
|
61
|
+
}), "f");
|
|
62
|
+
}
|
|
63
|
+
return __classPrivateFieldGet(this, _Server_listener, "f");
|
|
64
|
+
}
|
|
65
|
+
addRoutes(routes) {
|
|
66
|
+
routes.forEach((route) => __classPrivateFieldGet(this, _Server_instances, "m", _Server_regRoute).call(this, route));
|
|
67
|
+
}
|
|
68
|
+
addSchema(schemas) {
|
|
69
|
+
Object.assign(__classPrivateFieldGet(this, _Server_schemas, "f"), schemas);
|
|
70
|
+
}
|
|
71
|
+
async load() {
|
|
72
|
+
await this.onLoad();
|
|
73
|
+
}
|
|
74
|
+
register(router) {
|
|
75
|
+
const routers = Array.isArray(router) ? router : [router];
|
|
76
|
+
routers.map((router) => router.routes).forEach((routes) => this.addRoutes(routes));
|
|
77
|
+
}
|
|
78
|
+
test() {
|
|
79
|
+
return (0, supertest_1.default)(this.server);
|
|
80
|
+
}
|
|
81
|
+
async start(port) {
|
|
82
|
+
const postRoutesRouter = new routes_1.Router();
|
|
83
|
+
postRoutesRouter.get({
|
|
84
|
+
path: '__health',
|
|
85
|
+
schema: {
|
|
86
|
+
response: {
|
|
87
|
+
200: { type: 'string' },
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
})(async () => `${this.settings.appId} service running`);
|
|
91
|
+
this.register(postRoutesRouter);
|
|
92
|
+
const started = await this.startServer(port);
|
|
93
|
+
if (started)
|
|
94
|
+
await instance_1.Instance.get().logger.info(`${this.settings.appId} service listening on port`, port);
|
|
95
|
+
return started;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.Server = Server;
|
|
99
|
+
_Server_schemas = new WeakMap(), _Server_listener = new WeakMap(), _Server_instances = new WeakSet(), _Server_regRoute = function _Server_regRoute(route) {
|
|
100
|
+
const { method, path, middlewares = [], handler, schema, tags = [], security = [], onError, hideSchema = false } = route;
|
|
101
|
+
const { key = `${method.toLowerCase()} ${path}` } = route;
|
|
102
|
+
const allMiddlewares = [middlewares_1.parseAuthUser, ...middlewares];
|
|
103
|
+
allMiddlewares.forEach((m) => m.onSetup?.(route));
|
|
104
|
+
route.onSetupHandler?.(route);
|
|
105
|
+
onError?.onSetup?.(route);
|
|
106
|
+
const scheme = schema ?? __classPrivateFieldGet(this, _Server_schemas, "f")[key] ?? {};
|
|
107
|
+
const fullRoute = {
|
|
108
|
+
method, middlewares, handler, key,
|
|
109
|
+
path: path.replace(/(\/\s*)+/g, '/'),
|
|
110
|
+
onError,
|
|
111
|
+
schema: {
|
|
112
|
+
...scheme,
|
|
113
|
+
hide: hideSchema,
|
|
114
|
+
operationId: scheme.operationId ?? handler.name,
|
|
115
|
+
tags: [tags.join(' > ') || 'default'],
|
|
116
|
+
security,
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
this.registerRoute(fullRoute);
|
|
120
|
+
};
|