@getstrata/core 0.3.2 → 0.3.8
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/core/auth/abilityChecker.d.ts +6 -0
- package/dist/core/database/migrations/runner.d.ts +19 -0
- package/dist/core/database/migrations/types.d.ts +14 -0
- package/dist/core/database/model.d.ts +28 -0
- package/dist/core/http/requireAbilityMiddleware.d.ts +2 -2
- package/dist/core/http/webFormRequest.d.ts +1 -0
- package/dist/core/validation/rules.d.ts +17 -0
- package/dist/framework/public-api.d.ts +7 -1
- package/dist/index.js +259 -5
- package/package.json +1 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Migration, MigrationDatabase, MigrationStatus } from "./types";
|
|
2
|
+
type AppliedMigrationRow = {
|
|
3
|
+
name: string;
|
|
4
|
+
batch: number | string;
|
|
5
|
+
};
|
|
6
|
+
declare function ensureMigrationsTable(db: MigrationDatabase): Promise<void>;
|
|
7
|
+
declare function getAppliedMigrations(db: MigrationDatabase): Promise<AppliedMigrationRow[]>;
|
|
8
|
+
declare function loadMigrationsFromDirectory(directory: string): Promise<Migration[]>;
|
|
9
|
+
declare function getMigrationStatus(db: MigrationDatabase, migrations: Migration[]): Promise<MigrationStatus[]>;
|
|
10
|
+
declare function migrateDatabase(db: MigrationDatabase, migrations: Migration[], options?: {
|
|
11
|
+
onMigration?: (name: string) => void;
|
|
12
|
+
}): Promise<number>;
|
|
13
|
+
declare function rollbackDatabase(db: MigrationDatabase, migrations: Migration[], options?: {
|
|
14
|
+
onMigration?: (name: string) => void;
|
|
15
|
+
}): Promise<number>;
|
|
16
|
+
declare function freshDatabase(db: MigrationDatabase, migrations: Migration[], options?: {
|
|
17
|
+
onMigration?: (name: string) => void;
|
|
18
|
+
}): Promise<void>;
|
|
19
|
+
export { ensureMigrationsTable, freshDatabase, getAppliedMigrations, getMigrationStatus, loadMigrationsFromDirectory, migrateDatabase, rollbackDatabase, };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface MigrationDatabase {
|
|
2
|
+
unsafe<T = unknown>(query: string, params?: readonly unknown[]): Promise<T[]>;
|
|
3
|
+
}
|
|
4
|
+
interface Migration {
|
|
5
|
+
name: string;
|
|
6
|
+
up(db: MigrationDatabase): Promise<void>;
|
|
7
|
+
down(db: MigrationDatabase): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
type MigrationStatus = {
|
|
10
|
+
name: string;
|
|
11
|
+
status: "up" | "pending";
|
|
12
|
+
batch: number | null;
|
|
13
|
+
};
|
|
14
|
+
export type { Migration, MigrationDatabase, MigrationStatus };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type BaseRepository from "./baseRepository";
|
|
2
|
+
import type { BelongsToRelation, HasManyRelation } from "./relationships";
|
|
3
|
+
import type { RepositoryQuery } from "./repositoryQuery";
|
|
4
|
+
import type { QueryOptions, QueryWhere } from "./types";
|
|
5
|
+
interface ModelConstructor<TEntity extends object, PrimaryKey extends keyof TEntity & string, TModel extends Model<TEntity, PrimaryKey>> {
|
|
6
|
+
new (attributes: TEntity, repository: BaseRepository<TEntity, PrimaryKey>): TModel;
|
|
7
|
+
}
|
|
8
|
+
declare class Model<TEntity extends object, PrimaryKey extends keyof TEntity & string> {
|
|
9
|
+
protected readonly attributes: TEntity;
|
|
10
|
+
protected readonly repository: BaseRepository<TEntity, PrimaryKey>;
|
|
11
|
+
constructor(attributes: TEntity, repository: BaseRepository<TEntity, PrimaryKey>);
|
|
12
|
+
get<K extends keyof TEntity>(key: K): TEntity[K];
|
|
13
|
+
get id(): TEntity[PrimaryKey];
|
|
14
|
+
toObject(): TEntity;
|
|
15
|
+
protected primaryKey(): PrimaryKey;
|
|
16
|
+
static repository<TEntity extends object, PrimaryKey extends keyof TEntity & string, TModel extends Model<TEntity, PrimaryKey>>(this: ModelConstructor<TEntity, PrimaryKey, TModel>): BaseRepository<TEntity, PrimaryKey>;
|
|
17
|
+
static query<TEntity extends object, PrimaryKey extends keyof TEntity & string, TModel extends Model<TEntity, PrimaryKey>>(this: ModelConstructor<TEntity, PrimaryKey, TModel>): RepositoryQuery<TEntity, PrimaryKey>;
|
|
18
|
+
static find<TEntity extends object, PrimaryKey extends keyof TEntity & string, TModel extends Model<TEntity, PrimaryKey>>(this: ModelConstructor<TEntity, PrimaryKey, TModel>, id: TEntity[PrimaryKey]): Promise<TModel | null>;
|
|
19
|
+
static findOrFail<TEntity extends object, PrimaryKey extends keyof TEntity & string, TModel extends Model<TEntity, PrimaryKey>>(this: ModelConstructor<TEntity, PrimaryKey, TModel>, id: TEntity[PrimaryKey], errorFactory?: (id: TEntity[PrimaryKey]) => Error): Promise<TModel>;
|
|
20
|
+
static all<TEntity extends object, PrimaryKey extends keyof TEntity & string, TModel extends Model<TEntity, PrimaryKey>>(this: ModelConstructor<TEntity, PrimaryKey, TModel>, options?: QueryOptions<TEntity>): Promise<TModel[]>;
|
|
21
|
+
static firstWhere<TEntity extends object, PrimaryKey extends keyof TEntity & string, TModel extends Model<TEntity, PrimaryKey>>(this: ModelConstructor<TEntity, PrimaryKey, TModel>, where: QueryWhere<TEntity>, options?: Omit<QueryOptions<TEntity>, "where">): Promise<TModel | null>;
|
|
22
|
+
loadHasMany<TChild extends object, LocalKey extends keyof TEntity & string, ForeignKey extends keyof TChild & string, Alias extends string>(as: Alias, relation: HasManyRelation<TEntity, TChild, LocalKey, ForeignKey>, childRepository: BaseRepository<TChild, keyof TChild & string>, options?: Omit<QueryOptions<TChild>, "where">): Promise<this & Record<Alias, TChild[]>>;
|
|
23
|
+
loadBelongsTo<TParent extends object, ForeignKey extends keyof TEntity & string, OwnerKey extends keyof TParent & string, Alias extends string>(as: Alias, relation: BelongsToRelation<TEntity, TParent, ForeignKey, OwnerKey>, parentRepository: BaseRepository<TParent, OwnerKey>, options?: Omit<QueryOptions<TParent>, "where">): Promise<this & Record<Alias, TParent | undefined>>;
|
|
24
|
+
mergeAttributes(patch: Partial<TEntity>): this;
|
|
25
|
+
}
|
|
26
|
+
declare function registerModelRepository<TEntity extends object, PrimaryKey extends keyof TEntity & string, TModel extends Model<TEntity, PrimaryKey>>(model: ModelConstructor<TEntity, PrimaryKey, TModel>, repository: BaseRepository<TEntity, PrimaryKey>): ModelConstructor<TEntity, PrimaryKey, TModel> & typeof Model;
|
|
27
|
+
export type { ModelConstructor };
|
|
28
|
+
export { Model, registerModelRepository };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import type { AbilityChecker } from "../auth/abilityChecker";
|
|
2
2
|
import type { Middleware } from "./middleware";
|
|
3
|
-
declare function createRequireAbilityMiddleware(
|
|
3
|
+
declare function createRequireAbilityMiddleware(abilityChecker: AbilityChecker): (ability: string) => Middleware;
|
|
4
4
|
export { createRequireAbilityMiddleware };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
declare abstract class WebFormRequest<TOutput> {
|
|
2
2
|
authorize(_request: Request): boolean | Promise<boolean>;
|
|
3
3
|
protected abstract parse(payload: unknown): TOutput;
|
|
4
|
+
validatePayload(payload: unknown): TOutput;
|
|
4
5
|
validate(request: Request): Promise<TOutput>;
|
|
5
6
|
}
|
|
6
7
|
export { WebFormRequest };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
type ValidationRule = (field: string, value: unknown, payload: Record<string, unknown>) => string | undefined;
|
|
2
|
+
type ValidationSchema = Record<string, ValidationRule[]>;
|
|
3
|
+
declare function required(): ValidationRule;
|
|
4
|
+
declare function stringRule(): ValidationRule;
|
|
5
|
+
declare function minLength(minimum: number): ValidationRule;
|
|
6
|
+
declare function maxLength(maximum: number): ValidationRule;
|
|
7
|
+
declare function pattern(expression: RegExp): ValidationRule;
|
|
8
|
+
declare function enumRule<TValue extends string>(allowedValues: readonly TValue[]): ValidationRule;
|
|
9
|
+
declare function optional(): ValidationRule;
|
|
10
|
+
declare function integerRule(): ValidationRule;
|
|
11
|
+
declare function emailRule(): ValidationRule;
|
|
12
|
+
declare function confirmed(fieldName: string): ValidationRule;
|
|
13
|
+
declare function positiveIntegerRule(): ValidationRule;
|
|
14
|
+
declare function integerRange(minimum: number, maximum: number): ValidationRule;
|
|
15
|
+
declare function validateObject(payload: unknown, schema: ValidationSchema): Record<string, unknown>;
|
|
16
|
+
export type { ValidationRule, ValidationSchema };
|
|
17
|
+
export { confirmed, emailRule, enumRule, integerRange, integerRule, maxLength, minLength, optional, pattern, positiveIntegerRule, required, stringRule, validateObject, };
|
|
@@ -4,13 +4,17 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export type { ServiceProvider } from "../bootstrap/contracts";
|
|
6
6
|
export { ConfigStore, resolveService, ServiceContainer, } from "../bootstrap/contracts";
|
|
7
|
+
export type { AbilityChecker } from "../core/auth/abilityChecker";
|
|
7
8
|
export type { AuthUser } from "../core/auth/authContext";
|
|
8
9
|
export { currentAuthUser, runWithAuthUser } from "../core/auth/authContext";
|
|
9
|
-
export { Policy } from "../core/auth/policy";
|
|
10
|
+
export { Policy, PolicyGate } from "../core/auth/policy";
|
|
10
11
|
export { default as CacheRepository } from "../core/cache/repository";
|
|
11
12
|
export { CACHE_TAGS } from "../core/cache/tags";
|
|
12
13
|
export type { DatabaseConnection } from "../core/database/baseRepository";
|
|
13
14
|
export { default as BaseRepository } from "../core/database/baseRepository";
|
|
15
|
+
export { freshDatabase, getMigrationStatus, loadMigrationsFromDirectory, migrateDatabase, rollbackDatabase, } from "../core/database/migrations/runner";
|
|
16
|
+
export type { Migration, MigrationDatabase, MigrationStatus, } from "../core/database/migrations/types";
|
|
17
|
+
export { Model, registerModelRepository } from "../core/database/model";
|
|
14
18
|
export type { BelongsToRelation, HasManyRelation } from "../core/database/relationships";
|
|
15
19
|
export { belongsTo, hasMany, indexBelongsToRelation, indexHasManyRelation, } from "../core/database/relationships";
|
|
16
20
|
export { RepositoryQuery } from "../core/database/repositoryQuery";
|
|
@@ -33,3 +37,5 @@ export type { Queue, QueuePriority } from "../core/queue";
|
|
|
33
37
|
export { AsyncQueue, createQueue, Job, SyncQueue } from "../core/queue";
|
|
34
38
|
export type { StorageDriver } from "../core/storage/storage";
|
|
35
39
|
export { LocalStorageDriver, StorageManager } from "../core/storage/storage";
|
|
40
|
+
export type { ValidationRule, ValidationSchema } from "../core/validation/rules";
|
|
41
|
+
export { emailRule, maxLength, minLength, required, stringRule, validateObject, } from "../core/validation/rules";
|
package/dist/index.js
CHANGED
|
@@ -1024,6 +1024,166 @@ class BaseRepository {
|
|
|
1024
1024
|
}
|
|
1025
1025
|
}
|
|
1026
1026
|
var baseRepository_default = BaseRepository;
|
|
1027
|
+
// ../../src/core/database/migrations/runner.ts
|
|
1028
|
+
import { readdir } from "fs/promises";
|
|
1029
|
+
import { join } from "path";
|
|
1030
|
+
import { pathToFileURL } from "url";
|
|
1031
|
+
var MIGRATIONS_TABLE = "framework_migrations";
|
|
1032
|
+
async function ensureMigrationsTable(db2) {
|
|
1033
|
+
await db2.unsafe(`
|
|
1034
|
+
CREATE TABLE IF NOT EXISTS ${MIGRATIONS_TABLE} (
|
|
1035
|
+
name TEXT PRIMARY KEY,
|
|
1036
|
+
batch INTEGER NOT NULL,
|
|
1037
|
+
run_on TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
1038
|
+
)
|
|
1039
|
+
`);
|
|
1040
|
+
}
|
|
1041
|
+
async function getAppliedMigrations(db2) {
|
|
1042
|
+
await ensureMigrationsTable(db2);
|
|
1043
|
+
return await db2.unsafe(`
|
|
1044
|
+
SELECT name, batch
|
|
1045
|
+
FROM ${MIGRATIONS_TABLE}
|
|
1046
|
+
ORDER BY batch ASC, name ASC
|
|
1047
|
+
`);
|
|
1048
|
+
}
|
|
1049
|
+
async function loadMigrationsFromDirectory(directory) {
|
|
1050
|
+
const entries = await readdir(directory);
|
|
1051
|
+
const migrationFiles = entries.filter((entry) => entry.endsWith(".ts") && entry !== "types.ts" && entry !== "index.ts" && entry !== "runner.ts").sort();
|
|
1052
|
+
const loadedMigrations = await Promise.all(migrationFiles.map(async (fileName) => {
|
|
1053
|
+
const moduleUrl = pathToFileURL(join(directory, fileName)).href;
|
|
1054
|
+
const module = await import(moduleUrl);
|
|
1055
|
+
return module.default;
|
|
1056
|
+
}));
|
|
1057
|
+
return loadedMigrations.filter((migration) => migration?.name !== undefined);
|
|
1058
|
+
}
|
|
1059
|
+
async function getMigrationStatus(db2, migrations) {
|
|
1060
|
+
const applied = await getAppliedMigrations(db2);
|
|
1061
|
+
const appliedByName = new Map(applied.map(({ name, batch }) => [name, Number(batch)]));
|
|
1062
|
+
return migrations.map(({ name }) => ({
|
|
1063
|
+
name,
|
|
1064
|
+
status: appliedByName.has(name) ? "up" : "pending",
|
|
1065
|
+
batch: appliedByName.get(name) ?? null
|
|
1066
|
+
}));
|
|
1067
|
+
}
|
|
1068
|
+
async function migrateDatabase(db2, migrations, options = {}) {
|
|
1069
|
+
const applied = await getAppliedMigrations(db2);
|
|
1070
|
+
const appliedNames = new Set(applied.map(({ name }) => name));
|
|
1071
|
+
const nextBatch = applied.reduce((currentMax, { batch }) => Math.max(currentMax, Number(batch)), 0) + 1;
|
|
1072
|
+
const pendingMigrations = migrations.filter(({ name }) => !appliedNames.has(name));
|
|
1073
|
+
for (const migration of pendingMigrations) {
|
|
1074
|
+
options.onMigration?.(migration.name);
|
|
1075
|
+
await migration.up(db2);
|
|
1076
|
+
await db2.unsafe(`INSERT INTO ${MIGRATIONS_TABLE} (name, batch) VALUES ($1, $2) ON CONFLICT (name) DO NOTHING`, [migration.name, nextBatch]);
|
|
1077
|
+
}
|
|
1078
|
+
return pendingMigrations.length;
|
|
1079
|
+
}
|
|
1080
|
+
async function rollbackDatabase(db2, migrations, options = {}) {
|
|
1081
|
+
const applied = await getAppliedMigrations(db2);
|
|
1082
|
+
if (applied.length === 0) {
|
|
1083
|
+
return 0;
|
|
1084
|
+
}
|
|
1085
|
+
const lastBatch = applied.reduce((currentMax, { batch }) => Math.max(currentMax, Number(batch)), 0);
|
|
1086
|
+
const migrationsToRollback = new Set(applied.filter(({ batch }) => Number(batch) === lastBatch).map(({ name }) => name));
|
|
1087
|
+
let rolledBack = 0;
|
|
1088
|
+
for (const migration of [...migrations].reverse()) {
|
|
1089
|
+
if (!migrationsToRollback.has(migration.name)) {
|
|
1090
|
+
continue;
|
|
1091
|
+
}
|
|
1092
|
+
options.onMigration?.(migration.name);
|
|
1093
|
+
await migration.down(db2);
|
|
1094
|
+
await db2.unsafe(`DELETE FROM ${MIGRATIONS_TABLE} WHERE name = $1`, [migration.name]);
|
|
1095
|
+
rolledBack += 1;
|
|
1096
|
+
}
|
|
1097
|
+
return rolledBack;
|
|
1098
|
+
}
|
|
1099
|
+
async function freshDatabase(db2, migrations, options = {}) {
|
|
1100
|
+
const applied = await getAppliedMigrations(db2);
|
|
1101
|
+
const appliedNames = new Set(applied.map(({ name }) => name));
|
|
1102
|
+
const appliedMigrations = migrations.filter(({ name }) => appliedNames.has(name));
|
|
1103
|
+
for (const migration of [...appliedMigrations].reverse()) {
|
|
1104
|
+
options.onMigration?.(migration.name);
|
|
1105
|
+
await migration.down(db2);
|
|
1106
|
+
}
|
|
1107
|
+
if (appliedMigrations.length > 0) {
|
|
1108
|
+
await db2.unsafe(`DELETE FROM ${MIGRATIONS_TABLE}`);
|
|
1109
|
+
}
|
|
1110
|
+
await migrateDatabase(db2, migrations, options);
|
|
1111
|
+
}
|
|
1112
|
+
// ../../src/core/database/model.ts
|
|
1113
|
+
var modelRepositories = new WeakMap;
|
|
1114
|
+
function resolveModelRepository(model) {
|
|
1115
|
+
const repository = modelRepositories.get(model);
|
|
1116
|
+
if (!repository) {
|
|
1117
|
+
throw new Error(`${model.name}.repository() is not implemented.`);
|
|
1118
|
+
}
|
|
1119
|
+
return repository;
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
class Model {
|
|
1123
|
+
attributes;
|
|
1124
|
+
repository;
|
|
1125
|
+
constructor(attributes, repository) {
|
|
1126
|
+
this.attributes = attributes;
|
|
1127
|
+
this.repository = repository;
|
|
1128
|
+
}
|
|
1129
|
+
get(key) {
|
|
1130
|
+
return this.attributes[key];
|
|
1131
|
+
}
|
|
1132
|
+
get id() {
|
|
1133
|
+
return this.attributes[this.primaryKey()];
|
|
1134
|
+
}
|
|
1135
|
+
toObject() {
|
|
1136
|
+
return { ...this.attributes };
|
|
1137
|
+
}
|
|
1138
|
+
primaryKey() {
|
|
1139
|
+
throw new Error(`${this.constructor.name}.primaryKey() is not implemented.`);
|
|
1140
|
+
}
|
|
1141
|
+
static repository() {
|
|
1142
|
+
return resolveModelRepository(this);
|
|
1143
|
+
}
|
|
1144
|
+
static query() {
|
|
1145
|
+
return resolveModelRepository(this).query();
|
|
1146
|
+
}
|
|
1147
|
+
static async find(id) {
|
|
1148
|
+
const repository = resolveModelRepository(this);
|
|
1149
|
+
const record = await repository.findById(id);
|
|
1150
|
+
return record ? new this(record, repository) : null;
|
|
1151
|
+
}
|
|
1152
|
+
static async findOrFail(id, errorFactory) {
|
|
1153
|
+
const repository = resolveModelRepository(this);
|
|
1154
|
+
const record = await repository.findByIdOrThrow(id, errorFactory ?? ((value) => new Error(`Record ${String(value)} not found.`)));
|
|
1155
|
+
return new this(record, repository);
|
|
1156
|
+
}
|
|
1157
|
+
static async all(options = {}) {
|
|
1158
|
+
const repository = resolveModelRepository(this);
|
|
1159
|
+
const rows = await repository.findAll(options);
|
|
1160
|
+
return rows.map((row) => new this(row, repository));
|
|
1161
|
+
}
|
|
1162
|
+
static async firstWhere(where, options = {}) {
|
|
1163
|
+
const repository = resolveModelRepository(this);
|
|
1164
|
+
const rows = await repository.findAll({ ...options, where, limit: 1 });
|
|
1165
|
+
const record = rows[0];
|
|
1166
|
+
return record ? new this(record, repository) : null;
|
|
1167
|
+
}
|
|
1168
|
+
async loadHasMany(as, relation, childRepository, options = {}) {
|
|
1169
|
+
const grouped = await childRepository.withConnection(this.repository.getConnection()).loadHasManyForParents([this.attributes], relation, options);
|
|
1170
|
+
const loaded = grouped.get(this.attributes[relation.localKey]) ?? [];
|
|
1171
|
+
return Object.assign(this, { [as]: loaded });
|
|
1172
|
+
}
|
|
1173
|
+
async loadBelongsTo(as, relation, parentRepository, options = {}) {
|
|
1174
|
+
const grouped = await this.repository.loadBelongsToForParents([this.attributes], relation, parentRepository, options);
|
|
1175
|
+
const loaded = grouped.get(this.attributes[relation.foreignKey]);
|
|
1176
|
+
return Object.assign(this, { [as]: loaded });
|
|
1177
|
+
}
|
|
1178
|
+
mergeAttributes(patch) {
|
|
1179
|
+
Object.assign(this.attributes, patch);
|
|
1180
|
+
return this;
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
function registerModelRepository(model, repository) {
|
|
1184
|
+
modelRepositories.set(model, repository);
|
|
1185
|
+
return model;
|
|
1186
|
+
}
|
|
1027
1187
|
// ../../src/core/database/table.ts
|
|
1028
1188
|
function defineTable(definition) {
|
|
1029
1189
|
return definition;
|
|
@@ -1279,7 +1439,7 @@ function mailer() {
|
|
|
1279
1439
|
// ../../src/core/storage/storage.ts
|
|
1280
1440
|
var {S3Client } = globalThis.Bun;
|
|
1281
1441
|
import { mkdir, readFile, unlink, writeFile } from "fs/promises";
|
|
1282
|
-
import { dirname, join } from "path";
|
|
1442
|
+
import { dirname, join as join2 } from "path";
|
|
1283
1443
|
|
|
1284
1444
|
class LocalStorageDriver {
|
|
1285
1445
|
rootDirectory;
|
|
@@ -1287,7 +1447,7 @@ class LocalStorageDriver {
|
|
|
1287
1447
|
this.rootDirectory = rootDirectory;
|
|
1288
1448
|
}
|
|
1289
1449
|
resolvePath(path) {
|
|
1290
|
-
return
|
|
1450
|
+
return join2(this.rootDirectory, path.replace(/^\/+/, ""));
|
|
1291
1451
|
}
|
|
1292
1452
|
async put(path, contents) {
|
|
1293
1453
|
const absolutePath = this.resolvePath(path);
|
|
@@ -1434,9 +1594,9 @@ function isViewsEnabled() {
|
|
|
1434
1594
|
}
|
|
1435
1595
|
|
|
1436
1596
|
// ../../src/core/view/etaViewEngine.ts
|
|
1437
|
-
import { join as
|
|
1597
|
+
import { join as join3 } from "path";
|
|
1438
1598
|
import { Eta } from "eta";
|
|
1439
|
-
var DEFAULT_VIEWS_DIRECTORY =
|
|
1599
|
+
var DEFAULT_VIEWS_DIRECTORY = join3(process.cwd(), "resources/views");
|
|
1440
1600
|
// ../../src/core/view/htmlResponse.ts
|
|
1441
1601
|
function htmlResponse(html, init = {}) {
|
|
1442
1602
|
return new Response(html, {
|
|
@@ -2203,13 +2363,16 @@ class WebFormRequest {
|
|
|
2203
2363
|
authorize(_request) {
|
|
2204
2364
|
return true;
|
|
2205
2365
|
}
|
|
2366
|
+
validatePayload(payload) {
|
|
2367
|
+
return this.parse(payload);
|
|
2368
|
+
}
|
|
2206
2369
|
async validate(request) {
|
|
2207
2370
|
if (!await this.authorize(request)) {
|
|
2208
2371
|
throw new ForbiddenError;
|
|
2209
2372
|
}
|
|
2210
2373
|
const payload = requestPrefersJson(request) ? await parseJsonBody(request, (body) => body) : await parseFormBody(request);
|
|
2211
2374
|
try {
|
|
2212
|
-
return this.
|
|
2375
|
+
return this.validatePayload(payload);
|
|
2213
2376
|
} catch (error) {
|
|
2214
2377
|
if (error instanceof ValidationError) {
|
|
2215
2378
|
throw error;
|
|
@@ -2243,12 +2406,94 @@ class AsyncQueue {
|
|
|
2243
2406
|
function createQueue(driver) {
|
|
2244
2407
|
return driver === "async" ? new AsyncQueue : new SyncQueue;
|
|
2245
2408
|
}
|
|
2409
|
+
// ../../src/core/validation/rules.ts
|
|
2410
|
+
function required() {
|
|
2411
|
+
return (field, value) => {
|
|
2412
|
+
if (value === undefined || value === null || typeof value === "string" && value.trim() === "") {
|
|
2413
|
+
return `"${field}" is required.`;
|
|
2414
|
+
}
|
|
2415
|
+
return;
|
|
2416
|
+
};
|
|
2417
|
+
}
|
|
2418
|
+
function stringRule() {
|
|
2419
|
+
return (field, value) => {
|
|
2420
|
+
if (value === undefined || value === null) {
|
|
2421
|
+
return;
|
|
2422
|
+
}
|
|
2423
|
+
if (typeof value !== "string") {
|
|
2424
|
+
return `"${field}" must be a string.`;
|
|
2425
|
+
}
|
|
2426
|
+
return;
|
|
2427
|
+
};
|
|
2428
|
+
}
|
|
2429
|
+
function minLength(minimum) {
|
|
2430
|
+
return (field, value) => {
|
|
2431
|
+
if (typeof value !== "string") {
|
|
2432
|
+
return;
|
|
2433
|
+
}
|
|
2434
|
+
if (value.trim().length < minimum) {
|
|
2435
|
+
return `"${field}" must be at least ${minimum} characters.`;
|
|
2436
|
+
}
|
|
2437
|
+
return;
|
|
2438
|
+
};
|
|
2439
|
+
}
|
|
2440
|
+
function maxLength(maximum) {
|
|
2441
|
+
return (field, value) => {
|
|
2442
|
+
if (typeof value !== "string") {
|
|
2443
|
+
return;
|
|
2444
|
+
}
|
|
2445
|
+
if (value.trim().length > maximum) {
|
|
2446
|
+
return `"${field}" must be at most ${maximum} characters.`;
|
|
2447
|
+
}
|
|
2448
|
+
return;
|
|
2449
|
+
};
|
|
2450
|
+
}
|
|
2451
|
+
function emailRule() {
|
|
2452
|
+
return (field, value) => {
|
|
2453
|
+
if (typeof value !== "string") {
|
|
2454
|
+
return;
|
|
2455
|
+
}
|
|
2456
|
+
const normalized = value.trim();
|
|
2457
|
+
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(normalized)) {
|
|
2458
|
+
return `"${field}" must be a valid email address.`;
|
|
2459
|
+
}
|
|
2460
|
+
return;
|
|
2461
|
+
};
|
|
2462
|
+
}
|
|
2463
|
+
function validateObject(payload, schema) {
|
|
2464
|
+
if (payload === null || typeof payload !== "object" || Array.isArray(payload)) {
|
|
2465
|
+
throw new ValidationError("Request body must be a JSON object.");
|
|
2466
|
+
}
|
|
2467
|
+
const body = payload;
|
|
2468
|
+
const errors = {};
|
|
2469
|
+
const output = {};
|
|
2470
|
+
for (const [field, rules] of Object.entries(schema)) {
|
|
2471
|
+
const messages = rules.map((rule) => rule(field, body[field], body)).filter((message) => message !== undefined);
|
|
2472
|
+
if (messages.length > 0) {
|
|
2473
|
+
errors[field] = messages;
|
|
2474
|
+
continue;
|
|
2475
|
+
}
|
|
2476
|
+
if (field in body && body[field] !== undefined) {
|
|
2477
|
+
const rawValue = body[field];
|
|
2478
|
+
output[field] = typeof rawValue === "string" ? rawValue.trim() : rawValue;
|
|
2479
|
+
}
|
|
2480
|
+
}
|
|
2481
|
+
if (Object.keys(errors).length > 0) {
|
|
2482
|
+
throw new ValidationError("The given data was invalid.", errors);
|
|
2483
|
+
}
|
|
2484
|
+
return output;
|
|
2485
|
+
}
|
|
2246
2486
|
export {
|
|
2247
2487
|
withErrorHandling,
|
|
2488
|
+
validateObject,
|
|
2489
|
+
stringRule,
|
|
2248
2490
|
storageFacade as storage,
|
|
2249
2491
|
securedBindRouteModel,
|
|
2250
2492
|
runWithAuthUser,
|
|
2493
|
+
rollbackDatabase,
|
|
2251
2494
|
resolveService,
|
|
2495
|
+
required,
|
|
2496
|
+
registerModelRepository,
|
|
2252
2497
|
queue,
|
|
2253
2498
|
prometheusRegistry,
|
|
2254
2499
|
policyGate,
|
|
@@ -2256,16 +2501,23 @@ export {
|
|
|
2256
2501
|
paginatedResponse,
|
|
2257
2502
|
normalizeMetricPath,
|
|
2258
2503
|
noContentResponse,
|
|
2504
|
+
minLength,
|
|
2505
|
+
migrateDatabase,
|
|
2506
|
+
maxLength,
|
|
2259
2507
|
mailer,
|
|
2260
2508
|
mail,
|
|
2261
2509
|
log,
|
|
2510
|
+
loadMigrationsFromDirectory,
|
|
2262
2511
|
jsonResponse,
|
|
2263
2512
|
isEtagEnabled,
|
|
2264
2513
|
indexHasManyRelation,
|
|
2265
2514
|
indexBelongsToRelation,
|
|
2266
2515
|
hasMany,
|
|
2516
|
+
getMigrationStatus,
|
|
2517
|
+
freshDatabase,
|
|
2267
2518
|
events,
|
|
2268
2519
|
etagFromResource,
|
|
2520
|
+
emailRule,
|
|
2269
2521
|
defineTable,
|
|
2270
2522
|
currentAuthUser,
|
|
2271
2523
|
createdResponse,
|
|
@@ -2286,8 +2538,10 @@ export {
|
|
|
2286
2538
|
RepositoryQuery,
|
|
2287
2539
|
PrometheusRegistry,
|
|
2288
2540
|
PreconditionFailedError,
|
|
2541
|
+
PolicyGate,
|
|
2289
2542
|
Policy,
|
|
2290
2543
|
NotFoundError,
|
|
2544
|
+
Model,
|
|
2291
2545
|
Mailer,
|
|
2292
2546
|
LogMailDriver,
|
|
2293
2547
|
LocalStorageDriver,
|