equipped 5.0.0-beta.16 → 5.0.0-beta.7
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 +0 -69
- package/lib/db/_instance.d.ts +2 -2
- package/lib/db/mongoose/changes.d.ts +1 -1
- package/lib/db/mongoose/index.d.ts +2 -2
- package/lib/instance/index.js +1 -2
- package/lib/instance/settings.d.ts +1 -0
- package/lib/instance/settings.js +3 -2
- package/lib/listeners/emitter.d.ts +3 -3
- package/lib/scripts/json-schema.d.ts +1 -5
- package/lib/scripts/json-schema.js +20 -38
- package/lib/server/impls/base.d.ts +4 -4
- package/lib/server/impls/base.js +22 -32
- package/lib/server/impls/express.js +7 -0
- package/lib/server/impls/fastify.d.ts +1 -1
- package/lib/server/impls/fastify.js +11 -4
- package/lib/server/middlewares/errorHandler.d.ts +1 -1
- package/lib/server/middlewares/notFoundHandler.d.ts +1 -1
- package/lib/server/middlewares/parseAuthUser.d.ts +1 -1
- package/lib/server/middlewares/requireAuthUser.d.ts +1 -1
- package/lib/server/middlewares/requireAuthUser.js +0 -2
- package/lib/server/middlewares/requireRefreshUser.d.ts +1 -1
- package/lib/server/middlewares/requireRefreshUser.js +0 -2
- package/lib/server/routes.d.ts +2 -3
- package/lib/server/routes.js +5 -9
- package/lib/server/types.d.ts +7 -12
- package/lib/structure/baseEntity.d.ts +1 -1
- package/lib/structure/baseEntity.js +2 -3
- package/lib/utils/utils.d.ts +0 -1
- package/lib/utils/utils.js +1 -10
- package/lib/validations/index.d.ts +5 -9
- package/lib/validations/index.js +3 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,75 +2,6 @@
|
|
|
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.16](https://github.com/kevinand11/equipped/compare/v5.0.0-beta.15...v5.0.0-beta.16) (2024-06-03)
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
### Features
|
|
9
|
-
|
|
10
|
-
* support headers in schema ([85a9319](https://github.com/kevinand11/equipped/commit/85a93195ffb9b97927dc22a61d5f118f49598926))
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
### Bug Fixes
|
|
14
|
-
|
|
15
|
-
* error when cloning objects in toJSON ([1f4cf25](https://github.com/kevinand11/equipped/commit/1f4cf252a6924f0b2c329c11b41f5ac71c68f87b))
|
|
16
|
-
|
|
17
|
-
## [5.0.0-beta.15](https://github.com/kevinand11/equipped/compare/v5.0.0-beta.14...v5.0.0-beta.15) (2024-06-02)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
### Bug Fixes
|
|
21
|
-
|
|
22
|
-
* remove slowdown to avoid esm ([414b2ef](https://github.com/kevinand11/equipped/commit/414b2ef3c047a0c9843ae828b21a1aeaa10925b9))
|
|
23
|
-
|
|
24
|
-
## [5.0.0-beta.14](https://github.com/kevinand11/equipped/compare/v5.0.0-beta.13...v5.0.0-beta.14) (2024-06-02)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
### Bug Fixes
|
|
28
|
-
|
|
29
|
-
* use maps for registered maps for speed ([1882a24](https://github.com/kevinand11/equipped/commit/1882a24d02d666e7779212dbeb67e1022761c823))
|
|
30
|
-
|
|
31
|
-
## [5.0.0-beta.13](https://github.com/kevinand11/equipped/compare/v5.0.0-beta.12...v5.0.0-beta.13) (2024-06-01)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
### Features
|
|
35
|
-
|
|
36
|
-
* add description for each status code ([064a686](https://github.com/kevinand11/equipped/commit/064a68647ea91a5791aad75b1b95988accc38b32))
|
|
37
|
-
|
|
38
|
-
## [5.0.0-beta.12](https://github.com/kevinand11/equipped/compare/v5.0.0-beta.11...v5.0.0-beta.12) (2024-06-01)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
### Bug Fixes
|
|
42
|
-
|
|
43
|
-
* function overloading for validate function ([e1209ab](https://github.com/kevinand11/equipped/commit/e1209ab10f435514d7ac84c3c0af1788d7f3699d))
|
|
44
|
-
* references to BaseEntity ([071a07a](https://github.com/kevinand11/equipped/commit/071a07a827e6c4f73b5cf43ae462541c4518df21))
|
|
45
|
-
|
|
46
|
-
## [5.0.0-beta.11](https://github.com/kevinand11/equipped/compare/v5.0.0-beta.10...v5.0.0-beta.11) (2024-06-01)
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
### Features
|
|
50
|
-
|
|
51
|
-
* unique paths and keys for routes ([36d1776](https://github.com/kevinand11/equipped/commit/36d17762b6d31497e9f15883cc05073ff29a886e))
|
|
52
|
-
|
|
53
|
-
## [5.0.0-beta.10](https://github.com/kevinand11/equipped/compare/v5.0.0-beta.9...v5.0.0-beta.10) (2024-05-31)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
### Bug Fixes
|
|
57
|
-
|
|
58
|
-
* generateJSONSchema ([062360b](https://github.com/kevinand11/equipped/commit/062360bb92ccdaa1ae8fa3d167f9bbb71f156f7c))
|
|
59
|
-
|
|
60
|
-
## [5.0.0-beta.9](https://github.com/kevinand11/equipped/compare/v5.0.0-beta.8...v5.0.0-beta.9) (2024-05-31)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
### Bug Fixes
|
|
64
|
-
|
|
65
|
-
* generateJSONSchema ([39031fd](https://github.com/kevinand11/equipped/commit/39031fd7c8d0f18a93c55b3fc542fd33042e4e44))
|
|
66
|
-
|
|
67
|
-
## [5.0.0-beta.8](https://github.com/kevinand11/equipped/compare/v5.0.0-beta.7...v5.0.0-beta.8) (2024-05-31)
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
### Features
|
|
71
|
-
|
|
72
|
-
* route descriptions ([26b7323](https://github.com/kevinand11/equipped/commit/26b7323b523f83aedacd55096ae01f285fff1398))
|
|
73
|
-
|
|
74
5
|
## [5.0.0-beta.7](https://github.com/kevinand11/equipped/compare/v5.0.0-beta-6...v5.0.0-beta.7) (2024-05-31)
|
|
75
6
|
|
|
76
7
|
|
package/lib/db/_instance.d.ts
CHANGED
|
@@ -4,14 +4,14 @@ import { DebeziumSetup } from './debezium';
|
|
|
4
4
|
import { QueryParams, QueryResults } from './query';
|
|
5
5
|
export declare abstract class Db {
|
|
6
6
|
#private;
|
|
7
|
-
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>;
|
|
8
8
|
abstract query<Model>(collection: any, params: QueryParams): Promise<QueryResults<Model>>;
|
|
9
9
|
protected _addToDbChanges(dbChange: DbChange<any, any>): this;
|
|
10
10
|
startAllDbChanges(): Promise<void>;
|
|
11
11
|
abstract start(): Promise<void>;
|
|
12
12
|
abstract close(): Promise<void>;
|
|
13
13
|
}
|
|
14
|
-
export declare abstract class DbChange<Model, Entity extends BaseEntity
|
|
14
|
+
export declare abstract class DbChange<Model, Entity extends BaseEntity> {
|
|
15
15
|
#private;
|
|
16
16
|
constructor(callbacks: DbChangeCallbacks<Model, Entity>, mapper: (model: Model | null) => Entity | null);
|
|
17
17
|
abstract start(): Promise<void>;
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
import mongoose from 'mongoose';
|
|
27
27
|
import { BaseEntity } from '../../structure';
|
|
28
28
|
import { DbChange, DbChangeCallbacks } from '../_instance';
|
|
29
|
-
export declare class MongoDbChange<Model, Entity extends BaseEntity
|
|
29
|
+
export declare class MongoDbChange<Model, Entity extends BaseEntity> extends DbChange<Model, Entity> {
|
|
30
30
|
#private;
|
|
31
31
|
constructor(model: mongoose.Model<Model>, callbacks: DbChangeCallbacks<Model, Entity>, mapper: (model: Model | null) => Entity | null);
|
|
32
32
|
start(): Promise<void>;
|
|
@@ -25,15 +25,15 @@
|
|
|
25
25
|
/// <reference types="mongoose/types/inferrawdoctype" />
|
|
26
26
|
import mongoose from 'mongoose';
|
|
27
27
|
import { BaseEntity } from '../../structure';
|
|
28
|
-
import { Db, DbChangeCallbacks } from '../_instance';
|
|
29
28
|
import { QueryParams, QueryResults } from '../query';
|
|
29
|
+
import { Db, DbChangeCallbacks } from '../_instance';
|
|
30
30
|
import { MongoDbChange } from './changes';
|
|
31
31
|
export declare class MongoDb extends Db {
|
|
32
32
|
#private;
|
|
33
33
|
get Schema(): typeof mongoose.Schema;
|
|
34
34
|
get Id(): mongoose.Types.ObjectId;
|
|
35
35
|
use(dbName?: string): mongoose.Connection;
|
|
36
|
-
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>;
|
|
37
37
|
query<Model>(model: mongoose.Model<Model>, params: QueryParams): Promise<QueryResults<Model>>;
|
|
38
38
|
start(): Promise<void>;
|
|
39
39
|
close(): Promise<void>;
|
package/lib/instance/index.js
CHANGED
|
@@ -59,9 +59,8 @@ class Instance {
|
|
|
59
59
|
return __classPrivateFieldGet(this, _Instance_settings, "f");
|
|
60
60
|
}
|
|
61
61
|
static createLogger() {
|
|
62
|
-
const defaultLogLevel = 'info';
|
|
63
62
|
return (0, pino_1.default)({
|
|
64
|
-
level:
|
|
63
|
+
level: _a.get()?.settings?.logLevel ?? 'info',
|
|
65
64
|
serializers: {
|
|
66
65
|
err: pino_1.default.stdSerializers.err,
|
|
67
66
|
req: pino_1.default.stdSerializers.req,
|
package/lib/instance/settings.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.defaulInstanceSetting = void 0;
|
|
4
4
|
exports.defaulInstanceSetting = {
|
|
5
|
+
isDev: false,
|
|
5
6
|
accessTokenKey: 'accessTokenKey',
|
|
6
7
|
accessTokenTTL: 60 * 60,
|
|
7
8
|
refreshTokenKey: 'refreshTokenKey',
|
|
@@ -17,10 +18,10 @@ exports.defaulInstanceSetting = {
|
|
|
17
18
|
maxFileUploadSizeInMb: 500,
|
|
18
19
|
useRateLimit: false,
|
|
19
20
|
rateLimitPeriodInMs: 60 * 60 * 1000,
|
|
20
|
-
rateLimit:
|
|
21
|
+
rateLimit: 2500,
|
|
21
22
|
useSlowDown: false,
|
|
22
23
|
slowDownPeriodInMs: 10 * 60 * 1000,
|
|
23
|
-
slowDownAfter:
|
|
24
|
+
slowDownAfter: 1000,
|
|
24
25
|
slowDownDelayInMs: 500,
|
|
25
26
|
hashSaltRounds: 10,
|
|
26
27
|
paginationDefaultLimit: 100,
|
|
@@ -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
|
|
17
|
-
updated
|
|
18
|
-
deleted
|
|
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
|
}
|
|
@@ -1,6 +1,2 @@
|
|
|
1
|
-
import { Options } from 'ts-oas';
|
|
2
1
|
import { RouteSchema } from '../server';
|
|
3
|
-
export declare function generateJSONSchema(patterns: (string | RegExp)[], paths: string[],
|
|
4
|
-
tsConfigPath?: string | Record<string, unknown>;
|
|
5
|
-
options?: Options;
|
|
6
|
-
}): Record<string, RouteSchema>;
|
|
2
|
+
export declare function generateJSONSchema(patterns: (string | RegExp)[], paths: string[]): Record<string, RouteSchema>;
|
|
@@ -26,36 +26,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
26
|
exports.generateJSONSchema = void 0;
|
|
27
27
|
const ts_oas_1 = __importStar(require("ts-oas"));
|
|
28
28
|
const instance_1 = require("../instance");
|
|
29
|
-
const server_1 = require("../server");
|
|
30
29
|
const fileSchema = { type: 'string', format: 'binary' };
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
const logger = instance_1.Instance.createLogger();
|
|
35
|
-
const tsoas = new ts_oas_1.default(tsProgram, {
|
|
36
|
-
ref: false,
|
|
37
|
-
nullableKeyword: false,
|
|
38
|
-
...(options?.options ?? {})
|
|
39
|
-
});
|
|
30
|
+
function generateJSONSchema(patterns, paths) {
|
|
31
|
+
const tsProgram = (0, ts_oas_1.createProgram)(paths, { strictNullChecks: true });
|
|
32
|
+
const tsoas = new ts_oas_1.default(tsProgram, { ref: false, ignoreErrors: true });
|
|
40
33
|
const jsonSchema = tsoas.getSchemas(patterns);
|
|
41
34
|
return Object.entries(jsonSchema)
|
|
42
35
|
.map(([name, { properties: def }]) => {
|
|
43
36
|
try {
|
|
44
37
|
const key = def?.key?.enum?.at(0) ?? name;
|
|
45
38
|
if (!def || !key || !def.method)
|
|
46
|
-
|
|
39
|
+
throw new Error();
|
|
47
40
|
const response = def.responses.properties ?? def.responses.anyOf?.reduce((acc, cur) => {
|
|
48
41
|
if (cur.properties)
|
|
49
42
|
return { ...acc, ...cur.properties };
|
|
50
43
|
return acc;
|
|
51
44
|
}, {}) ?? undefined;
|
|
52
|
-
if (response) {
|
|
53
|
-
for (const [key, value] of Object.entries(response)) {
|
|
54
|
-
const status = statusCodes.find(([_, code]) => code.toString() === key);
|
|
55
|
-
if (status)
|
|
56
|
-
value['description'] = `${status[0]} Response`;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
45
|
const body = def.body ?? {};
|
|
60
46
|
if (def.files?.properties) {
|
|
61
47
|
const files = def.files ?? {};
|
|
@@ -67,36 +53,32 @@ function generateJSONSchema(patterns, paths, options) {
|
|
|
67
53
|
body.required = [];
|
|
68
54
|
Object.entries(files.properties ?? {}).forEach(([key, value]) => {
|
|
69
55
|
const isMultiple = !!value.enum?.at(0);
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
anyOf: [fileValue, body.properties[key]].filter(Boolean)
|
|
73
|
-
};
|
|
74
|
-
if (files.required?.includes(key) && !body.required?.includes(key))
|
|
56
|
+
body.properties[key] = isMultiple ? { type: 'array', items: fileSchema } : fileSchema;
|
|
57
|
+
if (files.required?.includes(key))
|
|
75
58
|
body.required.push(key);
|
|
76
59
|
});
|
|
77
60
|
}
|
|
78
|
-
const schema =
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
61
|
+
const schema = def
|
|
62
|
+
? {
|
|
63
|
+
body,
|
|
64
|
+
params: def.params,
|
|
65
|
+
querystring: def.query,
|
|
66
|
+
headers: def.headers,
|
|
67
|
+
response,
|
|
68
|
+
operationId: key,
|
|
69
|
+
summary: name,
|
|
70
|
+
}
|
|
71
|
+
: undefined;
|
|
87
72
|
return [key, schema];
|
|
88
73
|
}
|
|
89
74
|
catch (err) {
|
|
90
|
-
|
|
75
|
+
instance_1.Instance.createLogger().warn(`Error parsing ${name}: ${err.message}. Skipping route`);
|
|
91
76
|
return [undefined, undefined];
|
|
92
77
|
}
|
|
93
78
|
})
|
|
94
79
|
.reduce((acc, [path, schema]) => {
|
|
95
|
-
if (
|
|
96
|
-
|
|
97
|
-
if (acc[path])
|
|
98
|
-
logger.warn(`Duplicate route key '${path}' found for '${acc[path].summary}' & '${schema.summary}'. Make sure to use unique keys for all routes because only the last one will be used.`);
|
|
99
|
-
acc[path] = stripEmptyObjects(schema);
|
|
80
|
+
if (path && schema)
|
|
81
|
+
acc[path] = stripEmptyObjects(schema);
|
|
100
82
|
return acc;
|
|
101
83
|
}, {});
|
|
102
84
|
}
|
|
@@ -10,7 +10,7 @@ import { Defined } from '../../types';
|
|
|
10
10
|
import { Request } from '../requests';
|
|
11
11
|
import { Router } from '../routes';
|
|
12
12
|
import { Route } from '../types';
|
|
13
|
-
export type FullRoute = Required<Omit<Route, 'schema' | '
|
|
13
|
+
export type FullRoute = Required<Omit<Route, 'schema' | 'tags' | 'security' | 'hideSchema' | 'onError' | 'onSetupHandler' | '__def'>> & {
|
|
14
14
|
schema: FastifySchema;
|
|
15
15
|
onError?: Route['onError'];
|
|
16
16
|
};
|
|
@@ -27,10 +27,10 @@ export declare abstract class Server<Req = any, Res = any> {
|
|
|
27
27
|
protected abstract registerRoute(route: FullRoute): void;
|
|
28
28
|
constructor(server: http.Server);
|
|
29
29
|
get listener(): Listener;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
addSchema(...schemas: Schemas[]): void;
|
|
30
|
+
addRoutes(routes: Route[]): void;
|
|
31
|
+
addSchema(schemas: Schemas): void;
|
|
33
32
|
load(): Promise<void>;
|
|
33
|
+
register(router: Router | Router[]): void;
|
|
34
34
|
test(): import("supertest/lib/agent")<supertest.SuperTestStatic.Test>;
|
|
35
35
|
start(port: number): Promise<boolean>;
|
|
36
36
|
}
|
package/lib/server/impls/base.js
CHANGED
|
@@ -13,7 +13,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
13
13
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
15
|
};
|
|
16
|
-
var _Server_instances,
|
|
16
|
+
var _Server_instances, _Server_schemas, _Server_listener, _Server_regRoute;
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.Server = void 0;
|
|
19
19
|
const socket_io_1 = __importDefault(require("socket.io"));
|
|
@@ -22,11 +22,10 @@ const path_1 = __importDefault(require("path"));
|
|
|
22
22
|
const instance_1 = require("../../instance");
|
|
23
23
|
const listeners_1 = require("../../listeners");
|
|
24
24
|
const middlewares_1 = require("../middlewares");
|
|
25
|
+
const routes_1 = require("../routes");
|
|
25
26
|
class Server {
|
|
26
27
|
constructor(server) {
|
|
27
28
|
_Server_instances.add(this);
|
|
28
|
-
_Server_routesByPath.set(this, new Map());
|
|
29
|
-
_Server_routesByKey.set(this, new Map());
|
|
30
29
|
_Server_schemas.set(this, {});
|
|
31
30
|
_Server_listener.set(this, null);
|
|
32
31
|
this.staticPath = path_1.default.join(process.cwd(), 'public');
|
|
@@ -63,32 +62,33 @@ class Server {
|
|
|
63
62
|
}
|
|
64
63
|
return __classPrivateFieldGet(this, _Server_listener, "f");
|
|
65
64
|
}
|
|
66
|
-
|
|
67
|
-
routers.map((router) => router.routes).forEach((routes) => this.addRoute(...routes));
|
|
68
|
-
}
|
|
69
|
-
addRoute(...routes) {
|
|
65
|
+
addRoutes(routes) {
|
|
70
66
|
routes.forEach((route) => __classPrivateFieldGet(this, _Server_instances, "m", _Server_regRoute).call(this, route));
|
|
71
67
|
}
|
|
72
|
-
addSchema(
|
|
73
|
-
|
|
68
|
+
addSchema(schemas) {
|
|
69
|
+
Object.assign(__classPrivateFieldGet(this, _Server_schemas, "f"), schemas);
|
|
74
70
|
}
|
|
75
71
|
async load() {
|
|
76
72
|
await this.onLoad();
|
|
77
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
78
|
test() {
|
|
79
79
|
return (0, supertest_1.default)(this.server);
|
|
80
80
|
}
|
|
81
81
|
async start(port) {
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
const postRoutesRouter = new routes_1.Router();
|
|
83
|
+
postRoutesRouter.get({
|
|
84
84
|
path: '__health',
|
|
85
|
-
handler: async () => `${this.settings.appId} service running`,
|
|
86
85
|
schema: {
|
|
87
86
|
response: {
|
|
88
|
-
200: { type: 'string' }
|
|
87
|
+
200: { type: 'string' },
|
|
89
88
|
}
|
|
90
|
-
}
|
|
91
|
-
});
|
|
89
|
+
}
|
|
90
|
+
})(async () => `${this.settings.appId} service running`);
|
|
91
|
+
this.register(postRoutesRouter);
|
|
92
92
|
const started = await this.startServer(port);
|
|
93
93
|
if (started)
|
|
94
94
|
await instance_1.Instance.get().logger.info(`${this.settings.appId} service listening on port ${port}`);
|
|
@@ -96,16 +96,13 @@ class Server {
|
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
exports.Server = Server;
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const
|
|
103
|
-
|
|
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
104
|
route.onSetupHandler?.(route);
|
|
105
|
-
|
|
106
|
-
const { method, path, handler, schema, security, onError, hideSchema = false } = route;
|
|
107
|
-
const pathKey = `(${method.toUpperCase()}) ${path}`;
|
|
108
|
-
const { key = pathKey } = route;
|
|
105
|
+
onError?.onSetup?.(route);
|
|
109
106
|
const scheme = schema ?? __classPrivateFieldGet(this, _Server_schemas, "f")[key] ?? {};
|
|
110
107
|
const fullRoute = {
|
|
111
108
|
method, middlewares, handler, key,
|
|
@@ -115,16 +112,9 @@ _Server_routesByPath = new WeakMap(), _Server_routesByKey = new WeakMap(), _Serv
|
|
|
115
112
|
...scheme,
|
|
116
113
|
hide: hideSchema,
|
|
117
114
|
operationId: scheme.operationId ?? handler.name,
|
|
118
|
-
tags:
|
|
119
|
-
description: route.descriptions?.join(' | '),
|
|
115
|
+
tags: [tags.join(' > ') || 'default'],
|
|
120
116
|
security,
|
|
121
117
|
}
|
|
122
118
|
};
|
|
123
|
-
if (__classPrivateFieldGet(this, _Server_routesByPath, "f").get(pathKey))
|
|
124
|
-
throw new Error(`Route path ${pathKey} already registered. All route paths and methods combinations must be unique`);
|
|
125
|
-
if (__classPrivateFieldGet(this, _Server_routesByKey, "f").get(key))
|
|
126
|
-
throw new Error(`Route key ${fullRoute.key} already registered. All route keys must be unique`);
|
|
127
|
-
__classPrivateFieldGet(this, _Server_routesByPath, "f").set(pathKey, fullRoute);
|
|
128
|
-
__classPrivateFieldGet(this, _Server_routesByKey, "f").set(key, fullRoute);
|
|
129
119
|
this.registerRoute(fullRoute);
|
|
130
120
|
};
|
|
@@ -23,6 +23,7 @@ const cors_1 = __importDefault(require("cors"));
|
|
|
23
23
|
const express_1 = __importDefault(require("express"));
|
|
24
24
|
const express_fileupload_1 = __importDefault(require("express-fileupload"));
|
|
25
25
|
const express_rate_limit_1 = __importDefault(require("express-rate-limit"));
|
|
26
|
+
const express_slow_down_1 = __importDefault(require("express-slow-down"));
|
|
26
27
|
const helmet_1 = __importDefault(require("helmet"));
|
|
27
28
|
const http_1 = __importDefault(require("http"));
|
|
28
29
|
const json_schema_resolver_1 = __importDefault(require("json-schema-resolver"));
|
|
@@ -65,6 +66,12 @@ class ExpressServer extends base_1.Server {
|
|
|
65
66
|
limit: this.settings.rateLimit,
|
|
66
67
|
handler: (_, res) => res.status(types_1.StatusCodes.TooManyRequests).json([{ message: 'Too Many Requests' }])
|
|
67
68
|
}));
|
|
69
|
+
if (this.settings.useSlowDown)
|
|
70
|
+
app.use((0, express_slow_down_1.default)({
|
|
71
|
+
windowMs: this.settings.slowDownPeriodInMs,
|
|
72
|
+
delayAfter: this.settings.slowDownAfter,
|
|
73
|
+
delayMs: this.settings.slowDownDelayInMs
|
|
74
|
+
}));
|
|
68
75
|
}
|
|
69
76
|
registerRoute(route) {
|
|
70
77
|
const openapi = (0, utils_1.prepareOpenapiMethod)(route.schema, __classPrivateFieldGet(this, _ExpressServer_ref, "f"), this.baseOpenapiDoc, route.path);
|
|
@@ -11,7 +11,7 @@ export declare class FastifyServer extends Server<FastifyRequest, FastifyReply>
|
|
|
11
11
|
protected onLoad(): Promise<void>;
|
|
12
12
|
protected registerRoute(route: FullRoute): void;
|
|
13
13
|
protected startServer(port: number): Promise<boolean>;
|
|
14
|
-
protected parse(req: FastifyRequest, res: FastifyReply): Promise<Request<import("../types").Api<any, string, import("../types").MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>,
|
|
14
|
+
protected parse(req: FastifyRequest, res: FastifyReply): Promise<Request<import("../types").Api<any, string, import("../types").MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>, import("../types").SupportedStatusCodes>>>;
|
|
15
15
|
makeController(cb: Defined<Route['handler']>): RouteHandlerMethod;
|
|
16
16
|
makeMiddleware(cb: Defined<Route['middlewares']>[number]['cb']): preHandlerHookHandler<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").RouteGenericInterface, unknown, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
|
|
17
17
|
makeErrorMiddleware(cb: Defined<Route['onError']>['cb']): (error: import("fastify").FastifyError, request: FastifyRequest<import("fastify").RouteGenericInterface, import("fastify").RawServerDefault, import("http").IncomingMessage, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown, import("fastify").FastifyBaseLogger, import("fastify/types/type-provider").ResolveFastifyRequestType<import("fastify").FastifyTypeProviderDefault, import("fastify").FastifySchema, import("fastify").RouteGenericInterface>>, reply: FastifyReply<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").RouteGenericInterface, unknown, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown>) => void;
|
|
@@ -26,6 +26,7 @@ const static_1 = __importDefault(require("@fastify/static"));
|
|
|
26
26
|
const swagger_1 = __importDefault(require("@fastify/swagger"));
|
|
27
27
|
const swagger_ui_1 = __importDefault(require("@fastify/swagger-ui"));
|
|
28
28
|
const fastify_1 = __importDefault(require("fastify"));
|
|
29
|
+
const fastify_slow_down_1 = __importDefault(require("fastify-slow-down"));
|
|
29
30
|
const qs_1 = __importDefault(require("qs"));
|
|
30
31
|
const errors_1 = require("../../errors");
|
|
31
32
|
const exit_1 = require("../../exit");
|
|
@@ -74,6 +75,12 @@ class FastifyServer extends base_1.Server {
|
|
|
74
75
|
f.value = parsed;
|
|
75
76
|
}
|
|
76
77
|
});
|
|
78
|
+
if (this.settings.useSlowDown)
|
|
79
|
+
app.register(fastify_slow_down_1.default, {
|
|
80
|
+
timeWindow: this.settings.slowDownPeriodInMs,
|
|
81
|
+
delayAfter: this.settings.slowDownAfter,
|
|
82
|
+
delay: this.settings.slowDownDelayInMs
|
|
83
|
+
});
|
|
77
84
|
if (this.settings.useRateLimit)
|
|
78
85
|
app.register(rate_limit_1.default, {
|
|
79
86
|
max: this.settings.rateLimit,
|
|
@@ -114,11 +121,11 @@ class FastifyServer extends base_1.Server {
|
|
|
114
121
|
const allHeaders = Object.fromEntries(Object.entries(req.headers).map(([key, val]) => [key, val ?? null]));
|
|
115
122
|
const headers = {
|
|
116
123
|
...allHeaders,
|
|
117
|
-
AccessToken: req.headers['
|
|
118
|
-
RefreshToken: req.headers['
|
|
119
|
-
ContentType: req.headers['
|
|
124
|
+
AccessToken: req.headers['Access-Token']?.toString() ?? null,
|
|
125
|
+
RefreshToken: req.headers['Refresh-Token']?.toString() ?? null,
|
|
126
|
+
ContentType: req.headers['Content-Type']?.toString() ?? null,
|
|
120
127
|
Referer: req.headers['referer']?.toString() ?? null,
|
|
121
|
-
UserAgent: req.headers['
|
|
128
|
+
UserAgent: req.headers['User-Agent']?.toString() ?? null
|
|
122
129
|
};
|
|
123
130
|
const { body, files } = excludeBufferKeys(req.body ?? {});
|
|
124
131
|
return req.savedReq || (req.savedReq = new requests_1.Request({
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export declare const errorHandler: {
|
|
2
|
-
cb: import("../types").ErrorHandler<import("../types").Api<any, string, import("../types").MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>,
|
|
2
|
+
cb: import("../types").ErrorHandler<import("../types").Api<any, string, import("../types").MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>, import("../types").SupportedStatusCodes>>;
|
|
3
3
|
onSetup?: import("../types").HandlerSetup | undefined;
|
|
4
4
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export declare const notFoundHandler: {
|
|
2
|
-
cb: import("../types").RouteMiddlewareHandler<import("../types").Api<any, string, import("../types").MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>,
|
|
2
|
+
cb: import("../types").RouteMiddlewareHandler<import("../types").Api<any, string, import("../types").MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>, import("../types").SupportedStatusCodes>>;
|
|
3
3
|
onSetup?: import("../types").HandlerSetup | undefined;
|
|
4
4
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export declare const parseAuthUser: {
|
|
2
|
-
cb: import("../types").RouteMiddlewareHandler<import("../types").Api<any, string, import("../types").MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>,
|
|
2
|
+
cb: import("../types").RouteMiddlewareHandler<import("../types").Api<any, string, import("../types").MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>, import("../types").SupportedStatusCodes>>;
|
|
3
3
|
onSetup?: import("../types").HandlerSetup | undefined;
|
|
4
4
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export declare const requireAuthUser: {
|
|
2
|
-
cb: import("../types").RouteMiddlewareHandler<import("../types").Api<any, string, import("../types").MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>,
|
|
2
|
+
cb: import("../types").RouteMiddlewareHandler<import("../types").Api<any, string, import("../types").MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>, import("../types").SupportedStatusCodes>>;
|
|
3
3
|
onSetup?: import("../types").HandlerSetup | undefined;
|
|
4
4
|
};
|
|
@@ -13,6 +13,4 @@ exports.requireAuthUser = (0, types_1.makeMiddleware)(async (request) => {
|
|
|
13
13
|
}, (route) => {
|
|
14
14
|
route.security ?? (route.security = []);
|
|
15
15
|
route.security.push({ AccessToken: [] });
|
|
16
|
-
route.descriptions ?? (route.descriptions = []);
|
|
17
|
-
route.descriptions.push('Requires a valid Access-Token header.');
|
|
18
16
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export declare const requireRefreshUser: {
|
|
2
|
-
cb: import("../types").RouteMiddlewareHandler<import("../types").Api<any, string, import("../types").MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>,
|
|
2
|
+
cb: import("../types").RouteMiddlewareHandler<import("../types").Api<any, string, import("../types").MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>, import("../types").SupportedStatusCodes>>;
|
|
3
3
|
onSetup?: import("../types").HandlerSetup | undefined;
|
|
4
4
|
};
|
|
@@ -14,6 +14,4 @@ exports.requireRefreshUser = (0, types_1.makeMiddleware)(async (request) => {
|
|
|
14
14
|
}, (route) => {
|
|
15
15
|
route.security ?? (route.security = []);
|
|
16
16
|
route.security.push({ RefreshToken: [] });
|
|
17
|
-
route.descriptions ?? (route.descriptions = []);
|
|
18
|
-
route.descriptions.push('Requires a valid Refresh-Token header.');
|
|
19
17
|
});
|
package/lib/server/routes.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ export declare const groupRoutes: (config: GeneralConfig, routes: Route[]) => Ro
|
|
|
4
4
|
export declare class Router extends ClassPropertiesWrapper<AddMethodImpls> {
|
|
5
5
|
#private;
|
|
6
6
|
constructor(config?: GeneralConfig);
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
get routes(): Route<import("./types").ApiDef<import("./types").Api<any, string, import("./types").MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>, Record<string, string | string[]>, import("./types").SupportedStatusCodes>>>[];
|
|
7
|
+
include(router: Router): void;
|
|
8
|
+
get routes(): Route<import("./types").ApiDef<import("./types").Api<any, string, import("./types").MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>, import("./types").SupportedStatusCodes>>>[];
|
|
10
9
|
}
|
package/lib/server/routes.js
CHANGED
|
@@ -19,8 +19,8 @@ const groupRoutes = (config, routes) => routes
|
|
|
19
19
|
.map((route) => ({
|
|
20
20
|
...config,
|
|
21
21
|
...route,
|
|
22
|
-
path: `${config.path}/${route.path}
|
|
23
|
-
|
|
22
|
+
path: `${config.path}/${route.path}`,
|
|
23
|
+
tags: [...(config.tags ?? []), ...(route.tags ?? [])],
|
|
24
24
|
middlewares: [...(config.middlewares ?? []), ...(route.middlewares ?? [])],
|
|
25
25
|
security: [...(config.security ?? []), ...(route.security ?? [])],
|
|
26
26
|
}));
|
|
@@ -36,15 +36,11 @@ class Router extends valleyed_1.ClassPropertiesWrapper {
|
|
|
36
36
|
if (config)
|
|
37
37
|
__classPrivateFieldSet(this, _Router_config, config, "f");
|
|
38
38
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
__classPrivateFieldGet(this, _Router_routes, "f").push(...mapped);
|
|
42
|
-
}
|
|
43
|
-
nest(...routers) {
|
|
44
|
-
routers.forEach((router) => __classPrivateFieldGet(this, _Router_children, "f").push(router));
|
|
39
|
+
include(router) {
|
|
40
|
+
__classPrivateFieldGet(this, _Router_children, "f").push(router);
|
|
45
41
|
}
|
|
46
42
|
get routes() {
|
|
47
|
-
const routes =
|
|
43
|
+
const routes = __classPrivateFieldGet(this, _Router_routes, "f");
|
|
48
44
|
__classPrivateFieldGet(this, _Router_children, "f").forEach((child) => {
|
|
49
45
|
child.routes.forEach((route) => {
|
|
50
46
|
__classPrivateFieldGet(this, _Router_instances, "m", _Router_addRoute).call(this, route.method, route, routes)(route.handler);
|
package/lib/server/types.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export type SupportedStatusCodes = Enum<typeof StatusCodes>;
|
|
|
25
25
|
type GoodStatusCodes = 200;
|
|
26
26
|
type ApiErrors = Record<Exclude<SupportedStatusCodes, GoodStatusCodes>, JSONValue<CustomError['serializedErrors']>>;
|
|
27
27
|
type ApiResponse<T, StatusCode extends SupportedStatusCodes> = Record<StatusCode, JSONValue<T>> | Omit<ApiErrors, StatusCode>;
|
|
28
|
-
export interface Api<Res = any, Key extends string = string, Method extends MethodTypes = MethodTypes, Body = any, Files extends Record<string, boolean> = Record<string, boolean>, Params extends Record<string, string> = Record<string, string>, Query extends Record<string, any> = Record<string, any>,
|
|
28
|
+
export interface Api<Res = any, Key extends string = string, Method extends MethodTypes = MethodTypes, Body = any, Files extends Record<string, boolean> = Record<string, boolean>, Params extends Record<string, string> = Record<string, string>, Query extends Record<string, any> = Record<string, any>, DefaultStatus extends SupportedStatusCodes = SupportedStatusCodes> {
|
|
29
29
|
key: Key;
|
|
30
30
|
method: Method;
|
|
31
31
|
response: Res;
|
|
@@ -33,20 +33,18 @@ export interface Api<Res = any, Key extends string = string, Method extends Meth
|
|
|
33
33
|
files?: Files;
|
|
34
34
|
params?: Params;
|
|
35
35
|
query?: Query;
|
|
36
|
-
headers?: Headers;
|
|
37
36
|
defaultStatusCode?: DefaultStatus;
|
|
38
37
|
}
|
|
39
|
-
export interface ApiDef<T extends
|
|
38
|
+
export interface ApiDef<T extends AnyApi> {
|
|
40
39
|
key: Defined<T['key']>;
|
|
41
40
|
method: Defined<T['method']>;
|
|
42
41
|
body: Defined<T['body']>;
|
|
43
42
|
params: Defined<T['params']>;
|
|
44
43
|
query: Defined<T['query']>;
|
|
45
44
|
files: Defined<T['files']>;
|
|
46
|
-
headers: Defined<T['headers']>;
|
|
47
45
|
responses: ApiResponse<T['response'], GetDefaultStatusCode<T['defaultStatusCode']>>;
|
|
48
46
|
}
|
|
49
|
-
type AnyApi<Method extends MethodTypes = MethodTypes> = Api<any, any, Method, any, any, any, any, any
|
|
47
|
+
type AnyApi<Method extends MethodTypes = MethodTypes> = Api<any, any, Method, any, any, any, any, any>;
|
|
50
48
|
type Awaitable<T> = Promise<T> | T;
|
|
51
49
|
type Res<T, S extends SupportedStatusCodes> = Awaitable<Response<T, S> | T>;
|
|
52
50
|
type InferApiFromApiDef<T> = T extends ApiDef<infer A> ? A : never;
|
|
@@ -55,9 +53,7 @@ export type RouteHandler<Def extends Api = Api> = (req: Request<Def>) => Res<Def
|
|
|
55
53
|
export type ErrorHandler<Def extends Api = Api> = (req: Request<Def>, err: Error) => Res<CustomError['serializedErrors'], GetDefaultStatusCode<Def['defaultStatusCode']>>;
|
|
56
54
|
export type RouteMiddlewareHandler<Def extends Api = Api> = (req: Request<Def>) => Awaitable<void>;
|
|
57
55
|
export type HandlerSetup = (route: Route) => void;
|
|
58
|
-
export type RouteSchema = Omit<FastifySchema, 'tags' | 'security' | 'hide'
|
|
59
|
-
descriptions?: string[];
|
|
60
|
-
};
|
|
56
|
+
export type RouteSchema = Omit<FastifySchema, 'tags' | 'security' | 'hide'>;
|
|
61
57
|
export interface Route<Def extends ApiDef<AnyApi> = ApiDef<Api>> {
|
|
62
58
|
key?: Def['key'];
|
|
63
59
|
path: string;
|
|
@@ -67,8 +63,7 @@ export interface Route<Def extends ApiDef<AnyApi> = ApiDef<Api>> {
|
|
|
67
63
|
middlewares?: ReturnType<typeof makeMiddleware>[];
|
|
68
64
|
onError?: ReturnType<typeof makeErrorMiddleware>;
|
|
69
65
|
schema?: RouteSchema;
|
|
70
|
-
|
|
71
|
-
descriptions?: string[];
|
|
66
|
+
tags?: string[];
|
|
72
67
|
hideSchema?: boolean;
|
|
73
68
|
security?: Record<string, string[]>[];
|
|
74
69
|
}
|
|
@@ -83,6 +78,6 @@ declare class MiddlewareHandler<Cb extends Function> {
|
|
|
83
78
|
private constructor();
|
|
84
79
|
static make<Cb extends Function>(cb: Cb, onSetup?: HandlerSetup): MiddlewareHandler<Cb>;
|
|
85
80
|
}
|
|
86
|
-
export declare const makeMiddleware: <Def extends
|
|
87
|
-
export declare const makeErrorMiddleware: <Def extends
|
|
81
|
+
export declare const makeMiddleware: <Def extends AnyApi<MethodTypes> = Api<any, string, MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>, SupportedStatusCodes>>(cb: RouteMiddlewareHandler<Def>, onSetup?: HandlerSetup | undefined) => MiddlewareHandler<RouteMiddlewareHandler<Def>>;
|
|
82
|
+
export declare const makeErrorMiddleware: <Def extends AnyApi<MethodTypes> = Api<any, string, MethodTypes, any, Record<string, boolean>, Record<string, string>, Record<string, any>, SupportedStatusCodes>>(cb: ErrorHandler<Def>, onSetup?: HandlerSetup | undefined) => MiddlewareHandler<ErrorHandler<Def>>;
|
|
88
83
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ClassPropertiesWrapper } from 'valleyed';
|
|
2
|
-
export declare class BaseEntity<Keys extends object = object, Ignored extends string =
|
|
2
|
+
export declare class BaseEntity<Keys extends object = object, Ignored extends string = string> extends ClassPropertiesWrapper<Keys> {
|
|
3
3
|
__hash: string;
|
|
4
4
|
__type: string;
|
|
5
5
|
readonly __ignoreInJSON: Ignored[];
|
|
@@ -24,11 +24,10 @@ class BaseEntity extends valleyed_1.ClassPropertiesWrapper {
|
|
|
24
24
|
const json = {};
|
|
25
25
|
Object.keys(this)
|
|
26
26
|
.concat(Object.getOwnPropertyNames(Object.getPrototypeOf(this)))
|
|
27
|
+
.filter((k) => k !== 'constructor')
|
|
27
28
|
.forEach((key) => {
|
|
28
29
|
const value = this[key];
|
|
29
|
-
|
|
30
|
-
return;
|
|
31
|
-
json[key] = value?.toJSON?.(includeIgnored) ?? (0, utils_1.clone)(value);
|
|
30
|
+
json[key] = value?.toJSON?.(includeIgnored) ?? structuredClone(value);
|
|
32
31
|
});
|
|
33
32
|
if (!includeIgnored)
|
|
34
33
|
this.__ignoreInJSON.concat('__ignoreInJSON').forEach((k) => deleteKeyFromObject(json, k.split('.')));
|
package/lib/utils/utils.d.ts
CHANGED
package/lib/utils/utils.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.Random = void 0;
|
|
7
7
|
const crypto_1 = __importDefault(require("crypto"));
|
|
8
8
|
class Random {
|
|
9
9
|
static string(length = 20) {
|
|
@@ -14,12 +14,3 @@ class Random {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
exports.Random = Random;
|
|
17
|
-
function clone(value) {
|
|
18
|
-
try {
|
|
19
|
-
return structuredClone(value);
|
|
20
|
-
}
|
|
21
|
-
catch (err) {
|
|
22
|
-
return value;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
exports.clone = clone;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
import * as Validate from 'valleyed';
|
|
2
|
-
import { ExtractI } from 'valleyed/lib/api/base';
|
|
3
|
-
import { VCore } from 'valleyed/lib/api/core';
|
|
4
|
-
import { VObject } from 'valleyed/lib/api/objects';
|
|
5
2
|
import { StorageFile } from '../storage';
|
|
6
3
|
type Phone = {
|
|
7
4
|
code: string;
|
|
@@ -13,14 +10,14 @@ export declare const Schema: {
|
|
|
13
10
|
or: <T_2 extends Validate.VCore<any>[]>(options: T_2, err?: string | undefined) => import("valleyed/lib/api/junctions").VOr<T_2>;
|
|
14
11
|
and: <I>(options: Validate.VCore<I>[], err?: string | undefined) => import("valleyed/lib/api/junctions").VAnd<I>;
|
|
15
12
|
string: (err?: string | undefined) => import("valleyed/lib/api/strings").VString;
|
|
16
|
-
discriminate: <D extends Record<string, Validate.VCore<any>>>(discriminator: (val: ExtractI<D[keyof D]>) => string, schemas: D, err?: string | undefined) => import("valleyed/lib/api/junctions").VDiscriminator<D>;
|
|
13
|
+
discriminate: <D extends Record<string, Validate.VCore<any>>>(discriminator: (val: import("valleyed/lib/api/base").ExtractI<D[keyof D]>) => string, schemas: D, err?: string | undefined) => import("valleyed/lib/api/junctions").VDiscriminator<D>;
|
|
17
14
|
number: (err?: string | undefined) => import("valleyed/lib/api/numbers").VNumber;
|
|
18
15
|
boolean: (err?: string | undefined) => import("valleyed/lib/api/booleans").VBoolean;
|
|
19
16
|
time: <T_3 extends Validate.Timeable = Validate.Timeable>(err?: string | undefined) => import("valleyed/lib/api/times").VTime<T_3>;
|
|
20
17
|
file: (err?: string | undefined) => import("valleyed/lib/api/files").VFile;
|
|
21
18
|
array: <I_1>(comparer: Validate.VCore<I_1>, err?: string | undefined) => import("valleyed/lib/api/arrays").VArray<I_1>;
|
|
22
19
|
tuple: <T_4 extends readonly Validate.VCore<any>[]>(schema: readonly [...T_4], err?: string | undefined) => import("valleyed/lib/api/tuples").VTuple<T_4>;
|
|
23
|
-
object: <T_5 extends Record<string, Validate.VCore<any>>>(schema: T_5, trim?: boolean | undefined, err?: string | undefined) => VObject<T_5>;
|
|
20
|
+
object: <T_5 extends Record<string, Validate.VCore<any>>>(schema: T_5, trim?: boolean | undefined, err?: string | undefined) => import("valleyed/lib/api/objects").VObject<T_5>;
|
|
24
21
|
record: <VI>(vCom: Validate.VCore<VI>, err?: string | undefined) => import("valleyed/lib/api/records").VRecord<VI>;
|
|
25
22
|
map: <KI, VI_1>(kCom: Validate.VCore<KI>, vCom: Validate.VCore<VI_1>, err?: string | undefined) => import("valleyed/lib/api/records").VMap<KI, VI_1>;
|
|
26
23
|
null: (err?: string | undefined) => Validate.VCore<null>;
|
|
@@ -45,14 +42,14 @@ export declare const Validation: {
|
|
|
45
42
|
or: <T_2 extends Validate.VCore<any>[]>(options: T_2, err?: string | undefined) => import("valleyed/lib/api/junctions").VOr<T_2>;
|
|
46
43
|
and: <I>(options: Validate.VCore<I>[], err?: string | undefined) => import("valleyed/lib/api/junctions").VAnd<I>;
|
|
47
44
|
string: (err?: string | undefined) => import("valleyed/lib/api/strings").VString;
|
|
48
|
-
discriminate: <D extends Record<string, Validate.VCore<any>>>(discriminator: (val: ExtractI<D[keyof D]>) => string, schemas: D, err?: string | undefined) => import("valleyed/lib/api/junctions").VDiscriminator<D>;
|
|
45
|
+
discriminate: <D extends Record<string, Validate.VCore<any>>>(discriminator: (val: import("valleyed/lib/api/base").ExtractI<D[keyof D]>) => string, schemas: D, err?: string | undefined) => import("valleyed/lib/api/junctions").VDiscriminator<D>;
|
|
49
46
|
number: (err?: string | undefined) => import("valleyed/lib/api/numbers").VNumber;
|
|
50
47
|
boolean: (err?: string | undefined) => import("valleyed/lib/api/booleans").VBoolean;
|
|
51
48
|
time: <T_3 extends Validate.Timeable = Validate.Timeable>(err?: string | undefined) => import("valleyed/lib/api/times").VTime<T_3>;
|
|
52
49
|
file: (err?: string | undefined) => import("valleyed/lib/api/files").VFile;
|
|
53
50
|
array: <I_1>(comparer: Validate.VCore<I_1>, err?: string | undefined) => import("valleyed/lib/api/arrays").VArray<I_1>;
|
|
54
51
|
tuple: <T_4 extends readonly Validate.VCore<any>[]>(schema: readonly [...T_4], err?: string | undefined) => import("valleyed/lib/api/tuples").VTuple<T_4>;
|
|
55
|
-
object: <T_5 extends Record<string, Validate.VCore<any>>>(schema: T_5, trim?: boolean | undefined, err?: string | undefined) => VObject<T_5>;
|
|
52
|
+
object: <T_5 extends Record<string, Validate.VCore<any>>>(schema: T_5, trim?: boolean | undefined, err?: string | undefined) => import("valleyed/lib/api/objects").VObject<T_5>;
|
|
56
53
|
record: <VI>(vCom: Validate.VCore<VI>, err?: string | undefined) => import("valleyed/lib/api/records").VRecord<VI>;
|
|
57
54
|
map: <KI, VI_1>(kCom: Validate.VCore<KI>, vCom: Validate.VCore<VI_1>, err?: string | undefined) => import("valleyed/lib/api/records").VMap<KI, VI_1>;
|
|
58
55
|
null: (err?: string | undefined) => Validate.VCore<null>;
|
|
@@ -157,8 +154,7 @@ export declare const Validation: {
|
|
|
157
154
|
ignored: boolean;
|
|
158
155
|
};
|
|
159
156
|
};
|
|
160
|
-
export declare
|
|
161
|
-
export declare function validate<T extends VCore<any>>(schema: T, value: unknown): ExtractI<T>;
|
|
157
|
+
export declare const validate: <T extends Record<string, Validate.VCore<any>>, S extends Validate.VCore<any> = Validate.VCore<any>>(schema: T | S, value: unknown) => any;
|
|
162
158
|
export declare const Hash: {
|
|
163
159
|
hash: (password: string) => Promise<string>;
|
|
164
160
|
compare: (plainPassword: string, hashed: string) => Promise<boolean>;
|
package/lib/validations/index.js
CHANGED
|
@@ -47,8 +47,8 @@ const file = Validate.v.file;
|
|
|
47
47
|
Validate.v.file = (...args) => file(...args).addRule(isNotTruncated());
|
|
48
48
|
exports.Schema = Validate.v;
|
|
49
49
|
exports.Validation = { ...Validate, isNotTruncated, isValidPhone };
|
|
50
|
-
|
|
51
|
-
const validator = Validate.v.object(schema);
|
|
50
|
+
const validate = (schema, value) => {
|
|
51
|
+
const validator = schema instanceof Validate.VCore ? schema : Validate.v.object(schema);
|
|
52
52
|
const validity = validator.parse(value);
|
|
53
53
|
if (validity.valid)
|
|
54
54
|
return validity.value;
|
|
@@ -66,7 +66,7 @@ function validate(schema, value) {
|
|
|
66
66
|
}), {});
|
|
67
67
|
throw new errors_1.ValidationError(Object.entries(errorsObject)
|
|
68
68
|
.map(([key, value]) => ({ field: key, messages: value })));
|
|
69
|
-
}
|
|
69
|
+
};
|
|
70
70
|
exports.validate = validate;
|
|
71
71
|
const hash = async (password) => {
|
|
72
72
|
password = password.trim();
|