hexok 1.0.1 → 2.1.0
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/README.md +9 -22
- package/dist/adapter.d.ts +18 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +21 -0
- package/dist/adapter.js.map +1 -0
- package/dist/catalog.d.ts +88 -0
- package/dist/catalog.d.ts.map +1 -0
- package/dist/catalog.js +79 -0
- package/dist/catalog.js.map +1 -0
- package/dist/{core/coded-error.d.ts → coded-error.d.ts} +9 -4
- package/dist/coded-error.d.ts.map +1 -0
- package/dist/{core/coded-error.js → coded-error.js} +9 -4
- package/dist/coded-error.js.map +1 -0
- package/dist/cow-draft.d.ts.map +1 -0
- package/dist/cow-draft.js.map +1 -0
- package/dist/entity.d.ts +63 -0
- package/dist/entity.d.ts.map +1 -0
- package/dist/{domain/entity.js → entity.js} +52 -80
- package/dist/entity.js.map +1 -0
- package/dist/error-map.d.ts +22 -0
- package/dist/error-map.d.ts.map +1 -0
- package/dist/error-map.js +2 -0
- package/dist/error-map.js.map +1 -0
- package/dist/errors.d.ts +85 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +85 -0
- package/dist/errors.js.map +1 -0
- package/dist/event-handler.d.ts +31 -0
- package/dist/event-handler.d.ts.map +1 -0
- package/dist/event-handler.js +28 -0
- package/dist/event-handler.js.map +1 -0
- package/dist/event.d.ts +37 -0
- package/dist/event.d.ts.map +1 -0
- package/dist/event.js +44 -0
- package/dist/event.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/mapper.d.ts +100 -0
- package/dist/mapper.d.ts.map +1 -0
- package/dist/mapper.js +167 -0
- package/dist/mapper.js.map +1 -0
- package/dist/port.d.ts +20 -0
- package/dist/port.d.ts.map +1 -0
- package/dist/port.js +25 -0
- package/dist/port.js.map +1 -0
- package/dist/result.d.ts.map +1 -0
- package/dist/result.js.map +1 -0
- package/dist/schema.d.ts +37 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +50 -0
- package/dist/schema.js.map +1 -0
- package/dist/{core/standard-schema.d.ts → standard-schema.d.ts} +1 -1
- package/dist/standard-schema.d.ts.map +1 -0
- package/dist/standard-schema.js.map +1 -0
- package/dist/use-case.d.ts +183 -0
- package/dist/use-case.d.ts.map +1 -0
- package/dist/use-case.js +221 -0
- package/dist/use-case.js.map +1 -0
- package/dist/{core/schema.d.ts → validate.d.ts} +1 -1
- package/dist/validate.d.ts.map +1 -0
- package/dist/{core/schema.js → validate.js} +1 -1
- package/dist/validate.js.map +1 -0
- package/package.json +7 -26
- package/skills/hexok/SKILL.md +127 -0
- package/skills/hexok/references/adapter.md +66 -0
- package/skills/hexok/references/entity.md +74 -0
- package/skills/hexok/references/error.md +140 -0
- package/skills/hexok/references/event-catalog.md +57 -0
- package/skills/hexok/references/event-handler.md +56 -0
- package/skills/hexok/references/event.md +53 -0
- package/skills/hexok/references/mapper.md +77 -0
- package/skills/hexok/references/port.md +58 -0
- package/skills/hexok/references/schema.md +47 -0
- package/skills/hexok/references/use-case.md +164 -0
- package/dist/app/callable-use-case.d.ts +0 -34
- package/dist/app/callable-use-case.d.ts.map +0 -1
- package/dist/app/callable-use-case.js +0 -41
- package/dist/app/callable-use-case.js.map +0 -1
- package/dist/app/contract.d.ts +0 -46
- package/dist/app/contract.d.ts.map +0 -1
- package/dist/app/contract.js +0 -28
- package/dist/app/contract.js.map +0 -1
- package/dist/app/error-factory.d.ts +0 -9
- package/dist/app/error-factory.d.ts.map +0 -1
- package/dist/app/error-factory.js +0 -30
- package/dist/app/error-factory.js.map +0 -1
- package/dist/app/event-channel.d.ts +0 -133
- package/dist/app/event-channel.d.ts.map +0 -1
- package/dist/app/event-channel.js +0 -49
- package/dist/app/event-channel.js.map +0 -1
- package/dist/app/event-use-case.d.ts +0 -47
- package/dist/app/event-use-case.d.ts.map +0 -1
- package/dist/app/event-use-case.js +0 -54
- package/dist/app/event-use-case.js.map +0 -1
- package/dist/app/execute-ctx.d.ts +0 -92
- package/dist/app/execute-ctx.d.ts.map +0 -1
- package/dist/app/execute-ctx.js +0 -2
- package/dist/app/execute-ctx.js.map +0 -1
- package/dist/app/external-use-case.d.ts +0 -25
- package/dist/app/external-use-case.d.ts.map +0 -1
- package/dist/app/external-use-case.js +0 -25
- package/dist/app/external-use-case.js.map +0 -1
- package/dist/app/index.d.ts +0 -9
- package/dist/app/index.d.ts.map +0 -1
- package/dist/app/index.js +0 -8
- package/dist/app/index.js.map +0 -1
- package/dist/app/internal-use-case.d.ts +0 -26
- package/dist/app/internal-use-case.d.ts.map +0 -1
- package/dist/app/internal-use-case.js +0 -26
- package/dist/app/internal-use-case.js.map +0 -1
- package/dist/app/nest.d.ts +0 -9
- package/dist/app/nest.d.ts.map +0 -1
- package/dist/app/nest.js +0 -28
- package/dist/app/nest.js.map +0 -1
- package/dist/app/types.d.ts +0 -153
- package/dist/app/types.d.ts.map +0 -1
- package/dist/app/types.js +0 -17
- package/dist/app/types.js.map +0 -1
- package/dist/core/coded-error.d.ts.map +0 -1
- package/dist/core/coded-error.js.map +0 -1
- package/dist/core/error-map.d.ts +0 -21
- package/dist/core/error-map.d.ts.map +0 -1
- package/dist/core/error-map.js +0 -5
- package/dist/core/error-map.js.map +0 -1
- package/dist/core/index.d.ts +0 -6
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/index.js +0 -5
- package/dist/core/index.js.map +0 -1
- package/dist/core/result.d.ts.map +0 -1
- package/dist/core/result.js.map +0 -1
- package/dist/core/schema.d.ts.map +0 -1
- package/dist/core/schema.js.map +0 -1
- package/dist/core/standard-schema.d.ts.map +0 -1
- package/dist/core/standard-schema.js.map +0 -1
- package/dist/domain/capabilities.d.ts +0 -42
- package/dist/domain/capabilities.d.ts.map +0 -1
- package/dist/domain/capabilities.js +0 -2
- package/dist/domain/capabilities.js.map +0 -1
- package/dist/domain/catalog.d.ts +0 -46
- package/dist/domain/catalog.d.ts.map +0 -1
- package/dist/domain/catalog.js +0 -114
- package/dist/domain/catalog.js.map +0 -1
- package/dist/domain/channel.d.ts +0 -36
- package/dist/domain/channel.d.ts.map +0 -1
- package/dist/domain/channel.js +0 -2
- package/dist/domain/channel.js.map +0 -1
- package/dist/domain/cow-draft.d.ts.map +0 -1
- package/dist/domain/cow-draft.js.map +0 -1
- package/dist/domain/entity.d.ts +0 -120
- package/dist/domain/entity.d.ts.map +0 -1
- package/dist/domain/entity.js.map +0 -1
- package/dist/domain/envelope.d.ts +0 -59
- package/dist/domain/envelope.d.ts.map +0 -1
- package/dist/domain/envelope.js +0 -2
- package/dist/domain/envelope.js.map +0 -1
- package/dist/domain/event.d.ts +0 -44
- package/dist/domain/event.d.ts.map +0 -1
- package/dist/domain/event.js +0 -21
- package/dist/domain/event.js.map +0 -1
- package/dist/domain/index.d.ts +0 -8
- package/dist/domain/index.d.ts.map +0 -1
- package/dist/domain/index.js +0 -5
- package/dist/domain/index.js.map +0 -1
- package/dist/domain/port.d.ts +0 -45
- package/dist/domain/port.d.ts.map +0 -1
- package/dist/domain/port.js +0 -41
- package/dist/domain/port.js.map +0 -1
- package/dist/infra/adapter.d.ts +0 -22
- package/dist/infra/adapter.d.ts.map +0 -1
- package/dist/infra/adapter.js +0 -18
- package/dist/infra/adapter.js.map +0 -1
- package/dist/infra/index.d.ts +0 -3
- package/dist/infra/index.d.ts.map +0 -1
- package/dist/infra/index.js +0 -3
- package/dist/infra/index.js.map +0 -1
- package/dist/infra/mapper.d.ts +0 -48
- package/dist/infra/mapper.d.ts.map +0 -1
- package/dist/infra/mapper.js +0 -77
- package/dist/infra/mapper.js.map +0 -1
- package/dist/runtime/app.d.ts +0 -100
- package/dist/runtime/app.d.ts.map +0 -1
- package/dist/runtime/app.js +0 -292
- package/dist/runtime/app.js.map +0 -1
- package/dist/runtime/channel.d.ts +0 -34
- package/dist/runtime/channel.d.ts.map +0 -1
- package/dist/runtime/channel.js +0 -110
- package/dist/runtime/channel.js.map +0 -1
- package/dist/runtime/client.d.ts +0 -21
- package/dist/runtime/client.d.ts.map +0 -1
- package/dist/runtime/client.js +0 -2
- package/dist/runtime/client.js.map +0 -1
- package/dist/runtime/completeness.d.ts +0 -89
- package/dist/runtime/completeness.d.ts.map +0 -1
- package/dist/runtime/completeness.js +0 -2
- package/dist/runtime/completeness.js.map +0 -1
- package/dist/runtime/envelope.d.ts +0 -7
- package/dist/runtime/envelope.d.ts.map +0 -1
- package/dist/runtime/envelope.js +0 -69
- package/dist/runtime/envelope.js.map +0 -1
- package/dist/runtime/events.d.ts +0 -6
- package/dist/runtime/events.d.ts.map +0 -1
- package/dist/runtime/events.js +0 -48
- package/dist/runtime/events.js.map +0 -1
- package/dist/runtime/http-status.d.ts +0 -3
- package/dist/runtime/http-status.d.ts.map +0 -1
- package/dist/runtime/http-status.js +0 -11
- package/dist/runtime/http-status.js.map +0 -1
- package/dist/runtime/http.d.ts +0 -4
- package/dist/runtime/http.d.ts.map +0 -1
- package/dist/runtime/http.js +0 -58
- package/dist/runtime/http.js.map +0 -1
- package/dist/runtime/index.d.ts +0 -10
- package/dist/runtime/index.d.ts.map +0 -1
- package/dist/runtime/index.js +0 -6
- package/dist/runtime/index.js.map +0 -1
- package/dist/runtime/interceptor.d.ts +0 -61
- package/dist/runtime/interceptor.d.ts.map +0 -1
- package/dist/runtime/interceptor.js +0 -7
- package/dist/runtime/interceptor.js.map +0 -1
- package/dist/runtime/invoke.d.ts +0 -34
- package/dist/runtime/invoke.d.ts.map +0 -1
- package/dist/runtime/invoke.js +0 -235
- package/dist/runtime/invoke.js.map +0 -1
- package/dist/runtime/middleware.d.ts +0 -20
- package/dist/runtime/middleware.d.ts.map +0 -1
- package/dist/runtime/middleware.js +0 -2
- package/dist/runtime/middleware.js.map +0 -1
- package/dist/runtime/rpc.d.ts +0 -24
- package/dist/runtime/rpc.d.ts.map +0 -1
- package/dist/runtime/rpc.js +0 -16
- package/dist/runtime/rpc.js.map +0 -1
- package/dist/testing/app-test.d.ts +0 -77
- package/dist/testing/app-test.d.ts.map +0 -1
- package/dist/testing/app-test.js +0 -109
- package/dist/testing/app-test.js.map +0 -1
- package/dist/testing/in-memory-bus.d.ts +0 -19
- package/dist/testing/in-memory-bus.d.ts.map +0 -1
- package/dist/testing/in-memory-bus.js +0 -31
- package/dist/testing/in-memory-bus.js.map +0 -1
- package/dist/testing/in-memory-channel.d.ts +0 -32
- package/dist/testing/in-memory-channel.d.ts.map +0 -1
- package/dist/testing/in-memory-channel.js +0 -65
- package/dist/testing/in-memory-channel.js.map +0 -1
- package/dist/testing/in-memory-queue.d.ts +0 -24
- package/dist/testing/in-memory-queue.d.ts.map +0 -1
- package/dist/testing/in-memory-queue.js +0 -66
- package/dist/testing/in-memory-queue.js.map +0 -1
- package/dist/testing/in-memory-repository.d.ts +0 -44
- package/dist/testing/in-memory-repository.d.ts.map +0 -1
- package/dist/testing/in-memory-repository.js +0 -60
- package/dist/testing/in-memory-repository.js.map +0 -1
- package/dist/testing/index.d.ts +0 -6
- package/dist/testing/index.d.ts.map +0 -1
- package/dist/testing/index.js +0 -6
- package/dist/testing/index.js.map +0 -1
- /package/dist/{domain/cow-draft.d.ts → cow-draft.d.ts} +0 -0
- /package/dist/{domain/cow-draft.js → cow-draft.js} +0 -0
- /package/dist/{core/result.d.ts → result.d.ts} +0 -0
- /package/dist/{core/result.js → result.js} +0 -0
- /package/dist/{core/standard-schema.js → standard-schema.js} +0 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { Adapter } from './adapter.js';
|
|
2
|
+
export { EventCatalog, type EventInstance, type EventMessage, } from './catalog.js';
|
|
3
|
+
export { CodedError, validationError } from './coded-error.js';
|
|
4
|
+
export { type DeepReadonly, Entity, type EntityInstance } from './entity.js';
|
|
5
|
+
export type { ErrorDef, ErrorMap } from './error-map.js';
|
|
6
|
+
export { type CatalogMember, type CatalogUnion, catalogBrand, Errors, type ErrorsHandle, } from './errors.js';
|
|
7
|
+
export { Event } from './event.js';
|
|
8
|
+
export { EventHandler } from './event-handler.js';
|
|
9
|
+
export { Mapper, type MapperClass } from './mapper.js';
|
|
10
|
+
export { Port } from './port.js';
|
|
11
|
+
export { fail, ok, type Result } from './result.js';
|
|
12
|
+
export { type InferSchema, Schema, type SchemaSource } from './schema.js';
|
|
13
|
+
export type { StandardSchemaV1 } from './standard-schema.js';
|
|
14
|
+
export { UseCase } from './use-case.js';
|
|
15
|
+
export { type Infer, validate } from './validate.js';
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EACL,YAAY,EACZ,KAAK,aAAa,EAClB,KAAK,YAAY,GAClB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,EAAE,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7E,YAAY,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,YAAY,EACZ,MAAM,EACN,KAAK,YAAY,GAClB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,MAAM,EAAE,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,KAAK,MAAM,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,EAAE,KAAK,YAAY,EAAE,MAAM,aAAa,CAAC;AAC1E,YAAY,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,KAAK,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { Adapter } from './adapter.js';
|
|
2
|
+
export { EventCatalog, } from './catalog.js';
|
|
3
|
+
export { CodedError, validationError } from './coded-error.js';
|
|
4
|
+
export { Entity } from './entity.js';
|
|
5
|
+
export { catalogBrand, Errors, } from './errors.js';
|
|
6
|
+
export { Event } from './event.js';
|
|
7
|
+
export { EventHandler } from './event-handler.js';
|
|
8
|
+
export { Mapper } from './mapper.js';
|
|
9
|
+
export { Port } from './port.js';
|
|
10
|
+
export { fail, ok } from './result.js';
|
|
11
|
+
export { Schema } from './schema.js';
|
|
12
|
+
export { UseCase } from './use-case.js';
|
|
13
|
+
export { validate } from './validate.js';
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EACL,YAAY,GAGb,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAqB,MAAM,EAAuB,MAAM,aAAa,CAAC;AAE7E,OAAO,EAGL,YAAY,EACZ,MAAM,GAEP,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,MAAM,EAAoB,MAAM,aAAa,CAAC;AACvD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,EAAE,EAAe,MAAM,aAAa,CAAC;AACpD,OAAO,EAAoB,MAAM,EAAqB,MAAM,aAAa,CAAC;AAE1E,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAc,QAAQ,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/mapper.d.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { type InferSchema, type ResolvedSchema, type SchemaSource } from './schema.js';
|
|
2
|
+
import type { StandardSchemaV1 } from './standard-schema.js';
|
|
3
|
+
/** Instance type of a constructor. Raw schemas have no prototype, so this is `never`. */
|
|
4
|
+
type PrototypeOf<T> = T extends {
|
|
5
|
+
readonly prototype: infer P;
|
|
6
|
+
} ? P : never;
|
|
7
|
+
/**
|
|
8
|
+
* Entity instances expose `props` and `toProps`.
|
|
9
|
+
* A missing prototype is `never`, and `never extends …` is true, so raw schemas are excluded.
|
|
10
|
+
*/
|
|
11
|
+
type IsEntity<T> = [PrototypeOf<T>] extends [never] ? false : PrototypeOf<T> extends {
|
|
12
|
+
readonly props: object;
|
|
13
|
+
toProps(): unknown;
|
|
14
|
+
} ? true : false;
|
|
15
|
+
/** Value `fromSource` receives and `fromModel` returns. */
|
|
16
|
+
type SourceModel<T> = IsEntity<T> extends true ? PrototypeOf<T> : T extends SchemaSource ? InferSchema<T> : never;
|
|
17
|
+
/** Draft `toSource` returns. Entities yield props; the kit builds the instance. */
|
|
18
|
+
type SourceDraft<T> = IsEntity<T> extends true ? T extends {
|
|
19
|
+
readonly schema: infer S extends SchemaSource;
|
|
20
|
+
} ? InferSchema<S> : never : T extends SchemaSource ? InferSchema<T> : never;
|
|
21
|
+
type MapperOptions = {
|
|
22
|
+
readonly validate?: boolean;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Instance base of {@link Mapper}. Exported so declaration emit can name it.
|
|
26
|
+
* An anonymous base cannot publish protected `fromSource` or `toSource` (TS4094).
|
|
27
|
+
* Extend {@link Mapper}, not this class.
|
|
28
|
+
*/
|
|
29
|
+
export declare abstract class MapperClass<Token extends string, Source, Stored extends SchemaSource> {
|
|
30
|
+
#private;
|
|
31
|
+
/** Mapper name. Literal type of the string passed to {@link Mapper}. */
|
|
32
|
+
static readonly token: string;
|
|
33
|
+
/** Stored Standard Schema. A Schema class is stored as its definition. */
|
|
34
|
+
static readonly schema: StandardSchemaV1;
|
|
35
|
+
/** Entity, Schema class, or Standard Schema this mapper reads and builds. */
|
|
36
|
+
static readonly source: unknown;
|
|
37
|
+
protected constructor(brand: Token);
|
|
38
|
+
/**
|
|
39
|
+
* Source to an unchecked model draft.
|
|
40
|
+
* An entity source receives the instance. A schema source receives its output.
|
|
41
|
+
*/
|
|
42
|
+
protected abstract fromSource(source: SourceModel<Source>): unknown;
|
|
43
|
+
/**
|
|
44
|
+
* Model output to a source draft.
|
|
45
|
+
* An entity draft is props. A schema draft is that schema's output.
|
|
46
|
+
*/
|
|
47
|
+
protected abstract toSource(model: InferSchema<Stored>): SourceDraft<Source>;
|
|
48
|
+
/**
|
|
49
|
+
* Run `fromSource`. With `{ validate: true }`, check the draft against the model schema.
|
|
50
|
+
* A failure throws `CodedError` `VALIDATION` using this mapper's token.
|
|
51
|
+
* Do not override.
|
|
52
|
+
*/
|
|
53
|
+
toModel(source: SourceModel<Source>, options?: MapperOptions): InferSchema<Stored>;
|
|
54
|
+
/**
|
|
55
|
+
* Run `toSource` and build the source.
|
|
56
|
+
* With `{ validate: true }`, check `value` against the model schema first.
|
|
57
|
+
* An entity is then `parse`d. Otherwise an entity is `restore`d.
|
|
58
|
+
* Do not override.
|
|
59
|
+
*/
|
|
60
|
+
fromModel(value: unknown, options?: MapperOptions): SourceModel<Source>;
|
|
61
|
+
}
|
|
62
|
+
type MapperCtor<Token extends string, Source, Stored extends SchemaSource> = (abstract new () => MapperClass<Token, Source, Stored>) & {
|
|
63
|
+
readonly token: Token;
|
|
64
|
+
readonly schema: ResolvedSchema<Stored>;
|
|
65
|
+
readonly source: Source;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Persistence translator. An adapter maps the source a port speaks — an
|
|
69
|
+
* entity, a Schema class, or a Standard Schema — to the shape that adapter
|
|
70
|
+
* stores. Domain code does not import the stored schema. The mapper is
|
|
71
|
+
* stateless; do not `commit()` inside it. The adapter commits after the
|
|
72
|
+
* driver succeeds.
|
|
73
|
+
*
|
|
74
|
+
* The subclass writes `fromSource` and `toSource`. Adapters call `toModel` and
|
|
75
|
+
* `fromModel` and do not override them. Checks are off. Pass
|
|
76
|
+
* `{ validate: true }` to the factory or to a call to run the model schema.
|
|
77
|
+
* A checked `fromModel` uses `parse`. The default restores an entity.
|
|
78
|
+
*
|
|
79
|
+
* ```ts
|
|
80
|
+
* class MongoUser extends Mapper('mongo.User', User, mongoUserSchema) {
|
|
81
|
+
* protected fromSource(user: User) {
|
|
82
|
+
* const props = user.toProps()
|
|
83
|
+
* const [lat, lng] = props.location
|
|
84
|
+
* return {
|
|
85
|
+
* _id: props.id,
|
|
86
|
+
* name: props.name,
|
|
87
|
+
* location: { type: 'Point' as const, coordinates: [lng, lat] },
|
|
88
|
+
* }
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* protected toSource(doc: MongoUserDoc) {
|
|
92
|
+
* const [lng, lat] = doc.location.coordinates
|
|
93
|
+
* return { id: doc._id, name: doc.name, location: [lat, lng] }
|
|
94
|
+
* }
|
|
95
|
+
* }
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export declare function Mapper<const Token extends string, Source, Stored extends SchemaSource>(token: Token, source: Source, storedSchema: Stored, options?: MapperOptions): MapperCtor<Token, Source, Stored>;
|
|
99
|
+
export {};
|
|
100
|
+
//# sourceMappingURL=mapper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mapper.d.ts","sourceRoot":"","s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,KAAK,EACZ,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,MAAM,EACpB,OAAO,CAAC,EAAE,aAAa,GACtB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,CAkBnC"}
|
package/dist/mapper.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { validationError } from './coded-error.js';
|
|
2
|
+
import { schemaDefinition, } from './schema.js';
|
|
3
|
+
import { validate } from './validate.js';
|
|
4
|
+
const sourceKindKey = Symbol('hexok.mapper.source');
|
|
5
|
+
const validateKey = Symbol('hexok.mapper.validate');
|
|
6
|
+
/**
|
|
7
|
+
* Instance base of {@link Mapper}. Exported so declaration emit can name it.
|
|
8
|
+
* An anonymous base cannot publish protected `fromSource` or `toSource` (TS4094).
|
|
9
|
+
* Extend {@link Mapper}, not this class.
|
|
10
|
+
*/
|
|
11
|
+
export class MapperClass {
|
|
12
|
+
/** Mapper name. Literal type of the string passed to {@link Mapper}. */
|
|
13
|
+
static token;
|
|
14
|
+
/** Stored Standard Schema. A Schema class is stored as its definition. */
|
|
15
|
+
static schema;
|
|
16
|
+
/** Entity, Schema class, or Standard Schema this mapper reads and builds. */
|
|
17
|
+
static source;
|
|
18
|
+
/** Nominal marker. Each `Mapper(...)` call is a distinct class. */
|
|
19
|
+
#brand;
|
|
20
|
+
constructor(brand) {
|
|
21
|
+
this.#brand = brand;
|
|
22
|
+
void this.#brand;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Run `fromSource`. With `{ validate: true }`, check the draft against the model schema.
|
|
26
|
+
* A failure throws `CodedError` `VALIDATION` using this mapper's token.
|
|
27
|
+
* Do not override.
|
|
28
|
+
*/
|
|
29
|
+
toModel(source, options) {
|
|
30
|
+
const ctor = runtimeOf(this);
|
|
31
|
+
const draft = this.fromSource(source);
|
|
32
|
+
if (!checking(ctor, options))
|
|
33
|
+
return draft;
|
|
34
|
+
const parsed = validate(ctor.schema, draft);
|
|
35
|
+
if (!parsed.ok) {
|
|
36
|
+
throw validationError(`hexok: ${ctor.token} validation failed`, parsed.issues);
|
|
37
|
+
}
|
|
38
|
+
return parsed.value;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Run `toSource` and build the source.
|
|
42
|
+
* With `{ validate: true }`, check `value` against the model schema first.
|
|
43
|
+
* An entity is then `parse`d. Otherwise an entity is `restore`d.
|
|
44
|
+
* Do not override.
|
|
45
|
+
*/
|
|
46
|
+
fromModel(value, options) {
|
|
47
|
+
const ctor = runtimeOf(this);
|
|
48
|
+
const check = checking(ctor, options);
|
|
49
|
+
const model = check ? modelOutput(ctor, value) : value;
|
|
50
|
+
return buildSource(ctor[sourceKindKey], ctor.source, ctor.token, this.toSource(model), check);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Persistence translator. An adapter maps the source a port speaks — an
|
|
55
|
+
* entity, a Schema class, or a Standard Schema — to the shape that adapter
|
|
56
|
+
* stores. Domain code does not import the stored schema. The mapper is
|
|
57
|
+
* stateless; do not `commit()` inside it. The adapter commits after the
|
|
58
|
+
* driver succeeds.
|
|
59
|
+
*
|
|
60
|
+
* The subclass writes `fromSource` and `toSource`. Adapters call `toModel` and
|
|
61
|
+
* `fromModel` and do not override them. Checks are off. Pass
|
|
62
|
+
* `{ validate: true }` to the factory or to a call to run the model schema.
|
|
63
|
+
* A checked `fromModel` uses `parse`. The default restores an entity.
|
|
64
|
+
*
|
|
65
|
+
* ```ts
|
|
66
|
+
* class MongoUser extends Mapper('mongo.User', User, mongoUserSchema) {
|
|
67
|
+
* protected fromSource(user: User) {
|
|
68
|
+
* const props = user.toProps()
|
|
69
|
+
* const [lat, lng] = props.location
|
|
70
|
+
* return {
|
|
71
|
+
* _id: props.id,
|
|
72
|
+
* name: props.name,
|
|
73
|
+
* location: { type: 'Point' as const, coordinates: [lng, lat] },
|
|
74
|
+
* }
|
|
75
|
+
* }
|
|
76
|
+
*
|
|
77
|
+
* protected toSource(doc: MongoUserDoc) {
|
|
78
|
+
* const [lng, lat] = doc.location.coordinates
|
|
79
|
+
* return { id: doc._id, name: doc.name, location: [lat, lng] }
|
|
80
|
+
* }
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export function Mapper(token, source, storedSchema, options) {
|
|
85
|
+
const kind = sourceKind(token, source);
|
|
86
|
+
const stored = schemaDefinition(storedSchema);
|
|
87
|
+
const check = options?.validate === true;
|
|
88
|
+
class Runtime extends MapperClass {
|
|
89
|
+
static token = token;
|
|
90
|
+
static schema = stored;
|
|
91
|
+
static source = source;
|
|
92
|
+
static [sourceKindKey] = kind;
|
|
93
|
+
static [validateKey] = check;
|
|
94
|
+
constructor() {
|
|
95
|
+
super(token);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return Runtime;
|
|
99
|
+
}
|
|
100
|
+
function checking(ctor, options) {
|
|
101
|
+
return options?.validate ?? ctor[validateKey];
|
|
102
|
+
}
|
|
103
|
+
function modelOutput(ctor, value) {
|
|
104
|
+
const parsed = validate(ctor.schema, value);
|
|
105
|
+
if (!parsed.ok) {
|
|
106
|
+
throw validationError(`hexok: ${ctor.token} validation failed`, parsed.issues);
|
|
107
|
+
}
|
|
108
|
+
return parsed.value;
|
|
109
|
+
}
|
|
110
|
+
function runtimeOf(instance) {
|
|
111
|
+
return instance.constructor;
|
|
112
|
+
}
|
|
113
|
+
function sourceKind(token, source) {
|
|
114
|
+
if (isObjectLike(source)) {
|
|
115
|
+
if (isEntitySource(source))
|
|
116
|
+
return 'entity';
|
|
117
|
+
if (isSchemaClassSource(source))
|
|
118
|
+
return 'schema';
|
|
119
|
+
if (isStandardSchema(source))
|
|
120
|
+
return 'raw';
|
|
121
|
+
}
|
|
122
|
+
throw new Error(`hexok: ${token} source is not an entity or a schema`);
|
|
123
|
+
}
|
|
124
|
+
function isObjectLike(value) {
|
|
125
|
+
return ((typeof value === 'object' && value !== null) || typeof value === 'function');
|
|
126
|
+
}
|
|
127
|
+
function isStandardSchema(value) {
|
|
128
|
+
if (typeof value !== 'object' || value === null || !('~standard' in value)) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
const standard = value['~standard'];
|
|
132
|
+
return typeof standard === 'object' && standard !== null;
|
|
133
|
+
}
|
|
134
|
+
function isEntitySource(value) {
|
|
135
|
+
if (!('create' in value) ||
|
|
136
|
+
!('restore' in value) ||
|
|
137
|
+
!('parse' in value) ||
|
|
138
|
+
!('schema' in value)) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
return (typeof value.create === 'function' &&
|
|
142
|
+
typeof value.restore === 'function' &&
|
|
143
|
+
typeof value.parse === 'function' &&
|
|
144
|
+
isStandardSchema(value.schema));
|
|
145
|
+
}
|
|
146
|
+
function isSchemaClassSource(value) {
|
|
147
|
+
if (!('definition' in value) || !('parse' in value))
|
|
148
|
+
return false;
|
|
149
|
+
return (typeof value.parse === 'function' && isStandardSchema(value.definition));
|
|
150
|
+
}
|
|
151
|
+
function buildSource(kind, source, token, draft, check) {
|
|
152
|
+
if (!check) {
|
|
153
|
+
if (kind === 'entity') {
|
|
154
|
+
return source.restore(draft);
|
|
155
|
+
}
|
|
156
|
+
return draft;
|
|
157
|
+
}
|
|
158
|
+
if (kind === 'raw') {
|
|
159
|
+
const parsed = validate(source, draft);
|
|
160
|
+
if (!parsed.ok) {
|
|
161
|
+
throw validationError(`hexok: ${token} validation failed`, parsed.issues);
|
|
162
|
+
}
|
|
163
|
+
return parsed.value;
|
|
164
|
+
}
|
|
165
|
+
return source.parse(draft);
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=mapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mapper.js","sourceRoot":"","sources":["../src/mapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAIL,gBAAgB,GACjB,MAAM,aAAa,CAAC;AAErB,OAA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
|
1
|
+
/**
|
|
2
|
+
* A port is the contract an adapter implements.
|
|
3
|
+
* Extend the returned class and declare the abstract methods of the seam.
|
|
4
|
+
* Each call has its own private brand, so a structural lookalike is not a port.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* abstract class UserRepository extends Port('UserRepository') {
|
|
8
|
+
* abstract get(id: string): Promise<User | null>
|
|
9
|
+
* abstract save(user: User): Promise<void>
|
|
10
|
+
* }
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare function Port<const Token extends string>(token: Token): (abstract new () => {
|
|
14
|
+
/** Nominal marker. Each `Port(...)` call is a distinct class. */
|
|
15
|
+
readonly "__#private@#brand": true;
|
|
16
|
+
}) & {
|
|
17
|
+
/** Port name. Literal type of the string passed to {@link Port}. */
|
|
18
|
+
readonly token: Token;
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=port.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"port.d.ts","sourceRoot":"","sources":["../src/port.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,wBAAgB,IAAI,CAAC,KAAK,CAAC,KAAK,SAAS,MAAM,EAAE,KAAK,EAAE,KAAK;IAIzD,iEAAiE;kCAC/C,IAAI;;IAHtB,oEAAoE;oBAC7C,KAAK;EAU/B"}
|
package/dist/port.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A port is the contract an adapter implements.
|
|
3
|
+
* Extend the returned class and declare the abstract methods of the seam.
|
|
4
|
+
* Each call has its own private brand, so a structural lookalike is not a port.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* abstract class UserRepository extends Port('UserRepository') {
|
|
8
|
+
* abstract get(id: string): Promise<User | null>
|
|
9
|
+
* abstract save(user: User): Promise<void>
|
|
10
|
+
* }
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export function Port(token) {
|
|
14
|
+
class PortClass {
|
|
15
|
+
/** Port name. Literal type of the string passed to {@link Port}. */
|
|
16
|
+
static token = token;
|
|
17
|
+
/** Nominal marker. Each `Port(...)` call is a distinct class. */
|
|
18
|
+
#brand = true;
|
|
19
|
+
constructor() {
|
|
20
|
+
void this.#brand;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return PortClass;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=port.js.map
|
package/dist/port.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"port.js","sourceRoot":"","sources":["../src/port.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,IAAI,CAA6B,KAAY;IAC3D,MAAe,SAAS;QACtB,oEAAoE;QACpE,MAAM,CAAU,KAAK,GAAU,KAAK,CAAC;QACrC,iEAAiE;QACxD,MAAM,GAAG,IAAI,CAAC;QAEvB;YACE,KAAK,IAAI,CAAC,MAAM,CAAC;QACnB,CAAC;;IAGH,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAE7D;;;;;;;;GAQG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,GAAG,MAAM,IAC3C;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GACtB;IACE,EAAE,EAAE,KAAK,CAAC;IACV,IAAI,EAAE,CAAC,CAAC;IACR,MAAM,CAAC,EAAE,SAAS,gBAAgB,CAAC,KAAK,EAAE,CAAC;CAC5C,CAAC;AAEN,2FAA2F;AAC3F,wBAAgB,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,CAEhD;AAED,sGAAsG;AACtG,wBAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,EACnC,IAAI,EAAE,CAAC,EACP,MAAM,CAAC,EAAE,SAAS,gBAAgB,CAAC,KAAK,EAAE,GACzC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAIlB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result.js","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAmBA,2FAA2F;AAC3F,MAAM,UAAU,EAAE,CAAI,KAAQ;IAC5B,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AAC7B,CAAC;AAED,sGAAsG;AACtG,MAAM,UAAU,IAAI,CAClB,IAAO,EACP,MAA0C;IAE1C,OAAO,MAAM,KAAK,SAAS;QACzB,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE;QAC7B,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC1B,CAAC"}
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from './standard-schema.js';
|
|
2
|
+
import { type Infer } from './validate.js';
|
|
3
|
+
/** A Standard Schema value, or a {@link Schema} class that wraps one. */
|
|
4
|
+
export type SchemaSource = StandardSchemaV1 | {
|
|
5
|
+
readonly definition: StandardSchemaV1;
|
|
6
|
+
};
|
|
7
|
+
/** The Standard Schema inside `S`, or `S` itself when it already is one. */
|
|
8
|
+
export type ResolvedSchema<S> = S extends {
|
|
9
|
+
readonly definition: infer D extends StandardSchemaV1;
|
|
10
|
+
} ? D : S extends StandardSchemaV1 ? S : never;
|
|
11
|
+
/** Output of a schema value or a Schema class. */
|
|
12
|
+
export type InferSchema<S extends SchemaSource> = Infer<ResolvedSchema<S>>;
|
|
13
|
+
/** Read the Standard Schema from a schema value or a Schema class. */
|
|
14
|
+
export declare function schemaDefinition<S extends SchemaSource>(schema: S): ResolvedSchema<S>;
|
|
15
|
+
/**
|
|
16
|
+
* Named schema. The token and the Standard Schema are arguments, so neither
|
|
17
|
+
* can be omitted. Override `parse` or `check` to customize.
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* class UserSchema extends Schema('User', z.object({ id: z.string() })) {}
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function Schema<const Token extends string, S extends StandardSchemaV1>(token: Token, definition: S): (abstract new () => {}) & {
|
|
24
|
+
/** Schema name. Literal type of the string passed to {@link Schema}. */
|
|
25
|
+
readonly token: Token;
|
|
26
|
+
/** The Standard Schema this class wraps. */
|
|
27
|
+
readonly definition: S;
|
|
28
|
+
/**
|
|
29
|
+
* Validate `value` and return the schema output.
|
|
30
|
+
* Throws `CodedError` `VALIDATION` when the schema rejects it.
|
|
31
|
+
* Override to preprocess or postprocess.
|
|
32
|
+
*/
|
|
33
|
+
parse(value: unknown): Infer<S>;
|
|
34
|
+
/** Same as {@link parse}, but returns the schema output for overrides that call `super`. */
|
|
35
|
+
check(value: unknown): Infer<S>;
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,EAAE,KAAK,KAAK,EAAY,MAAM,eAAe,CAAC;AAErD,yEAAyE;AACzE,MAAM,MAAM,YAAY,GACpB,gBAAgB,GAChB;IAAE,QAAQ,CAAC,UAAU,EAAE,gBAAgB,CAAA;CAAE,CAAC;AAE9C,4EAA4E;AAC5E,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI,CAAC,SAAS;IACxC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC,SAAS,gBAAgB,CAAC;CACvD,GACG,CAAC,GACD,CAAC,SAAS,gBAAgB,GACxB,CAAC,GACD,KAAK,CAAC;AAEZ,kDAAkD;AAClD,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,YAAY,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC;AAc3E,sEAAsE;AACtE,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,YAAY,EACrD,MAAM,EAAE,CAAC,GACR,cAAc,CAAC,CAAC,CAAC,CAGnB;AAED;;;;;;;GAOG;AACH,wBAAgB,MAAM,CAAC,KAAK,CAAC,KAAK,SAAS,MAAM,EAAE,CAAC,SAAS,gBAAgB,EAC3E,KAAK,EAAE,KAAK,EACZ,UAAU,EAAE,CAAC;IAGX,wEAAwE;oBACjD,KAAK;IAC5B,4CAA4C;yBAChB,CAAC;IAE7B;;;;OAIG;iBACiB,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC;IAItC,4FAA4F;iBACxE,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC;EAazC"}
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { validationError } from './coded-error.js';
|
|
2
|
+
import { validate } from './validate.js';
|
|
3
|
+
function isSchemaClass(value) {
|
|
4
|
+
if (!('definition' in value))
|
|
5
|
+
return false;
|
|
6
|
+
const definition = value.definition;
|
|
7
|
+
return (typeof definition === 'object' &&
|
|
8
|
+
definition !== null &&
|
|
9
|
+
'~standard' in definition);
|
|
10
|
+
}
|
|
11
|
+
/** Read the Standard Schema from a schema value or a Schema class. */
|
|
12
|
+
export function schemaDefinition(schema) {
|
|
13
|
+
if (isSchemaClass(schema))
|
|
14
|
+
return schema.definition;
|
|
15
|
+
return schema;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Named schema. The token and the Standard Schema are arguments, so neither
|
|
19
|
+
* can be omitted. Override `parse` or `check` to customize.
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* class UserSchema extends Schema('User', z.object({ id: z.string() })) {}
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function Schema(token, definition) {
|
|
26
|
+
class SchemaClass {
|
|
27
|
+
/** Schema name. Literal type of the string passed to {@link Schema}. */
|
|
28
|
+
static token = token;
|
|
29
|
+
/** The Standard Schema this class wraps. */
|
|
30
|
+
static definition = definition;
|
|
31
|
+
/**
|
|
32
|
+
* Validate `value` and return the schema output.
|
|
33
|
+
* Throws `CodedError` `VALIDATION` when the schema rejects it.
|
|
34
|
+
* Override to preprocess or postprocess.
|
|
35
|
+
*/
|
|
36
|
+
static parse(value) {
|
|
37
|
+
return SchemaClass.check(value);
|
|
38
|
+
}
|
|
39
|
+
/** Same as {@link parse}, but returns the schema output for overrides that call `super`. */
|
|
40
|
+
static check(value) {
|
|
41
|
+
const parsed = validate(definition, value);
|
|
42
|
+
if (!parsed.ok) {
|
|
43
|
+
throw validationError(`hexok: ${token} validation failed`, parsed.issues);
|
|
44
|
+
}
|
|
45
|
+
return parsed.value;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return SchemaClass;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAEnD,OAAO,EAAc,QAAQ,EAAE,MAAM,eAAe,CAAC;AAmBrD,SAAS,aAAa,CACpB,KAAmB;IAEnB,IAAI,CAAC,CAAC,YAAY,IAAI,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAC3C,MAAM,UAAU,GAAG,KAAK,CAAC,UAAU,CAAC;IACpC,OAAO,CACL,OAAO,UAAU,KAAK,QAAQ;QAC9B,UAAU,KAAK,IAAI;QACnB,WAAW,IAAI,UAAU,CAC1B,CAAC;AACJ,CAAC;AAED,sEAAsE;AACtE,MAAM,UAAU,gBAAgB,CAC9B,MAAS;IAET,IAAI,aAAa,CAAC,MAAM,CAAC;QAAE,OAAO,MAAM,CAAC,UAA+B,CAAC;IACzE,OAAO,MAA2B,CAAC;AACrC,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,MAAM,CACpB,KAAY,EACZ,UAAa;IAEb,MAAe,WAAW;QACxB,wEAAwE;QACxE,MAAM,CAAU,KAAK,GAAU,KAAK,CAAC;QACrC,4CAA4C;QAC5C,MAAM,CAAU,UAAU,GAAM,UAAU,CAAC;QAE3C;;;;WAIG;QACH,MAAM,CAAC,KAAK,CAAC,KAAc;YACzB,OAAO,WAAW,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAClC,CAAC;QAED,4FAA4F;QAC5F,MAAM,CAAC,KAAK,CAAC,KAAc;YACzB,MAAM,MAAM,GAAG,QAAQ,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;YAC3C,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC;gBACf,MAAM,eAAe,CACnB,UAAU,KAAK,oBAAoB,EACnC,MAAM,CAAC,MAAM,CACd,CAAC;YACJ,CAAC;YACD,OAAO,MAAM,CAAC,KAAK,CAAC;QACtB,CAAC;;IAGH,OAAO,WAAW,CAAC;AACrB,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Vendored Standard Schema V1. Copy of https://standardschema.dev
|
|
3
|
-
* so
|
|
3
|
+
* so hexok has zero runtime dependencies.
|
|
4
4
|
*/
|
|
5
5
|
export interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
6
6
|
/** Spec bag: version, vendor, validate, optional inferred types. */
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard-schema.d.ts","sourceRoot":"","sources":["../src/standard-schema.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,gBAAgB,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK;IAC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