@venizia/ignis-kernel 0.4.8-5 → 0.4.8-6
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/esm/base/applications/common/types/application.d.ts +3 -4
- package/dist/esm/base/applications/common/types/application.js.map +1 -1
- package/dist/esm/base/applications/rest.d.ts +13 -7
- package/dist/esm/base/applications/rest.js +52 -7
- package/dist/esm/base/applications/rest.js.map +1 -1
- package/dist/esm/base/applications/router-factory.d.ts +27 -0
- package/dist/esm/base/applications/router-factory.js +28 -0
- package/dist/esm/base/applications/router-factory.js.map +1 -0
- package/dist/esm/base/auth/authenticate/common/types/jwt.d.ts +6 -4
- package/dist/esm/base/auth/authenticate/common/types/jwt.js.map +1 -1
- package/dist/esm/base/auth/authenticate/common/types/service.d.ts +1 -1
- package/dist/esm/base/auth/authenticate/common/types/service.js.map +1 -1
- package/dist/esm/base/controllers/factory/crud/readable.js +2 -1
- package/dist/esm/base/controllers/factory/crud/readable.js.map +1 -1
- package/dist/esm/base/controllers/rest/abstract.js +5 -0
- package/dist/esm/base/controllers/rest/abstract.js.map +1 -1
- package/dist/esm/base/datasources/abstract.d.ts +69 -2
- package/dist/esm/base/datasources/abstract.js +186 -1
- package/dist/esm/base/datasources/abstract.js.map +1 -1
- package/dist/esm/base/datasources/common/types.d.ts +60 -1
- package/dist/esm/base/datasources/common/types.js +14 -0
- package/dist/esm/base/datasources/common/types.js.map +1 -1
- package/dist/esm/base/datasources/opening-handles.d.ts +8 -0
- package/dist/esm/base/datasources/opening-handles.js +8 -0
- package/dist/esm/base/datasources/opening-handles.js.map +1 -0
- package/dist/esm/base/metadata/persistents.js +15 -1
- package/dist/esm/base/metadata/persistents.js.map +1 -1
- package/dist/esm/base/middlewares/app-error/app-error.middleware.js +12 -1
- package/dist/esm/base/middlewares/app-error/app-error.middleware.js.map +1 -1
- package/dist/esm/base/middlewares/app-error/common/types.d.ts +10 -8
- package/dist/esm/base/middlewares/app-error/common/types.js.map +1 -1
- package/dist/esm/base/middlewares/app-error/database.handler.js +14 -14
- package/dist/esm/base/middlewares/app-error/database.handler.js.map +1 -1
- package/dist/esm/base/middlewares/app-error/index.d.ts +2 -0
- package/dist/esm/base/middlewares/app-error/index.js +1 -0
- package/dist/esm/base/middlewares/app-error/index.js.map +1 -1
- package/dist/esm/base/middlewares/app-error/postgres-errors.d.ts +20 -0
- package/dist/esm/base/middlewares/app-error/postgres-errors.js +74 -0
- package/dist/esm/base/middlewares/app-error/postgres-errors.js.map +1 -0
- package/dist/esm/base/repositories/common/request-filter-guard.d.ts +52 -2
- package/dist/esm/base/repositories/common/request-filter-guard.js +110 -25
- package/dist/esm/base/repositories/common/request-filter-guard.js.map +1 -1
- package/dist/esm/helpers/inversion/common/types/persistence.d.ts +3 -1
- package/dist/esm/helpers/inversion/common/types/persistence.js.map +1 -1
- package/dist/esm/repository.d.ts +2 -0
- package/dist/esm/repository.js +2 -0
- package/dist/esm/repository.js.map +1 -1
- package/package.json +5 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"database.handler.js","sourceRoot":"","sources":["../../../../../src/base/middlewares/app-error/database.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sCAAsC,EACtC,8BAA8B,EAC9B,6BAA6B,EAC7B,8BAA8B,GAC/B,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"database.handler.js","sourceRoot":"","sources":["../../../../../src/base/middlewares/app-error/database.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sCAAsC,EACtC,8BAA8B,EAC9B,6BAA6B,EAC7B,8BAA8B,GAC/B,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEnD,MAAM,OAAO,oBAAoB;IAC/B,wMAAwM;IACxM,MAAM,CAAC,qBAAqB,CAAC,IAA6C;QAIxE,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,GAAG,IAAI,CAAC;QACrC,gGAAgG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KAAK,EAAE,CAAC,CAAC;QAE3D,OAAO,OAAO,KAAK,SAAS,IAAI,8BAA8B,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACxF,CAAC;CACF","sourcesContent":["import {\n DATABASE_CLIENT_ERROR_FALLBACK_MESSAGE,\n DATABASE_CLIENT_ERROR_MESSAGES,\n POSTGRES_CLIENT_ERROR_CLASSES,\n POSTGRES_RETRYABLE_ERROR_CODES,\n} from './definition';\nimport { PostgresErrors } from './postgres-errors';\n\nexport class DatabaseErrorHandler {\n /** True for a database error caused by the request (SQLSTATE class 22/23/44) -> HTTP 400; in production the detail/table/constraint context is suppressed so no row values or schema internals leak. */\n static isDatabaseClientError(opts: { error: Error; isProduction: boolean }): {\n isClientError: boolean;\n message?: string;\n } {\n const { error, isProduction } = opts;\n // A value without a SQLSTATE `code` (a gRPC numeric one, `ECONNRESET`) is not a Postgres error.\n const pgError = PostgresErrors.find({ error });\n\n // Only request-caused SQLSTATE classes are client errors.\n if (!pgError || !POSTGRES_CLIENT_ERROR_CLASSES.includes(pgError.code.slice(0, 2))) {\n return { isClientError: false };\n }\n\n const { code } = pgError;\n\n const baseMessage =\n DATABASE_CLIENT_ERROR_MESSAGES[code] ?? DATABASE_CLIENT_ERROR_FALLBACK_MESSAGE;\n\n // In production expose ONLY the generic base message: `detail` can echo row values and `table`/`constraint` reveal schema internals.\n if (isProduction) {\n return { isClientError: true, message: baseMessage };\n }\n\n // Non-production: append driver-provided context to aid debugging.\n const lines = [baseMessage];\n\n if (pgError.detail) {\n lines.push(`Detail: ${pgError.detail}`);\n }\n\n if (pgError.table) {\n lines.push(`Table: ${pgError.table}`);\n }\n\n if (pgError.constraint) {\n lines.push(`Constraint: ${pgError.constraint}`);\n }\n\n return {\n isClientError: true,\n message: lines.join('\\n'),\n };\n }\n\n /** True for a transient, retryable transaction conflict (serialization failure / deadlock - SQLSTATE 40001 / 40P01) -> HTTP 409: the client can retry the same request. */\n static isRetryableDatabaseError(opts: { error: Error }): boolean {\n const pgError = PostgresErrors.find({ error: opts.error });\n\n return pgError !== undefined && POSTGRES_RETRYABLE_ERROR_CODES.includes(pgError.code);\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/base/middlewares/app-error/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC","sourcesContent":["export * from './app-error.middleware';\nexport { PostgresErrorCodes } from './definition';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/base/middlewares/app-error/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC","sourcesContent":["export * from './app-error.middleware';\nexport { PostgresErrorCodes } from './definition';\nexport { PostgresErrors } from './postgres-errors';\nexport type { IPostgresError } from './common';\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { IPostgresError } from './common/index.js';
|
|
2
|
+
/** Finds the Postgres error in a thrown value, through the `cause` chain an ORM builds around a driver error. The error handler uses it; so can your code. */
|
|
3
|
+
export declare class PostgresErrors {
|
|
4
|
+
/** How many `cause` hops the walk follows: the thrown error is hop 0, so an error 10 hops down is found and one 11 hops down is not. */
|
|
5
|
+
static readonly MAX_DEPTH = 10;
|
|
6
|
+
/** Reads one property and never throws: a getter that throws, or a Proxy trap that does, reads as `undefined`. */
|
|
7
|
+
private static read;
|
|
8
|
+
/** The first spelling of a field that holds a string, or `undefined`. */
|
|
9
|
+
private static readString;
|
|
10
|
+
/** Normalizes one driver error into the result type, or `undefined` when its `code` is not a SQLSTATE. */
|
|
11
|
+
private static normalize;
|
|
12
|
+
/**
|
|
13
|
+
* Finds the Postgres error in a thrown value, looking through `cause` (an ORM wraps the driver error, sometimes more than once). The outermost error with a SQLSTATE `code` wins.
|
|
14
|
+
* Never throws: a value that is not an object, a cycle, a getter that throws and a Proxy all end in `undefined`.
|
|
15
|
+
*/
|
|
16
|
+
static find(opts: {
|
|
17
|
+
error: unknown;
|
|
18
|
+
}): IPostgresError | undefined;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=postgres-errors.d.ts.map
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/** A SQLSTATE is five characters of digits and upper-case letters (Postgres appendix A); `ECONNRESET`, a gRPC number or a lower-case word is not one. */
|
|
2
|
+
const SQLSTATE_PATTERN = /^[0-9A-Z]{5}$/;
|
|
3
|
+
/** The driver spellings of each optional field, in the order tried: node-postgres and PGlite use the first, postgres-js the second. */
|
|
4
|
+
const FIELD_SPELLINGS = {
|
|
5
|
+
constraint: ['constraint', 'constraint_name'],
|
|
6
|
+
detail: ['detail'],
|
|
7
|
+
table: ['table', 'table_name'],
|
|
8
|
+
column: ['column', 'column_name'],
|
|
9
|
+
schema: ['schema', 'schema_name'],
|
|
10
|
+
message: ['message'],
|
|
11
|
+
};
|
|
12
|
+
/** Finds the Postgres error in a thrown value, through the `cause` chain an ORM builds around a driver error. The error handler uses it; so can your code. */
|
|
13
|
+
export class PostgresErrors {
|
|
14
|
+
/** How many `cause` hops the walk follows: the thrown error is hop 0, so an error 10 hops down is found and one 11 hops down is not. */
|
|
15
|
+
static { this.MAX_DEPTH = 10; }
|
|
16
|
+
/** Reads one property and never throws: a getter that throws, or a Proxy trap that does, reads as `undefined`. */
|
|
17
|
+
static read(opts) {
|
|
18
|
+
try {
|
|
19
|
+
return Reflect.get(opts.target, opts.key);
|
|
20
|
+
// eslint-disable-next-line venizia/no-silent-catch -- a hostile getter or Proxy is the input this helper exists to survive; the caller treats the property as absent
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** The first spelling of a field that holds a string, or `undefined`. */
|
|
27
|
+
static readString(opts) {
|
|
28
|
+
for (const key of opts.spellings) {
|
|
29
|
+
const value = PostgresErrors.read({ target: opts.target, key });
|
|
30
|
+
if (typeof value === 'string') {
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
/** Normalizes one driver error into the result type, or `undefined` when its `code` is not a SQLSTATE. */
|
|
37
|
+
static normalize(opts) {
|
|
38
|
+
const { target } = opts;
|
|
39
|
+
const code = PostgresErrors.read({ target, key: 'code' });
|
|
40
|
+
if (typeof code !== 'string' || !SQLSTATE_PATTERN.test(code)) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
const result = { code };
|
|
44
|
+
// Only the fields the driver set: an absent field stays absent rather than becoming `undefined`.
|
|
45
|
+
for (const [name, spellings] of Object.entries(FIELD_SPELLINGS)) {
|
|
46
|
+
const value = PostgresErrors.readString({ target, spellings });
|
|
47
|
+
if (value !== undefined) {
|
|
48
|
+
Reflect.set(result, name, value);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Finds the Postgres error in a thrown value, looking through `cause` (an ORM wraps the driver error, sometimes more than once). The outermost error with a SQLSTATE `code` wins.
|
|
55
|
+
* Never throws: a value that is not an object, a cycle, a getter that throws and a Proxy all end in `undefined`.
|
|
56
|
+
*/
|
|
57
|
+
static find(opts) {
|
|
58
|
+
const visited = new Set();
|
|
59
|
+
let current = opts.error;
|
|
60
|
+
for (let depth = 0; depth <= PostgresErrors.MAX_DEPTH; depth += 1) {
|
|
61
|
+
if (typeof current !== 'object' || current === null || visited.has(current)) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
visited.add(current);
|
|
65
|
+
const found = PostgresErrors.normalize({ target: current });
|
|
66
|
+
if (found) {
|
|
67
|
+
return found;
|
|
68
|
+
}
|
|
69
|
+
current = PostgresErrors.read({ target: current, key: 'cause' });
|
|
70
|
+
}
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=postgres-errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgres-errors.js","sourceRoot":"","sources":["../../../../../src/base/middlewares/app-error/postgres-errors.ts"],"names":[],"mappings":"AAEA,yJAAyJ;AACzJ,MAAM,gBAAgB,GAAG,eAAe,CAAC;AAEzC,uIAAuI;AACvI,MAAM,eAAe,GAAqE;IACxF,UAAU,EAAE,CAAC,YAAY,EAAE,iBAAiB,CAAC;IAC7C,MAAM,EAAE,CAAC,QAAQ,CAAC;IAClB,KAAK,EAAE,CAAC,OAAO,EAAE,YAAY,CAAC;IAC9B,MAAM,EAAE,CAAC,QAAQ,EAAE,aAAa,CAAC;IACjC,MAAM,EAAE,CAAC,QAAQ,EAAE,aAAa,CAAC;IACjC,OAAO,EAAE,CAAC,SAAS,CAAC;CACrB,CAAC;AAEF,8JAA8J;AAC9J,MAAM,OAAO,cAAc;IACzB,wIAAwI;aACxH,cAAS,GAAG,EAAE,CAAC;IAE/B,kHAAkH;IAC1G,MAAM,CAAC,IAAI,CAAC,IAAqC;QACvD,IAAI,CAAC;YACH,OAAO,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;YAC1C,qKAAqK;QACvK,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,SAAS,CAAC;QACnB,CAAC;IACH,CAAC;IAED,yEAAyE;IACjE,MAAM,CAAC,UAAU,CAAC,IAGzB;QACC,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YACjC,MAAM,KAAK,GAAG,cAAc,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;YAEhE,IAAI,OAAO,KAAK,KAAK,QAAQ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AAC;gBAC5E,OAAO,SAAS,CAAC;YACnB,CAAC;YAED,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;YAErB,MAAM,KAAK,GAAG,cAAc,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;YAE5D,IAAI,KAAK,EAAE,CAAC;gBACV,OAAO,KAAK,CAAC;YACf,CAAC;YAED,OAAO,GAAG,cAAc,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,CAAC;QACnE,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC","sourcesContent":["import type { IPostgresError } from './common';\n\n/** A SQLSTATE is five characters of digits and upper-case letters (Postgres appendix A); `ECONNRESET`, a gRPC number or a lower-case word is not one. */\nconst SQLSTATE_PATTERN = /^[0-9A-Z]{5}$/;\n\n/** The driver spellings of each optional field, in the order tried: node-postgres and PGlite use the first, postgres-js the second. */\nconst FIELD_SPELLINGS: Record<Exclude<keyof IPostgresError, 'code'>, readonly string[]> = {\n constraint: ['constraint', 'constraint_name'],\n detail: ['detail'],\n table: ['table', 'table_name'],\n column: ['column', 'column_name'],\n schema: ['schema', 'schema_name'],\n message: ['message'],\n};\n\n/** Finds the Postgres error in a thrown value, through the `cause` chain an ORM builds around a driver error. The error handler uses it; so can your code. */\nexport class PostgresErrors {\n /** How many `cause` hops the walk follows: the thrown error is hop 0, so an error 10 hops down is found and one 11 hops down is not. */\n static readonly MAX_DEPTH = 10;\n\n /** Reads one property and never throws: a getter that throws, or a Proxy trap that does, reads as `undefined`. */\n private static read(opts: { target: object; key: string }): unknown {\n try {\n return Reflect.get(opts.target, opts.key);\n // eslint-disable-next-line venizia/no-silent-catch -- a hostile getter or Proxy is the input this helper exists to survive; the caller treats the property as absent\n } catch {\n return undefined;\n }\n }\n\n /** The first spelling of a field that holds a string, or `undefined`. */\n private static readString(opts: {\n target: object;\n spellings: readonly string[];\n }): string | undefined {\n for (const key of opts.spellings) {\n const value = PostgresErrors.read({ target: opts.target, key });\n\n if (typeof value === 'string') {\n return value;\n }\n }\n\n return undefined;\n }\n\n /** Normalizes one driver error into the result type, or `undefined` when its `code` is not a SQLSTATE. */\n private static normalize(opts: { target: object }): IPostgresError | undefined {\n const { target } = opts;\n const code = PostgresErrors.read({ target, key: 'code' });\n\n if (typeof code !== 'string' || !SQLSTATE_PATTERN.test(code)) {\n return undefined;\n }\n\n const result: IPostgresError = { code };\n\n // Only the fields the driver set: an absent field stays absent rather than becoming `undefined`.\n for (const [name, spellings] of Object.entries(FIELD_SPELLINGS)) {\n const value = PostgresErrors.readString({ target, spellings });\n\n if (value !== undefined) {\n Reflect.set(result, name, value);\n }\n }\n\n return result;\n }\n\n /**\n * Finds the Postgres error in a thrown value, looking through `cause` (an ORM wraps the driver error, sometimes more than once). The outermost error with a SQLSTATE `code` wins.\n * Never throws: a value that is not an object, a cycle, a getter that throws and a Proxy all end in `undefined`.\n */\n static find(opts: { error: unknown }): IPostgresError | undefined {\n const visited = new Set<object>();\n let current: unknown = opts.error;\n\n for (let depth = 0; depth <= PostgresErrors.MAX_DEPTH; depth += 1) {\n if (typeof current !== 'object' || current === null || visited.has(current)) {\n return undefined;\n }\n\n visited.add(current);\n\n const found = PostgresErrors.normalize({ target: current });\n\n if (found) {\n return found;\n }\n\n current = PostgresErrors.read({ target: current, key: 'cause' });\n }\n\n return undefined;\n }\n}\n"]}
|
|
@@ -1,15 +1,40 @@
|
|
|
1
|
-
|
|
1
|
+
import type { TConstValue } from '@venizia/ignis-helpers/common';
|
|
2
|
+
/** What a request filter holds that a query reads: `where`, `order`, the `include` scopes, and the `fields` that can decide the order it reads them in. */
|
|
2
3
|
export interface IRequestFilterShape {
|
|
3
4
|
where?: unknown;
|
|
4
5
|
order?: unknown;
|
|
5
6
|
include?: unknown;
|
|
7
|
+
fields?: unknown;
|
|
6
8
|
}
|
|
7
9
|
/** Which part of a filter a key came from: the refusal reads like the query's own refusal of an unknown column of that part. */
|
|
8
10
|
export type TRequestFilterKeyKind = 'where' | 'order';
|
|
11
|
+
/** The parts of a request filter a query reads, one at a time, in an order the query decides. */
|
|
12
|
+
export declare class RequestFilterParts {
|
|
13
|
+
static readonly WHERE = "where";
|
|
14
|
+
static readonly ORDER = "order";
|
|
15
|
+
static readonly INCLUDE = "include";
|
|
16
|
+
}
|
|
17
|
+
/** A part of a request filter: `where`, `order` or `include`. */
|
|
18
|
+
export type TRequestFilterPart = TConstValue<typeof RequestFilterParts>;
|
|
19
|
+
/** One piece of a request filter, as the query reads it on its own: a `where` of one key, one `order` entry, or one `include` entry. */
|
|
20
|
+
export type TRequestFilterPiece = {
|
|
21
|
+
part: typeof RequestFilterParts.WHERE;
|
|
22
|
+
value: Record<string, unknown>;
|
|
23
|
+
} | {
|
|
24
|
+
part: typeof RequestFilterParts.ORDER;
|
|
25
|
+
value: string;
|
|
26
|
+
} | {
|
|
27
|
+
part: typeof RequestFilterParts.INCLUDE;
|
|
28
|
+
value: unknown;
|
|
29
|
+
};
|
|
9
30
|
/**
|
|
10
31
|
* How one model judges a key of a request filter. The repository of a model supplies it, so the judgment reads a key the way
|
|
11
32
|
* that model's own query does (a relational JSON path names its first part, a search field is a dotted name), and a refusal
|
|
12
33
|
* is the error that query gives for a column or field that does not exist.
|
|
34
|
+
*
|
|
35
|
+
* A filter that names a hidden field is refused with the first refusal the query itself would meet if the hidden name were
|
|
36
|
+
* missing: `readOrder` gives the order the query reads the parts in, `filterRefusal` and `queryRefusal` the errors it throws
|
|
37
|
+
* on its own before that point. Without them the parts are read `where`, `order`, `include`, and nothing else is refused.
|
|
13
38
|
*/
|
|
14
39
|
export interface IRequestFilterJudge {
|
|
15
40
|
/** Whether the key reaches a field the model hides. */
|
|
@@ -26,6 +51,22 @@ export interface IRequestFilterJudge {
|
|
|
26
51
|
relation?(opts: {
|
|
27
52
|
relation: string;
|
|
28
53
|
}): IRequestFilterJudge | undefined;
|
|
54
|
+
/** The parts of `filter` in the order this model's query reads them. */
|
|
55
|
+
readOrder?(opts: {
|
|
56
|
+
filter: IRequestFilterShape;
|
|
57
|
+
}): ReadonlyArray<TRequestFilterPart>;
|
|
58
|
+
/** The error the query throws for `filter` as a whole before it reads any key of it, or `undefined`. */
|
|
59
|
+
filterRefusal?(opts: {
|
|
60
|
+
filter: IRequestFilterShape;
|
|
61
|
+
tableName: string;
|
|
62
|
+
}): Error | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* The error the query throws for one piece of a filter on its own, or `undefined` when it reads the piece. A piece is a
|
|
65
|
+
* `where` of one key (`{ [key]: value }`), one `order` entry, or one `include` entry.
|
|
66
|
+
*/
|
|
67
|
+
queryRefusal?(opts: TRequestFilterPiece & {
|
|
68
|
+
tableName: string;
|
|
69
|
+
}): Error | undefined;
|
|
29
70
|
}
|
|
30
71
|
/**
|
|
31
72
|
* A filter that comes from a request is not allowed to name a field the model hides. Hidden fields are never returned, but a
|
|
@@ -57,12 +98,21 @@ export declare class RequestFilterGuard {
|
|
|
57
98
|
}): void;
|
|
58
99
|
/**
|
|
59
100
|
* Refuses a filter that names a hidden field in its `where`, its `order`, or the `scope` of any `include`, each level
|
|
60
|
-
* judged by the model it reads (`judge.relation` answers it).
|
|
101
|
+
* judged by the model it reads (`judge.relation` answers it). The parts are read in the order the query reads them, and a
|
|
102
|
+
* filter that the query refuses for another reason before it meets the hidden field gets that refusal instead, so the answer
|
|
103
|
+
* is the one the same filter gets with a missing name in place of the hidden one. A filter that names no hidden field is
|
|
104
|
+
* left to the query.
|
|
61
105
|
*/
|
|
62
106
|
static assertFilter(opts: {
|
|
63
107
|
filter?: IRequestFilterShape;
|
|
64
108
|
judge: IRequestFilterJudge;
|
|
65
109
|
tableName: string;
|
|
66
110
|
}): void;
|
|
111
|
+
/** Throws for a hidden key: the refusal met before it, or the hidden key's own. Otherwise keeps the query's refusal of the piece when it is the first. */
|
|
112
|
+
private static visitKey;
|
|
113
|
+
private static walkFilter;
|
|
114
|
+
private static walkWhere;
|
|
115
|
+
private static walkOrder;
|
|
116
|
+
private static walkInclude;
|
|
67
117
|
}
|
|
68
118
|
//# sourceMappingURL=request-filter-guard.d.ts.map
|
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import { HTTP } from '@venizia/ignis-helpers/common';
|
|
2
2
|
import { getError } from '@venizia/ignis-helpers/core';
|
|
3
3
|
import { QueryOperators, Sorts } from '@venizia/ignis-filter';
|
|
4
|
+
/** The parts of a request filter a query reads, one at a time, in an order the query decides. */
|
|
5
|
+
export class RequestFilterParts {
|
|
6
|
+
static { this.WHERE = 'where'; }
|
|
7
|
+
static { this.ORDER = 'order'; }
|
|
8
|
+
static { this.INCLUDE = 'include'; }
|
|
9
|
+
}
|
|
10
|
+
const DEFAULT_READ_ORDER = [
|
|
11
|
+
RequestFilterParts.WHERE,
|
|
12
|
+
RequestFilterParts.ORDER,
|
|
13
|
+
RequestFilterParts.INCLUDE,
|
|
14
|
+
];
|
|
4
15
|
/**
|
|
5
16
|
* A filter that comes from a request is not allowed to name a field the model hides. Hidden fields are never returned, but a
|
|
6
17
|
* `where` or an `order` on one reveals its value one question at a time. The check runs where a request enters
|
|
@@ -27,7 +38,58 @@ export class RequestFilterGuard {
|
|
|
27
38
|
}
|
|
28
39
|
/** Refuses a `where` that names a hidden field, at any depth of `and` and `or`. */
|
|
29
40
|
static assertWhere(opts) {
|
|
30
|
-
|
|
41
|
+
RequestFilterGuard.walkWhere({ ...opts, walk: {} });
|
|
42
|
+
}
|
|
43
|
+
/** Refuses an `order` that names a hidden field (a string or a list of `'field DIRECTION'` entries), reading each entry as the query does. */
|
|
44
|
+
static assertOrder(opts) {
|
|
45
|
+
RequestFilterGuard.walkOrder({ ...opts, walk: {} });
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Refuses a filter that names a hidden field in its `where`, its `order`, or the `scope` of any `include`, each level
|
|
49
|
+
* judged by the model it reads (`judge.relation` answers it). The parts are read in the order the query reads them, and a
|
|
50
|
+
* filter that the query refuses for another reason before it meets the hidden field gets that refusal instead, so the answer
|
|
51
|
+
* is the one the same filter gets with a missing name in place of the hidden one. A filter that names no hidden field is
|
|
52
|
+
* left to the query.
|
|
53
|
+
*/
|
|
54
|
+
static assertFilter(opts) {
|
|
55
|
+
RequestFilterGuard.walkFilter({ ...opts, walk: {} });
|
|
56
|
+
}
|
|
57
|
+
/** Throws for a hidden key: the refusal met before it, or the hidden key's own. Otherwise keeps the query's refusal of the piece when it is the first. */
|
|
58
|
+
static visitKey(opts) {
|
|
59
|
+
const { walk, judge, tableName, key, piece } = opts;
|
|
60
|
+
if (judge.isHidden({ key })) {
|
|
61
|
+
throw walk.refusal ?? judge.refuse({ kind: piece.part, key, tableName });
|
|
62
|
+
}
|
|
63
|
+
walk.refusal ??= judge.queryRefusal?.({ ...piece, tableName });
|
|
64
|
+
}
|
|
65
|
+
static walkFilter(opts) {
|
|
66
|
+
const { filter, judge, tableName, walk } = opts;
|
|
67
|
+
if (typeof filter !== 'object' || filter === null) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
walk.refusal ??= judge.filterRefusal?.({ filter, tableName });
|
|
71
|
+
for (const part of judge.readOrder?.({ filter }) ?? DEFAULT_READ_ORDER) {
|
|
72
|
+
switch (part) {
|
|
73
|
+
case RequestFilterParts.WHERE: {
|
|
74
|
+
RequestFilterGuard.walkWhere({ where: filter.where, judge, tableName, walk });
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
case RequestFilterParts.ORDER: {
|
|
78
|
+
RequestFilterGuard.walkOrder({ order: filter.order, judge, tableName, walk });
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
case RequestFilterParts.INCLUDE: {
|
|
82
|
+
RequestFilterGuard.walkInclude({ include: filter.include, judge, tableName, walk });
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
default: {
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
static walkWhere(opts) {
|
|
92
|
+
const { where, judge, tableName, walk } = opts;
|
|
31
93
|
if (typeof where !== 'object' || where === null) {
|
|
32
94
|
return;
|
|
33
95
|
}
|
|
@@ -35,18 +97,21 @@ export class RequestFilterGuard {
|
|
|
35
97
|
const value = where[key];
|
|
36
98
|
if (QueryOperators.LOGICAL_GROUP_OPERATORS.has(key)) {
|
|
37
99
|
for (const branch of Array.isArray(value) ? value : [value]) {
|
|
38
|
-
RequestFilterGuard.
|
|
100
|
+
RequestFilterGuard.walkWhere({ where: branch, judge, tableName, walk });
|
|
39
101
|
}
|
|
40
102
|
continue;
|
|
41
103
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
104
|
+
RequestFilterGuard.visitKey({
|
|
105
|
+
walk,
|
|
106
|
+
judge,
|
|
107
|
+
tableName,
|
|
108
|
+
key,
|
|
109
|
+
piece: { part: RequestFilterParts.WHERE, value: { [key]: value } },
|
|
110
|
+
});
|
|
45
111
|
}
|
|
46
112
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const { order, judge, tableName } = opts;
|
|
113
|
+
static walkOrder(opts) {
|
|
114
|
+
const { order, judge, tableName, walk } = opts;
|
|
50
115
|
if (order === undefined || order === null) {
|
|
51
116
|
return;
|
|
52
117
|
}
|
|
@@ -54,27 +119,42 @@ export class RequestFilterGuard {
|
|
|
54
119
|
if (typeof entry !== 'string') {
|
|
55
120
|
continue;
|
|
56
121
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
122
|
+
let field;
|
|
123
|
+
// A judge with `queryRefusal` reads order entries with this parser, so an entry it cannot parse is the query's own
|
|
124
|
+
// refusal and later entries are still judged. Any other judge's query may read the entry differently: refuse it here.
|
|
125
|
+
try {
|
|
126
|
+
field = Sorts.parseEntry({ entry }).field;
|
|
60
127
|
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
if (!judge.queryRefusal) {
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
walk.refusal ??=
|
|
133
|
+
error instanceof Error
|
|
134
|
+
? error
|
|
135
|
+
: getError({ message: 'A value that is not an Error was thrown', cause: error });
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
RequestFilterGuard.visitKey({
|
|
139
|
+
walk,
|
|
140
|
+
judge,
|
|
141
|
+
tableName,
|
|
142
|
+
key: field,
|
|
143
|
+
piece: { part: RequestFilterParts.ORDER, value: entry },
|
|
144
|
+
});
|
|
61
145
|
}
|
|
62
146
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
*/
|
|
67
|
-
static assertFilter(opts) {
|
|
68
|
-
const { filter, judge, tableName } = opts;
|
|
69
|
-
if (typeof filter !== 'object' || filter === null) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
RequestFilterGuard.assertWhere({ where: filter.where, judge, tableName });
|
|
73
|
-
RequestFilterGuard.assertOrder({ order: filter.order, judge, tableName });
|
|
74
|
-
if (!Array.isArray(filter.include)) {
|
|
147
|
+
static walkInclude(opts) {
|
|
148
|
+
const { include, judge, tableName, walk } = opts;
|
|
149
|
+
if (!Array.isArray(include)) {
|
|
75
150
|
return;
|
|
76
151
|
}
|
|
77
|
-
for (const entry of
|
|
152
|
+
for (const entry of include) {
|
|
153
|
+
walk.refusal ??= judge.queryRefusal?.({
|
|
154
|
+
part: RequestFilterParts.INCLUDE,
|
|
155
|
+
value: entry,
|
|
156
|
+
tableName,
|
|
157
|
+
});
|
|
78
158
|
if (typeof entry !== 'object' || entry === null) {
|
|
79
159
|
continue;
|
|
80
160
|
}
|
|
@@ -87,7 +167,12 @@ export class RequestFilterGuard {
|
|
|
87
167
|
if (relationJudge === undefined) {
|
|
88
168
|
continue;
|
|
89
169
|
}
|
|
90
|
-
RequestFilterGuard.
|
|
170
|
+
RequestFilterGuard.walkFilter({
|
|
171
|
+
filter: scope,
|
|
172
|
+
judge: relationJudge,
|
|
173
|
+
tableName: relation,
|
|
174
|
+
walk,
|
|
175
|
+
});
|
|
91
176
|
}
|
|
92
177
|
}
|
|
93
178
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-filter-guard.js","sourceRoot":"","sources":["../../../../../src/base/repositories/common/request-filter-guard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,+BAA+B,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AA4B9D;;;;GAIG;AACH,MAAM,OAAO,kBAAkB;IAC7B,2KAA2K;IAC3K,MAAM,CAAC,cAAc,CAAC,IAAkD;QACtE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;QAE7B,KAAK,MAAM,IAAI,IAAI,MAAM,EAAE,CAAC;YAC1B,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,CAAC,UAAU,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,GAAG,GAAG,CAAC,EAAE,CAAC;gBAC7E,OAAO,IAAI,CAAC;YACd,CAAC;QACH,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAED,kGAAkG;IAClG,MAAM,CAAC,cAAc,CAAC,IAAoC;QACxD,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAC7C,8BAA8B,EAC9B,CAAC,SAAS,EAAE,EAAE,CAAC,MAAM,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAC7E,CAAC;QAEF,OAAO,QAAQ,CAAC;YACd,UAAU,EAAE,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU;YAC5C,OAAO,EAAE,IAAI,IAAI,CAAC,KAAK,6BAA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,CAAC;YAEzF,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACxD,SAAS;YACX,CAAC;YAED,MAAM,aAAa,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC;YAErD,qGAAqG;YACrG,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;gBAChC,SAAS;YACX,CAAC;YAED,kBAAkB,CAAC,YAAY,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC,CAAC;QAChG,CAAC;IACH,CAAC;CACF","sourcesContent":["import { HTTP } from '@venizia/ignis-helpers/common';\nimport { getError } from '@venizia/ignis-helpers/core';\nimport { QueryOperators, Sorts } from '@venizia/ignis-filter';\n\n/** What a request filter holds that names fields: `where`, `order` and the `include` scopes. */\nexport interface IRequestFilterShape {\n where?: unknown;\n order?: unknown;\n include?: unknown;\n}\n\n/** Which part of a filter a key came from: the refusal reads like the query's own refusal of an unknown column of that part. */\nexport type TRequestFilterKeyKind = 'where' | 'order';\n\n/**\n * How one model judges a key of a request filter. The repository of a model supplies it, so the judgment reads a key the way\n * that model's own query does (a relational JSON path names its first part, a search field is a dotted name), and a refusal\n * is the error that query gives for a column or field that does not exist.\n */\nexport interface IRequestFilterJudge {\n /** Whether the key reaches a field the model hides. */\n isHidden(opts: { key: string }): boolean;\n\n /** The error to throw for a key that is hidden: the one an unknown column or field gets, so a caller cannot tell them apart. */\n refuse(opts: { kind: TRequestFilterKeyKind; key: string; tableName: string }): Error;\n\n /** The judge of the model a relation of this model points to; `undefined` when the relation is unknown (the query refuses it). A nested include is judged by the relation judge's own `relation`. */\n relation?(opts: { relation: string }): IRequestFilterJudge | undefined;\n}\n\n/**\n * A filter that comes from a request is not allowed to name a field the model hides. Hidden fields are never returned, but a\n * `where` or an `order` on one reveals its value one question at a time. The check runs where a request enters\n * (the generated controllers), never in the repository's own verbs, so server code can still filter and sort on a hidden field.\n */\nexport class RequestFilterGuard {\n /** The names of `hidden` a dotted key reaches: the key is one, a dotted prefix of one, or has one as its dotted prefix (`meta` and `meta.pin.x` both reach `meta.pin`). */\n static isHiddenByName(opts: { key: string; hidden: ReadonlySet<string> }): boolean {\n const { key, hidden } = opts;\n\n for (const name of hidden) {\n if (key === name || key.startsWith(`${name}.`) || name.startsWith(`${key}.`)) {\n return true;\n }\n }\n\n return false;\n }\n\n /** The refusal when a model has no query error of its own to borrow: a 400 `Column NOT FOUND`. */\n static columnNotFound(opts: { scope: string; key: string }): Error {\n const quoted = JSON.stringify(opts.key).replace(\n /[\\u007f-\\u009f\\u2028\\u2029]/g,\n (character) => `\\\\u${character.charCodeAt(0).toString(16).padStart(4, '0')}`,\n );\n\n return getError({\n statusCode: HTTP.ResultCodes.RS_4.BadRequest,\n message: `[${opts.scope}] Column NOT FOUND | key: ${quoted}`,\n });\n }\n\n /** Refuses a `where` that names a hidden field, at any depth of `and` and `or`. */\n static assertWhere(opts: {\n where?: unknown;\n judge: IRequestFilterJudge;\n tableName: string;\n }): void {\n const { where, judge, tableName } = opts;\n\n if (typeof where !== 'object' || where === null) {\n return;\n }\n\n for (const key of Object.keys(where)) {\n const value = (where as Record<string, unknown>)[key];\n\n if (QueryOperators.LOGICAL_GROUP_OPERATORS.has(key)) {\n for (const branch of Array.isArray(value) ? value : [value]) {\n RequestFilterGuard.assertWhere({ where: branch, judge, tableName });\n }\n\n continue;\n }\n\n if (judge.isHidden({ key })) {\n throw judge.refuse({ kind: 'where', key, tableName });\n }\n }\n }\n\n /** Refuses an `order` that names a hidden field (a string or a list of `'field DIRECTION'` entries), reading each entry as the query does. */\n static assertOrder(opts: {\n order?: unknown;\n judge: IRequestFilterJudge;\n tableName: string;\n }): void {\n const { order, judge, tableName } = opts;\n\n if (order === undefined || order === null) {\n return;\n }\n\n for (const entry of Array.isArray(order) ? order : [order]) {\n if (typeof entry !== 'string') {\n continue;\n }\n\n const { field } = Sorts.parseEntry({ entry });\n\n if (judge.isHidden({ key: field })) {\n throw judge.refuse({ kind: 'order', key: field, tableName });\n }\n }\n }\n\n /**\n * Refuses a filter that names a hidden field in its `where`, its `order`, or the `scope` of any `include`, each level\n * judged by the model it reads (`judge.relation` answers it).\n */\n static assertFilter(opts: {\n filter?: IRequestFilterShape;\n judge: IRequestFilterJudge;\n tableName: string;\n }): void {\n const { filter, judge, tableName } = opts;\n\n if (typeof filter !== 'object' || filter === null) {\n return;\n }\n\n RequestFilterGuard.assertWhere({ where: filter.where, judge, tableName });\n RequestFilterGuard.assertOrder({ order: filter.order, judge, tableName });\n\n if (!Array.isArray(filter.include)) {\n return;\n }\n\n for (const entry of filter.include) {\n if (typeof entry !== 'object' || entry === null) {\n continue;\n }\n\n const { relation, scope } = entry as { relation?: unknown; scope?: IRequestFilterShape };\n\n if (typeof relation !== 'string' || scope === undefined) {\n continue;\n }\n\n const relationJudge = judge.relation?.({ relation });\n\n // An unknown relation is refused by the query itself; here only a known one has a model to judge by.\n if (relationJudge === undefined) {\n continue;\n }\n\n RequestFilterGuard.assertFilter({ filter: scope, judge: relationJudge, tableName: relation });\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"request-filter-guard.js","sourceRoot":"","sources":["../../../../../src/base/repositories/common/request-filter-guard.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,+BAA+B,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAa9D,iGAAiG;AACjG,MAAM,OAAO,kBAAkB;aACb,UAAK,GAAG,OAAO,CAAC;aAChB,UAAK,GAAG,OAAO,CAAC;aAChB,YAAO,GAAG,SAAS,CAAC;;AAiDtC,MAAM,kBAAkB,GAAsC;IAC5D,kBAAkB,CAAC,KAAK;IACxB,kBAAkB,CAAC,KAAK;IACxB,kBAAkB,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CAAC;CACF","sourcesContent":["import type { TConstValue } from '@venizia/ignis-helpers/common';\nimport { HTTP } from '@venizia/ignis-helpers/common';\nimport { getError } from '@venizia/ignis-helpers/core';\nimport { QueryOperators, Sorts } from '@venizia/ignis-filter';\n\n/** What a request filter holds that a query reads: `where`, `order`, the `include` scopes, and the `fields` that can decide the order it reads them in. */\nexport interface IRequestFilterShape {\n where?: unknown;\n order?: unknown;\n include?: unknown;\n fields?: unknown;\n}\n\n/** Which part of a filter a key came from: the refusal reads like the query's own refusal of an unknown column of that part. */\nexport type TRequestFilterKeyKind = 'where' | 'order';\n\n/** The parts of a request filter a query reads, one at a time, in an order the query decides. */\nexport class RequestFilterParts {\n static readonly WHERE = 'where';\n static readonly ORDER = 'order';\n static readonly INCLUDE = 'include';\n}\n\n/** A part of a request filter: `where`, `order` or `include`. */\nexport type TRequestFilterPart = TConstValue<typeof RequestFilterParts>;\n\n/** One piece of a request filter, as the query reads it on its own: a `where` of one key, one `order` entry, or one `include` entry. */\nexport type TRequestFilterPiece =\n | { part: typeof RequestFilterParts.WHERE; value: Record<string, unknown> }\n | { part: typeof RequestFilterParts.ORDER; value: string }\n | { part: typeof RequestFilterParts.INCLUDE; value: unknown };\n\n/**\n * How one model judges a key of a request filter. The repository of a model supplies it, so the judgment reads a key the way\n * that model's own query does (a relational JSON path names its first part, a search field is a dotted name), and a refusal\n * is the error that query gives for a column or field that does not exist.\n *\n * A filter that names a hidden field is refused with the first refusal the query itself would meet if the hidden name were\n * missing: `readOrder` gives the order the query reads the parts in, `filterRefusal` and `queryRefusal` the errors it throws\n * on its own before that point. Without them the parts are read `where`, `order`, `include`, and nothing else is refused.\n */\nexport interface IRequestFilterJudge {\n /** Whether the key reaches a field the model hides. */\n isHidden(opts: { key: string }): boolean;\n\n /** The error to throw for a key that is hidden: the one an unknown column or field gets, so a caller cannot tell them apart. */\n refuse(opts: { kind: TRequestFilterKeyKind; key: string; tableName: string }): Error;\n\n /** The judge of the model a relation of this model points to; `undefined` when the relation is unknown (the query refuses it). A nested include is judged by the relation judge's own `relation`. */\n relation?(opts: { relation: string }): IRequestFilterJudge | undefined;\n\n /** The parts of `filter` in the order this model's query reads them. */\n readOrder?(opts: { filter: IRequestFilterShape }): ReadonlyArray<TRequestFilterPart>;\n\n /** The error the query throws for `filter` as a whole before it reads any key of it, or `undefined`. */\n filterRefusal?(opts: { filter: IRequestFilterShape; tableName: string }): Error | undefined;\n\n /**\n * The error the query throws for one piece of a filter on its own, or `undefined` when it reads the piece. A piece is a\n * `where` of one key (`{ [key]: value }`), one `order` entry, or one `include` entry.\n */\n queryRefusal?(opts: TRequestFilterPiece & { tableName: string }): Error | undefined;\n}\n\n/** The first refusal a walk of a filter met, before any hidden key. */\ninterface IRequestFilterWalk {\n refusal?: Error;\n}\n\nconst DEFAULT_READ_ORDER: ReadonlyArray<TRequestFilterPart> = [\n RequestFilterParts.WHERE,\n RequestFilterParts.ORDER,\n RequestFilterParts.INCLUDE,\n];\n\n/**\n * A filter that comes from a request is not allowed to name a field the model hides. Hidden fields are never returned, but a\n * `where` or an `order` on one reveals its value one question at a time. The check runs where a request enters\n * (the generated controllers), never in the repository's own verbs, so server code can still filter and sort on a hidden field.\n */\nexport class RequestFilterGuard {\n /** The names of `hidden` a dotted key reaches: the key is one, a dotted prefix of one, or has one as its dotted prefix (`meta` and `meta.pin.x` both reach `meta.pin`). */\n static isHiddenByName(opts: { key: string; hidden: ReadonlySet<string> }): boolean {\n const { key, hidden } = opts;\n\n for (const name of hidden) {\n if (key === name || key.startsWith(`${name}.`) || name.startsWith(`${key}.`)) {\n return true;\n }\n }\n\n return false;\n }\n\n /** The refusal when a model has no query error of its own to borrow: a 400 `Column NOT FOUND`. */\n static columnNotFound(opts: { scope: string; key: string }): Error {\n const quoted = JSON.stringify(opts.key).replace(\n /[\\u007f-\\u009f\\u2028\\u2029]/g,\n (character) => `\\\\u${character.charCodeAt(0).toString(16).padStart(4, '0')}`,\n );\n\n return getError({\n statusCode: HTTP.ResultCodes.RS_4.BadRequest,\n message: `[${opts.scope}] Column NOT FOUND | key: ${quoted}`,\n });\n }\n\n /** Refuses a `where` that names a hidden field, at any depth of `and` and `or`. */\n static assertWhere(opts: {\n where?: unknown;\n judge: IRequestFilterJudge;\n tableName: string;\n }): void {\n RequestFilterGuard.walkWhere({ ...opts, walk: {} });\n }\n\n /** Refuses an `order` that names a hidden field (a string or a list of `'field DIRECTION'` entries), reading each entry as the query does. */\n static assertOrder(opts: {\n order?: unknown;\n judge: IRequestFilterJudge;\n tableName: string;\n }): void {\n RequestFilterGuard.walkOrder({ ...opts, walk: {} });\n }\n\n /**\n * Refuses a filter that names a hidden field in its `where`, its `order`, or the `scope` of any `include`, each level\n * judged by the model it reads (`judge.relation` answers it). The parts are read in the order the query reads them, and a\n * filter that the query refuses for another reason before it meets the hidden field gets that refusal instead, so the answer\n * is the one the same filter gets with a missing name in place of the hidden one. A filter that names no hidden field is\n * left to the query.\n */\n static assertFilter(opts: {\n filter?: IRequestFilterShape;\n judge: IRequestFilterJudge;\n tableName: string;\n }): void {\n RequestFilterGuard.walkFilter({ ...opts, walk: {} });\n }\n\n /** Throws for a hidden key: the refusal met before it, or the hidden key's own. Otherwise keeps the query's refusal of the piece when it is the first. */\n private static visitKey(opts: {\n walk: IRequestFilterWalk;\n judge: IRequestFilterJudge;\n tableName: string;\n key: string;\n piece: Exclude<TRequestFilterPiece, { part: typeof RequestFilterParts.INCLUDE }>;\n }): void {\n const { walk, judge, tableName, key, piece } = opts;\n\n if (judge.isHidden({ key })) {\n throw walk.refusal ?? judge.refuse({ kind: piece.part, key, tableName });\n }\n\n walk.refusal ??= judge.queryRefusal?.({ ...piece, tableName });\n }\n\n private static walkFilter(opts: {\n filter?: IRequestFilterShape;\n judge: IRequestFilterJudge;\n tableName: string;\n walk: IRequestFilterWalk;\n }): void {\n const { filter, judge, tableName, walk } = opts;\n\n if (typeof filter !== 'object' || filter === null) {\n return;\n }\n\n walk.refusal ??= judge.filterRefusal?.({ filter, tableName });\n\n for (const part of judge.readOrder?.({ filter }) ?? DEFAULT_READ_ORDER) {\n switch (part) {\n case RequestFilterParts.WHERE: {\n RequestFilterGuard.walkWhere({ where: filter.where, judge, tableName, walk });\n break;\n }\n case RequestFilterParts.ORDER: {\n RequestFilterGuard.walkOrder({ order: filter.order, judge, tableName, walk });\n break;\n }\n case RequestFilterParts.INCLUDE: {\n RequestFilterGuard.walkInclude({ include: filter.include, judge, tableName, walk });\n break;\n }\n default: {\n break;\n }\n }\n }\n }\n\n private static walkWhere(opts: {\n where?: unknown;\n judge: IRequestFilterJudge;\n tableName: string;\n walk: IRequestFilterWalk;\n }): void {\n const { where, judge, tableName, walk } = opts;\n\n if (typeof where !== 'object' || where === null) {\n return;\n }\n\n for (const key of Object.keys(where)) {\n const value = (where as Record<string, unknown>)[key];\n\n if (QueryOperators.LOGICAL_GROUP_OPERATORS.has(key)) {\n for (const branch of Array.isArray(value) ? value : [value]) {\n RequestFilterGuard.walkWhere({ where: branch, judge, tableName, walk });\n }\n\n continue;\n }\n\n RequestFilterGuard.visitKey({\n walk,\n judge,\n tableName,\n key,\n piece: { part: RequestFilterParts.WHERE, value: { [key]: value } },\n });\n }\n }\n\n private static walkOrder(opts: {\n order?: unknown;\n judge: IRequestFilterJudge;\n tableName: string;\n walk: IRequestFilterWalk;\n }): void {\n const { order, judge, tableName, walk } = opts;\n\n if (order === undefined || order === null) {\n return;\n }\n\n for (const entry of Array.isArray(order) ? order : [order]) {\n if (typeof entry !== 'string') {\n continue;\n }\n\n let field: string;\n\n // A judge with `queryRefusal` reads order entries with this parser, so an entry it cannot parse is the query's own\n // refusal and later entries are still judged. Any other judge's query may read the entry differently: refuse it here.\n try {\n field = Sorts.parseEntry({ entry }).field;\n } catch (error) {\n if (!judge.queryRefusal) {\n throw error;\n }\n\n walk.refusal ??=\n error instanceof Error\n ? error\n : getError({ message: 'A value that is not an Error was thrown', cause: error });\n continue;\n }\n\n RequestFilterGuard.visitKey({\n walk,\n judge,\n tableName,\n key: field,\n piece: { part: RequestFilterParts.ORDER, value: entry },\n });\n }\n }\n\n private static walkInclude(opts: {\n include?: unknown;\n judge: IRequestFilterJudge;\n tableName: string;\n walk: IRequestFilterWalk;\n }): void {\n const { include, judge, tableName, walk } = opts;\n\n if (!Array.isArray(include)) {\n return;\n }\n\n for (const entry of include) {\n walk.refusal ??= judge.queryRefusal?.({\n part: RequestFilterParts.INCLUDE,\n value: entry,\n tableName,\n });\n\n if (typeof entry !== 'object' || entry === null) {\n continue;\n }\n\n const { relation, scope } = entry as { relation?: unknown; scope?: IRequestFilterShape };\n\n if (typeof relation !== 'string' || scope === undefined) {\n continue;\n }\n\n const relationJudge = judge.relation?.({ relation });\n\n // An unknown relation is refused by the query itself; here only a known one has a model to judge by.\n if (relationJudge === undefined) {\n continue;\n }\n\n RequestFilterGuard.walkFilter({\n filter: scope,\n judge: relationJudge,\n tableName: relation,\n walk,\n });\n }\n }\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { IDataSource, TDataSourceDriverClass } from '../../../../base/datasources/index.js';
|
|
1
|
+
import type { IDataSource, IDataSourceOpenOptions, TDataSourceDriverClass } from '../../../../base/datasources/index.js';
|
|
2
2
|
import type { AbstractEntity } from '../../../../base/models/index.js';
|
|
3
3
|
import type { IRepository, RepositoryTypes, TRepositoryOperationScope, TRepositoryType } from '../../../../base/repositories/index.js';
|
|
4
4
|
import type { TClass, TValueOrResolver } from '@venizia/ignis-helpers/common';
|
|
@@ -6,6 +6,8 @@ import type { IArtifactRegistrationOptions, TDecoratorTarget } from './artifact.
|
|
|
6
6
|
/** Metadata accepted by `@datasource`. */
|
|
7
7
|
export interface IDataSourceMetadata extends IArtifactRegistrationOptions {
|
|
8
8
|
driver?: TDataSourceDriverClass;
|
|
9
|
+
/** When the datasource opens. `{ on: 'first-use' }` leaves it closed at start and opens it at the first call that needs it. */
|
|
10
|
+
open?: IDataSourceOpenOptions;
|
|
9
11
|
/**
|
|
10
12
|
* `false` stops model schema discovery for the datasource.
|
|
11
13
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"persistence.js","sourceRoot":"","sources":["../../../../../../src/helpers/inversion/common/types/persistence.ts"],"names":[],"mappings":"","sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"persistence.js","sourceRoot":"","sources":["../../../../../../src/helpers/inversion/common/types/persistence.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n IDataSource,\n IDataSourceOpenOptions,\n TDataSourceDriverClass,\n} from '@/base/datasources';\nimport type { AbstractEntity } from '@/base/models';\nimport type {\n IRepository,\n RepositoryTypes,\n TRepositoryOperationScope,\n TRepositoryType,\n} from '@/base/repositories';\nimport type { TClass, TValueOrResolver } from '@venizia/ignis-helpers/common';\nimport type { IArtifactRegistrationOptions, TDecoratorTarget } from './artifact';\n\n/** Metadata accepted by `@datasource`. */\nexport interface IDataSourceMetadata extends IArtifactRegistrationOptions {\n driver?: TDataSourceDriverClass;\n\n /** When the datasource opens. `{ on: 'first-use' }` leaves it closed at start and opens it at the first call that needs it. */\n open?: IDataSourceOpenOptions;\n\n /**\n * `false` stops model schema discovery for the datasource.\n *\n * @default true\n */\n autoDiscovery?: boolean;\n}\n\n/** Members every repository type shares. */\nexport interface IRepositoryMetadataBase<\n DataSource extends IDataSource = IDataSource,\n> extends IArtifactRegistrationOptions {\n dataSource: string | TValueOrResolver<TClass<DataSource>>;\n operationScope?: TRepositoryOperationScope;\n}\n\n/** `RepositoryTypes.MODEL` - a model this application declares. */\nexport interface IRepositoryMetadata<\n Model extends AbstractEntity = AbstractEntity,\n DataSource extends IDataSource = IDataSource,\n> extends IRepositoryMetadataBase<DataSource> {\n /** @default RepositoryTypes.MODEL */\n type?: typeof RepositoryTypes.MODEL;\n model: TValueOrResolver<TClass<Model>>;\n}\n\n/** `RepositoryTypes.REMOTE` - data behind another service's API: a datasource and no model. */\nexport interface IRemoteRepositoryMetadata<\n DataSource extends IDataSource = IDataSource,\n> extends IRepositoryMetadataBase<DataSource> {\n type: typeof RepositoryTypes.REMOTE;\n model?: never;\n}\n\n/** What `@repository` accepts, discriminated on `type`. */\nexport type TRepositoryMetadata<\n Model extends AbstractEntity = AbstractEntity,\n DataSource extends IDataSource = IDataSource,\n> = IRepositoryMetadata<Model, DataSource> | IRemoteRepositoryMetadata<DataSource>;\n\n/** Resolved repository metadata after lazy evaluation. */\nexport interface IResolvedRepositoryMetadata<\n Model extends AbstractEntity = AbstractEntity,\n DataSource extends IDataSource = IDataSource,\n> {\n type?: TRepositoryType;\n model?: TClass<Model>;\n dataSource?: string | TClass<DataSource>;\n operationScope?: TRepositoryOperationScope;\n}\n\n/** Links a model to the repository and datasource that serve it, each given as a value or a resolver. */\nexport interface IRepositoryBinding<\n Model extends AbstractEntity = AbstractEntity,\n DataSource extends IDataSource = IDataSource,\n> {\n model: TValueOrResolver<TClass<Model>>;\n repository: TValueOrResolver<TDecoratorTarget<IRepository>>;\n dataSource: TValueOrResolver<string | TDecoratorTarget<DataSource>>;\n}\n"]}
|
package/dist/esm/repository.d.ts
CHANGED
|
@@ -29,4 +29,6 @@ export { AbstractEntity } from './base/models/base.js';
|
|
|
29
29
|
export { SchemaTypes } from './base/models/common/constants.js';
|
|
30
30
|
export type { TIdSchemaType, TSchemaType } from './base/models/common/index.js';
|
|
31
31
|
export type { TByIdFilter, TFields, TFilter, TInclusion, TRow, TSelectedRow, TWhere, } from '@venizia/ignis-filter';
|
|
32
|
+
export { RequestFilterParts } from './base/repositories/common/request-filter-guard.js';
|
|
33
|
+
export type { IRequestFilterJudge, IRequestFilterShape, TRequestFilterPart, TRequestFilterPiece, } from './base/repositories/common/request-filter-guard.js';
|
|
32
34
|
//# sourceMappingURL=repository.d.ts.map
|
package/dist/esm/repository.js
CHANGED
|
@@ -28,4 +28,6 @@ export { NotSupportedUtility } from './utilities/not-supported.utility.js';
|
|
|
28
28
|
// Engine-neutral like the datasource: a name, `getSchema({ type })`, and an id type.
|
|
29
29
|
export { AbstractEntity } from './base/models/base.js';
|
|
30
30
|
export { SchemaTypes } from './base/models/common/constants.js';
|
|
31
|
+
// What a repository on any transport needs to refuse a request filter that names a hidden property.
|
|
32
|
+
export { RequestFilterParts } from './base/repositories/common/request-filter-guard.js';
|
|
31
33
|
//# sourceMappingURL=repository.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"repository.js","sourceRoot":"","sources":["../../src/repository.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AACvE,OAAO,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAC;AAYjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAE5E,gGAAgG;AAChG,yCAAyC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,4CAA4C,CAAC;AAChF,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAG5E,mGAAmG;AACnG,mEAAmE;AACnE,OAAO,EACL,aAAa,EACb,iBAAiB,EACjB,yBAAyB,EACzB,eAAe,GAChB,MAAM,sCAAsC,CAAC;AAE9C,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxE,OAAO,EAAE,mBAAmB,EAAE,MAAM,mCAAmC,CAAC;AAKxE,mGAAmG;AACnG,qFAAqF;AACrF,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAC","sourcesContent":["/**\n * The persistence contract, and nothing about how a row is served or validated.\n *\n * A repository talks to a datasource. `AbstractDataSource` is engine-neutral - its only required\n * member is `configure()` - so \"an HTTP request to another server\" is a datasource in the same sense\n * Drizzle-over-Postgres is one: same role, different transport. This entry exists so a consumer can\n * implement that without taking the OpenAPI layer, which `base/repositories` reaches through\n * `query-schemas` and `result-schemas`.\n *\n * Both halves already speak `@venizia/ignis-filter`, so a consumer on another transport shares the\n * query vocabulary rather than translating into one.\n *\n * Weight is a contract, checked by `src/__tests__/bundle/browser-weight.test.ts`.\n */\n\nexport { AbstractRepository } from '@/base/repositories/core/abstract';\nexport { AbstractDataSource } from '@/base/datasources/abstract';\n\nexport type {\n ICreatableRepository,\n ICrudRepository,\n IDeletableRepository,\n IPersistableRepository,\n IReadableRepository,\n IRepository,\n IUpdatableRepository,\n} from '@/base/repositories/common/types/contracts';\n\nexport { DataRangeUtility } from '@/base/repositories/common/types/results';\n\n// How a client reads what an IGNIS server answers - shared by the http connector and any client\n// that reads the same server without it.\nexport { HttpResponseReader } from '@/base/repositories/common/response-reader';\nexport { HttpExtraRequest } from '@/base/repositories/common/extra-request';\nexport type { TExtraRequest, TExtraResult } from '@/base/repositories/common/extra-request';\n\n// What a repository on any transport raises and bounds itself by - the constants, the error codes,\n// and the helper an engine that lacks an operation throws through.\nexport {\n DEFAULT_LIMIT,\n DEFAULT_MAX_LIMIT,\n RepositoryOperationScopes,\n RepositoryTypes,\n} from '@/base/repositories/common/constants';\nexport type { TRepositoryType } from '@/base/repositories/common/constants';\nexport { CoreErrorCodes, SearchErrorCodes } from '@/common/error-codes';\nexport { NotSupportedUtility } from '@/utilities/not-supported.utility';\nexport type { TCount, TDataRange, TDataWithRange } from '@/base/repositories/common/types/results';\n\nexport type { IDataSource } from '@/base/datasources/common/types';\n\n// `IRepository` names an entity, so a consumer implementing the contract needs the base to extend.\n// Engine-neutral like the datasource: a name, `getSchema({ type })`, and an id type.\nexport { AbstractEntity } from '@/base/models/base';\nexport { SchemaTypes } from '@/base/models/common/constants';\nexport type { TIdSchemaType, TSchemaType } from '@/base/models/common';\n\n// The query vocabulary both transports share.\nexport type {\n TByIdFilter,\n TFields,\n TFilter,\n TInclusion,\n TRow,\n TSelectedRow,\n TWhere,\n} from '@venizia/ignis-filter';\n"]}
|
|
1
|
+
{"version":3,"file":"repository.js","sourceRoot":"","sources":["../../src/repository.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AACvE,OAAO,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAC;AAYjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAE5E,gGAAgG;AAChG,yCAAyC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,4CAA4C,CAAC;AAChF,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAG5E,mGAAmG;AACnG,mEAAmE;AACnE,OAAO,EACL,aAAa,EACb,iBAAiB,EACjB,yBAAyB,EACzB,eAAe,GAChB,MAAM,sCAAsC,CAAC;AAE9C,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxE,OAAO,EAAE,mBAAmB,EAAE,MAAM,mCAAmC,CAAC;AAKxE,mGAAmG;AACnG,qFAAqF;AACrF,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAC;AAc7D,oGAAoG;AACpG,OAAO,EAAE,kBAAkB,EAAE,MAAM,iDAAiD,CAAC","sourcesContent":["/**\n * The persistence contract, and nothing about how a row is served or validated.\n *\n * A repository talks to a datasource. `AbstractDataSource` is engine-neutral - its only required\n * member is `configure()` - so \"an HTTP request to another server\" is a datasource in the same sense\n * Drizzle-over-Postgres is one: same role, different transport. This entry exists so a consumer can\n * implement that without taking the OpenAPI layer, which `base/repositories` reaches through\n * `query-schemas` and `result-schemas`.\n *\n * Both halves already speak `@venizia/ignis-filter`, so a consumer on another transport shares the\n * query vocabulary rather than translating into one.\n *\n * Weight is a contract, checked by `src/__tests__/bundle/browser-weight.test.ts`.\n */\n\nexport { AbstractRepository } from '@/base/repositories/core/abstract';\nexport { AbstractDataSource } from '@/base/datasources/abstract';\n\nexport type {\n ICreatableRepository,\n ICrudRepository,\n IDeletableRepository,\n IPersistableRepository,\n IReadableRepository,\n IRepository,\n IUpdatableRepository,\n} from '@/base/repositories/common/types/contracts';\n\nexport { DataRangeUtility } from '@/base/repositories/common/types/results';\n\n// How a client reads what an IGNIS server answers - shared by the http connector and any client\n// that reads the same server without it.\nexport { HttpResponseReader } from '@/base/repositories/common/response-reader';\nexport { HttpExtraRequest } from '@/base/repositories/common/extra-request';\nexport type { TExtraRequest, TExtraResult } from '@/base/repositories/common/extra-request';\n\n// What a repository on any transport raises and bounds itself by - the constants, the error codes,\n// and the helper an engine that lacks an operation throws through.\nexport {\n DEFAULT_LIMIT,\n DEFAULT_MAX_LIMIT,\n RepositoryOperationScopes,\n RepositoryTypes,\n} from '@/base/repositories/common/constants';\nexport type { TRepositoryType } from '@/base/repositories/common/constants';\nexport { CoreErrorCodes, SearchErrorCodes } from '@/common/error-codes';\nexport { NotSupportedUtility } from '@/utilities/not-supported.utility';\nexport type { TCount, TDataRange, TDataWithRange } from '@/base/repositories/common/types/results';\n\nexport type { IDataSource } from '@/base/datasources/common/types';\n\n// `IRepository` names an entity, so a consumer implementing the contract needs the base to extend.\n// Engine-neutral like the datasource: a name, `getSchema({ type })`, and an id type.\nexport { AbstractEntity } from '@/base/models/base';\nexport { SchemaTypes } from '@/base/models/common/constants';\nexport type { TIdSchemaType, TSchemaType } from '@/base/models/common';\n\n// The query vocabulary both transports share.\nexport type {\n TByIdFilter,\n TFields,\n TFilter,\n TInclusion,\n TRow,\n TSelectedRow,\n TWhere,\n} from '@venizia/ignis-filter';\n\n// What a repository on any transport needs to refuse a request filter that names a hidden property.\nexport { RequestFilterParts } from '@/base/repositories/common/request-filter-guard';\nexport type {\n IRequestFilterJudge,\n IRequestFilterShape,\n TRequestFilterPart,\n TRequestFilterPiece,\n} from '@/base/repositories/common/request-filter-guard';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@venizia/ignis-kernel",
|
|
3
|
-
"version": "0.4.8-
|
|
3
|
+
"version": "0.4.8-6",
|
|
4
4
|
"description": "Browser-pure kernel of the IGNIS framework: dependency injection, lifecycle, REST controllers, repository and datasource abstractions, and the authentication and authorization seams. No node builtin and no server-only peer, so the same kernel serves a Bun server and a browser Worker.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"authentication",
|
|
@@ -69,14 +69,14 @@
|
|
|
69
69
|
"force-update": "sh ../../scripts/build/force-update.sh"
|
|
70
70
|
},
|
|
71
71
|
"dependencies": {
|
|
72
|
-
"@venizia/ignis-filter": "0.4.7-
|
|
73
|
-
"@venizia/ignis-helpers": "0.4.8-
|
|
74
|
-
"@venizia/ignis-inversion": "0.4.7-
|
|
72
|
+
"@venizia/ignis-filter": "0.4.7-4",
|
|
73
|
+
"@venizia/ignis-helpers": "0.4.8-6",
|
|
74
|
+
"@venizia/ignis-inversion": "0.4.7-3",
|
|
75
75
|
"reflect-metadata": "^0.2.2"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
78
|
"@types/bun": "^1.4.3",
|
|
79
|
-
"@venizia/dev-configs": "0.4.8-
|
|
79
|
+
"@venizia/dev-configs": "0.4.8-3",
|
|
80
80
|
"casbin": "^5.51.1",
|
|
81
81
|
"eslint": "^10.13.0",
|
|
82
82
|
"oxlint": "1.89.0",
|