mobx-keystone 2.3.1 → 2.4.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.
Files changed (51) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/mobx-keystone.esm.js +5157 -3981
  3. package/dist/mobx-keystone.esm.mjs +5157 -3981
  4. package/dist/mobx-keystone.umd.js +5170 -3980
  5. package/dist/types/action/CallKind.d.ts +29 -0
  6. package/dist/types/action/applyAction.d.ts +7 -1
  7. package/dist/types/action/index.d.ts +1 -0
  8. package/dist/types/action/resolveCallName.d.ts +1 -0
  9. package/dist/types/actionMiddlewares/actionSerialization/actionSerialization.d.ts +14 -0
  10. package/dist/types/actionMiddlewares/actionSerialization/applySerializedAction.d.ts +6 -0
  11. package/dist/types/actionMiddlewares/actionSerialization/core.d.ts +7 -0
  12. package/dist/types/index.d.ts +1 -0
  13. package/dist/types/patch/emitPatch.d.ts +6 -0
  14. package/dist/types/serialization/Call.d.ts +55 -0
  15. package/dist/types/serialization/CallError.d.ts +96 -0
  16. package/dist/types/serialization/JsonValue.d.ts +6 -0
  17. package/dist/types/serialization/Serializer.d.ts +84 -0
  18. package/dist/types/serialization/ValueSerializer.d.ts +88 -0
  19. package/dist/types/serialization/applyCall.d.ts +54 -0
  20. package/dist/types/serialization/builtinSerializers.d.ts +49 -0
  21. package/dist/types/serialization/codec.d.ts +1 -0
  22. package/dist/types/serialization/index.d.ts +8 -0
  23. package/dist/types/serialization/references.d.ts +74 -0
  24. package/dist/types/serialization/resolvedCall.d.ts +1 -0
  25. package/package.json +1 -1
  26. package/src/action/CallKind.ts +29 -0
  27. package/src/action/applyAction.ts +22 -49
  28. package/src/action/applySet.ts +21 -1
  29. package/src/action/index.ts +1 -0
  30. package/src/action/resolveCallName.ts +207 -0
  31. package/src/actionMiddlewares/actionSerialization/actionSerialization.ts +14 -0
  32. package/src/actionMiddlewares/actionSerialization/applySerializedAction.ts +6 -0
  33. package/src/actionMiddlewares/actionSerialization/core.ts +7 -0
  34. package/src/actionMiddlewares/actionSerialization/index.ts +2 -0
  35. package/src/actionMiddlewares/actionSerialization/objectPathSerializer.ts +3 -2
  36. package/src/index.ts +1 -0
  37. package/src/parent/path.ts +45 -12
  38. package/src/patch/emitPatch.ts +6 -0
  39. package/src/serialization/Call.ts +58 -0
  40. package/src/serialization/CallError.ts +153 -0
  41. package/src/serialization/JsonValue.ts +12 -0
  42. package/src/serialization/Serializer.ts +321 -0
  43. package/src/serialization/ValueSerializer.ts +98 -0
  44. package/src/serialization/applyCall.ts +190 -0
  45. package/src/serialization/builtinSerializers.ts +261 -0
  46. package/src/serialization/codec.ts +338 -0
  47. package/src/serialization/index.ts +15 -0
  48. package/src/serialization/references.ts +225 -0
  49. package/src/serialization/resolvedCall.ts +169 -0
  50. package/src/standardActions/actions.ts +11 -3
  51. package/src/utils/errorDiagnostics.ts +9 -4
