@devbro/pashmak 0.1.7 → 0.1.9
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/dist/{DatabaseServiceProvider.js → DatabaseServiceProvider.mjs} +5 -9
- package/dist/DatabaseServiceProvider.mjs.map +1 -0
- package/dist/app/console/{DefaultCommand.js → DefaultCommand.mjs} +2 -2
- package/dist/app/console/DefaultCommand.mjs.map +1 -0
- package/dist/app/console/{KeyGenerateCommand.js → KeyGenerateCommand.mjs} +6 -6
- package/dist/app/console/KeyGenerateCommand.mjs.map +1 -0
- package/dist/app/console/{StartCommand.js → StartCommand.mjs} +2 -2
- package/dist/app/console/StartCommand.mjs.map +1 -0
- package/dist/app/console/generate/{GenerateControllerCommand.js → GenerateControllerCommand.mjs} +3 -2
- package/dist/app/console/generate/GenerateControllerCommand.mjs.map +1 -0
- package/dist/app/console/generate/controller.tpl +14 -3
- package/dist/app/console/generate/{index.d.ts → index.d.mts} +1 -1
- package/dist/app/console/generate/{index.js → index.mjs} +1 -1
- package/dist/app/console/index.d.mts +9 -0
- package/dist/app/console/{index.js → index.mjs} +2 -2
- package/dist/app/console/index.mjs.map +1 -0
- package/dist/app/console/migrate/{MakeMigrateCommand.d.ts → GenerateMigrateCommand.d.mts} +2 -2
- package/dist/app/console/migrate/{MakeMigrateCommand.js → GenerateMigrateCommand.mjs} +8 -8
- package/dist/app/console/migrate/GenerateMigrateCommand.mjs.map +1 -0
- package/dist/app/console/migrate/{MigrateCommand.js → MigrateCommand.mjs} +2 -2
- package/dist/app/console/migrate/MigrateCommand.mjs.map +1 -0
- package/dist/app/console/migrate/{MigrateRollbackCommand.js → MigrateRollbackCommand.mjs} +2 -2
- package/dist/app/console/migrate/MigrateRollbackCommand.mjs.map +1 -0
- package/dist/app/console/migrate/index.d.mts +4 -0
- package/dist/app/console/migrate/{index.js → index.mjs} +2 -2
- package/dist/app/console/migrate/index.mjs.map +1 -0
- package/dist/app/console/project/{CreateProjectCommand.js → CreateProjectCommand.mjs} +2 -2
- package/dist/app/console/project/CreateProjectCommand.mjs.map +1 -0
- package/dist/app/console/project/base_project/package.json.tpl +1 -1
- package/dist/bin/DatabaseServiceProvider.cjs +88 -0
- package/dist/bin/app/console/DefaultCommand.cjs +506 -0
- package/dist/bin/app/console/KeyGenerateCommand.cjs +554 -0
- package/dist/bin/app/console/StartCommand.cjs +520 -0
- package/dist/bin/app/console/generate/GenerateControllerCommand.cjs +532 -0
- package/dist/bin/app/console/generate/index.cjs +532 -0
- package/dist/bin/app/console/index.cjs +2167 -0
- package/dist/bin/app/console/migrate/GenerateMigrateCommand.cjs +529 -0
- package/dist/bin/app/console/migrate/MigrateCommand.cjs +565 -0
- package/dist/bin/app/console/migrate/MigrateRollbackCommand.cjs +1684 -0
- package/dist/bin/app/console/migrate/index.cjs +1829 -0
- package/dist/bin/app/console/project/CreateProjectCommand.cjs +155 -0
- package/dist/bin/{pashmak_cli.cjs → bin/pashmak_cli.cjs} +4 -4
- package/dist/bin/config.cjs +24 -0
- package/dist/bin/context.cjs +24 -0
- package/dist/bin/facades.cjs +491 -0
- package/dist/bin/helper.cjs +24 -0
- package/dist/bin/http.cjs +24 -0
- package/dist/bin/index.cjs +2452 -0
- package/dist/bin/logger.cjs +24 -0
- package/dist/bin/mailer.cjs +24 -0
- package/dist/bin/middlewares.cjs +512 -0
- package/dist/bin/orm.cjs +24 -0
- package/dist/bin/{pashmak_cli.js → pashmak_cli.mjs} +1 -1
- package/dist/bin/router.cjs +547 -0
- package/dist/bin/sql.cjs +24 -0
- package/dist/{config.js → config.mjs} +1 -1
- package/dist/config.mjs.map +1 -0
- package/dist/{context.js → context.mjs} +1 -1
- package/dist/context.mjs.map +1 -0
- package/dist/{facades.js → facades.mjs} +30 -57
- package/dist/facades.mjs.map +1 -0
- package/dist/{helper.js → helper.mjs} +1 -1
- package/dist/helper.mjs.map +1 -0
- package/dist/{http.js → http.mjs} +1 -1
- package/dist/http.mjs.map +1 -0
- package/dist/{index.js → index.mjs} +2 -2
- package/dist/index.mjs.map +1 -0
- package/dist/{logger.js → logger.mjs} +1 -1
- package/dist/logger.mjs.map +1 -0
- package/dist/{mailer.js → mailer.mjs} +1 -1
- package/dist/mailer.mjs.map +1 -0
- package/dist/{middlewares.js → middlewares.mjs} +3 -5
- package/dist/middlewares.mjs.map +1 -0
- package/dist/{orm.js → orm.mjs} +1 -1
- package/dist/orm.mjs.map +1 -0
- package/dist/{router.d.ts → router.d.mts} +1 -1
- package/dist/{router.js → router.mjs} +2 -4
- package/dist/router.mjs.map +1 -0
- package/dist/{sql.js → sql.mjs} +1 -1
- package/dist/sql.mjs.map +1 -0
- package/package.json +14 -15
- package/dist/DatabaseServiceProvider.js.map +0 -1
- package/dist/app/console/DefaultCommand.js.map +0 -1
- package/dist/app/console/KeyGenerateCommand.js.map +0 -1
- package/dist/app/console/StartCommand.js.map +0 -1
- package/dist/app/console/generate/GenerateControllerCommand.js.map +0 -1
- package/dist/app/console/index.d.ts +0 -9
- package/dist/app/console/index.js.map +0 -1
- package/dist/app/console/migrate/MakeMigrateCommand.js.map +0 -1
- package/dist/app/console/migrate/MigrateCommand.js.map +0 -1
- package/dist/app/console/migrate/MigrateRollbackCommand.js.map +0 -1
- package/dist/app/console/migrate/index.d.ts +0 -4
- package/dist/app/console/migrate/index.js.map +0 -1
- package/dist/app/console/project/CreateProjectCommand.js.map +0 -1
- package/dist/config.js.map +0 -1
- package/dist/context.js.map +0 -1
- package/dist/facades.js.map +0 -1
- package/dist/helper.js.map +0 -1
- package/dist/http.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/logger.js.map +0 -1
- package/dist/mailer.js.map +0 -1
- package/dist/middlewares.js.map +0 -1
- package/dist/orm.js.map +0 -1
- package/dist/router.js.map +0 -1
- package/dist/sql.js.map +0 -1
- /package/dist/{DatabaseServiceProvider.d.ts → DatabaseServiceProvider.d.mts} +0 -0
- /package/dist/app/console/{DefaultCommand.d.ts → DefaultCommand.d.mts} +0 -0
- /package/dist/app/console/{KeyGenerateCommand.d.ts → KeyGenerateCommand.d.mts} +0 -0
- /package/dist/app/console/{StartCommand.d.ts → StartCommand.d.mts} +0 -0
- /package/dist/app/console/generate/{GenerateControllerCommand.d.ts → GenerateControllerCommand.d.mts} +0 -0
- /package/dist/app/console/generate/{index.js.map → index.mjs.map} +0 -0
- /package/dist/app/console/migrate/{MigrateCommand.d.ts → MigrateCommand.d.mts} +0 -0
- /package/dist/app/console/migrate/{MigrateRollbackCommand.d.ts → MigrateRollbackCommand.d.mts} +0 -0
- /package/dist/app/console/project/{CreateProjectCommand.d.ts → CreateProjectCommand.d.mts} +0 -0
- /package/dist/bin/{pashmak_cli.d.ts → pashmak_cli.d.mts} +0 -0
- /package/dist/bin/{pashmak_cli.js.map → pashmak_cli.mjs.map} +0 -0
- /package/dist/{config.d.ts → config.d.mts} +0 -0
- /package/dist/{context.d.ts → context.d.mts} +0 -0
- /package/dist/{facades.d.ts → facades.d.mts} +0 -0
- /package/dist/{helper.d.ts → helper.d.mts} +0 -0
- /package/dist/{http.d.ts → http.d.mts} +0 -0
- /package/dist/{index.d.ts → index.d.mts} +0 -0
- /package/dist/{logger.d.ts → logger.d.mts} +0 -0
- /package/dist/{mailer.d.ts → mailer.d.mts} +0 -0
- /package/dist/{middlewares.d.ts → middlewares.d.mts} +0 -0
- /package/dist/{orm.d.ts → orm.d.mts} +0 -0
- /package/dist/{sql.d.ts → sql.d.mts} +0 -0
|
@@ -0,0 +1,1829 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
10
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
11
|
+
};
|
|
12
|
+
var __export = (target, all) => {
|
|
13
|
+
for (var name in all)
|
|
14
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
|
+
};
|
|
16
|
+
var __copyProps = (to, from, except, desc) => {
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
+
for (let key of __getOwnPropNames(from))
|
|
19
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
20
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
25
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
26
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
27
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
28
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
29
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
30
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
31
|
+
mod
|
|
32
|
+
));
|
|
33
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
34
|
+
|
|
35
|
+
// ../node_modules/typanion/lib/index.js
|
|
36
|
+
var require_lib = __commonJS({
|
|
37
|
+
"../node_modules/typanion/lib/index.js"(exports2) {
|
|
38
|
+
"use strict";
|
|
39
|
+
Object.defineProperty(exports2, "__esModule", {
|
|
40
|
+
value: true
|
|
41
|
+
});
|
|
42
|
+
var simpleKeyRegExp = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
|
|
43
|
+
function getPrintable(value) {
|
|
44
|
+
if (value === null) return `null`;
|
|
45
|
+
if (value === void 0) return `undefined`;
|
|
46
|
+
if (value === ``) return `an empty string`;
|
|
47
|
+
if (typeof value === "symbol") return `<${value.toString()}>`;
|
|
48
|
+
if (Array.isArray(value)) return `an array`;
|
|
49
|
+
return JSON.stringify(value);
|
|
50
|
+
}
|
|
51
|
+
__name(getPrintable, "getPrintable");
|
|
52
|
+
function getPrintableArray(value, conjunction) {
|
|
53
|
+
if (value.length === 0) return `nothing`;
|
|
54
|
+
if (value.length === 1) return getPrintable(value[0]);
|
|
55
|
+
const rest = value.slice(0, -1);
|
|
56
|
+
const trailing = value[value.length - 1];
|
|
57
|
+
const separator = value.length > 2 ? `, ${conjunction} ` : ` ${conjunction} `;
|
|
58
|
+
return `${rest.map((value2) => getPrintable(value2)).join(`, `)}${separator}${getPrintable(trailing)}`;
|
|
59
|
+
}
|
|
60
|
+
__name(getPrintableArray, "getPrintableArray");
|
|
61
|
+
function computeKey(state, key) {
|
|
62
|
+
var _a, _b, _c;
|
|
63
|
+
if (typeof key === `number`) {
|
|
64
|
+
return `${(_a = state === null || state === void 0 ? void 0 : state.p) !== null && _a !== void 0 ? _a : `.`}[${key}]`;
|
|
65
|
+
} else if (simpleKeyRegExp.test(key)) {
|
|
66
|
+
return `${(_b = state === null || state === void 0 ? void 0 : state.p) !== null && _b !== void 0 ? _b : ``}.${key}`;
|
|
67
|
+
} else {
|
|
68
|
+
return `${(_c = state === null || state === void 0 ? void 0 : state.p) !== null && _c !== void 0 ? _c : `.`}[${JSON.stringify(key)}]`;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
__name(computeKey, "computeKey");
|
|
72
|
+
function plural(n, singular, plural2) {
|
|
73
|
+
return n === 1 ? singular : plural2;
|
|
74
|
+
}
|
|
75
|
+
__name(plural, "plural");
|
|
76
|
+
var colorStringRegExp = /^#[0-9a-f]{6}$/i;
|
|
77
|
+
var colorStringAlphaRegExp = /^#[0-9a-f]{6}([0-9a-f]{2})?$/i;
|
|
78
|
+
var base64RegExp = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
|
|
79
|
+
var uuid4RegExp = /^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}$/i;
|
|
80
|
+
var iso8601RegExp = /^(?:[1-9]\d{3}(-?)(?:(?:0[1-9]|1[0-2])\1(?:0[1-9]|1\d|2[0-8])|(?:0[13-9]|1[0-2])\1(?:29|30)|(?:0[13578]|1[02])(?:\1)31|00[1-9]|0[1-9]\d|[12]\d{2}|3(?:[0-5]\d|6[0-5]))|(?:[1-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00)(?:(-?)02(?:\2)29|-?366))T(?:[01]\d|2[0-3])(:?)[0-5]\d(?:\3[0-5]\d)?(?:Z|[+-][01]\d(?:\3[0-5]\d)?)$/;
|
|
81
|
+
function pushError({ errors, p } = {}, message) {
|
|
82
|
+
errors === null || errors === void 0 ? void 0 : errors.push(`${p !== null && p !== void 0 ? p : `.`}: ${message}`);
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
__name(pushError, "pushError");
|
|
86
|
+
function makeSetter(target, key) {
|
|
87
|
+
return (v) => {
|
|
88
|
+
target[key] = v;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
__name(makeSetter, "makeSetter");
|
|
92
|
+
function makeCoercionFn(target, key) {
|
|
93
|
+
return (v) => {
|
|
94
|
+
const previous = target[key];
|
|
95
|
+
target[key] = v;
|
|
96
|
+
return makeCoercionFn(target, key).bind(null, previous);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
__name(makeCoercionFn, "makeCoercionFn");
|
|
100
|
+
function makeLazyCoercionFn(fn2, orig, generator) {
|
|
101
|
+
const commit = /* @__PURE__ */ __name(() => {
|
|
102
|
+
fn2(generator());
|
|
103
|
+
return revert;
|
|
104
|
+
}, "commit");
|
|
105
|
+
const revert = /* @__PURE__ */ __name(() => {
|
|
106
|
+
fn2(orig);
|
|
107
|
+
return commit;
|
|
108
|
+
}, "revert");
|
|
109
|
+
return commit;
|
|
110
|
+
}
|
|
111
|
+
__name(makeLazyCoercionFn, "makeLazyCoercionFn");
|
|
112
|
+
function isUnknown() {
|
|
113
|
+
return makeValidator({
|
|
114
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
115
|
+
return true;
|
|
116
|
+
}, "test")
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
__name(isUnknown, "isUnknown");
|
|
120
|
+
function isLiteral(expected) {
|
|
121
|
+
return makeValidator({
|
|
122
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
123
|
+
if (value !== expected) return pushError(state, `Expected ${getPrintable(expected)} (got ${getPrintable(value)})`);
|
|
124
|
+
return true;
|
|
125
|
+
}, "test")
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
__name(isLiteral, "isLiteral");
|
|
129
|
+
function isString() {
|
|
130
|
+
return makeValidator({
|
|
131
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
132
|
+
if (typeof value !== `string`) return pushError(state, `Expected a string (got ${getPrintable(value)})`);
|
|
133
|
+
return true;
|
|
134
|
+
}, "test")
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
__name(isString, "isString");
|
|
138
|
+
function isEnum(enumSpec) {
|
|
139
|
+
const valuesArray = Array.isArray(enumSpec) ? enumSpec : Object.values(enumSpec);
|
|
140
|
+
const isAlphaNum = valuesArray.every((item) => typeof item === "string" || typeof item === "number");
|
|
141
|
+
const values = new Set(valuesArray);
|
|
142
|
+
if (values.size === 1) return isLiteral([
|
|
143
|
+
...values
|
|
144
|
+
][0]);
|
|
145
|
+
return makeValidator({
|
|
146
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
147
|
+
if (!values.has(value)) {
|
|
148
|
+
if (isAlphaNum) {
|
|
149
|
+
return pushError(state, `Expected one of ${getPrintableArray(valuesArray, `or`)} (got ${getPrintable(value)})`);
|
|
150
|
+
} else {
|
|
151
|
+
return pushError(state, `Expected a valid enumeration value (got ${getPrintable(value)})`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return true;
|
|
155
|
+
}, "test")
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
__name(isEnum, "isEnum");
|
|
159
|
+
var BOOLEAN_COERCIONS = /* @__PURE__ */ new Map([
|
|
160
|
+
[
|
|
161
|
+
`true`,
|
|
162
|
+
true
|
|
163
|
+
],
|
|
164
|
+
[
|
|
165
|
+
`True`,
|
|
166
|
+
true
|
|
167
|
+
],
|
|
168
|
+
[
|
|
169
|
+
`1`,
|
|
170
|
+
true
|
|
171
|
+
],
|
|
172
|
+
[
|
|
173
|
+
1,
|
|
174
|
+
true
|
|
175
|
+
],
|
|
176
|
+
[
|
|
177
|
+
`false`,
|
|
178
|
+
false
|
|
179
|
+
],
|
|
180
|
+
[
|
|
181
|
+
`False`,
|
|
182
|
+
false
|
|
183
|
+
],
|
|
184
|
+
[
|
|
185
|
+
`0`,
|
|
186
|
+
false
|
|
187
|
+
],
|
|
188
|
+
[
|
|
189
|
+
0,
|
|
190
|
+
false
|
|
191
|
+
]
|
|
192
|
+
]);
|
|
193
|
+
function isBoolean() {
|
|
194
|
+
return makeValidator({
|
|
195
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
196
|
+
var _a;
|
|
197
|
+
if (typeof value !== `boolean`) {
|
|
198
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
199
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
200
|
+
const coercion = BOOLEAN_COERCIONS.get(value);
|
|
201
|
+
if (typeof coercion !== `undefined`) {
|
|
202
|
+
state.coercions.push([
|
|
203
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
204
|
+
state.coercion.bind(null, coercion)
|
|
205
|
+
]);
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return pushError(state, `Expected a boolean (got ${getPrintable(value)})`);
|
|
210
|
+
}
|
|
211
|
+
return true;
|
|
212
|
+
}, "test")
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
__name(isBoolean, "isBoolean");
|
|
216
|
+
function isNumber2() {
|
|
217
|
+
return makeValidator({
|
|
218
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
219
|
+
var _a;
|
|
220
|
+
if (typeof value !== `number`) {
|
|
221
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
222
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
223
|
+
let coercion;
|
|
224
|
+
if (typeof value === `string`) {
|
|
225
|
+
let val;
|
|
226
|
+
try {
|
|
227
|
+
val = JSON.parse(value);
|
|
228
|
+
} catch (_b) {
|
|
229
|
+
}
|
|
230
|
+
if (typeof val === `number`) {
|
|
231
|
+
if (JSON.stringify(val) === value) {
|
|
232
|
+
coercion = val;
|
|
233
|
+
} else {
|
|
234
|
+
return pushError(state, `Received a number that can't be safely represented by the runtime (${value})`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (typeof coercion !== `undefined`) {
|
|
239
|
+
state.coercions.push([
|
|
240
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
241
|
+
state.coercion.bind(null, coercion)
|
|
242
|
+
]);
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return pushError(state, `Expected a number (got ${getPrintable(value)})`);
|
|
247
|
+
}
|
|
248
|
+
return true;
|
|
249
|
+
}, "test")
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
__name(isNumber2, "isNumber");
|
|
253
|
+
function isPayload(spec) {
|
|
254
|
+
return makeValidator({
|
|
255
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
256
|
+
var _a;
|
|
257
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) === `undefined`) return pushError(state, `The isPayload predicate can only be used with coercion enabled`);
|
|
258
|
+
if (typeof state.coercion === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
259
|
+
if (typeof value !== `string`) return pushError(state, `Expected a string (got ${getPrintable(value)})`);
|
|
260
|
+
let inner;
|
|
261
|
+
try {
|
|
262
|
+
inner = JSON.parse(value);
|
|
263
|
+
} catch (_b) {
|
|
264
|
+
return pushError(state, `Expected a JSON string (got ${getPrintable(value)})`);
|
|
265
|
+
}
|
|
266
|
+
const wrapper = {
|
|
267
|
+
value: inner
|
|
268
|
+
};
|
|
269
|
+
if (!spec(inner, Object.assign(Object.assign({}, state), {
|
|
270
|
+
coercion: makeCoercionFn(wrapper, `value`)
|
|
271
|
+
}))) return false;
|
|
272
|
+
state.coercions.push([
|
|
273
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
274
|
+
state.coercion.bind(null, wrapper.value)
|
|
275
|
+
]);
|
|
276
|
+
return true;
|
|
277
|
+
}, "test")
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
__name(isPayload, "isPayload");
|
|
281
|
+
function isDate() {
|
|
282
|
+
return makeValidator({
|
|
283
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
284
|
+
var _a;
|
|
285
|
+
if (!(value instanceof Date)) {
|
|
286
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
287
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
288
|
+
let coercion;
|
|
289
|
+
if (typeof value === `string` && iso8601RegExp.test(value)) {
|
|
290
|
+
coercion = new Date(value);
|
|
291
|
+
} else {
|
|
292
|
+
let timestamp;
|
|
293
|
+
if (typeof value === `string`) {
|
|
294
|
+
let val;
|
|
295
|
+
try {
|
|
296
|
+
val = JSON.parse(value);
|
|
297
|
+
} catch (_b) {
|
|
298
|
+
}
|
|
299
|
+
if (typeof val === `number`) {
|
|
300
|
+
timestamp = val;
|
|
301
|
+
}
|
|
302
|
+
} else if (typeof value === `number`) {
|
|
303
|
+
timestamp = value;
|
|
304
|
+
}
|
|
305
|
+
if (typeof timestamp !== `undefined`) {
|
|
306
|
+
if (Number.isSafeInteger(timestamp) || !Number.isSafeInteger(timestamp * 1e3)) {
|
|
307
|
+
coercion = new Date(timestamp * 1e3);
|
|
308
|
+
} else {
|
|
309
|
+
return pushError(state, `Received a timestamp that can't be safely represented by the runtime (${value})`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (typeof coercion !== `undefined`) {
|
|
314
|
+
state.coercions.push([
|
|
315
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
316
|
+
state.coercion.bind(null, coercion)
|
|
317
|
+
]);
|
|
318
|
+
return true;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return pushError(state, `Expected a date (got ${getPrintable(value)})`);
|
|
322
|
+
}
|
|
323
|
+
return true;
|
|
324
|
+
}, "test")
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
__name(isDate, "isDate");
|
|
328
|
+
function isArray(spec, { delimiter } = {}) {
|
|
329
|
+
return makeValidator({
|
|
330
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
331
|
+
var _a;
|
|
332
|
+
const originalValue = value;
|
|
333
|
+
if (typeof value === `string` && typeof delimiter !== `undefined`) {
|
|
334
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
335
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
336
|
+
value = value.split(delimiter);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (!Array.isArray(value)) return pushError(state, `Expected an array (got ${getPrintable(value)})`);
|
|
340
|
+
let valid = true;
|
|
341
|
+
for (let t2 = 0, T = value.length; t2 < T; ++t2) {
|
|
342
|
+
valid = spec(value[t2], Object.assign(Object.assign({}, state), {
|
|
343
|
+
p: computeKey(state, t2),
|
|
344
|
+
coercion: makeCoercionFn(value, t2)
|
|
345
|
+
})) && valid;
|
|
346
|
+
if (!valid && (state === null || state === void 0 ? void 0 : state.errors) == null) {
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (value !== originalValue) state.coercions.push([
|
|
351
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
352
|
+
state.coercion.bind(null, value)
|
|
353
|
+
]);
|
|
354
|
+
return valid;
|
|
355
|
+
}, "test")
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
__name(isArray, "isArray");
|
|
359
|
+
function isSet(spec, { delimiter } = {}) {
|
|
360
|
+
const isArrayValidator = isArray(spec, {
|
|
361
|
+
delimiter
|
|
362
|
+
});
|
|
363
|
+
return makeValidator({
|
|
364
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
365
|
+
var _a, _b;
|
|
366
|
+
if (Object.getPrototypeOf(value).toString() === `[object Set]`) {
|
|
367
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
368
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
369
|
+
const originalValues = [
|
|
370
|
+
...value
|
|
371
|
+
];
|
|
372
|
+
const coercedValues = [
|
|
373
|
+
...value
|
|
374
|
+
];
|
|
375
|
+
if (!isArrayValidator(coercedValues, Object.assign(Object.assign({}, state), {
|
|
376
|
+
coercion: void 0
|
|
377
|
+
}))) return false;
|
|
378
|
+
const updateValue = /* @__PURE__ */ __name(() => coercedValues.some((val, t2) => val !== originalValues[t2]) ? new Set(coercedValues) : value, "updateValue");
|
|
379
|
+
state.coercions.push([
|
|
380
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
381
|
+
makeLazyCoercionFn(state.coercion, value, updateValue)
|
|
382
|
+
]);
|
|
383
|
+
return true;
|
|
384
|
+
} else {
|
|
385
|
+
let valid = true;
|
|
386
|
+
for (const subValue of value) {
|
|
387
|
+
valid = spec(subValue, Object.assign({}, state)) && valid;
|
|
388
|
+
if (!valid && (state === null || state === void 0 ? void 0 : state.errors) == null) {
|
|
389
|
+
break;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return valid;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
396
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
397
|
+
const store = {
|
|
398
|
+
value
|
|
399
|
+
};
|
|
400
|
+
if (!isArrayValidator(value, Object.assign(Object.assign({}, state), {
|
|
401
|
+
coercion: makeCoercionFn(store, `value`)
|
|
402
|
+
}))) return false;
|
|
403
|
+
state.coercions.push([
|
|
404
|
+
(_b = state.p) !== null && _b !== void 0 ? _b : `.`,
|
|
405
|
+
makeLazyCoercionFn(state.coercion, value, () => new Set(store.value))
|
|
406
|
+
]);
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
return pushError(state, `Expected a set (got ${getPrintable(value)})`);
|
|
410
|
+
}, "test")
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
__name(isSet, "isSet");
|
|
414
|
+
function isMap(keySpec, valueSpec) {
|
|
415
|
+
const isArrayValidator = isArray(isTuple([
|
|
416
|
+
keySpec,
|
|
417
|
+
valueSpec
|
|
418
|
+
]));
|
|
419
|
+
const isRecordValidator = isRecord(valueSpec, {
|
|
420
|
+
keys: keySpec
|
|
421
|
+
});
|
|
422
|
+
return makeValidator({
|
|
423
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
424
|
+
var _a, _b, _c;
|
|
425
|
+
if (Object.getPrototypeOf(value).toString() === `[object Map]`) {
|
|
426
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
427
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
428
|
+
const originalValues = [
|
|
429
|
+
...value
|
|
430
|
+
];
|
|
431
|
+
const coercedValues = [
|
|
432
|
+
...value
|
|
433
|
+
];
|
|
434
|
+
if (!isArrayValidator(coercedValues, Object.assign(Object.assign({}, state), {
|
|
435
|
+
coercion: void 0
|
|
436
|
+
}))) return false;
|
|
437
|
+
const updateValue = /* @__PURE__ */ __name(() => coercedValues.some((val, t2) => val[0] !== originalValues[t2][0] || val[1] !== originalValues[t2][1]) ? new Map(coercedValues) : value, "updateValue");
|
|
438
|
+
state.coercions.push([
|
|
439
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
440
|
+
makeLazyCoercionFn(state.coercion, value, updateValue)
|
|
441
|
+
]);
|
|
442
|
+
return true;
|
|
443
|
+
} else {
|
|
444
|
+
let valid = true;
|
|
445
|
+
for (const [key, subValue] of value) {
|
|
446
|
+
valid = keySpec(key, Object.assign({}, state)) && valid;
|
|
447
|
+
if (!valid && (state === null || state === void 0 ? void 0 : state.errors) == null) {
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
valid = valueSpec(subValue, Object.assign(Object.assign({}, state), {
|
|
451
|
+
p: computeKey(state, key)
|
|
452
|
+
})) && valid;
|
|
453
|
+
if (!valid && (state === null || state === void 0 ? void 0 : state.errors) == null) {
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return valid;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
461
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
462
|
+
const store = {
|
|
463
|
+
value
|
|
464
|
+
};
|
|
465
|
+
if (Array.isArray(value)) {
|
|
466
|
+
if (!isArrayValidator(value, Object.assign(Object.assign({}, state), {
|
|
467
|
+
coercion: void 0
|
|
468
|
+
}))) return false;
|
|
469
|
+
state.coercions.push([
|
|
470
|
+
(_b = state.p) !== null && _b !== void 0 ? _b : `.`,
|
|
471
|
+
makeLazyCoercionFn(state.coercion, value, () => new Map(store.value))
|
|
472
|
+
]);
|
|
473
|
+
return true;
|
|
474
|
+
} else {
|
|
475
|
+
if (!isRecordValidator(value, Object.assign(Object.assign({}, state), {
|
|
476
|
+
coercion: makeCoercionFn(store, `value`)
|
|
477
|
+
}))) return false;
|
|
478
|
+
state.coercions.push([
|
|
479
|
+
(_c = state.p) !== null && _c !== void 0 ? _c : `.`,
|
|
480
|
+
makeLazyCoercionFn(state.coercion, value, () => new Map(Object.entries(store.value)))
|
|
481
|
+
]);
|
|
482
|
+
return true;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return pushError(state, `Expected a map (got ${getPrintable(value)})`);
|
|
486
|
+
}, "test")
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
__name(isMap, "isMap");
|
|
490
|
+
function isTuple(spec, { delimiter } = {}) {
|
|
491
|
+
const lengthValidator = hasExactLength(spec.length);
|
|
492
|
+
return makeValidator({
|
|
493
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
494
|
+
var _a;
|
|
495
|
+
if (typeof value === `string` && typeof delimiter !== `undefined`) {
|
|
496
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
497
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
498
|
+
value = value.split(delimiter);
|
|
499
|
+
state.coercions.push([
|
|
500
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
501
|
+
state.coercion.bind(null, value)
|
|
502
|
+
]);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
if (!Array.isArray(value)) return pushError(state, `Expected a tuple (got ${getPrintable(value)})`);
|
|
506
|
+
let valid = lengthValidator(value, Object.assign({}, state));
|
|
507
|
+
for (let t2 = 0, T = value.length; t2 < T && t2 < spec.length; ++t2) {
|
|
508
|
+
valid = spec[t2](value[t2], Object.assign(Object.assign({}, state), {
|
|
509
|
+
p: computeKey(state, t2),
|
|
510
|
+
coercion: makeCoercionFn(value, t2)
|
|
511
|
+
})) && valid;
|
|
512
|
+
if (!valid && (state === null || state === void 0 ? void 0 : state.errors) == null) {
|
|
513
|
+
break;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return valid;
|
|
517
|
+
}, "test")
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
__name(isTuple, "isTuple");
|
|
521
|
+
function isRecord(spec, { keys: keySpec = null } = {}) {
|
|
522
|
+
const isArrayValidator = isArray(isTuple([
|
|
523
|
+
keySpec !== null && keySpec !== void 0 ? keySpec : isString(),
|
|
524
|
+
spec
|
|
525
|
+
]));
|
|
526
|
+
return makeValidator({
|
|
527
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
528
|
+
var _a;
|
|
529
|
+
if (Array.isArray(value)) {
|
|
530
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
531
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
532
|
+
if (!isArrayValidator(value, Object.assign(Object.assign({}, state), {
|
|
533
|
+
coercion: void 0
|
|
534
|
+
}))) return false;
|
|
535
|
+
value = Object.fromEntries(value);
|
|
536
|
+
state.coercions.push([
|
|
537
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
538
|
+
state.coercion.bind(null, value)
|
|
539
|
+
]);
|
|
540
|
+
return true;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
if (typeof value !== `object` || value === null) return pushError(state, `Expected an object (got ${getPrintable(value)})`);
|
|
544
|
+
const keys = Object.keys(value);
|
|
545
|
+
let valid = true;
|
|
546
|
+
for (let t2 = 0, T = keys.length; t2 < T && (valid || (state === null || state === void 0 ? void 0 : state.errors) != null); ++t2) {
|
|
547
|
+
const key = keys[t2];
|
|
548
|
+
const sub = value[key];
|
|
549
|
+
if (key === `__proto__` || key === `constructor`) {
|
|
550
|
+
valid = pushError(Object.assign(Object.assign({}, state), {
|
|
551
|
+
p: computeKey(state, key)
|
|
552
|
+
}), `Unsafe property name`);
|
|
553
|
+
continue;
|
|
554
|
+
}
|
|
555
|
+
if (keySpec !== null && !keySpec(key, state)) {
|
|
556
|
+
valid = false;
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
if (!spec(sub, Object.assign(Object.assign({}, state), {
|
|
560
|
+
p: computeKey(state, key),
|
|
561
|
+
coercion: makeCoercionFn(value, key)
|
|
562
|
+
}))) {
|
|
563
|
+
valid = false;
|
|
564
|
+
continue;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
return valid;
|
|
568
|
+
}, "test")
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
__name(isRecord, "isRecord");
|
|
572
|
+
function isDict(spec, opts = {}) {
|
|
573
|
+
return isRecord(spec, opts);
|
|
574
|
+
}
|
|
575
|
+
__name(isDict, "isDict");
|
|
576
|
+
function isObject(props, { extra: extraSpec = null } = {}) {
|
|
577
|
+
const specKeys = Object.keys(props);
|
|
578
|
+
const validator = makeValidator({
|
|
579
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
580
|
+
if (typeof value !== `object` || value === null) return pushError(state, `Expected an object (got ${getPrintable(value)})`);
|
|
581
|
+
const keys = /* @__PURE__ */ new Set([
|
|
582
|
+
...specKeys,
|
|
583
|
+
...Object.keys(value)
|
|
584
|
+
]);
|
|
585
|
+
const extra = {};
|
|
586
|
+
let valid = true;
|
|
587
|
+
for (const key of keys) {
|
|
588
|
+
if (key === `constructor` || key === `__proto__`) {
|
|
589
|
+
valid = pushError(Object.assign(Object.assign({}, state), {
|
|
590
|
+
p: computeKey(state, key)
|
|
591
|
+
}), `Unsafe property name`);
|
|
592
|
+
} else {
|
|
593
|
+
const spec = Object.prototype.hasOwnProperty.call(props, key) ? props[key] : void 0;
|
|
594
|
+
const sub = Object.prototype.hasOwnProperty.call(value, key) ? value[key] : void 0;
|
|
595
|
+
if (typeof spec !== `undefined`) {
|
|
596
|
+
valid = spec(sub, Object.assign(Object.assign({}, state), {
|
|
597
|
+
p: computeKey(state, key),
|
|
598
|
+
coercion: makeCoercionFn(value, key)
|
|
599
|
+
})) && valid;
|
|
600
|
+
} else if (extraSpec === null) {
|
|
601
|
+
valid = pushError(Object.assign(Object.assign({}, state), {
|
|
602
|
+
p: computeKey(state, key)
|
|
603
|
+
}), `Extraneous property (got ${getPrintable(sub)})`);
|
|
604
|
+
} else {
|
|
605
|
+
Object.defineProperty(extra, key, {
|
|
606
|
+
enumerable: true,
|
|
607
|
+
get: /* @__PURE__ */ __name(() => sub, "get"),
|
|
608
|
+
set: makeSetter(value, key)
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
if (!valid && (state === null || state === void 0 ? void 0 : state.errors) == null) {
|
|
613
|
+
break;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
if (extraSpec !== null && (valid || (state === null || state === void 0 ? void 0 : state.errors) != null)) valid = extraSpec(extra, state) && valid;
|
|
617
|
+
return valid;
|
|
618
|
+
}, "test")
|
|
619
|
+
});
|
|
620
|
+
return Object.assign(validator, {
|
|
621
|
+
properties: props
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
__name(isObject, "isObject");
|
|
625
|
+
function isPartial(props) {
|
|
626
|
+
return isObject(props, {
|
|
627
|
+
extra: isRecord(isUnknown())
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
__name(isPartial, "isPartial");
|
|
631
|
+
var isInstanceOf = /* @__PURE__ */ __name((constructor) => makeValidator({
|
|
632
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
633
|
+
if (!(value instanceof constructor)) return pushError(state, `Expected an instance of ${constructor.name} (got ${getPrintable(value)})`);
|
|
634
|
+
return true;
|
|
635
|
+
}, "test")
|
|
636
|
+
}), "isInstanceOf");
|
|
637
|
+
var isOneOf = /* @__PURE__ */ __name((specs, { exclusive = false } = {}) => makeValidator({
|
|
638
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
639
|
+
var _a, _b, _c;
|
|
640
|
+
const matches = [];
|
|
641
|
+
const errorBuffer = typeof (state === null || state === void 0 ? void 0 : state.errors) !== `undefined` ? [] : void 0;
|
|
642
|
+
for (let t2 = 0, T = specs.length; t2 < T; ++t2) {
|
|
643
|
+
const subErrors = typeof (state === null || state === void 0 ? void 0 : state.errors) !== `undefined` ? [] : void 0;
|
|
644
|
+
const subCoercions = typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined` ? [] : void 0;
|
|
645
|
+
if (specs[t2](value, Object.assign(Object.assign({}, state), {
|
|
646
|
+
errors: subErrors,
|
|
647
|
+
coercions: subCoercions,
|
|
648
|
+
p: `${(_a = state === null || state === void 0 ? void 0 : state.p) !== null && _a !== void 0 ? _a : `.`}#${t2 + 1}`
|
|
649
|
+
}))) {
|
|
650
|
+
matches.push([
|
|
651
|
+
`#${t2 + 1}`,
|
|
652
|
+
subCoercions
|
|
653
|
+
]);
|
|
654
|
+
if (!exclusive) {
|
|
655
|
+
break;
|
|
656
|
+
}
|
|
657
|
+
} else {
|
|
658
|
+
errorBuffer === null || errorBuffer === void 0 ? void 0 : errorBuffer.push(subErrors[0]);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
if (matches.length === 1) {
|
|
662
|
+
const [, subCoercions] = matches[0];
|
|
663
|
+
if (typeof subCoercions !== `undefined`) (_b = state === null || state === void 0 ? void 0 : state.coercions) === null || _b === void 0 ? void 0 : _b.push(...subCoercions);
|
|
664
|
+
return true;
|
|
665
|
+
}
|
|
666
|
+
if (matches.length > 1) pushError(state, `Expected to match exactly a single predicate (matched ${matches.join(`, `)})`);
|
|
667
|
+
else (_c = state === null || state === void 0 ? void 0 : state.errors) === null || _c === void 0 ? void 0 : _c.push(...errorBuffer);
|
|
668
|
+
return false;
|
|
669
|
+
}, "test")
|
|
670
|
+
}), "isOneOf");
|
|
671
|
+
function makeTrait(value) {
|
|
672
|
+
return () => {
|
|
673
|
+
return value;
|
|
674
|
+
};
|
|
675
|
+
}
|
|
676
|
+
__name(makeTrait, "makeTrait");
|
|
677
|
+
function makeValidator({ test }) {
|
|
678
|
+
return makeTrait(test)();
|
|
679
|
+
}
|
|
680
|
+
__name(makeValidator, "makeValidator");
|
|
681
|
+
var TypeAssertionError = class TypeAssertionError extends Error {
|
|
682
|
+
static {
|
|
683
|
+
__name(this, "TypeAssertionError");
|
|
684
|
+
}
|
|
685
|
+
constructor({ errors } = {}) {
|
|
686
|
+
let errorMessage = `Type mismatch`;
|
|
687
|
+
if (errors && errors.length > 0) {
|
|
688
|
+
errorMessage += `
|
|
689
|
+
`;
|
|
690
|
+
for (const error of errors) {
|
|
691
|
+
errorMessage += `
|
|
692
|
+
- ${error}`;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
super(errorMessage);
|
|
696
|
+
}
|
|
697
|
+
};
|
|
698
|
+
function assert(val, validator) {
|
|
699
|
+
if (!validator(val)) {
|
|
700
|
+
throw new TypeAssertionError();
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
__name(assert, "assert");
|
|
704
|
+
function assertWithErrors(val, validator) {
|
|
705
|
+
const errors = [];
|
|
706
|
+
if (!validator(val, {
|
|
707
|
+
errors
|
|
708
|
+
})) {
|
|
709
|
+
throw new TypeAssertionError({
|
|
710
|
+
errors
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
__name(assertWithErrors, "assertWithErrors");
|
|
715
|
+
function softAssert(val, validator) {
|
|
716
|
+
}
|
|
717
|
+
__name(softAssert, "softAssert");
|
|
718
|
+
function as(value, validator, { coerce = false, errors: storeErrors, throw: throws } = {}) {
|
|
719
|
+
const errors = storeErrors ? [] : void 0;
|
|
720
|
+
if (!coerce) {
|
|
721
|
+
if (validator(value, {
|
|
722
|
+
errors
|
|
723
|
+
})) {
|
|
724
|
+
return throws ? value : {
|
|
725
|
+
value,
|
|
726
|
+
errors: void 0
|
|
727
|
+
};
|
|
728
|
+
} else if (!throws) {
|
|
729
|
+
return {
|
|
730
|
+
value: void 0,
|
|
731
|
+
errors: errors !== null && errors !== void 0 ? errors : true
|
|
732
|
+
};
|
|
733
|
+
} else {
|
|
734
|
+
throw new TypeAssertionError({
|
|
735
|
+
errors
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
const state = {
|
|
740
|
+
value
|
|
741
|
+
};
|
|
742
|
+
const coercion = makeCoercionFn(state, `value`);
|
|
743
|
+
const coercions = [];
|
|
744
|
+
if (!validator(value, {
|
|
745
|
+
errors,
|
|
746
|
+
coercion,
|
|
747
|
+
coercions
|
|
748
|
+
})) {
|
|
749
|
+
if (!throws) {
|
|
750
|
+
return {
|
|
751
|
+
value: void 0,
|
|
752
|
+
errors: errors !== null && errors !== void 0 ? errors : true
|
|
753
|
+
};
|
|
754
|
+
} else {
|
|
755
|
+
throw new TypeAssertionError({
|
|
756
|
+
errors
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
for (const [, apply] of coercions) apply();
|
|
761
|
+
if (throws) {
|
|
762
|
+
return state.value;
|
|
763
|
+
} else {
|
|
764
|
+
return {
|
|
765
|
+
value: state.value,
|
|
766
|
+
errors: void 0
|
|
767
|
+
};
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
__name(as, "as");
|
|
771
|
+
function fn(validators, fn2) {
|
|
772
|
+
const isValidArgList = isTuple(validators);
|
|
773
|
+
return (...args) => {
|
|
774
|
+
const check = isValidArgList(args);
|
|
775
|
+
if (!check) throw new TypeAssertionError();
|
|
776
|
+
return fn2(...args);
|
|
777
|
+
};
|
|
778
|
+
}
|
|
779
|
+
__name(fn, "fn");
|
|
780
|
+
function hasMinLength(length) {
|
|
781
|
+
return makeValidator({
|
|
782
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
783
|
+
if (!(value.length >= length)) return pushError(state, `Expected to have a length of at least ${length} elements (got ${value.length})`);
|
|
784
|
+
return true;
|
|
785
|
+
}, "test")
|
|
786
|
+
});
|
|
787
|
+
}
|
|
788
|
+
__name(hasMinLength, "hasMinLength");
|
|
789
|
+
function hasMaxLength(length) {
|
|
790
|
+
return makeValidator({
|
|
791
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
792
|
+
if (!(value.length <= length)) return pushError(state, `Expected to have a length of at most ${length} elements (got ${value.length})`);
|
|
793
|
+
return true;
|
|
794
|
+
}, "test")
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
__name(hasMaxLength, "hasMaxLength");
|
|
798
|
+
function hasExactLength(length) {
|
|
799
|
+
return makeValidator({
|
|
800
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
801
|
+
if (!(value.length === length)) return pushError(state, `Expected to have a length of exactly ${length} elements (got ${value.length})`);
|
|
802
|
+
return true;
|
|
803
|
+
}, "test")
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
__name(hasExactLength, "hasExactLength");
|
|
807
|
+
function hasUniqueItems({ map } = {}) {
|
|
808
|
+
return makeValidator({
|
|
809
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
810
|
+
const set = /* @__PURE__ */ new Set();
|
|
811
|
+
const dup = /* @__PURE__ */ new Set();
|
|
812
|
+
for (let t2 = 0, T = value.length; t2 < T; ++t2) {
|
|
813
|
+
const sub = value[t2];
|
|
814
|
+
const key = typeof map !== `undefined` ? map(sub) : sub;
|
|
815
|
+
if (set.has(key)) {
|
|
816
|
+
if (dup.has(key)) continue;
|
|
817
|
+
pushError(state, `Expected to contain unique elements; got a duplicate with ${getPrintable(value)}`);
|
|
818
|
+
dup.add(key);
|
|
819
|
+
} else {
|
|
820
|
+
set.add(key);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
return dup.size === 0;
|
|
824
|
+
}, "test")
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
__name(hasUniqueItems, "hasUniqueItems");
|
|
828
|
+
function isNegative() {
|
|
829
|
+
return makeValidator({
|
|
830
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
831
|
+
if (!(value <= 0)) return pushError(state, `Expected to be negative (got ${value})`);
|
|
832
|
+
return true;
|
|
833
|
+
}, "test")
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
__name(isNegative, "isNegative");
|
|
837
|
+
function isPositive() {
|
|
838
|
+
return makeValidator({
|
|
839
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
840
|
+
if (!(value >= 0)) return pushError(state, `Expected to be positive (got ${value})`);
|
|
841
|
+
return true;
|
|
842
|
+
}, "test")
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
__name(isPositive, "isPositive");
|
|
846
|
+
function isAtLeast(n) {
|
|
847
|
+
return makeValidator({
|
|
848
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
849
|
+
if (!(value >= n)) return pushError(state, `Expected to be at least ${n} (got ${value})`);
|
|
850
|
+
return true;
|
|
851
|
+
}, "test")
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
__name(isAtLeast, "isAtLeast");
|
|
855
|
+
function isAtMost(n) {
|
|
856
|
+
return makeValidator({
|
|
857
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
858
|
+
if (!(value <= n)) return pushError(state, `Expected to be at most ${n} (got ${value})`);
|
|
859
|
+
return true;
|
|
860
|
+
}, "test")
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
__name(isAtMost, "isAtMost");
|
|
864
|
+
function isInInclusiveRange(a, b) {
|
|
865
|
+
return makeValidator({
|
|
866
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
867
|
+
if (!(value >= a && value <= b)) return pushError(state, `Expected to be in the [${a}; ${b}] range (got ${value})`);
|
|
868
|
+
return true;
|
|
869
|
+
}, "test")
|
|
870
|
+
});
|
|
871
|
+
}
|
|
872
|
+
__name(isInInclusiveRange, "isInInclusiveRange");
|
|
873
|
+
function isInExclusiveRange(a, b) {
|
|
874
|
+
return makeValidator({
|
|
875
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
876
|
+
if (!(value >= a && value < b)) return pushError(state, `Expected to be in the [${a}; ${b}[ range (got ${value})`);
|
|
877
|
+
return true;
|
|
878
|
+
}, "test")
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
__name(isInExclusiveRange, "isInExclusiveRange");
|
|
882
|
+
function isInteger({ unsafe = false } = {}) {
|
|
883
|
+
return makeValidator({
|
|
884
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
885
|
+
if (value !== Math.round(value)) return pushError(state, `Expected to be an integer (got ${value})`);
|
|
886
|
+
if (!unsafe && !Number.isSafeInteger(value)) return pushError(state, `Expected to be a safe integer (got ${value})`);
|
|
887
|
+
return true;
|
|
888
|
+
}, "test")
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
__name(isInteger, "isInteger");
|
|
892
|
+
function matchesRegExp(regExp) {
|
|
893
|
+
return makeValidator({
|
|
894
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
895
|
+
if (!regExp.test(value)) return pushError(state, `Expected to match the pattern ${regExp.toString()} (got ${getPrintable(value)})`);
|
|
896
|
+
return true;
|
|
897
|
+
}, "test")
|
|
898
|
+
});
|
|
899
|
+
}
|
|
900
|
+
__name(matchesRegExp, "matchesRegExp");
|
|
901
|
+
function isLowerCase() {
|
|
902
|
+
return makeValidator({
|
|
903
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
904
|
+
if (value !== value.toLowerCase()) return pushError(state, `Expected to be all-lowercase (got ${value})`);
|
|
905
|
+
return true;
|
|
906
|
+
}, "test")
|
|
907
|
+
});
|
|
908
|
+
}
|
|
909
|
+
__name(isLowerCase, "isLowerCase");
|
|
910
|
+
function isUpperCase() {
|
|
911
|
+
return makeValidator({
|
|
912
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
913
|
+
if (value !== value.toUpperCase()) return pushError(state, `Expected to be all-uppercase (got ${value})`);
|
|
914
|
+
return true;
|
|
915
|
+
}, "test")
|
|
916
|
+
});
|
|
917
|
+
}
|
|
918
|
+
__name(isUpperCase, "isUpperCase");
|
|
919
|
+
function isUUID4() {
|
|
920
|
+
return makeValidator({
|
|
921
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
922
|
+
if (!uuid4RegExp.test(value)) return pushError(state, `Expected to be a valid UUID v4 (got ${getPrintable(value)})`);
|
|
923
|
+
return true;
|
|
924
|
+
}, "test")
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
__name(isUUID4, "isUUID4");
|
|
928
|
+
function isISO8601() {
|
|
929
|
+
return makeValidator({
|
|
930
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
931
|
+
if (!iso8601RegExp.test(value)) return pushError(state, `Expected to be a valid ISO 8601 date string (got ${getPrintable(value)})`);
|
|
932
|
+
return true;
|
|
933
|
+
}, "test")
|
|
934
|
+
});
|
|
935
|
+
}
|
|
936
|
+
__name(isISO8601, "isISO8601");
|
|
937
|
+
function isHexColor({ alpha = false }) {
|
|
938
|
+
return makeValidator({
|
|
939
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
940
|
+
const res = alpha ? colorStringRegExp.test(value) : colorStringAlphaRegExp.test(value);
|
|
941
|
+
if (!res) return pushError(state, `Expected to be a valid hexadecimal color string (got ${getPrintable(value)})`);
|
|
942
|
+
return true;
|
|
943
|
+
}, "test")
|
|
944
|
+
});
|
|
945
|
+
}
|
|
946
|
+
__name(isHexColor, "isHexColor");
|
|
947
|
+
function isBase64() {
|
|
948
|
+
return makeValidator({
|
|
949
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
950
|
+
if (!base64RegExp.test(value)) return pushError(state, `Expected to be a valid base 64 string (got ${getPrintable(value)})`);
|
|
951
|
+
return true;
|
|
952
|
+
}, "test")
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
__name(isBase64, "isBase64");
|
|
956
|
+
function isJSON(spec = isUnknown()) {
|
|
957
|
+
return makeValidator({
|
|
958
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
959
|
+
let data;
|
|
960
|
+
try {
|
|
961
|
+
data = JSON.parse(value);
|
|
962
|
+
} catch (_a) {
|
|
963
|
+
return pushError(state, `Expected to be a valid JSON string (got ${getPrintable(value)})`);
|
|
964
|
+
}
|
|
965
|
+
return spec(data, state);
|
|
966
|
+
}, "test")
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
__name(isJSON, "isJSON");
|
|
970
|
+
function cascade(spec, ...followups) {
|
|
971
|
+
const resolvedFollowups = Array.isArray(followups[0]) ? followups[0] : followups;
|
|
972
|
+
return makeValidator({
|
|
973
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
974
|
+
var _a, _b;
|
|
975
|
+
const context = {
|
|
976
|
+
value
|
|
977
|
+
};
|
|
978
|
+
const subCoercion = typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined` ? makeCoercionFn(context, `value`) : void 0;
|
|
979
|
+
const subCoercions = typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined` ? [] : void 0;
|
|
980
|
+
if (!spec(value, Object.assign(Object.assign({}, state), {
|
|
981
|
+
coercion: subCoercion,
|
|
982
|
+
coercions: subCoercions
|
|
983
|
+
}))) return false;
|
|
984
|
+
const reverts = [];
|
|
985
|
+
if (typeof subCoercions !== `undefined`) for (const [, coercion] of subCoercions) reverts.push(coercion());
|
|
986
|
+
try {
|
|
987
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
988
|
+
if (context.value !== value) {
|
|
989
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
990
|
+
state.coercions.push([
|
|
991
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
992
|
+
state.coercion.bind(null, context.value)
|
|
993
|
+
]);
|
|
994
|
+
}
|
|
995
|
+
(_b = state === null || state === void 0 ? void 0 : state.coercions) === null || _b === void 0 ? void 0 : _b.push(...subCoercions);
|
|
996
|
+
}
|
|
997
|
+
return resolvedFollowups.every((spec2) => {
|
|
998
|
+
return spec2(context.value, state);
|
|
999
|
+
});
|
|
1000
|
+
} finally {
|
|
1001
|
+
for (const revert of reverts) {
|
|
1002
|
+
revert();
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
}, "test")
|
|
1006
|
+
});
|
|
1007
|
+
}
|
|
1008
|
+
__name(cascade, "cascade");
|
|
1009
|
+
function applyCascade(spec, ...followups) {
|
|
1010
|
+
const resolvedFollowups = Array.isArray(followups[0]) ? followups[0] : followups;
|
|
1011
|
+
return cascade(spec, resolvedFollowups);
|
|
1012
|
+
}
|
|
1013
|
+
__name(applyCascade, "applyCascade");
|
|
1014
|
+
function isOptional(spec) {
|
|
1015
|
+
return makeValidator({
|
|
1016
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1017
|
+
if (typeof value === `undefined`) return true;
|
|
1018
|
+
return spec(value, state);
|
|
1019
|
+
}, "test")
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
__name(isOptional, "isOptional");
|
|
1023
|
+
function isNullable(spec) {
|
|
1024
|
+
return makeValidator({
|
|
1025
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1026
|
+
if (value === null) return true;
|
|
1027
|
+
return spec(value, state);
|
|
1028
|
+
}, "test")
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
__name(isNullable, "isNullable");
|
|
1032
|
+
var checks = {
|
|
1033
|
+
missing: /* @__PURE__ */ __name((keys, key) => keys.has(key), "missing"),
|
|
1034
|
+
undefined: /* @__PURE__ */ __name((keys, key, value) => keys.has(key) && typeof value[key] !== `undefined`, "undefined"),
|
|
1035
|
+
nil: /* @__PURE__ */ __name((keys, key, value) => keys.has(key) && value[key] != null, "nil"),
|
|
1036
|
+
falsy: /* @__PURE__ */ __name((keys, key, value) => keys.has(key) && !!value[key], "falsy")
|
|
1037
|
+
};
|
|
1038
|
+
function hasRequiredKeys(requiredKeys, options) {
|
|
1039
|
+
var _a;
|
|
1040
|
+
const requiredSet = new Set(requiredKeys);
|
|
1041
|
+
const check = checks[(_a = options === null || options === void 0 ? void 0 : options.missingIf) !== null && _a !== void 0 ? _a : "missing"];
|
|
1042
|
+
return makeValidator({
|
|
1043
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1044
|
+
const keys = new Set(Object.keys(value));
|
|
1045
|
+
const problems = [];
|
|
1046
|
+
for (const key of requiredSet) if (!check(keys, key, value)) problems.push(key);
|
|
1047
|
+
if (problems.length > 0) return pushError(state, `Missing required ${plural(problems.length, `property`, `properties`)} ${getPrintableArray(problems, `and`)}`);
|
|
1048
|
+
return true;
|
|
1049
|
+
}, "test")
|
|
1050
|
+
});
|
|
1051
|
+
}
|
|
1052
|
+
__name(hasRequiredKeys, "hasRequiredKeys");
|
|
1053
|
+
function hasAtLeastOneKey(requiredKeys, options) {
|
|
1054
|
+
var _a;
|
|
1055
|
+
const requiredSet = new Set(requiredKeys);
|
|
1056
|
+
const check = checks[(_a = options === null || options === void 0 ? void 0 : options.missingIf) !== null && _a !== void 0 ? _a : "missing"];
|
|
1057
|
+
return makeValidator({
|
|
1058
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1059
|
+
const keys = Object.keys(value);
|
|
1060
|
+
const valid = keys.some((key) => check(requiredSet, key, value));
|
|
1061
|
+
if (!valid) return pushError(state, `Missing at least one property from ${getPrintableArray(Array.from(requiredSet), `or`)}`);
|
|
1062
|
+
return true;
|
|
1063
|
+
}, "test")
|
|
1064
|
+
});
|
|
1065
|
+
}
|
|
1066
|
+
__name(hasAtLeastOneKey, "hasAtLeastOneKey");
|
|
1067
|
+
function hasForbiddenKeys(forbiddenKeys, options) {
|
|
1068
|
+
var _a;
|
|
1069
|
+
const forbiddenSet = new Set(forbiddenKeys);
|
|
1070
|
+
const check = checks[(_a = options === null || options === void 0 ? void 0 : options.missingIf) !== null && _a !== void 0 ? _a : "missing"];
|
|
1071
|
+
return makeValidator({
|
|
1072
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1073
|
+
const keys = new Set(Object.keys(value));
|
|
1074
|
+
const problems = [];
|
|
1075
|
+
for (const key of forbiddenSet) if (check(keys, key, value)) problems.push(key);
|
|
1076
|
+
if (problems.length > 0) return pushError(state, `Forbidden ${plural(problems.length, `property`, `properties`)} ${getPrintableArray(problems, `and`)}`);
|
|
1077
|
+
return true;
|
|
1078
|
+
}, "test")
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
1081
|
+
__name(hasForbiddenKeys, "hasForbiddenKeys");
|
|
1082
|
+
function hasMutuallyExclusiveKeys(exclusiveKeys, options) {
|
|
1083
|
+
var _a;
|
|
1084
|
+
const exclusiveSet = new Set(exclusiveKeys);
|
|
1085
|
+
const check = checks[(_a = options === null || options === void 0 ? void 0 : options.missingIf) !== null && _a !== void 0 ? _a : "missing"];
|
|
1086
|
+
return makeValidator({
|
|
1087
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1088
|
+
const keys = new Set(Object.keys(value));
|
|
1089
|
+
const used = [];
|
|
1090
|
+
for (const key of exclusiveSet) if (check(keys, key, value)) used.push(key);
|
|
1091
|
+
if (used.length > 1) return pushError(state, `Mutually exclusive properties ${getPrintableArray(used, `and`)}`);
|
|
1092
|
+
return true;
|
|
1093
|
+
}, "test")
|
|
1094
|
+
});
|
|
1095
|
+
}
|
|
1096
|
+
__name(hasMutuallyExclusiveKeys, "hasMutuallyExclusiveKeys");
|
|
1097
|
+
(function(KeyRelationship) {
|
|
1098
|
+
KeyRelationship["Forbids"] = "Forbids";
|
|
1099
|
+
KeyRelationship["Requires"] = "Requires";
|
|
1100
|
+
})(exports2.KeyRelationship || (exports2.KeyRelationship = {}));
|
|
1101
|
+
var keyRelationships = {
|
|
1102
|
+
[exports2.KeyRelationship.Forbids]: {
|
|
1103
|
+
expect: false,
|
|
1104
|
+
message: `forbids using`
|
|
1105
|
+
},
|
|
1106
|
+
[exports2.KeyRelationship.Requires]: {
|
|
1107
|
+
expect: true,
|
|
1108
|
+
message: `requires using`
|
|
1109
|
+
}
|
|
1110
|
+
};
|
|
1111
|
+
function hasKeyRelationship(subject, relationship, others, options) {
|
|
1112
|
+
var _a, _b;
|
|
1113
|
+
const skipped = new Set((_a = options === null || options === void 0 ? void 0 : options.ignore) !== null && _a !== void 0 ? _a : []);
|
|
1114
|
+
const check = checks[(_b = options === null || options === void 0 ? void 0 : options.missingIf) !== null && _b !== void 0 ? _b : "missing"];
|
|
1115
|
+
const otherSet = new Set(others);
|
|
1116
|
+
const spec = keyRelationships[relationship];
|
|
1117
|
+
const conjunction = relationship === exports2.KeyRelationship.Forbids ? `or` : `and`;
|
|
1118
|
+
return makeValidator({
|
|
1119
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1120
|
+
const keys = new Set(Object.keys(value));
|
|
1121
|
+
if (!check(keys, subject, value) || skipped.has(value[subject])) return true;
|
|
1122
|
+
const problems = [];
|
|
1123
|
+
for (const key of otherSet) if ((check(keys, key, value) && !skipped.has(value[key])) !== spec.expect) problems.push(key);
|
|
1124
|
+
if (problems.length >= 1) return pushError(state, `Property "${subject}" ${spec.message} ${plural(problems.length, `property`, `properties`)} ${getPrintableArray(problems, conjunction)}`);
|
|
1125
|
+
return true;
|
|
1126
|
+
}, "test")
|
|
1127
|
+
});
|
|
1128
|
+
}
|
|
1129
|
+
__name(hasKeyRelationship, "hasKeyRelationship");
|
|
1130
|
+
exports2.TypeAssertionError = TypeAssertionError;
|
|
1131
|
+
exports2.applyCascade = applyCascade;
|
|
1132
|
+
exports2.as = as;
|
|
1133
|
+
exports2.assert = assert;
|
|
1134
|
+
exports2.assertWithErrors = assertWithErrors;
|
|
1135
|
+
exports2.cascade = cascade;
|
|
1136
|
+
exports2.fn = fn;
|
|
1137
|
+
exports2.hasAtLeastOneKey = hasAtLeastOneKey;
|
|
1138
|
+
exports2.hasExactLength = hasExactLength;
|
|
1139
|
+
exports2.hasForbiddenKeys = hasForbiddenKeys;
|
|
1140
|
+
exports2.hasKeyRelationship = hasKeyRelationship;
|
|
1141
|
+
exports2.hasMaxLength = hasMaxLength;
|
|
1142
|
+
exports2.hasMinLength = hasMinLength;
|
|
1143
|
+
exports2.hasMutuallyExclusiveKeys = hasMutuallyExclusiveKeys;
|
|
1144
|
+
exports2.hasRequiredKeys = hasRequiredKeys;
|
|
1145
|
+
exports2.hasUniqueItems = hasUniqueItems;
|
|
1146
|
+
exports2.isArray = isArray;
|
|
1147
|
+
exports2.isAtLeast = isAtLeast;
|
|
1148
|
+
exports2.isAtMost = isAtMost;
|
|
1149
|
+
exports2.isBase64 = isBase64;
|
|
1150
|
+
exports2.isBoolean = isBoolean;
|
|
1151
|
+
exports2.isDate = isDate;
|
|
1152
|
+
exports2.isDict = isDict;
|
|
1153
|
+
exports2.isEnum = isEnum;
|
|
1154
|
+
exports2.isHexColor = isHexColor;
|
|
1155
|
+
exports2.isISO8601 = isISO8601;
|
|
1156
|
+
exports2.isInExclusiveRange = isInExclusiveRange;
|
|
1157
|
+
exports2.isInInclusiveRange = isInInclusiveRange;
|
|
1158
|
+
exports2.isInstanceOf = isInstanceOf;
|
|
1159
|
+
exports2.isInteger = isInteger;
|
|
1160
|
+
exports2.isJSON = isJSON;
|
|
1161
|
+
exports2.isLiteral = isLiteral;
|
|
1162
|
+
exports2.isLowerCase = isLowerCase;
|
|
1163
|
+
exports2.isMap = isMap;
|
|
1164
|
+
exports2.isNegative = isNegative;
|
|
1165
|
+
exports2.isNullable = isNullable;
|
|
1166
|
+
exports2.isNumber = isNumber2;
|
|
1167
|
+
exports2.isObject = isObject;
|
|
1168
|
+
exports2.isOneOf = isOneOf;
|
|
1169
|
+
exports2.isOptional = isOptional;
|
|
1170
|
+
exports2.isPartial = isPartial;
|
|
1171
|
+
exports2.isPayload = isPayload;
|
|
1172
|
+
exports2.isPositive = isPositive;
|
|
1173
|
+
exports2.isRecord = isRecord;
|
|
1174
|
+
exports2.isSet = isSet;
|
|
1175
|
+
exports2.isString = isString;
|
|
1176
|
+
exports2.isTuple = isTuple;
|
|
1177
|
+
exports2.isUUID4 = isUUID4;
|
|
1178
|
+
exports2.isUnknown = isUnknown;
|
|
1179
|
+
exports2.isUpperCase = isUpperCase;
|
|
1180
|
+
exports2.makeTrait = makeTrait;
|
|
1181
|
+
exports2.makeValidator = makeValidator;
|
|
1182
|
+
exports2.matchesRegExp = matchesRegExp;
|
|
1183
|
+
exports2.softAssert = softAssert;
|
|
1184
|
+
}
|
|
1185
|
+
});
|
|
1186
|
+
|
|
1187
|
+
// src/app/console/migrate/index.ts
|
|
1188
|
+
var migrate_exports = {};
|
|
1189
|
+
__export(migrate_exports, {
|
|
1190
|
+
GenerateMigrateCommand: () => GenerateMigrateCommand,
|
|
1191
|
+
MigrateCommand: () => MigrateCommand,
|
|
1192
|
+
MigrateRollbackCommand: () => MigrateRollbackCommand
|
|
1193
|
+
});
|
|
1194
|
+
module.exports = __toCommonJS(migrate_exports);
|
|
1195
|
+
|
|
1196
|
+
// src/router.mts
|
|
1197
|
+
var import_neko_context = require("@devbro/neko-context");
|
|
1198
|
+
var import_errors = require("@devbro/neko-http/errors");
|
|
1199
|
+
|
|
1200
|
+
// ../neko-router/dist/Middleware.mjs
|
|
1201
|
+
var Middleware = class {
|
|
1202
|
+
static {
|
|
1203
|
+
__name(this, "Middleware");
|
|
1204
|
+
}
|
|
1205
|
+
constructor(params = {}) {
|
|
1206
|
+
}
|
|
1207
|
+
static getInstance(params) {
|
|
1208
|
+
throw new Error("Method not implemented. Please implement a static getInstance method.");
|
|
1209
|
+
}
|
|
1210
|
+
};
|
|
1211
|
+
|
|
1212
|
+
// ../neko-router/dist/MiddlewareFactory.mjs
|
|
1213
|
+
var MiddlewareFactory = class {
|
|
1214
|
+
static {
|
|
1215
|
+
__name(this, "MiddlewareFactory");
|
|
1216
|
+
}
|
|
1217
|
+
static create(func) {
|
|
1218
|
+
const cls = class extends Middleware {
|
|
1219
|
+
static {
|
|
1220
|
+
__name(this, "cls");
|
|
1221
|
+
}
|
|
1222
|
+
call(req, res, next) {
|
|
1223
|
+
return func(req, res, next);
|
|
1224
|
+
}
|
|
1225
|
+
constructor(params = {}) {
|
|
1226
|
+
super(params);
|
|
1227
|
+
}
|
|
1228
|
+
};
|
|
1229
|
+
return new cls();
|
|
1230
|
+
}
|
|
1231
|
+
};
|
|
1232
|
+
|
|
1233
|
+
// ../neko-router/dist/CompiledRoute.mjs
|
|
1234
|
+
var CompiledRoute = class {
|
|
1235
|
+
static {
|
|
1236
|
+
__name(this, "CompiledRoute");
|
|
1237
|
+
}
|
|
1238
|
+
constructor(route, request, response, globalMiddlewares = []) {
|
|
1239
|
+
this.route = route;
|
|
1240
|
+
this.request = request;
|
|
1241
|
+
this.response = response;
|
|
1242
|
+
this.globalMiddlewares = globalMiddlewares;
|
|
1243
|
+
this.prepareMiddlewares();
|
|
1244
|
+
}
|
|
1245
|
+
middlewares = [];
|
|
1246
|
+
getMiddlewares() {
|
|
1247
|
+
return this.middlewares;
|
|
1248
|
+
}
|
|
1249
|
+
prepareMiddlewares() {
|
|
1250
|
+
this.middlewares = [];
|
|
1251
|
+
for (const middleware of [...this.globalMiddlewares, ...this.route.getMiddlewares()]) {
|
|
1252
|
+
if (middleware instanceof Middleware) {
|
|
1253
|
+
this.middlewares.push(middleware);
|
|
1254
|
+
} else if (this.isClass(middleware)) {
|
|
1255
|
+
this.middlewares.push(middleware.getInstance({}));
|
|
1256
|
+
} else if (typeof middleware === "function") {
|
|
1257
|
+
this.middlewares.push(MiddlewareFactory.create(middleware));
|
|
1258
|
+
} else {
|
|
1259
|
+
throw new Error("Invalid middleware type");
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
isClass(func) {
|
|
1264
|
+
return typeof func === "function" && /^class\s/.test(Function.prototype.toString.call(func));
|
|
1265
|
+
}
|
|
1266
|
+
async run() {
|
|
1267
|
+
return await this.runMiddlewares(this.middlewares, this.request, this.response);
|
|
1268
|
+
}
|
|
1269
|
+
prepareOutputJsonFormat(obj) {
|
|
1270
|
+
function traverse(value) {
|
|
1271
|
+
if (!value || typeof value !== "object") {
|
|
1272
|
+
return value;
|
|
1273
|
+
}
|
|
1274
|
+
if (typeof value.toJson === "function") {
|
|
1275
|
+
return traverse(value.toJson());
|
|
1276
|
+
}
|
|
1277
|
+
if (Array.isArray(value)) {
|
|
1278
|
+
return value.map(traverse);
|
|
1279
|
+
}
|
|
1280
|
+
const result = {};
|
|
1281
|
+
for (const key in value) {
|
|
1282
|
+
if (Object.prototype.hasOwnProperty.call(value, key)) {
|
|
1283
|
+
result[key] = traverse(value[key]);
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
return result;
|
|
1287
|
+
}
|
|
1288
|
+
__name(traverse, "traverse");
|
|
1289
|
+
return traverse(obj);
|
|
1290
|
+
}
|
|
1291
|
+
convertToString(obj) {
|
|
1292
|
+
if (typeof obj === "string") {
|
|
1293
|
+
return obj;
|
|
1294
|
+
} else if (obj instanceof Buffer) {
|
|
1295
|
+
return obj.toString();
|
|
1296
|
+
} else if (typeof obj === "object") {
|
|
1297
|
+
return JSON.stringify(this.prepareOutputJsonFormat(obj));
|
|
1298
|
+
}
|
|
1299
|
+
return String(obj);
|
|
1300
|
+
}
|
|
1301
|
+
processResponseBody(res, controller_rc) {
|
|
1302
|
+
if (controller_rc && res.writableEnded) {
|
|
1303
|
+
throw new Error("cannot write to response, response has already ended");
|
|
1304
|
+
}
|
|
1305
|
+
if (res.writableEnded) {
|
|
1306
|
+
return;
|
|
1307
|
+
}
|
|
1308
|
+
if (controller_rc) {
|
|
1309
|
+
const header_content_type = res.getHeader("Content-Type");
|
|
1310
|
+
if (!header_content_type && typeof controller_rc === "object") {
|
|
1311
|
+
res.setHeader("Content-Type", "application/json");
|
|
1312
|
+
} else if (!header_content_type) {
|
|
1313
|
+
res.setHeader("Content-Type", "text/plain");
|
|
1314
|
+
}
|
|
1315
|
+
res.end(this.convertToString(controller_rc));
|
|
1316
|
+
return;
|
|
1317
|
+
} else {
|
|
1318
|
+
res.statusCode = [200].includes(res.statusCode) ? 204 : res.statusCode;
|
|
1319
|
+
res.end();
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
async runMiddlewares(middlewares, req, res) {
|
|
1323
|
+
let index = 0;
|
|
1324
|
+
const me = this;
|
|
1325
|
+
async function next() {
|
|
1326
|
+
if (index >= middlewares.length) {
|
|
1327
|
+
const controller_rc = await me.route.callHanlder(req, res);
|
|
1328
|
+
await me.processResponseBody(res, controller_rc);
|
|
1329
|
+
return;
|
|
1330
|
+
}
|
|
1331
|
+
const middleware = middlewares[index++];
|
|
1332
|
+
if (middleware instanceof Middleware) {
|
|
1333
|
+
await middleware.call(req, res, next);
|
|
1334
|
+
} else if (typeof middleware === "function") {
|
|
1335
|
+
await middleware(req, res, next);
|
|
1336
|
+
} else {
|
|
1337
|
+
throw new Error("does not know how to run middleware");
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
__name(next, "next");
|
|
1341
|
+
await next();
|
|
1342
|
+
}
|
|
1343
|
+
};
|
|
1344
|
+
|
|
1345
|
+
// ../neko-router/dist/Route.mjs
|
|
1346
|
+
var Route = class {
|
|
1347
|
+
static {
|
|
1348
|
+
__name(this, "Route");
|
|
1349
|
+
}
|
|
1350
|
+
constructor(methods, path5, handler) {
|
|
1351
|
+
this.methods = methods;
|
|
1352
|
+
this.path = path5;
|
|
1353
|
+
this.handler = handler;
|
|
1354
|
+
this.urlRegex = this.pathToRegex(path5);
|
|
1355
|
+
}
|
|
1356
|
+
middlewares = [];
|
|
1357
|
+
urlRegex;
|
|
1358
|
+
pathToRegex(path5) {
|
|
1359
|
+
const lex = this.lexUrlPath(path5);
|
|
1360
|
+
return this.tokensToRegex(lex);
|
|
1361
|
+
}
|
|
1362
|
+
lexUrlPath(path5) {
|
|
1363
|
+
const tokens = [];
|
|
1364
|
+
let i = 0;
|
|
1365
|
+
while (i < path5.length) {
|
|
1366
|
+
const char = path5[i];
|
|
1367
|
+
if (char === "/") {
|
|
1368
|
+
tokens.push({ type: "SLASH", value: "/" });
|
|
1369
|
+
i++;
|
|
1370
|
+
} else if (char === ":") {
|
|
1371
|
+
let start = i + 1;
|
|
1372
|
+
while (start < path5.length && /[a-zA-Z0-9_]/.test(path5[start])) {
|
|
1373
|
+
start++;
|
|
1374
|
+
}
|
|
1375
|
+
tokens.push({ type: "PARAM", value: path5.slice(i + 1, start) });
|
|
1376
|
+
i = start;
|
|
1377
|
+
} else if (char === "*") {
|
|
1378
|
+
let start = i + 1;
|
|
1379
|
+
while (start < path5.length && /[a-zA-Z0-9_]/.test(path5[start])) {
|
|
1380
|
+
start++;
|
|
1381
|
+
}
|
|
1382
|
+
tokens.push({ type: "WILDCARD", value: path5.slice(i + 1, start) });
|
|
1383
|
+
i = start;
|
|
1384
|
+
} else {
|
|
1385
|
+
let start = i;
|
|
1386
|
+
while (start < path5.length && !["/", ":", "*"].includes(path5[start])) {
|
|
1387
|
+
start++;
|
|
1388
|
+
}
|
|
1389
|
+
tokens.push({ type: "TEXT", value: path5.slice(i, start) });
|
|
1390
|
+
i = start;
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
return tokens;
|
|
1394
|
+
}
|
|
1395
|
+
tokensToRegex(tokens) {
|
|
1396
|
+
const regexParts = [];
|
|
1397
|
+
for (const token of tokens) {
|
|
1398
|
+
if (token.type === "SLASH") {
|
|
1399
|
+
regexParts.push("\\/");
|
|
1400
|
+
} else if (token.type === "PARAM") {
|
|
1401
|
+
regexParts.push(`(?<${token.value}>[^\\/]+)`);
|
|
1402
|
+
} else if (token.type === "WILDCARD") {
|
|
1403
|
+
regexParts.push("(.+)");
|
|
1404
|
+
} else if (token.type === "TEXT") {
|
|
1405
|
+
regexParts.push(token.value.replace(/[-\/\\^$.*+?()[\]{}|]/g, "\\$&"));
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
if (regexParts.length > 0 && regexParts[regexParts.length - 1] === "\\/") {
|
|
1409
|
+
regexParts[regexParts.length - 1] = "\\/?";
|
|
1410
|
+
} else {
|
|
1411
|
+
regexParts.push("\\/?");
|
|
1412
|
+
}
|
|
1413
|
+
return new RegExp(`^${regexParts.join("")}$`);
|
|
1414
|
+
}
|
|
1415
|
+
/**
|
|
1416
|
+
* to evaludate if request is a match for this route
|
|
1417
|
+
* @param request http request
|
|
1418
|
+
* @returns return true if route is a match for this request
|
|
1419
|
+
*/
|
|
1420
|
+
test(request) {
|
|
1421
|
+
if (this.methods.indexOf(request.method) === -1) {
|
|
1422
|
+
return false;
|
|
1423
|
+
}
|
|
1424
|
+
const url = new URL(request.url || "/", "http://localhost");
|
|
1425
|
+
return this.testPath(url.pathname);
|
|
1426
|
+
}
|
|
1427
|
+
testPath(pathname) {
|
|
1428
|
+
return this.urlRegex.test(pathname);
|
|
1429
|
+
}
|
|
1430
|
+
/**
|
|
1431
|
+
* returns details of the match, otherwise it returns false
|
|
1432
|
+
* @param request the request to match
|
|
1433
|
+
* @returns object cotaining details of match including matched params
|
|
1434
|
+
*/
|
|
1435
|
+
match(request) {
|
|
1436
|
+
if (this.methods.indexOf(request.method) === -1) {
|
|
1437
|
+
return false;
|
|
1438
|
+
}
|
|
1439
|
+
const url = new URL(request.url || "/", "http://localhost");
|
|
1440
|
+
const r = this.urlRegex.exec(url.pathname);
|
|
1441
|
+
if (!r) {
|
|
1442
|
+
return false;
|
|
1443
|
+
}
|
|
1444
|
+
return {
|
|
1445
|
+
url,
|
|
1446
|
+
params: r.groups || {}
|
|
1447
|
+
};
|
|
1448
|
+
}
|
|
1449
|
+
addMiddleware(middlewares) {
|
|
1450
|
+
this.middlewares = this.middlewares.concat(middlewares);
|
|
1451
|
+
return this;
|
|
1452
|
+
}
|
|
1453
|
+
getMiddlewares() {
|
|
1454
|
+
return this.middlewares;
|
|
1455
|
+
}
|
|
1456
|
+
callHanlder(request, response) {
|
|
1457
|
+
return this.handler(request, response);
|
|
1458
|
+
}
|
|
1459
|
+
};
|
|
1460
|
+
|
|
1461
|
+
// ../neko-router/dist/Router.mjs
|
|
1462
|
+
var import_path = __toESM(require("path"), 1);
|
|
1463
|
+
var Router = class {
|
|
1464
|
+
static {
|
|
1465
|
+
__name(this, "Router");
|
|
1466
|
+
}
|
|
1467
|
+
middlewares = [];
|
|
1468
|
+
routes = [];
|
|
1469
|
+
addRoute(methods, path22, handler) {
|
|
1470
|
+
const route = new Route(methods, path22, handler);
|
|
1471
|
+
this.routes.push(route);
|
|
1472
|
+
return route;
|
|
1473
|
+
}
|
|
1474
|
+
getMiddlewares() {
|
|
1475
|
+
return [...this.middlewares];
|
|
1476
|
+
}
|
|
1477
|
+
addController(controller) {
|
|
1478
|
+
const basePath = controller.basePath || "";
|
|
1479
|
+
for (const route of controller.routes) {
|
|
1480
|
+
const urlPath = import_path.default.join(basePath, route.path);
|
|
1481
|
+
this.addRoute(route.methods, urlPath, async (req, res) => {
|
|
1482
|
+
const controllerInstance = controller.getInstance();
|
|
1483
|
+
return await controllerInstance[route.handler]();
|
|
1484
|
+
}).addMiddleware([...controller.baseMiddlewares, ...route.middlewares]);
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
addGlobalMiddleware(middlewares) {
|
|
1488
|
+
this.middlewares = this.middlewares.concat(middlewares);
|
|
1489
|
+
}
|
|
1490
|
+
resolve(request) {
|
|
1491
|
+
for (const route of this.routes) {
|
|
1492
|
+
if (route.test(request)) {
|
|
1493
|
+
return route;
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
return void 0;
|
|
1497
|
+
}
|
|
1498
|
+
resolveMultiple(request) {
|
|
1499
|
+
const rc = [];
|
|
1500
|
+
const url = new URL(request.url || "/", "http://localhost");
|
|
1501
|
+
for (const route of this.routes) {
|
|
1502
|
+
if (route.testPath(url.pathname)) {
|
|
1503
|
+
rc.push(route);
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
return rc;
|
|
1507
|
+
}
|
|
1508
|
+
getCompiledRoute(request, response) {
|
|
1509
|
+
const route = this.resolve(request);
|
|
1510
|
+
if (!route) {
|
|
1511
|
+
return void 0;
|
|
1512
|
+
}
|
|
1513
|
+
const match = route.match(request);
|
|
1514
|
+
if (!match) {
|
|
1515
|
+
return void 0;
|
|
1516
|
+
}
|
|
1517
|
+
request.params = match.params;
|
|
1518
|
+
return new CompiledRoute(route, request, response, this.middlewares);
|
|
1519
|
+
}
|
|
1520
|
+
};
|
|
1521
|
+
|
|
1522
|
+
// src/facades.mts
|
|
1523
|
+
var import_neko_scheduler = require("@devbro/neko-scheduler");
|
|
1524
|
+
var import_neko_helper = require("@devbro/neko-helper");
|
|
1525
|
+
var import_neko_context2 = require("@devbro/neko-context");
|
|
1526
|
+
var import_neko_storage = require("@devbro/neko-storage");
|
|
1527
|
+
var import_neko_mailer = require("@devbro/neko-mailer");
|
|
1528
|
+
var import_neko_config = require("@devbro/neko-config");
|
|
1529
|
+
var import_clipanion = require("clipanion");
|
|
1530
|
+
|
|
1531
|
+
// src/http.mts
|
|
1532
|
+
var http_exports = {};
|
|
1533
|
+
__reExport(http_exports, require("@devbro/neko-http"));
|
|
1534
|
+
|
|
1535
|
+
// src/facades.mts
|
|
1536
|
+
var yup = __toESM(require("yup"), 1);
|
|
1537
|
+
var import_neko_logger = require("@devbro/neko-logger");
|
|
1538
|
+
var router = (0, import_neko_helper.createSingleton)(() => new Router());
|
|
1539
|
+
var scheduler = (0, import_neko_helper.createSingleton)(() => {
|
|
1540
|
+
const rc = new import_neko_scheduler.Scheduler();
|
|
1541
|
+
rc.setErrorHandler((err, job) => {
|
|
1542
|
+
logger().error({
|
|
1543
|
+
msg: "Scheduled job error",
|
|
1544
|
+
err,
|
|
1545
|
+
job_name: job.getName()
|
|
1546
|
+
});
|
|
1547
|
+
});
|
|
1548
|
+
return rc;
|
|
1549
|
+
});
|
|
1550
|
+
var db = /* @__PURE__ */ __name((label = "default") => (0, import_neko_context2.ctx)().getOrThrow(["database", label]), "db");
|
|
1551
|
+
var storage = (0, import_neko_helper.createSingleton)(
|
|
1552
|
+
(label = "default") => import_neko_storage.StorageFactory.create(import_neko_config.config.get(["storages", label].join(".")))
|
|
1553
|
+
);
|
|
1554
|
+
var cli = (0, import_neko_helper.createSingleton)(() => {
|
|
1555
|
+
const [node, app, ...args] = process.argv;
|
|
1556
|
+
return new import_clipanion.Cli({
|
|
1557
|
+
binaryLabel: `My Application`,
|
|
1558
|
+
binaryName: `${node} ${app}`,
|
|
1559
|
+
binaryVersion: `1.0.0`
|
|
1560
|
+
});
|
|
1561
|
+
});
|
|
1562
|
+
var httpServer = (0, import_neko_helper.createSingleton)(() => {
|
|
1563
|
+
const server = new http_exports.HttpServer();
|
|
1564
|
+
server.setErrorHandler(async (err, req, res) => {
|
|
1565
|
+
if (err instanceof http_exports.HttpError) {
|
|
1566
|
+
res.writeHead(err.statusCode, { "Content-Type": "application/json" });
|
|
1567
|
+
res.end(JSON.stringify({ message: err.message, error: err.code }));
|
|
1568
|
+
logger().warn({ msg: "HttpError: " + err.message, err });
|
|
1569
|
+
return;
|
|
1570
|
+
} else if (err instanceof yup.ValidationError) {
|
|
1571
|
+
res.writeHead(422, { "Content-Type": "application/json" });
|
|
1572
|
+
const errs = {};
|
|
1573
|
+
err.inner.forEach((e) => {
|
|
1574
|
+
const sanitizedParams = { ...e.params };
|
|
1575
|
+
if (/passw/i.test(e.path)) {
|
|
1576
|
+
sanitizedParams.value = "******";
|
|
1577
|
+
sanitizedParams.originalValue = "******";
|
|
1578
|
+
}
|
|
1579
|
+
errs[e.path] = {
|
|
1580
|
+
type: e.type,
|
|
1581
|
+
message: e.message,
|
|
1582
|
+
params: sanitizedParams
|
|
1583
|
+
};
|
|
1584
|
+
});
|
|
1585
|
+
res.end(JSON.stringify({ message: "validation error", errors: errs }));
|
|
1586
|
+
logger().warn({ msg: "ValidationError: " + err.message, err });
|
|
1587
|
+
return;
|
|
1588
|
+
} else {
|
|
1589
|
+
logger().error({ msg: "Error: " + err.message, err });
|
|
1590
|
+
}
|
|
1591
|
+
res.writeHead(500, { "Content-Type": "" });
|
|
1592
|
+
res.end(JSON.stringify({ error: "Internal Server Error" }));
|
|
1593
|
+
});
|
|
1594
|
+
server.setRouter(router());
|
|
1595
|
+
return server;
|
|
1596
|
+
});
|
|
1597
|
+
var logger = (0, import_neko_helper.createSingleton)((label) => {
|
|
1598
|
+
const logger_config = import_neko_config.config.get(["loggers", label].join("."));
|
|
1599
|
+
const rc = new import_neko_logger.Logger(logger_config);
|
|
1600
|
+
rc.setExtrasFunction((message) => {
|
|
1601
|
+
message.requestId = (0, import_neko_context2.ctxSafe)()?.get("requestId") || "N/A";
|
|
1602
|
+
return message;
|
|
1603
|
+
});
|
|
1604
|
+
return rc;
|
|
1605
|
+
});
|
|
1606
|
+
var mailer = (0, import_neko_helper.createSingleton)((label) => {
|
|
1607
|
+
const mailer_config = import_neko_config.config.get(["mailer", label].join("."));
|
|
1608
|
+
let provider;
|
|
1609
|
+
if (mailer_config.provider === "logger") {
|
|
1610
|
+
provider = new import_neko_mailer.FunctionProvider((mail) => {
|
|
1611
|
+
logger().info({
|
|
1612
|
+
msg: "Sending email",
|
|
1613
|
+
mail
|
|
1614
|
+
});
|
|
1615
|
+
});
|
|
1616
|
+
} else if (mailer_config.provider === "SES") {
|
|
1617
|
+
provider = new import_neko_mailer.SESProvider(mailer_config.config);
|
|
1618
|
+
} else if (mailer_config.provider === "SMTP") {
|
|
1619
|
+
provider = new import_neko_mailer.SMTPProvider(mailer_config.config);
|
|
1620
|
+
} else if (mailer_config.provider === "MEMORY") {
|
|
1621
|
+
provider = new import_neko_mailer.MemoryProvider();
|
|
1622
|
+
}
|
|
1623
|
+
if (!provider) {
|
|
1624
|
+
throw new Error(
|
|
1625
|
+
`cannot initiate mailer provider: ${mailer_config?.provider}`
|
|
1626
|
+
);
|
|
1627
|
+
}
|
|
1628
|
+
const rc = new import_neko_mailer.Mailer(provider);
|
|
1629
|
+
return rc;
|
|
1630
|
+
});
|
|
1631
|
+
|
|
1632
|
+
// src/app/console/migrate/MigrateCommand.ts
|
|
1633
|
+
var import_clipanion2 = require("clipanion");
|
|
1634
|
+
var import_neko_context3 = require("@devbro/neko-context");
|
|
1635
|
+
var import_path2 = __toESM(require("path"));
|
|
1636
|
+
var import_promises = __toESM(require("fs/promises"));
|
|
1637
|
+
var import_neko_config2 = require("@devbro/neko-config");
|
|
1638
|
+
var MigrateCommand = class extends import_clipanion2.Command {
|
|
1639
|
+
static {
|
|
1640
|
+
__name(this, "MigrateCommand");
|
|
1641
|
+
}
|
|
1642
|
+
static paths = [
|
|
1643
|
+
[
|
|
1644
|
+
`migrate`
|
|
1645
|
+
]
|
|
1646
|
+
];
|
|
1647
|
+
fresh = import_clipanion2.Option.Boolean("--fresh", false);
|
|
1648
|
+
async execute() {
|
|
1649
|
+
await import_neko_context3.context_provider.run(async () => {
|
|
1650
|
+
const db2 = db();
|
|
1651
|
+
const schema = db2.getSchema();
|
|
1652
|
+
if (this.fresh) {
|
|
1653
|
+
logger().info("dropping all tables!!");
|
|
1654
|
+
let retry = true;
|
|
1655
|
+
let retry_count = 0;
|
|
1656
|
+
while (retry && retry_count < 10) {
|
|
1657
|
+
retry = false;
|
|
1658
|
+
retry_count++;
|
|
1659
|
+
const tables = await schema.tables();
|
|
1660
|
+
for (const table of tables) {
|
|
1661
|
+
logger().info(`dropping table ${table.name}`);
|
|
1662
|
+
try {
|
|
1663
|
+
await schema.dropTable(table.name);
|
|
1664
|
+
} catch {
|
|
1665
|
+
logger().info(`failed to drop ${table.name}`);
|
|
1666
|
+
retry = true;
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1671
|
+
if (!await schema.tableExists("migrations")) {
|
|
1672
|
+
await schema.createTable("migrations", (blueprint) => {
|
|
1673
|
+
blueprint.id();
|
|
1674
|
+
blueprint.timestamps();
|
|
1675
|
+
blueprint.string("filename");
|
|
1676
|
+
blueprint.integer("batch");
|
|
1677
|
+
});
|
|
1678
|
+
}
|
|
1679
|
+
const migrationsDir = import_neko_config2.config.get("migration.path");
|
|
1680
|
+
let files = [];
|
|
1681
|
+
const dirEntries = await import_promises.default.readdir(migrationsDir);
|
|
1682
|
+
files = dirEntries.filter((entry) => entry.endsWith(".ts") || entry.endsWith(".js")).sort();
|
|
1683
|
+
let batch_number = await db2.runQuery({
|
|
1684
|
+
sql: "select max(batch) as next_batch from migrations",
|
|
1685
|
+
bindings: []
|
|
1686
|
+
});
|
|
1687
|
+
batch_number = batch_number[0].next_batch || 0;
|
|
1688
|
+
batch_number++;
|
|
1689
|
+
const migrations = await db2.runQuery({
|
|
1690
|
+
sql: "select * from migrations order by created_at ASC",
|
|
1691
|
+
bindings: []
|
|
1692
|
+
});
|
|
1693
|
+
const completed_migrations = migrations.map((r) => r.filename);
|
|
1694
|
+
const pending_migrations = files.filter((file) => !completed_migrations.includes(file));
|
|
1695
|
+
let migrated_count = 0;
|
|
1696
|
+
for (const class_to_migrate of pending_migrations) {
|
|
1697
|
+
logger().info(`migrating up ${class_to_migrate}`);
|
|
1698
|
+
const ClassToMigrate = (await import(import_path2.default.join(migrationsDir, class_to_migrate))).default;
|
|
1699
|
+
const c = new ClassToMigrate();
|
|
1700
|
+
await c.up(db2.getSchema());
|
|
1701
|
+
await db2.runQuery({
|
|
1702
|
+
sql: "insert into migrations (filename, batch) values ($1,$2)",
|
|
1703
|
+
bindings: [
|
|
1704
|
+
class_to_migrate,
|
|
1705
|
+
batch_number
|
|
1706
|
+
]
|
|
1707
|
+
});
|
|
1708
|
+
migrated_count++;
|
|
1709
|
+
}
|
|
1710
|
+
if (migrated_count === 0) {
|
|
1711
|
+
logger().warn("no migrations to run!");
|
|
1712
|
+
return;
|
|
1713
|
+
}
|
|
1714
|
+
logger().info(`migrated ${migrated_count} migrations successfully!`);
|
|
1715
|
+
});
|
|
1716
|
+
}
|
|
1717
|
+
};
|
|
1718
|
+
cli().register(MigrateCommand);
|
|
1719
|
+
|
|
1720
|
+
// src/app/console/migrate/GenerateMigrateCommand.ts
|
|
1721
|
+
var import_clipanion3 = require("clipanion");
|
|
1722
|
+
var import_change_case_all = require("change-case-all");
|
|
1723
|
+
var import_path3 = __toESM(require("path"));
|
|
1724
|
+
var fs2 = __toESM(require("fs/promises"));
|
|
1725
|
+
var import_neko_config3 = require("@devbro/neko-config");
|
|
1726
|
+
var import_handlebars = __toESM(require("handlebars"));
|
|
1727
|
+
var import_url = require("url");
|
|
1728
|
+
var import_meta = {};
|
|
1729
|
+
var GenerateMigrateCommand = class extends import_clipanion3.Command {
|
|
1730
|
+
static {
|
|
1731
|
+
__name(this, "GenerateMigrateCommand");
|
|
1732
|
+
}
|
|
1733
|
+
static paths = [
|
|
1734
|
+
[
|
|
1735
|
+
`generate`,
|
|
1736
|
+
`migrate`
|
|
1737
|
+
],
|
|
1738
|
+
[
|
|
1739
|
+
"generate",
|
|
1740
|
+
"migration"
|
|
1741
|
+
]
|
|
1742
|
+
];
|
|
1743
|
+
name = import_clipanion3.Option.String({
|
|
1744
|
+
required: true
|
|
1745
|
+
});
|
|
1746
|
+
async execute() {
|
|
1747
|
+
const date = /* @__PURE__ */ new Date();
|
|
1748
|
+
const year = date.getFullYear();
|
|
1749
|
+
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
1750
|
+
const day = String(date.getDate()).padStart(2, "0");
|
|
1751
|
+
const secondsOfDay = String(date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds()).padStart(5, "0");
|
|
1752
|
+
const fixed_name = import_change_case_all.Case.snake(this.name);
|
|
1753
|
+
const filename = `${year}_${month}_${day}_${secondsOfDay}_${fixed_name}.ts`;
|
|
1754
|
+
this.context.stdout.write(`creating migration file ${filename}
|
|
1755
|
+
`);
|
|
1756
|
+
await fs2.mkdir(import_neko_config3.config.get("migration.path"), {
|
|
1757
|
+
recursive: true
|
|
1758
|
+
});
|
|
1759
|
+
let dirname = typeof __dirname === "string" ? __dirname : void 0;
|
|
1760
|
+
if (!dirname) {
|
|
1761
|
+
dirname = import_path3.default.dirname((0, import_url.fileURLToPath)(import_meta.url));
|
|
1762
|
+
}
|
|
1763
|
+
const compiledTemplate = import_handlebars.default.compile((await fs2.readFile(import_path3.default.join(dirname, "./make_migration.tpl"))).toString());
|
|
1764
|
+
const template = await compiledTemplate({
|
|
1765
|
+
className: import_change_case_all.Case.pascal(this.name) + "Migration",
|
|
1766
|
+
tableName: import_change_case_all.Case.snake(this.name)
|
|
1767
|
+
});
|
|
1768
|
+
await fs2.writeFile(import_path3.default.join(import_neko_config3.config.get("migration.path"), filename), template);
|
|
1769
|
+
}
|
|
1770
|
+
};
|
|
1771
|
+
cli().register(GenerateMigrateCommand);
|
|
1772
|
+
|
|
1773
|
+
// src/app/console/migrate/MigrateRollbackCommand.ts
|
|
1774
|
+
var import_clipanion4 = require("clipanion");
|
|
1775
|
+
var import_neko_context4 = require("@devbro/neko-context");
|
|
1776
|
+
var import_path4 = __toESM(require("path"));
|
|
1777
|
+
var import_promises2 = __toESM(require("fs/promises"));
|
|
1778
|
+
var import_neko_config4 = require("@devbro/neko-config");
|
|
1779
|
+
var t = __toESM(require_lib());
|
|
1780
|
+
var MigrateRollbackCommand = class extends import_clipanion4.Command {
|
|
1781
|
+
static {
|
|
1782
|
+
__name(this, "MigrateRollbackCommand");
|
|
1783
|
+
}
|
|
1784
|
+
static paths = [
|
|
1785
|
+
[
|
|
1786
|
+
`migrate`,
|
|
1787
|
+
"rollback"
|
|
1788
|
+
]
|
|
1789
|
+
];
|
|
1790
|
+
steps = import_clipanion4.Option.String(`--steps`, {
|
|
1791
|
+
description: `how many migrations to rollback`,
|
|
1792
|
+
validator: t.isNumber()
|
|
1793
|
+
});
|
|
1794
|
+
async execute() {
|
|
1795
|
+
await import_neko_context4.context_provider.run(async () => {
|
|
1796
|
+
const db2 = db();
|
|
1797
|
+
const schema = db2.getSchema();
|
|
1798
|
+
const migrationsDir = import_neko_config4.config.get("migration.path");
|
|
1799
|
+
let files = [];
|
|
1800
|
+
const dirEntries = await import_promises2.default.readdir(migrationsDir);
|
|
1801
|
+
files = dirEntries.filter((entry) => entry.endsWith(".ts")).sort();
|
|
1802
|
+
const migrations = await db2.runQuery({
|
|
1803
|
+
sql: "select * from migrations order by created_at DESC",
|
|
1804
|
+
bindings: []
|
|
1805
|
+
});
|
|
1806
|
+
const count = 0;
|
|
1807
|
+
for (const migration of migrations) {
|
|
1808
|
+
const class_to_migrate = migration.filename;
|
|
1809
|
+
this.context.stdout.write(`rolling back ${class_to_migrate}`);
|
|
1810
|
+
const ClassToMigrate = (await import(import_path4.default.join(migrationsDir, class_to_migrate))).default;
|
|
1811
|
+
const c = new ClassToMigrate();
|
|
1812
|
+
await c.down(db2.getSchema());
|
|
1813
|
+
await db2.runQuery({
|
|
1814
|
+
sql: "delete from migrations where id = $1",
|
|
1815
|
+
bindings: [
|
|
1816
|
+
migration.id
|
|
1817
|
+
]
|
|
1818
|
+
});
|
|
1819
|
+
}
|
|
1820
|
+
});
|
|
1821
|
+
}
|
|
1822
|
+
};
|
|
1823
|
+
cli().register(MigrateRollbackCommand);
|
|
1824
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1825
|
+
0 && (module.exports = {
|
|
1826
|
+
GenerateMigrateCommand,
|
|
1827
|
+
MigrateCommand,
|
|
1828
|
+
MigrateRollbackCommand
|
|
1829
|
+
});
|