@venizia/ignis-kernel 0.4.8-4 → 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/body-limit.d.ts +29 -0
- package/dist/esm/base/applications/body-limit.js +49 -0
- package/dist/esm/base/applications/body-limit.js.map +1 -0
- 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/common/types/middlewares.d.ts +7 -1
- package/dist/esm/base/applications/common/types/middlewares.js.map +1 -1
- package/dist/esm/base/applications/index.d.ts +1 -0
- package/dist/esm/base/applications/index.js +1 -0
- package/dist/esm/base/applications/index.js.map +1 -1
- package/dist/esm/base/applications/rest.d.ts +15 -7
- package/dist/esm/base/applications/rest.js +67 -21
- 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 +59 -24
- package/dist/esm/base/auth/authenticate/common/types/jwt.js.map +1 -1
- package/dist/esm/base/auth/authenticate/common/types/service.d.ts +3 -0
- package/dist/esm/base/auth/authenticate/common/types/service.js.map +1 -1
- package/dist/esm/base/auth/authenticate/providers/authentication.provider.js +5 -0
- package/dist/esm/base/auth/authenticate/providers/authentication.provider.js.map +1 -1
- package/dist/esm/base/controllers/factory/controller.d.ts +7 -0
- package/dist/esm/base/controllers/factory/crud/abstract.d.ts +11 -0
- package/dist/esm/base/controllers/factory/crud/abstract.js +7 -0
- package/dist/esm/base/controllers/factory/crud/abstract.js.map +1 -1
- package/dist/esm/base/controllers/factory/crud/persistable.js +2 -0
- package/dist/esm/base/controllers/factory/crud/persistable.js.map +1 -1
- package/dist/esm/base/controllers/factory/crud/readable.js +6 -0
- package/dist/esm/base/controllers/factory/crud/readable.js.map +1 -1
- package/dist/esm/base/controllers/factory/definition.d.ts +16 -16
- 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.d.ts +14 -0
- package/dist/esm/base/middlewares/app-error/app-error.middleware.js +80 -3
- 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/middlewares/app-error/zod.handler.js +4 -1
- package/dist/esm/base/middlewares/app-error/zod.handler.js.map +1 -1
- package/dist/esm/base/repositories/common/index.d.ts +1 -0
- package/dist/esm/base/repositories/common/index.js +1 -0
- package/dist/esm/base/repositories/common/index.js.map +1 -1
- package/dist/esm/base/repositories/common/request-filter-guard.d.ts +118 -0
- package/dist/esm/base/repositories/common/request-filter-guard.js +179 -0
- package/dist/esm/base/repositories/common/request-filter-guard.js.map +1 -0
- package/dist/esm/base/repositories/core/abstract.d.ts +16 -0
- package/dist/esm/base/repositories/core/abstract.js +26 -0
- package/dist/esm/base/repositories/core/abstract.js.map +1 -1
- package/dist/esm/base/repositories/query-schemas/index.d.ts +6 -6
- 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
|
@@ -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;YAC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,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC;YAClE,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;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"]}
|
|
@@ -53,7 +53,10 @@ export class ZodErrorHandler {
|
|
|
53
53
|
expected: el.expected,
|
|
54
54
|
received: el.received,
|
|
55
55
|
}))
|
|
56
|
-
:
|
|
56
|
+
: // A ZodError whose message is not an issue list: the raw error is for a developer, never for a production client.
|
|
57
|
+
isProduction
|
|
58
|
+
? undefined
|
|
59
|
+
: validationErrors,
|
|
57
60
|
},
|
|
58
61
|
},
|
|
59
62
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.handler.js","sourceRoot":"","sources":["../../../../../src/base/middlewares/app-error/zod.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,IAAI,EAAE,MAAM,+BAA+B,CAAC;AAIrD,MAAM,OAAO,eAAe;aACF,+BAA0B,GAAG,iBAAiB,CAAC;IAEvE,iGAAiG;IACzF,MAAM,CAAC,gBAAgB,CAAC,IAA8B;QAC5D,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC;QAE5C,IAAI,OAAO,UAAU,KAAK,QAAQ,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC5D,OAAO,UAAU,CAAC;QACpB,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,2DAA2D;IAC3D,MAAM,CAAC,cAAc,CAAC,IAMrB;QACC,MAAM,EAAE,YAAY,EAAE,SAAS,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC;QAC3D,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,mBAAmB,CAAC;QAE7D,IAAI,gBAAyB,CAAC;QAE9B,IAAI,CAAC;YACH,gBAAgB,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC/C,CAAC;QAAC,MAAM,CAAC;YACP,gBAAgB,GAAG,KAAK,CAAC;QAC3B,CAAC;QAED,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAE,gBAAoC,CAAC,CAAC,CAAC,IAAI,CAAC;QAE9F,6EAA6E;QAC7E,IAAI,WAA+B,CAAC;QACpC,IAA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AAgB;iBACvB;aACF;SACF,CAAC;IACJ,CAAC","sourcesContent":["import { MessageCode } from '@venizia/ignis-helpers/core';\nimport { HTTP } from '@venizia/ignis-helpers/common';\nimport type { HTTPResponseError } from 'hono/types';\nimport type { IZodIssueLike } from './common';\n\nexport class ZodErrorHandler {\n private static readonly DEFAULT_VALIDATION_MESSAGE = 'ValidationError';\n\n /** Returns a schema-author-supplied code (`issue.params.code`) when it is a non-empty string. */\n private static extractIssueCode(opts: { issue: IZodIssueLike }) {\n const paramsCode = opts.issue?.params?.code;\n\n if (typeof paramsCode === 'string' && paramsCode.length > 0) {\n return paramsCode;\n }\n\n return undefined;\n }\n\n /** Formats a ZodError into the 422 validation response. */\n static formatZodError(opts: {\n isProduction: boolean;\n requestId: string;\n url: string;\n path: string;\n error: Error | HTTPResponseError;\n }) {\n const { isProduction, requestId, url, path, error } = opts;\n const statusCode = HTTP.ResultCodes.RS_4.UnprocessableEntity;\n\n let validationErrors: unknown;\n\n try {\n validationErrors = JSON.parse(error.message);\n } catch {\n validationErrors = error;\n }\n\n const issues = Array.isArray(validationErrors) ? (validationErrors as IZodIssueLike[]) : null;\n\n // Prefer the first issue with a custom `params.code`, else its raw Zod code.\n let messageCode: string | undefined;\n let message = ZodErrorHandler.DEFAULT_VALIDATION_MESSAGE;\n\n if (issues && issues.length > 0) {\n const primaryIssue =\n issues.find((issue) => ZodErrorHandler.extractIssueCode({ issue }) !== undefined) ??\n issues[0];\n messageCode = ZodErrorHandler.extractIssueCode({ issue: primaryIssue }) ?? primaryIssue.code;\n message = primaryIssue.message;\n }\n\n // A ZodError we could not parse into issues yields no code of its own; the response still carries one, so no error response is ever missing the field a client branches on.\n const resolvedMessageCode = MessageCode.resolve(messageCode);\n\n return {\n statusCode,\n response: {\n message,\n statusCode,\n // `args` is empty: a Zod issue carries no interpolation values - per-field detail is in `details.cause`.\n normalized: { text: message, code: resolvedMessageCode, args: {} },\n requestId,\n details: {\n url,\n path,\n stack: !isProduction ? error.stack : undefined,\n cause: issues\n ? issues.map((el) => ({\n path: el.path.join('.') || 'root',\n message: el.message,\n code: el.code,\n expected: el.expected,\n received: el.received,\n }))\n : // A ZodError whose message is not an issue list: the raw error is for a developer, never for a production client.\n isProduction\n ? undefined\n : validationErrors,\n },\n },\n };\n }\n}\n"]}
|
|
@@ -3,6 +3,7 @@ export * from '@venizia/ignis-filter';
|
|
|
3
3
|
export * from './errors.js';
|
|
4
4
|
export * from '../query-schemas/index.js';
|
|
5
5
|
export * from './constants.js';
|
|
6
|
+
export * from './request-filter-guard.js';
|
|
6
7
|
export * from './scope-through.js';
|
|
7
8
|
export * from './types/index.js';
|
|
8
9
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -3,6 +3,7 @@ export * from '@venizia/ignis-filter';
|
|
|
3
3
|
export * from './errors.js';
|
|
4
4
|
export * from '../query-schemas/index.js';
|
|
5
5
|
export * from './constants.js';
|
|
6
|
+
export * from './request-filter-guard.js';
|
|
6
7
|
export * from './scope-through.js';
|
|
7
8
|
export * from './types/index.js';
|
|
8
9
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/base/repositories/common/index.ts"],"names":[],"mappings":"AAAA,2OAA2O;AAC3O,cAAc,uBAAuB,CAAC;AAEtC,cAAc,UAAU,CAAC;AACzB,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,SAAS,CAAC","sourcesContent":["/** The filter vocabulary (operators, sorts, the `TFilter` shape) lives in `@venizia/ignis-filter` so a browser data layer can speak the same language; re-exported here so every existing `@venizia/ignis-core` import keeps resolving. */\nexport * from '@venizia/ignis-filter';\n\nexport * from './errors';\nexport * from '../query-schemas';\nexport * from './constants';\nexport * from './scope-through';\nexport * from './types';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/base/repositories/common/index.ts"],"names":[],"mappings":"AAAA,2OAA2O;AAC3O,cAAc,uBAAuB,CAAC;AAEtC,cAAc,UAAU,CAAC;AACzB,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC;AAChC,cAAc,SAAS,CAAC","sourcesContent":["/** The filter vocabulary (operators, sorts, the `TFilter` shape) lives in `@venizia/ignis-filter` so a browser data layer can speak the same language; re-exported here so every existing `@venizia/ignis-core` import keeps resolving. */\nexport * from '@venizia/ignis-filter';\n\nexport * from './errors';\nexport * from '../query-schemas';\nexport * from './constants';\nexport * from './request-filter-guard';\nexport * from './scope-through';\nexport * from './types';\n"]}
|
|
@@ -0,0 +1,118 @@
|
|
|
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. */
|
|
3
|
+
export interface IRequestFilterShape {
|
|
4
|
+
where?: unknown;
|
|
5
|
+
order?: unknown;
|
|
6
|
+
include?: unknown;
|
|
7
|
+
fields?: unknown;
|
|
8
|
+
}
|
|
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. */
|
|
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
|
+
};
|
|
30
|
+
/**
|
|
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
|
|
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
|
|
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.
|
|
38
|
+
*/
|
|
39
|
+
export interface IRequestFilterJudge {
|
|
40
|
+
/** Whether the key reaches a field the model hides. */
|
|
41
|
+
isHidden(opts: {
|
|
42
|
+
key: string;
|
|
43
|
+
}): boolean;
|
|
44
|
+
/** 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. */
|
|
45
|
+
refuse(opts: {
|
|
46
|
+
kind: TRequestFilterKeyKind;
|
|
47
|
+
key: string;
|
|
48
|
+
tableName: string;
|
|
49
|
+
}): Error;
|
|
50
|
+
/** 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`. */
|
|
51
|
+
relation?(opts: {
|
|
52
|
+
relation: string;
|
|
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;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
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
|
|
73
|
+
* `where` or an `order` on one reveals its value one question at a time. The check runs where a request enters
|
|
74
|
+
* (the generated controllers), never in the repository's own verbs, so server code can still filter and sort on a hidden field.
|
|
75
|
+
*/
|
|
76
|
+
export declare class RequestFilterGuard {
|
|
77
|
+
/** 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`). */
|
|
78
|
+
static isHiddenByName(opts: {
|
|
79
|
+
key: string;
|
|
80
|
+
hidden: ReadonlySet<string>;
|
|
81
|
+
}): boolean;
|
|
82
|
+
/** The refusal when a model has no query error of its own to borrow: a 400 `Column NOT FOUND`. */
|
|
83
|
+
static columnNotFound(opts: {
|
|
84
|
+
scope: string;
|
|
85
|
+
key: string;
|
|
86
|
+
}): Error;
|
|
87
|
+
/** Refuses a `where` that names a hidden field, at any depth of `and` and `or`. */
|
|
88
|
+
static assertWhere(opts: {
|
|
89
|
+
where?: unknown;
|
|
90
|
+
judge: IRequestFilterJudge;
|
|
91
|
+
tableName: string;
|
|
92
|
+
}): void;
|
|
93
|
+
/** Refuses an `order` that names a hidden field (a string or a list of `'field DIRECTION'` entries), reading each entry as the query does. */
|
|
94
|
+
static assertOrder(opts: {
|
|
95
|
+
order?: unknown;
|
|
96
|
+
judge: IRequestFilterJudge;
|
|
97
|
+
tableName: string;
|
|
98
|
+
}): void;
|
|
99
|
+
/**
|
|
100
|
+
* Refuses a filter that names a hidden field in its `where`, its `order`, or the `scope` of any `include`, each level
|
|
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.
|
|
105
|
+
*/
|
|
106
|
+
static assertFilter(opts: {
|
|
107
|
+
filter?: IRequestFilterShape;
|
|
108
|
+
judge: IRequestFilterJudge;
|
|
109
|
+
tableName: string;
|
|
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;
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=request-filter-guard.d.ts.map
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { HTTP } from '@venizia/ignis-helpers/common';
|
|
2
|
+
import { getError } from '@venizia/ignis-helpers/core';
|
|
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
|
+
];
|
|
15
|
+
/**
|
|
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
|
|
17
|
+
* `where` or an `order` on one reveals its value one question at a time. The check runs where a request enters
|
|
18
|
+
* (the generated controllers), never in the repository's own verbs, so server code can still filter and sort on a hidden field.
|
|
19
|
+
*/
|
|
20
|
+
export class RequestFilterGuard {
|
|
21
|
+
/** 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`). */
|
|
22
|
+
static isHiddenByName(opts) {
|
|
23
|
+
const { key, hidden } = opts;
|
|
24
|
+
for (const name of hidden) {
|
|
25
|
+
if (key === name || key.startsWith(`${name}.`) || name.startsWith(`${key}.`)) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
/** The refusal when a model has no query error of its own to borrow: a 400 `Column NOT FOUND`. */
|
|
32
|
+
static columnNotFound(opts) {
|
|
33
|
+
const quoted = JSON.stringify(opts.key).replace(/[\u007f-\u009f\u2028\u2029]/g, (character) => `\\u${character.charCodeAt(0).toString(16).padStart(4, '0')}`);
|
|
34
|
+
return getError({
|
|
35
|
+
statusCode: HTTP.ResultCodes.RS_4.BadRequest,
|
|
36
|
+
message: `[${opts.scope}] Column NOT FOUND | key: ${quoted}`,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/** Refuses a `where` that names a hidden field, at any depth of `and` and `or`. */
|
|
40
|
+
static assertWhere(opts) {
|
|
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;
|
|
93
|
+
if (typeof where !== 'object' || where === null) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
for (const key of Object.keys(where)) {
|
|
97
|
+
const value = where[key];
|
|
98
|
+
if (QueryOperators.LOGICAL_GROUP_OPERATORS.has(key)) {
|
|
99
|
+
for (const branch of Array.isArray(value) ? value : [value]) {
|
|
100
|
+
RequestFilterGuard.walkWhere({ where: branch, judge, tableName, walk });
|
|
101
|
+
}
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
RequestFilterGuard.visitKey({
|
|
105
|
+
walk,
|
|
106
|
+
judge,
|
|
107
|
+
tableName,
|
|
108
|
+
key,
|
|
109
|
+
piece: { part: RequestFilterParts.WHERE, value: { [key]: value } },
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
static walkOrder(opts) {
|
|
114
|
+
const { order, judge, tableName, walk } = opts;
|
|
115
|
+
if (order === undefined || order === null) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
for (const entry of Array.isArray(order) ? order : [order]) {
|
|
119
|
+
if (typeof entry !== 'string') {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
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;
|
|
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
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
static walkInclude(opts) {
|
|
148
|
+
const { include, judge, tableName, walk } = opts;
|
|
149
|
+
if (!Array.isArray(include)) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
for (const entry of include) {
|
|
153
|
+
walk.refusal ??= judge.queryRefusal?.({
|
|
154
|
+
part: RequestFilterParts.INCLUDE,
|
|
155
|
+
value: entry,
|
|
156
|
+
tableName,
|
|
157
|
+
});
|
|
158
|
+
if (typeof entry !== 'object' || entry === null) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const { relation, scope } = entry;
|
|
162
|
+
if (typeof relation !== 'string' || scope === undefined) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
const relationJudge = judge.relation?.({ relation });
|
|
166
|
+
// An unknown relation is refused by the query itself; here only a known one has a model to judge by.
|
|
167
|
+
if (relationJudge === undefined) {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
RequestFilterGuard.walkFilter({
|
|
171
|
+
filter: scope,
|
|
172
|
+
judge: relationJudge,
|
|
173
|
+
tableName: relation,
|
|
174
|
+
walk,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
//# sourceMappingURL=request-filter-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,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AAE,CAAC,CAAC;YAErD,qGAAqG;YACrG,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;gBAChC,SAAS;YACX,CAAC;YAED,kBAAkB,CAAC,UAAU,CAAC;gBAC5B,MAAM,EAAE,KAAK;gBACb,KAAK,EAAE,aAAa;gBACpB,SAAS,EAAE,QAAQ;gBACnB,IAAI;aACL,CAAC,CAAC;QACL,CAAC;IACH,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"]}
|
|
@@ -4,6 +4,7 @@ import type { IModelMetadata } from '../../../helpers/inversion/index.js';
|
|
|
4
4
|
import type { TClass, TNullable } from '@venizia/ignis-helpers/common';
|
|
5
5
|
import { BaseHelper } from '@venizia/ignis-helpers/core';
|
|
6
6
|
import type { IExtraOptions, IPersistableRepository, IWithReadRetry, TCount, TDataWithRange, TFindOneOptions, TFindOptions, TFindRangeOptions, TRepositoryOperationScope } from '../common/index.js';
|
|
7
|
+
import type { IRequestFilterJudge, IRequestFilterShape } from '../common/request-filter-guard.js';
|
|
7
8
|
import type { TByIdFilter, TFields, TFilter, TRow, TWhere } from '@venizia/ignis-filter';
|
|
8
9
|
/**
|
|
9
10
|
* Engine-neutral repository plumbing - lazy dataSource/entity resolution, class-keyed `@model`
|
|
@@ -42,6 +43,21 @@ export declare abstract class AbstractRepository<TDataObject extends object, TPe
|
|
|
42
43
|
*/
|
|
43
44
|
protected get modelSettings(): IModelMetadata['settings'];
|
|
44
45
|
protected get hiddenFields(): string[];
|
|
46
|
+
/**
|
|
47
|
+
* Refuses (400, `Column NOT FOUND`) a filter that came from a request and names a field the model hides, in its
|
|
48
|
+
* `where`, its `order` or the `scope` of an `include` (judged by the hidden fields of the model it reads). The generated
|
|
49
|
+
* controllers call it on what a client sends; the repository's own verbs do not, so server code can still filter and
|
|
50
|
+
* sort on a hidden field.
|
|
51
|
+
*/
|
|
52
|
+
assertRequestFilter(opts: {
|
|
53
|
+
filter?: IRequestFilterShape;
|
|
54
|
+
}): void;
|
|
55
|
+
/**
|
|
56
|
+
* How this repository judges the keys of a request filter. The default reads a key as a dotted field name and refuses it with
|
|
57
|
+
* a 400 `Column NOT FOUND`; a relational or a search repository overrides it to read keys as its own query does and to
|
|
58
|
+
* refuse with its own query's error for a column that does not exist.
|
|
59
|
+
*/
|
|
60
|
+
protected createRequestFilterJudge(): IRequestFilterJudge;
|
|
45
61
|
/** Default where from @model settings.query.filter.where, AND-merged into every query unless skipped. */
|
|
46
62
|
protected get defaultWhere(): TWhere | undefined;
|
|
47
63
|
protected get defaultLimit(): number | undefined;
|
|
@@ -5,6 +5,7 @@ import { RetryBackoffStrategies, RetryHelper, RetryJitterModes } from '@venizia/
|
|
|
5
5
|
// The file, not the `../common` barrel: the barrel re-exports the OpenAPI schemas, and this module is
|
|
6
6
|
// on the `./repository` entry, which must load without `@hono/zod-openapi` installed.
|
|
7
7
|
import { DEFAULT_MAX_LIMIT, RepositoryErrorCodes, RepositoryOperationScopes, } from '../common/constants.js';
|
|
8
|
+
import { RequestFilterGuard } from '../common/request-filter-guard.js';
|
|
8
9
|
/**
|
|
9
10
|
* Engine-neutral repository plumbing - lazy dataSource/entity resolution, class-keyed `@model`
|
|
10
11
|
* settings, operation scope. `TOptions` defaults to `IExtraOptions` so connectors can narrow it
|
|
@@ -72,6 +73,31 @@ export class AbstractRepository extends BaseHelper {
|
|
|
72
73
|
get hiddenFields() {
|
|
73
74
|
return this.modelSettings?.hiddenProperties ?? [];
|
|
74
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Refuses (400, `Column NOT FOUND`) a filter that came from a request and names a field the model hides, in its
|
|
78
|
+
* `where`, its `order` or the `scope` of an `include` (judged by the hidden fields of the model it reads). The generated
|
|
79
|
+
* controllers call it on what a client sends; the repository's own verbs do not, so server code can still filter and
|
|
80
|
+
* sort on a hidden field.
|
|
81
|
+
*/
|
|
82
|
+
assertRequestFilter(opts) {
|
|
83
|
+
RequestFilterGuard.assertFilter({
|
|
84
|
+
filter: opts.filter,
|
|
85
|
+
judge: this.createRequestFilterJudge(),
|
|
86
|
+
tableName: this.entity?.name ?? this.constructor.name,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* How this repository judges the keys of a request filter. The default reads a key as a dotted field name and refuses it with
|
|
91
|
+
* a 400 `Column NOT FOUND`; a relational or a search repository overrides it to read keys as its own query does and to
|
|
92
|
+
* refuse with its own query's error for a column that does not exist.
|
|
93
|
+
*/
|
|
94
|
+
createRequestFilterJudge() {
|
|
95
|
+
const hidden = new Set(this.hiddenFields);
|
|
96
|
+
return {
|
|
97
|
+
isHidden: ({ key }) => RequestFilterGuard.isHiddenByName({ key, hidden }),
|
|
98
|
+
refuse: ({ key }) => RequestFilterGuard.columnNotFound({ scope: this.constructor.name, key }),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
75
101
|
/** Default where from @model settings.query.filter.where, AND-merged into every query unless skipped. */
|
|
76
102
|
get defaultWhere() {
|
|
77
103
|
return this.modelSettings?.query?.filter?.where;
|