@@ -0,0 +1,29 @@
1
+ /**
2
+ * What a call's name resolved to on its target.
3
+ */
4
+ export declare enum CallKind {
5
+ /**
6
+ * An action declared on a class: a model's `@modelAction` / `@modelFlow` (including
7
+ * `withSetter()` setters), or a data model's action.
8
+ */
9
+ ModelAction = "modelAction",
10
+ /**
11
+ * An action the app defines with `standaloneAction` / `standaloneFlow`, under a global name.
12
+ */
13
+ StandaloneAction = "standaloneAction",
14
+ /**
15
+ * A standalone action the library ships for editing any array or plain object
16
+ * (`arrayActions`, `objectActions`).
17
+ */
18
+ StandardAction = "standardAction",
19
+ /**
20
+ * A built-in action, as library functions record when they change a node: `$$applySnapshot`,
21
+ * `$$applyPatches`, `$$detach`, `$$applySet`, `$$applyDelete`, `$$applyMethodCall` (see
22
+ * `BuiltInAction`).
23
+ */
24
+ BuiltInAction = "builtInAction",
25
+ /**
26
+ * Not an action: a method, getter or function field of a model's or data model's own classes.
27
+ */
28
+ View = "view"
29
+ }
@@ -21,13 +21,19 @@ export interface ActionCall {
21
21
  readonly targetPathIds: ReadonlyArray<string | null>;
22
22
  /**
23
23
  * Marks this action call as non-serialized.
24
+ *
25
+ * @deprecated Not needed with `createSerializer`: a `Call` is never serialized, an
26
+ * `EncodedCall` always is.
24
27
  */
25
28
  readonly serialized?: boolean;
26
29
  }
27
30
  /**
28
31
  * Applies (runs) an action over a target object.
29
32
  *
30
- * If you intend to apply serialized actions check one of the `applySerializedAction` methods instead.
33
+ * It runs any kind of action (built-in, standalone, model actions and flows), but not views.
34
+ * To apply calls that come from outside (e.g. as JSON), decode them with a serializer
35
+ * (`defaultSerializer.decodeCall`) and use `applyCall` / `applyCallAsync` instead, which check
36
+ * what may be called.
31
37
  *
32
38
  * @param subtreeRoot Subtree root target object to run the action over.
33
39
  * @param call The action, usually as coming from `onActionMiddleware`.
@@ -3,6 +3,7 @@ export * from "./applyDelete";
3
3
  export * from "./applyMethodCall";
4
4
  export * from "./applySet";
5
5
  export * from "./builtInActions";
6
+ export * from "./CallKind";
6
7
  export * from "./context";
7
8
  export * from "./hookActions";
8
9
  export * from "./isModelAction";
@@ -0,0 +1 @@
1
+ export {};
@@ -8,10 +8,14 @@ import { type ActionCallArgumentSerializer } from "./core";
8
8
  *
9
9
  * @param serializer Serializer to register.
10
10
  * @returns A disposer to unregister the serializer.
11
+ *
12
+ * @deprecated Use a `ValueSerializer` in the list passed to `createSerializer` instead.
11
13
  */
12
14
  export declare function registerActionCallArgumentSerializer(serializer: ActionCallArgumentSerializer<any, any>): () => void;
13
15
  /**
14
16
  * Serialized action call argument.
17
+ *
18
+ * @deprecated Replaced by the marked value `{ "$mobxKeystone": tag, "v": payload }` of `createSerializer`.
15
19
  */
16
20
  export interface SerializedActionCallArgument {
17
21
  /**
@@ -25,6 +29,8 @@ export interface SerializedActionCallArgument {
25
29
  }
26
30
  /**
27
31
  * A serialized action call.
32
+ *
33
+ * @deprecated Use `EncodedCall` instead.
28
34
  */
29
35
  export interface SerializedActionCall extends Omit<ActionCall, "serialized"> {
30
36
  /**
@@ -54,6 +60,8 @@ export interface SerializedActionCall extends Omit<ActionCall, "serialized"> {
54
60
  * @param argValue Argument value to be transformed into its serializable form.
55
61
  * @param [targetRoot] Target root node of the model where this action is being performed.
56
62
  * @returns The serializable form of the passed value.
63
+ *
64
+ * @deprecated Use `serializer.encodeValue(root, value)` instead (e.g. with `defaultSerializer`).
57
65
  */
58
66
  export declare function serializeActionCallArgument(argValue: any, targetRoot?: object): SerializedActionCallArgument | JSONPrimitiveValue;
59
67
  /**
@@ -63,6 +71,8 @@ export declare function serializeActionCallArgument(argValue: any, targetRoot?:
63
71
  * @param actionCall Action call to convert.
64
72
  * @param [targetRoot] Target root node of the model where this action is being performed.
65
73
  * @returns The serializable action call.
74
+ *
75
+ * @deprecated Use `serializer.encodeCall(root, call)` instead (e.g. with `defaultSerializer`).
66
76
  */
67
77
  export declare function serializeActionCall(actionCall: ActionCall, targetRoot?: object): SerializedActionCall;
68
78
  /**
@@ -71,6 +81,8 @@ export declare function serializeActionCall(actionCall: ActionCall, targetRoot?:
71
81
  * @param argValue Argument value to be transformed into its deserialized form.
72
82
  * @param [targetRoot] Target root node of the model where this action is being performed.
73
83
  * @returns The deserialized form of the passed value.
84
+ *
85
+ * @deprecated Use `serializer.decodeValue(root, json)` instead (e.g. with `defaultSerializer`).
74
86
  */
75
87
  export declare function deserializeActionCallArgument(argValue: SerializedActionCallArgument | JSONPrimitiveValue, targetRoot?: object): any;
76
88
  /**
@@ -80,5 +92,7 @@ export declare function deserializeActionCallArgument(argValue: SerializedAction
80
92
  * @param actionCall Action call to convert.
81
93
  * @param [targetRoot] Target root node of the model where this action is being performed.
82
94
  * @returns The deserialized action call.
95
+ *
96
+ * @deprecated Use `serializer.decodeCall(root, json)` instead (e.g. with `defaultSerializer`), then `applyCall`.
83
97
  */
84
98
  export declare function deserializeActionCall(actionCall: SerializedActionCall, targetRoot?: object): ActionCall;
@@ -4,6 +4,8 @@ import { type SerializedActionCall } from "./actionSerialization";
4
4
  * Serialized action call with model ID overrides.
5
5
  * Can be generated with `applySerializedActionAndTrackNewModelIds`.
6
6
  * To be applied with `applySerializedActionAndSyncNewModelIds`.
7
+ *
8
+ * @deprecated Use `EncodedCall` instead; model id overrides aren't needed when replicas apply the patches the server broadcasts.
7
9
  */
8
10
  export interface SerializedActionCallWithModelIdOverrides extends SerializedActionCall {
9
11
  /**
@@ -24,6 +26,8 @@ export interface SerializedActionCallWithModelIdOverrides extends SerializedActi
24
26
  * @param call The serialized action, usually as coming from the server/client.
25
27
  * @returns The return value of the action, if any, plus a new serialized action
26
28
  * with model overrides.
29
+ *
30
+ * @deprecated Use `applyCall` / `applyCallAsync` on the server instead, broadcasting the patches the call produces.
27
31
  */
28
32
  export declare function applySerializedActionAndTrackNewModelIds<TRet = any>(subtreeRoot: object, call: SerializedActionCall): {
29
33
  returnValue: TRet;
@@ -41,5 +45,7 @@ export declare function applySerializedActionAndTrackNewModelIds<TRet = any>(sub
41
45
  * @param subtreeRoot Subtree root target object to run the action over.
42
46
  * @param call The serialized action, usually as coming from the server/client.
43
47
  * @returns The return value of the action, if any.
48
+ *
49
+ * @deprecated Use `applyPatches` on the patches the server broadcasts instead.
44
50
  */
45
51
  export declare function applySerializedActionAndSyncNewModelIds<TRet = any>(subtreeRoot: object, call: SerializedActionCallWithModelIdOverrides): TRet;
@@ -1,6 +1,13 @@
1
+ /**
2
+ * Returned by an `ActionCallArgumentSerializer` that can't serialize a value.
3
+ *
4
+ * @deprecated Use `ValueSerializer.is` returning false instead.
5
+ */
1
6
  export declare const cannotSerialize: unique symbol;
2
7
  /**
3
8
  * Serializer of action call arguments.
9
+ *
10
+ * @deprecated Use `ValueSerializer` instead.
4
11
  */
5
12
  export interface ActionCallArgumentSerializer<TOriginal, TSerialized> {
6
13
  /**
@@ -13,6 +13,7 @@ export * from "./patch";
13
13
  export * from "./redux";
14
14
  export * from "./ref";
15
15
  export * from "./rootStore";
16
+ export * from "./serialization";
16
17
  export * from "./snapshot";
17
18
  export * from "./standardActions";
18
19
  export * from "./transforms";
@@ -14,6 +14,9 @@ export type OnPatchesDisposer = () => void;
14
14
  /**
15
15
  * Adds a listener that will be called every time a patch is generated for the tree of the given target object.
16
16
  *
17
+ * The listener runs inside the action that made the change, so `getCurrentActionContext()` returns
18
+ * that action's context: its `rootContext` is the top-level action (for a flow step, the flow).
19
+ *
17
20
  * @param subtreeRoot Subtree root object of the patch listener.
18
21
  * @param listener The listener function that will be called everytime a patch is generated for the object or its children.
19
22
  * @returns A disposer to stop listening to patches.
@@ -23,6 +26,9 @@ export declare function onPatches(subtreeRoot: object, listener: OnPatchesListen
23
26
  * Adds a listener that will be called every time a patch is generated anywhere.
24
27
  * Usually prefer using `onPatches`.
25
28
  *
29
+ * The listener runs inside the action that made the change, so `getCurrentActionContext()` returns
30
+ * that action's context: its `rootContext` is the top-level action (for a flow step, the flow).
31
+ *
26
32
  * @param listener The listener function that will be called everytime a patch is generated anywhere.
27
33
  * @returns A disposer to stop listening to patches.
28
34
  */
@@ -0,0 +1,55 @@
1
+ import type { CallKind } from "../action/CallKind";
2
+ import type { JsonValue } from "./JsonValue";
3
+ /**
4
+ * A call as JSON, as `Serializer.encodeCall` returns it and `Serializer.decodeCall` takes it.
5
+ */
6
+ export interface EncodedCall {
7
+ /**
8
+ * A reference to the node the call runs on; for a standalone action, its first argument.
9
+ */
10
+ target: JsonValue;
11
+ /**
12
+ * Name of the action or view.
13
+ */
14
+ name: string;
15
+ /**
16
+ * Encoded arguments; left out to read a getter.
17
+ */
18
+ args?: JsonValue[];
19
+ }
20
+ /**
21
+ * A call to an action or a view of a node.
22
+ */
23
+ export interface Call {
24
+ /**
25
+ * The node the call runs on; for a standalone action, its first argument.
26
+ */
27
+ target: object;
28
+ /**
29
+ * Name of the action or view.
30
+ */
31
+ name: string;
32
+ /**
33
+ * Arguments (for a standalone action, without the target); left out to read a getter.
34
+ */
35
+ args?: unknown[];
36
+ }
37
+ /**
38
+ * A call whose target and name are resolved, as `Serializer.decodeCall` returns it and `canCall`
39
+ * gets it.
40
+ */
41
+ export interface ResolvedCall extends Readonly<Call> {
42
+ /**
43
+ * What `name` resolved to on the target.
44
+ */
45
+ readonly kind: CallKind;
46
+ /**
47
+ * Whether it is a `@modelFlow` or a `standaloneFlow`, which needs `applyCallAsync`.
48
+ */
49
+ readonly isFlow: boolean;
50
+ /**
51
+ * The arguments as JSON, only on a call returned by `decodeCall`. Reading `args` decodes them
52
+ * (once).
53
+ */
54
+ readonly rawArgs?: JsonValue[];
55
+ }
@@ -0,0 +1,96 @@
1
+ import type { Path } from "../parent/pathTypes";
2
+ import { MobxKeystoneError } from "../utils";
3
+ /**
4
+ * The reason a call failed.
5
+ */
6
+ export declare enum CallErrorCode {
7
+ /**
8
+ * The target, or a reference in the arguments, doesn't resolve.
9
+ */
10
+ NotFound = "notFound",
11
+ /**
12
+ * Nothing by that name can be called on the target.
13
+ */
14
+ UnknownName = "unknownName",
15
+ /**
16
+ * The name exists, but can't be used as asked (e.g. a method read without `args`).
17
+ */
18
+ WrongKind = "wrongKind",
19
+ /**
20
+ * Refused by `canCall`.
21
+ */
22
+ Forbidden = "forbidden",
23
+ /**
24
+ * An argument (or any other value being decoded) failed to decode.
25
+ */
26
+ BadArgument = "badArgument",
27
+ /**
28
+ * A value (usually the result) can't be encoded.
29
+ */
30
+ CannotEncode = "cannotEncode"
31
+ }
32
+ /**
33
+ * What a `CallError` is built from, e.g. by a custom `ValueSerializer` that wants to choose the
34
+ * code (a `CallError` it throws passes through as it is; any other error is wrapped).
35
+ */
36
+ export interface CallErrorData {
37
+ /**
38
+ * The reason.
39
+ */
40
+ code: CallErrorCode;
41
+ /**
42
+ * The message, without the path and value preview the error adds to it.
43
+ */
44
+ message: string;
45
+ /**
46
+ * Path relative to the call or value, e.g. `["args", 1, "items", 3]`.
47
+ * Defaults to where the value being encoded or decoded is.
48
+ */
49
+ path?: Path;
50
+ /**
51
+ * The value the error is about, shown in the message.
52
+ */
53
+ previewValue?: unknown;
54
+ /**
55
+ * The error that caused it, if any. Its message is added to `message`, but left out of
56
+ * `toJSON()`, since it may hold the server's data.
57
+ */
58
+ cause?: unknown;
59
+ }
60
+ /**
61
+ * The JSON form of a `CallError`, safe to send back to the caller.
62
+ */
63
+ export interface CallErrorJson {
64
+ code: CallErrorCode;
65
+ message: string;
66
+ path: Path;
67
+ }
68
+ /**
69
+ * Thrown when a call or a value can't be decoded, resolved, allowed, run or encoded
70
+ * (extends `MobxKeystoneError`). Errors thrown by the action or view being called are not
71
+ * `CallError`s: they are rethrown as they are.
72
+ *
73
+ * Use `instanceof CallError` to distinguish call errors from other
74
+ * `MobxKeystoneError` instances.
75
+ */
76
+ export declare class CallError extends MobxKeystoneError {
77
+ /**
78
+ * The reason.
79
+ */
80
+ readonly code: CallErrorCode;
81
+ /**
82
+ * Where in the call or value the error is, e.g. `["args", 1]`.
83
+ */
84
+ readonly path: Path;
85
+ /**
86
+ * The error that caused it, if any.
87
+ */
88
+ readonly cause?: unknown;
89
+ private readonly jsonMessage;
90
+ constructor(data: CallErrorData);
91
+ /**
92
+ * The error as JSON, for RPC and tool layers. It leaves out the cause's message, and the value
93
+ * preview of a `CannotEncode` error, since they may hold the server's data.
94
+ */
95
+ toJSON(): CallErrorJson;
96
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * A JSON value: what `JSON.parse` can return.
3
+ */
4
+ export type JsonValue = null | boolean | number | string | JsonValue[] | {
5
+ [key: string]: JsonValue;
6
+ };
@@ -0,0 +1,84 @@
1
+ import type { ActionCall } from "../action/applyAction";
2
+ import type { Call, EncodedCall, ResolvedCall } from "./Call";
3
+ import type { JsonValue } from "./JsonValue";
4
+ import type { ValueSerializer } from "./ValueSerializer";
5
+ /**
6
+ * Encodes and decodes values and calls against a tree, with a list of value serializers.
7
+ * Created with `createSerializer`.
8
+ *
9
+ * Every operation takes the root of the tree as its first argument; it must be a tree root.
10
+ */
11
+ export interface Serializer {
12
+ /**
13
+ * The list of value serializers it was created with, without the built-ins.
14
+ */
15
+ readonly serializers: readonly ValueSerializer[];
16
+ /**
17
+ * Encodes a value as JSON. Nodes of the tree go as references when a reference serializer in
18
+ * the list claims them.
19
+ *
20
+ * @param root Root of the tree.
21
+ * @param value Value to encode.
22
+ * @returns The encoded value.
23
+ */
24
+ encodeValue(root: object, value: unknown): JsonValue;
25
+ /**
26
+ * Decodes a value encoded with `encodeValue`. References are resolved against the tree;
27
+ * snapshots become new instances.
28
+ *
29
+ * @param root Root of the tree.
30
+ * @param json Value to decode.
31
+ * @returns The decoded value.
32
+ */
33
+ decodeValue(root: object, json: JsonValue): unknown;
34
+ /**
35
+ * Encodes a call (an `ActionCall`, e.g. from `onActionMiddleware`'s `onStart`, or a `Call`)
36
+ * as JSON. The target always goes as a reference; for a standalone action, it isn't repeated
37
+ * in the arguments.
38
+ *
39
+ * Encode an `ActionCall` before the action runs (e.g. in `onStart`), so its arguments are
40
+ * encoded against the tree the receiver has.
41
+ *
42
+ * @param root Root of the tree.
43
+ * @param call The call.
44
+ * @returns The encoded call.
45
+ */
46
+ encodeCall(root: object, call: ActionCall | Call): EncodedCall;
47
+ /**
48
+ * Decodes a call encoded with `encodeCall`: the target is resolved and the name resolved on
49
+ * it, nothing is run. The arguments are decoded when `args` is first read.
50
+ *
51
+ * @param root Root of the tree.
52
+ * @param json The encoded call.
53
+ * @returns The resolved call, to pass to `applyCall` or `applyCallAsync`.
54
+ */
55
+ decodeCall(root: object, json: EncodedCall | JsonValue): ResolvedCall;
56
+ }
57
+ /**
58
+ * Options of `createSerializer`.
59
+ */
60
+ export interface SerializerOptions {
61
+ /**
62
+ * Value serializers, tried in order before the built-ins when encoding.
63
+ */
64
+ serializers?: readonly ValueSerializer[];
65
+ /**
66
+ * Whether to append the built-in serializers (`builtinSerializers`) to the list.
67
+ * Defaults to `true`.
68
+ */
69
+ builtins?: boolean;
70
+ }
71
+ /**
72
+ * Creates a serializer from a list of value serializers. When encoding, a value goes through the
73
+ * first serializer whose `is` accepts it, then the built-ins (unless `builtins` is `false`),
74
+ * then as plain JSON; decoding picks the serializer by tag.
75
+ *
76
+ * @param options Options.
77
+ * @returns The serializer.
78
+ */
79
+ export declare function createSerializer(options?: SerializerOptions): Serializer;
80
+ /**
81
+ * The default serializer: nodes of the tree as path references (`pathRefs()`, value-type models
82
+ * excepted), plus the built-ins.
83
+ */
84
+ export declare const defaultSerializer: Serializer;
@@ -0,0 +1,88 @@
1
+ import type { PathElement } from "../parent/pathTypes";
2
+ import type { JsonValue } from "./JsonValue";
3
+ /**
4
+ * The key that marks a special value in the encoded JSON:
5
+ * `{ "$mobxKeystone": <tag>, "v": <payload> }`.
6
+ */
7
+ export declare const serializationMarkerKey = "$mobxKeystone";
8
+ /**
9
+ * Context passed to `ValueSerializer.is` and `ValueSerializer.encode`.
10
+ */
11
+ export interface EncodeContext {
12
+ /**
13
+ * Root of the tree the value is encoded against.
14
+ */
15
+ readonly root: object;
16
+ /**
17
+ * Encodes a nested value with the same serializer.
18
+ *
19
+ * @param value Value to encode.
20
+ * @param key Where the value is inside its parent, for error paths.
21
+ * @returns The encoded value.
22
+ */
23
+ encode(value: unknown, key?: PathElement): JsonValue;
24
+ }
25
+ /**
26
+ * Context passed to `ValueSerializer.decode`.
27
+ */
28
+ export interface DecodeContext {
29
+ /**
30
+ * Root of the tree the value is decoded against.
31
+ */
32
+ readonly root: object;
33
+ /**
34
+ * Decodes a nested value with the same serializer.
35
+ *
36
+ * @param json Value to decode.
37
+ * @param key Where the value is inside its parent, for error paths.
38
+ * @returns The decoded value.
39
+ */
40
+ decode(json: JsonValue, key?: PathElement): unknown;
41
+ }
42
+ /**
43
+ * Handles one kind of value: encodes it as `{ "$mobxKeystone": tag, "v": payload }` and decodes
44
+ * it back. `createSerializer` composes a list of them into a `Serializer`.
45
+ *
46
+ * @template T Type of the values it handles.
47
+ * @template J Type of the encoded payload (`undefined` to carry none).
48
+ */
49
+ export interface ValueSerializer<T = any, J extends JsonValue | undefined = any> {
50
+ /**
51
+ * Unique, e.g. `"geom/Polygon"`; the `$mobxKeystone` of the encoded value.
52
+ */
53
+ readonly tag: string;
54
+ /**
55
+ * Whether this serializer encodes `value`.
56
+ *
57
+ * @param value Value to check.
58
+ * @param ctx Encoding context; `ctx.root` is the tree the value is encoded against.
59
+ */
60
+ is(value: unknown, ctx: EncodeContext): value is T;
61
+ /**
62
+ * Encodes a value `is` accepted. Use `ctx.encode(child)` for nested values.
63
+ *
64
+ * @param value Value to encode.
65
+ * @param ctx Encoding context.
66
+ * @returns The payload (`v`), or `undefined` to carry none.
67
+ */
68
+ encode(value: T, ctx: EncodeContext): J;
69
+ /**
70
+ * Decodes a payload back into a value. Use `ctx.decode(child)` for nested values.
71
+ *
72
+ * @param json The payload (`v`).
73
+ * @param ctx Decoding context.
74
+ * @returns The decoded value.
75
+ */
76
+ decode(json: J, ctx: DecodeContext): T;
77
+ /**
78
+ * Only for reference serializers: whether this serializer references `value`, a node of the
79
+ * tree, when it is a call's target. Unlike `is`, it ignores options such as `when`, since the
80
+ * call must run on the node itself. A call's target goes through the first serializer whose
81
+ * `claimsTarget` accepts it (else as a `pathRef`), and only serializers with `claimsTarget`
82
+ * decode it.
83
+ *
84
+ * @param value The call's target.
85
+ * @param ctx Encoding context.
86
+ */
87
+ claimsTarget?(value: object, ctx: EncodeContext): boolean;
88
+ }
@@ -0,0 +1,54 @@
1
+ import type { Call, ResolvedCall } from "./Call";
2
+ /**
3
+ * Options of `applyCall` and `applyCallAsync`.
4
+ */
5
+ export interface ApplyCallOptions {
6
+ /**
7
+ * Decides whether a call may run, after its target and name are resolved and before anything
8
+ * runs. Returning false throws a `CallError` with code `Forbidden`; an error it throws passes
9
+ * through as it is. Reading `call.args` decodes the arguments (once).
10
+ *
11
+ * Defaults to `allowModelActions`.
12
+ */
13
+ canCall?(call: ResolvedCall): boolean;
14
+ }
15
+ /**
16
+ * The default `canCall`: allows model actions (`CallKind.ModelAction`) only.
17
+ *
18
+ * @param call The call.
19
+ * @returns Whether it is a model action.
20
+ */
21
+ export declare function allowModelActions(call: ResolvedCall): boolean;
22
+ /**
23
+ * A `canCall` that allows every call, for calls from a source that is trusted.
24
+ *
25
+ * @param _call The call.
26
+ * @returns `true`.
27
+ */
28
+ export declare function allowAnyCall(_call: ResolvedCall): boolean;
29
+ /**
30
+ * Runs a call (as returned by `Serializer.decodeCall`, or built in process) on a tree and returns
31
+ * its result as is. Flows are refused (`call.isFlow`); use `applyCallAsync` for them.
32
+ *
33
+ * The call's name is resolved on its target (see `CallKind`), then `canCall` decides whether it
34
+ * may run (by default only model actions may). Errors about the call are `CallError`s; errors
35
+ * thrown by the action or view are rethrown as they are.
36
+ *
37
+ * @template TRet Return type.
38
+ * @param root Root of the tree.
39
+ * @param call The call.
40
+ * @param options Options.
41
+ * @returns The result of the call.
42
+ */
43
+ export declare function applyCall<TRet = unknown>(root: object, call: Call | ResolvedCall, options?: ApplyCallOptions): TRet;
44
+ /**
45
+ * Like `applyCall`, but accepts flows too and always returns a promise, which resolves to the
46
+ * result once the call (or the flow) ends. Every error is a rejection.
47
+ *
48
+ * @template TRet Return type.
49
+ * @param root Root of the tree.
50
+ * @param call The call.
51
+ * @param options Options.
52
+ * @returns A promise of the result of the call.
53
+ */
54
+ export declare function applyCallAsync<TRet = unknown>(root: object, call: Call | ResolvedCall, options?: ApplyCallOptions): Promise<TRet>;
@@ -0,0 +1,49 @@
1
+ import type { AnyDataModel } from "../dataModel/BaseDataModel";
2
+ import type { JsonValue } from "./JsonValue";
3
+ import type { ValueSerializer } from "./ValueSerializer";
4
+ /**
5
+ * The built-in value serializers, appended in this order to every serializer's list (unless it is
6
+ * created with `builtins: false`). Pick some of them to build a list by hand, e.g.
7
+ * `createSerializer({ serializers: [builtinSerializers.date], builtins: false })`.
8
+ */
9
+ export declare const builtinSerializers: Readonly<{
10
+ /**
11
+ * Models and frozen data that aren't references, as their snapshots (unmarked, since snapshots
12
+ * mark themselves with `$modelType` / `$frozen`), decoded with `fromSnapshot` into new
13
+ * instances. Tree arrays and plain objects that aren't references are walked like any other.
14
+ */
15
+ snapshot: ValueSerializer<object, any>;
16
+ /**
17
+ * Data model instances, as their registered name and their data (encoded, so data of the tree
18
+ * goes as a reference), decoded as a new instance over the decoded data.
19
+ */
20
+ dataModel: ValueSerializer<AnyDataModel, {
21
+ type: string;
22
+ data: JsonValue;
23
+ }>;
24
+ /**
25
+ * Dates, as `toISOString()` (an invalid date as `null`); decodes an ISO string, a timestamp or
26
+ * `null`.
27
+ */
28
+ date: ValueSerializer<Date, string | number | null>;
29
+ /**
30
+ * Maps (plain or observable), as an array of `[key, value]` entries; decoded as plain maps.
31
+ */
32
+ map: ValueSerializer<Map<unknown, unknown>, [JsonValue, JsonValue][]>;
33
+ /**
34
+ * Sets (plain or observable), as an array of their values; decoded as plain sets.
35
+ */
36
+ set: ValueSerializer<Set<unknown>, JsonValue[]>;
37
+ /**
38
+ * The numbers JSON can't hold: `"NaN"`, `"Infinity"`, `"-Infinity"`.
39
+ */
40
+ number: ValueSerializer<number, string>;
41
+ /**
42
+ * Bigints, as a decimal string, with no loss of precision.
43
+ */
44
+ bigint: ValueSerializer<bigint, string>;
45
+ /**
46
+ * `undefined` (as an array item, an argument or an object property, which keeps its key).
47
+ */
48
+ undefined: ValueSerializer<undefined, undefined>;
49
+ }>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ export * from "./applyCall";
2
+ export { builtinSerializers } from "./builtinSerializers";
3
+ export * from "./Call";
4
+ export { CallError, CallErrorCode, type CallErrorData, type CallErrorJson } from "./CallError";
5
+ export * from "./JsonValue";
6
+ export { type IdRefJson, idRefs, type PathRefJson, pathRefs, type ReferenceNodeInfo, type ReferenceSerializerOptions, } from "./references";
7
+ export * from "./Serializer";
8
+ export * from "./ValueSerializer";