@diphyx/harlemify 4.0.1 → 5.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.
Files changed (47) hide show
  1. package/README.md +30 -44
  2. package/dist/module.d.mts +5 -0
  3. package/dist/module.d.ts +5 -0
  4. package/dist/module.json +1 -1
  5. package/dist/runtime/composables/action.d.ts +16 -3
  6. package/dist/runtime/composables/action.js +47 -3
  7. package/dist/runtime/composables/model.d.ts +22 -0
  8. package/dist/runtime/composables/model.js +32 -0
  9. package/dist/runtime/composables/view.d.ts +33 -0
  10. package/dist/runtime/composables/view.js +54 -0
  11. package/dist/runtime/core/layers/action.d.ts +3 -2
  12. package/dist/runtime/core/layers/action.js +37 -69
  13. package/dist/runtime/core/layers/model.js +14 -0
  14. package/dist/runtime/core/layers/shape.d.ts +2 -2
  15. package/dist/runtime/core/layers/shape.js +3 -2
  16. package/dist/runtime/core/layers/view.d.ts +2 -2
  17. package/dist/runtime/core/layers/view.js +27 -5
  18. package/dist/runtime/core/store.d.ts +5 -23
  19. package/dist/runtime/core/store.js +8 -28
  20. package/dist/runtime/core/types/action.d.ts +79 -121
  21. package/dist/runtime/core/types/action.js +0 -16
  22. package/dist/runtime/core/types/base.d.ts +6 -0
  23. package/dist/runtime/core/types/base.js +0 -0
  24. package/dist/runtime/core/types/model.d.ts +47 -32
  25. package/dist/runtime/core/types/model.js +14 -0
  26. package/dist/runtime/core/types/shape.d.ts +30 -5
  27. package/dist/runtime/core/types/store.d.ts +14 -0
  28. package/dist/runtime/core/types/store.js +0 -0
  29. package/dist/runtime/core/types/view.d.ts +35 -24
  30. package/dist/runtime/core/types/view.js +5 -0
  31. package/dist/runtime/core/utils/action.d.ts +4 -4
  32. package/dist/runtime/core/utils/action.js +217 -207
  33. package/dist/runtime/core/utils/base.d.ts +14 -0
  34. package/dist/runtime/core/utils/base.js +109 -0
  35. package/dist/runtime/core/utils/error.d.ts +21 -0
  36. package/dist/runtime/core/utils/error.js +36 -0
  37. package/dist/runtime/core/utils/model.d.ts +3 -11
  38. package/dist/runtime/core/utils/model.js +104 -110
  39. package/dist/runtime/core/utils/shape.d.ts +6 -3
  40. package/dist/runtime/core/utils/shape.js +218 -14
  41. package/dist/runtime/core/utils/store.d.ts +8 -0
  42. package/dist/runtime/core/utils/store.js +35 -0
  43. package/dist/runtime/core/utils/view.d.ts +3 -4
  44. package/dist/runtime/core/utils/view.js +35 -14
  45. package/dist/runtime/index.d.ts +14 -5
  46. package/dist/runtime/index.js +7 -10
  47. package/package.json +2 -1
@@ -1,23 +1,5 @@
1
- import type { ComputedRef } from "vue";
2
- import type { Model, ModelFactory } from "./types/model.js";
3
- import type { ViewDefinitions, ViewResult, ViewFactory } from "./types/view.js";
4
- import { type Action, type ActionApiChain, type ActionCommitChain, type ActionCommitter, type ActionDefinition, type ActionDefinitions, type ActionFactory, type ActionHandleChain } from "./types/action.js";
5
- export type StoreModel<M extends Model> = ActionCommitter<M>;
6
- export type StoreView<M extends Model, VD extends ViewDefinitions<M>> = {
7
- readonly [K in keyof VD]: ComputedRef<ViewResult<M, VD[K]>>;
8
- };
9
- export type StoreAction<M extends Model, V, AD extends Record<string, ActionDefinition<M, V, unknown>>> = {
10
- [K in keyof AD]: Action<V>;
11
- };
12
- export interface StoreConfig<M extends Model, VD extends ViewDefinitions<M>, _AD extends ActionDefinitions<M, StoreView<M, VD>>> {
13
- name: string;
14
- model: (factory: ModelFactory) => M;
15
- view: (factory: ViewFactory<M>) => VD;
16
- action: (factory: ActionFactory<M, StoreView<M, VD>>) => Record<string, ActionApiChain<M, StoreView<M, VD>, unknown> | ActionHandleChain<M, StoreView<M, VD>, unknown> | ActionCommitChain<M, StoreView<M, VD>, unknown>>;
17
- }
18
- export interface Store<M extends Model, VD extends ViewDefinitions<M>, AD extends ActionDefinitions<M, StoreView<M, VD>>> {
19
- model: StoreModel<M>;
20
- view: StoreView<M, VD>;
21
- action: StoreAction<M, StoreView<M, VD>, AD>;
22
- }
23
- export declare function createStore<M extends Model, VD extends ViewDefinitions<M>, AD extends ActionDefinitions<M, StoreView<M, VD>>>(config: StoreConfig<M, VD, AD>): Store<M, VD, AD>;
1
+ import type { ModelDefinitions } from "./types/model.js";
2
+ import type { ViewDefinitions } from "./types/view.js";
3
+ import type { ActionDefinitions } from "./types/action.js";
4
+ import type { Store, StoreConfig } from "./types/store.js";
5
+ export declare function createStore<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>, AD extends ActionDefinitions<MD, VD>>(config: StoreConfig<MD, VD, AD>): Store<MD, VD, AD>;
@@ -1,28 +1,10 @@
1
1
  import { createConsola } from "consola";
2
- import { createStore as createSourceStore } from "@harlem/core";
2
+ import { createStore as createStoreSource } from "@harlem/core";
3
3
  import { runtimeConfig } from "../config.js";
4
4
  import { createModelFactory } from "./layers/model.js";
5
5
  import { createViewFactory } from "./layers/view.js";
6
6
  import { createActionFactory } from "./layers/action.js";
7
- import { initializeState, createMutations, createCommitter } from "./utils/model.js";
8
- import { createView } from "./utils/view.js";
9
- import { createAction } from "./utils/action.js";
10
- import {
11
- DEFINITION
12
- } from "./types/action.js";
13
- function createStoreModel(mutations) {
14
- return createCommitter(mutations);
15
- }
16
- function createStoreView(source, viewDefinitions) {
17
- return createView(source, viewDefinitions);
18
- }
19
- function createStoreAction(actionDefinitions, view, mutations) {
20
- const actions = {};
21
- for (const [key, chain] of Object.entries(actionDefinitions)) {
22
- actions[key] = createAction(chain[DEFINITION], mutations, view, key);
23
- }
24
- return actions;
25
- }
7
+ import { createStoreState, createStoreModel, createStoreView, createStoreAction } from "./utils/store.js";
26
8
  export function createStore(config) {
27
9
  const logger = createConsola({
28
10
  level: runtimeConfig.logger,
@@ -30,20 +12,18 @@ export function createStore(config) {
30
12
  tag: `harlemify:${config.name}`
31
13
  }
32
14
  });
15
+ logger.info("Creating store");
33
16
  const modelFactory = createModelFactory(runtimeConfig.model, logger);
34
17
  const viewFactory = createViewFactory(runtimeConfig.view, logger);
35
18
  const actionFactory = createActionFactory(runtimeConfig.action, logger);
36
- logger.info("Creating store");
37
19
  const modelDefinitions = config.model(modelFactory);
38
20
  const viewDefinitions = config.view(viewFactory);
39
21
  const actionDefinitions = config.action(actionFactory);
40
- logger.debug("Initializing store");
41
- const state = initializeState(modelDefinitions);
42
- const source = createSourceStore(config.name, state);
43
- const mutations = createMutations(source, modelDefinitions);
44
- const model = createStoreModel(mutations);
45
- const view = createStoreView(source, viewDefinitions);
46
- const action = createStoreAction(actionDefinitions, view, mutations);
22
+ const state = createStoreState(modelDefinitions);
23
+ const source = createStoreSource(config.name, state);
24
+ const model = createStoreModel(modelDefinitions, source);
25
+ const view = createStoreView(viewDefinitions, source);
26
+ const action = createStoreAction(actionDefinitions, model, view);
47
27
  logger.info("Store created");
48
28
  return {
49
29
  model,
@@ -1,6 +1,8 @@
1
- import type { ConsolaInstance } from "consola";
2
1
  import type { ComputedRef, DeepReadonly, MaybeRefOrGetter, Ref } from "vue";
3
- import type { Model, ModelShape, MutationsOneOptions, MutationsManyOptions } from "./model.js";
2
+ import type { BaseDefinition } from "./base.js";
3
+ import type { ModelDefinitions, ModelOneCommitOptions, ModelManyCommitOptions, StoreModel } from "./model.js";
4
+ import { ModelOneMode, ModelManyMode } from "./model.js";
5
+ import type { ViewDefinitions, StoreView } from "./view.js";
4
6
  export interface RuntimeActionConfig {
5
7
  endpoint?: string;
6
8
  headers?: Record<string, string>;
@@ -8,20 +10,6 @@ export interface RuntimeActionConfig {
8
10
  timeout?: number;
9
11
  concurrent?: ActionConcurrent;
10
12
  }
11
- export declare const DEFINITION: unique symbol;
12
- export declare const AUTO: unique symbol;
13
- export declare enum ActionOneMode {
14
- SET = "set",
15
- RESET = "reset",
16
- PATCH = "patch"
17
- }
18
- export declare enum ActionManyMode {
19
- SET = "set",
20
- RESET = "reset",
21
- PATCH = "patch",
22
- REMOVE = "remove",
23
- ADD = "add"
24
- }
25
13
  export declare enum ActionStatus {
26
14
  IDLE = "idle",
27
15
  PENDING = "pending",
@@ -42,122 +30,92 @@ export declare enum ActionApiMethod {
42
30
  PATCH = "PATCH",
43
31
  DELETE = "DELETE"
44
32
  }
45
- export interface ActionApiError extends Error {
46
- name: "ActionApiError";
47
- status?: number;
48
- statusText?: string;
49
- data?: unknown;
50
- }
51
- export interface ActionHandleError extends Error {
52
- name: "ActionHandleError";
53
- cause: Error;
54
- }
55
- export interface ActionCommitError extends Error {
56
- name: "ActionCommitError";
57
- cause: Error;
58
- }
59
- export interface ActionConcurrentError extends Error {
60
- name: "ActionConcurrentError";
61
- }
62
- export type ActionError = ActionApiError | ActionHandleError | ActionCommitError | ActionConcurrentError;
63
- export type ActionApiValue<V, T> = MaybeRefOrGetter<T> | ((view: DeepReadonly<V>) => T);
64
- export interface ActionApiDefinition<V> {
33
+ export type ActionApiRequestValue<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>, T> = MaybeRefOrGetter<T> | ((view: DeepReadonly<StoreView<MD, VD>>) => T);
34
+ export interface ActionApiRequest<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>> {
65
35
  endpoint?: string;
66
- url: ActionApiValue<V, string>;
67
- method: ActionApiMethod;
68
- headers?: ActionApiValue<V, Record<string, string>>;
69
- query?: ActionApiValue<V, Record<string, unknown>>;
70
- body?: ActionApiValue<V, unknown>;
71
- timeout?: number;
36
+ url: ActionApiRequestValue<MD, VD, string>;
37
+ method: ActionApiRequestValue<MD, VD, ActionApiMethod>;
38
+ headers?: ActionApiRequestValue<MD, VD, Record<string, string>>;
39
+ query?: ActionApiRequestValue<MD, VD, Record<string, unknown>>;
40
+ body?: ActionApiRequestValue<MD, VD, unknown>;
41
+ timeout?: ActionApiRequestValue<MD, VD, number>;
72
42
  concurrent?: ActionConcurrent;
73
43
  }
74
- export type DeepPartial<T> = T extends object ? {
75
- [K in keyof T]?: DeepPartial<T[K]>;
76
- } : T;
77
- export type ActionCommitValue<M extends Model, K extends keyof M, Mode> = Mode extends ActionOneMode.SET ? ModelShape<M, K> : Mode extends ActionOneMode.PATCH ? DeepPartial<ModelShape<M, K>> : Mode extends ActionOneMode.RESET ? never : Mode extends ActionManyMode.SET ? ModelShape<M, K>[] : Mode extends ActionManyMode.PATCH ? DeepPartial<ModelShape<M, K>> | DeepPartial<ModelShape<M, K>>[] : Mode extends ActionManyMode.REMOVE ? ModelShape<M, K> | ModelShape<M, K>[] : Mode extends ActionManyMode.ADD ? ModelShape<M, K> | ModelShape<M, K>[] : Mode extends ActionManyMode.RESET ? never : never;
78
- export type ActionCommitter<M extends Model> = {
79
- <K extends keyof M, Mode extends ActionOneMode>(model: K, mode: Mode, ...args: Mode extends ActionOneMode.RESET ? [] : [value: ActionCommitValue<M, K, Mode>, options?: MutationsOneOptions]): void;
80
- <K extends keyof M, Mode extends ActionManyMode>(model: K, mode: Mode, ...args: Mode extends ActionManyMode.RESET ? [] : [value: ActionCommitValue<M, K, Mode>, options?: MutationsManyOptions]): void;
44
+ export type ActionApiRequestShortcut<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>> = Omit<ActionApiRequest<MD, VD>, "method">;
45
+ export interface ActionApiCommit<MD extends ModelDefinitions> {
46
+ model: keyof MD;
47
+ mode: ModelOneMode | ModelManyMode;
48
+ value?: (data: unknown) => unknown;
49
+ options?: ModelOneCommitOptions | ModelManyCommitOptions;
50
+ }
51
+ export interface ActionApiDefinition<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>> extends BaseDefinition {
52
+ request: ActionApiRequest<MD, VD>;
53
+ commit?: ActionApiCommit<MD>;
54
+ }
55
+ export type ActionHandlerCallback<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>, R = void> = (context: {
56
+ model: StoreModel<MD>;
57
+ view: StoreView<MD, VD>;
58
+ }) => Promise<R>;
59
+ export interface ActionHandlerDefinition<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>, R = void> extends BaseDefinition {
60
+ callback: ActionHandlerCallback<MD, VD, R>;
61
+ }
62
+ export type ActionDefinition<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>> = ActionApiDefinition<MD, VD> | ActionHandlerDefinition<MD, VD, unknown>;
63
+ export type ActionDefinitions<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>> = Record<string, ActionDefinition<MD, VD>>;
64
+ export type StoreAction<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>, AD extends ActionDefinitions<MD, VD>> = {
65
+ [K in keyof AD]: ActionCall;
81
66
  };
82
- export interface ActionHandleContext<M extends Model, V, ApiResponse = unknown> {
83
- api: <T = ApiResponse>() => Promise<T>;
84
- view: DeepReadonly<V>;
85
- commit: ActionCommitter<M>;
86
- }
87
- export interface ActionHandleContextNoApi<M extends Model, V> {
88
- view: DeepReadonly<V>;
89
- commit: ActionCommitter<M>;
90
- }
91
- export type ActionHandleCallback<M extends Model, V, R = void, ApiResponse = unknown> = (context: ActionHandleContext<M, V, ApiResponse>) => Promise<R>;
92
- export type ActionHandleCallbackNoApi<M extends Model, V, R = void> = (context: ActionHandleContextNoApi<M, V>) => Promise<R>;
93
- export type ActionHandleResolver<R = void> = (...args: unknown[]) => Promise<R>;
94
- export interface ActionDefinition<M extends Model, V, R = void> {
95
- api?: ActionApiDefinition<V>;
96
- handle?: ActionHandleCallback<M, V, R, unknown> | ActionHandleCallbackNoApi<M, V, R>;
97
- commit?: {
98
- model: keyof M;
99
- mode: ActionOneMode | ActionManyMode;
100
- value?: unknown;
101
- options?: MutationsOneOptions | MutationsManyOptions;
102
- };
103
- logger?: ConsolaInstance;
104
- }
105
- export type ActionDefinitions<M extends Model, V> = Record<string, ActionDefinition<M, V, unknown>>;
106
- export interface ActionCommitMethod<M extends Model, V, R> {
107
- <K extends keyof M, Mode extends ActionOneMode>(model: K, mode: Mode, ...args: Mode extends ActionOneMode.RESET ? [] : [value?: ActionCommitValue<M, K, Mode> | typeof AUTO, options?: MutationsOneOptions]): ActionCommitChain<M, V, R>;
108
- <K extends keyof M, Mode extends ActionManyMode>(model: K, mode: Mode, ...args: Mode extends ActionManyMode.RESET ? [] : [value?: ActionCommitValue<M, K, Mode> | typeof AUTO, options?: MutationsManyOptions]): ActionCommitChain<M, V, R>;
109
- }
110
- export interface ActionApiChain<M extends Model, V, ApiResponse> {
111
- handle<R>(callback: ActionHandleCallback<M, V, R, ApiResponse>): ActionHandleChain<M, V, R>;
112
- commit: ActionCommitMethod<M, V, ApiResponse>;
113
- readonly [DEFINITION]: ActionDefinition<M, V, ApiResponse>;
114
- }
115
- export interface ActionHandleChain<M extends Model, V, R> {
116
- commit: ActionCommitMethod<M, V, R>;
117
- readonly [DEFINITION]: ActionDefinition<M, V, R>;
118
- }
119
- export interface ActionCommitChain<M extends Model, V, R> {
120
- readonly [DEFINITION]: ActionDefinition<M, V, R>;
121
- }
122
- export type ActionApiShortcutDefinition<V> = Omit<ActionApiDefinition<V>, "method">;
123
- export interface ActionApiFactory<M extends Model, V> {
124
- <A>(definition: ActionApiDefinition<V>): ActionApiChain<M, V, A>;
125
- get<A>(definition: ActionApiShortcutDefinition<V>): ActionApiChain<M, V, A>;
126
- head<A>(definition: ActionApiShortcutDefinition<V>): ActionApiChain<M, V, A>;
127
- post<A>(definition: ActionApiShortcutDefinition<V>): ActionApiChain<M, V, A>;
128
- put<A>(definition: ActionApiShortcutDefinition<V>): ActionApiChain<M, V, A>;
129
- patch<A>(definition: ActionApiShortcutDefinition<V>): ActionApiChain<M, V, A>;
130
- delete<A>(definition: ActionApiShortcutDefinition<V>): ActionApiChain<M, V, A>;
131
- }
132
- export interface ActionFactory<M extends Model, V> {
133
- api: ActionApiFactory<M, V>;
134
- handle<R>(callback: ActionHandleCallbackNoApi<M, V, R>): ActionHandleChain<M, V, R>;
135
- commit: ActionCommitMethod<M, V, void>;
136
- }
137
- export interface ActionCallBind {
67
+ export interface ActionApiFactory<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>> {
68
+ (request: ActionApiRequest<MD, VD>, commit?: ActionApiCommit<MD>): ActionApiDefinition<MD, VD>;
69
+ get(request: ActionApiRequestShortcut<MD, VD>, commit?: ActionApiCommit<MD>): ActionApiDefinition<MD, VD>;
70
+ head(request: ActionApiRequestShortcut<MD, VD>, commit?: ActionApiCommit<MD>): ActionApiDefinition<MD, VD>;
71
+ post(request: ActionApiRequestShortcut<MD, VD>, commit?: ActionApiCommit<MD>): ActionApiDefinition<MD, VD>;
72
+ put(request: ActionApiRequestShortcut<MD, VD>, commit?: ActionApiCommit<MD>): ActionApiDefinition<MD, VD>;
73
+ patch(request: ActionApiRequestShortcut<MD, VD>, commit?: ActionApiCommit<MD>): ActionApiDefinition<MD, VD>;
74
+ delete(request: ActionApiRequestShortcut<MD, VD>, commit?: ActionApiCommit<MD>): ActionApiDefinition<MD, VD>;
75
+ }
76
+ export interface ActionHandlerFactory<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>> {
77
+ <R>(callback: ActionHandlerCallback<MD, VD, R>): ActionHandlerDefinition<MD, VD, R>;
78
+ }
79
+ export interface ActionFactory<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>> {
80
+ api: ActionApiFactory<MD, VD>;
81
+ handler: ActionHandlerFactory<MD, VD>;
82
+ }
83
+ export interface ActionCallBindOptions {
138
84
  status?: Ref<ActionStatus>;
139
- error?: Ref<ActionError | null>;
85
+ error?: Ref<Error | null>;
140
86
  }
141
- export interface ActionCallCommit {
142
- mode?: ActionOneMode | ActionManyMode;
87
+ export interface ActionCallCommitOptions {
88
+ mode?: ModelOneMode | ModelManyMode;
143
89
  }
144
- export interface ActionCallPayload<V, T = unknown, R = T> {
145
- headers?: Record<string, string> | ((view: DeepReadonly<V>) => Record<string, string>);
146
- query?: Record<string, unknown> | ((view: DeepReadonly<V>) => Record<string, unknown>);
147
- body?: unknown | ((view: DeepReadonly<V>) => unknown);
90
+ export interface ActionResolvedApi {
91
+ url: string;
92
+ method: ActionApiMethod;
93
+ headers: Record<string, string>;
94
+ query: Record<string, unknown>;
95
+ body?: Record<string, unknown> | BodyInit | null;
96
+ timeout?: number;
97
+ signal: AbortSignal;
98
+ }
99
+ export interface ActionCallTransformerOptions {
100
+ request?: (api: ActionResolvedApi) => ActionResolvedApi;
101
+ response?: (data: unknown) => unknown;
102
+ }
103
+ export interface ActionCallOptions {
104
+ params?: Record<string, string>;
105
+ headers?: Record<string, string>;
106
+ query?: Record<string, unknown>;
107
+ body?: unknown;
148
108
  timeout?: number;
149
109
  signal?: AbortSignal;
150
- transformer?: (response: T) => R;
110
+ transformer?: ActionCallTransformerOptions;
151
111
  concurrent?: ActionConcurrent;
152
- bind?: ActionCallBind;
153
- commit?: ActionCallCommit;
112
+ bind?: ActionCallBindOptions;
113
+ commit?: ActionCallCommitOptions;
154
114
  }
155
- export interface Action<V, T = void> {
156
- (payload?: ActionCallPayload<V, T>): Promise<T>;
157
- <R>(payload: ActionCallPayload<V, T, R>): Promise<R>;
158
- readonly loading: ComputedRef<boolean>;
115
+ export interface ActionCall<T = void> {
116
+ (options?: ActionCallOptions): Promise<T>;
117
+ readonly error: Readonly<Ref<Error | null>>;
159
118
  readonly status: Readonly<Ref<ActionStatus>>;
160
- readonly error: Readonly<Ref<ActionError | null>>;
161
- readonly data: DeepReadonly<T> | null;
119
+ readonly loading: ComputedRef<boolean>;
162
120
  reset: () => void;
163
121
  }
@@ -1,19 +1,3 @@
1
- export const DEFINITION = Symbol("definition");
2
- export const AUTO = Symbol("auto");
3
- export var ActionOneMode = /* @__PURE__ */ ((ActionOneMode2) => {
4
- ActionOneMode2["SET"] = "set";
5
- ActionOneMode2["RESET"] = "reset";
6
- ActionOneMode2["PATCH"] = "patch";
7
- return ActionOneMode2;
8
- })(ActionOneMode || {});
9
- export var ActionManyMode = /* @__PURE__ */ ((ActionManyMode2) => {
10
- ActionManyMode2["SET"] = "set";
11
- ActionManyMode2["RESET"] = "reset";
12
- ActionManyMode2["PATCH"] = "patch";
13
- ActionManyMode2["REMOVE"] = "remove";
14
- ActionManyMode2["ADD"] = "add";
15
- return ActionManyMode2;
16
- })(ActionManyMode || {});
17
1
  export var ActionStatus = /* @__PURE__ */ ((ActionStatus2) => {
18
2
  ActionStatus2["IDLE"] = "idle";
19
3
  ActionStatus2["PENDING"] = "pending";
@@ -0,0 +1,6 @@
1
+ import type { ConsolaInstance } from "consola";
2
+ export interface BaseDefinition {
3
+ readonly key: string;
4
+ logger?: ConsolaInstance;
5
+ setKey(key: string): void;
6
+ }
File without changes
@@ -1,4 +1,4 @@
1
- import type { ConsolaInstance } from "consola";
1
+ import type { BaseDefinition } from "./base.js";
2
2
  import type { Shape, ShapeType } from "./shape.js";
3
3
  export interface RuntimeModelConfig {
4
4
  identifier?: string;
@@ -7,64 +7,79 @@ export declare enum ModelKind {
7
7
  OBJECT = "object",
8
8
  ARRAY = "array"
9
9
  }
10
- export interface ModelOneOptions<S extends Shape> {
10
+ export declare enum ModelOneMode {
11
+ SET = "set",
12
+ RESET = "reset",
13
+ PATCH = "patch"
14
+ }
15
+ export declare enum ModelManyMode {
16
+ SET = "set",
17
+ RESET = "reset",
18
+ PATCH = "patch",
19
+ REMOVE = "remove",
20
+ ADD = "add"
21
+ }
22
+ export interface ModelOneDefinitionOptions<S extends Shape> {
11
23
  identifier?: keyof S;
12
24
  default?: S;
13
25
  }
14
- export interface ModelManyOptions<S extends Shape> {
26
+ export interface ModelManyDefinitionOptions<S extends Shape> {
15
27
  identifier?: keyof S;
16
28
  default?: S[];
17
29
  }
18
- export interface ModelOneDefinition<S extends Shape> {
30
+ export interface ModelOneDefinition<S extends Shape> extends BaseDefinition {
19
31
  shape: ShapeType<S>;
20
32
  kind: ModelKind.OBJECT;
21
- options?: ModelOneOptions<S>;
22
- logger?: ConsolaInstance;
33
+ options?: ModelOneDefinitionOptions<S>;
23
34
  }
24
- export interface ModelManyDefinition<S extends Shape> {
35
+ export interface ModelManyDefinition<S extends Shape> extends BaseDefinition {
25
36
  shape: ShapeType<S>;
26
37
  kind: ModelKind.ARRAY;
27
- options?: ModelManyOptions<S>;
28
- logger?: ConsolaInstance;
38
+ options?: ModelManyDefinitionOptions<S>;
29
39
  }
30
40
  export type ModelDefinition<S extends Shape> = ModelOneDefinition<S> | ModelManyDefinition<S>;
31
- export type Model = Record<string, ModelDefinition<any>>;
32
- export type ModelInstance<M extends Model, K extends keyof M> = M[K] extends ModelOneDefinition<infer S> ? S | null : M[K] extends ModelManyDefinition<infer S> ? S[] : never;
33
- export type ModelShape<M extends Model, K extends keyof M> = M[K] extends ModelDefinition<infer S> ? S : never;
34
- export type ModelOneKey<M extends Model> = {
35
- [K in keyof M]: M[K] extends ModelOneDefinition<infer _S> ? K : never;
36
- }[keyof M];
37
- export type ModelManyKey<M extends Model> = {
38
- [K in keyof M]: M[K] extends ModelManyDefinition<infer _S> ? K : never;
39
- }[keyof M];
40
- export type ModelStateOf<M extends Model> = {
41
- [K in keyof M]: ModelInstance<M, K>;
41
+ export type ModelDefinitions = Record<string, ModelDefinition<any>>;
42
+ export type ModelDefinitionInfer<MD extends ModelDefinitions, K extends keyof MD> = MD[K] extends ModelOneDefinition<infer S> ? S | null : MD[K] extends ModelManyDefinition<infer S> ? S[] : never;
43
+ export type ModelDefinitionInferTuple<MD extends ModelDefinitions, K extends readonly (keyof MD)[]> = {
44
+ [I in keyof K]: K[I] extends keyof MD ? ModelDefinitionInfer<MD, K[I]> : never;
45
+ };
46
+ export type ModelDefinitionsInfer<MD extends ModelDefinitions> = {
47
+ [K in keyof MD]: ModelDefinitionInfer<MD, K>;
42
48
  };
43
49
  export interface ModelFactory {
44
- one<S extends Shape>(shape: ShapeType<S>, options?: ModelOneOptions<S>): ModelOneDefinition<S>;
45
- many<S extends Shape>(shape: ShapeType<S>, options?: ModelManyOptions<S>): ModelManyDefinition<S>;
50
+ one<S extends Shape>(shape: ShapeType<S>, options?: ModelOneDefinitionOptions<S>): ModelOneDefinition<S>;
51
+ many<S extends Shape>(shape: ShapeType<S>, options?: ModelManyDefinitionOptions<S>): ModelManyDefinition<S>;
46
52
  }
47
- export interface MutationsOneOptions {
53
+ export interface ModelOneCommitOptions {
48
54
  deep?: boolean;
49
55
  }
50
- export interface MutationsManyOptions {
56
+ export interface ModelManyCommitOptions {
51
57
  by?: string;
52
58
  prepend?: boolean;
53
59
  unique?: boolean;
54
60
  deep?: boolean;
55
61
  }
56
- export interface MutationsOne<S extends Shape> {
62
+ export interface ModelOneCommit<S extends Shape> {
57
63
  set: (value: S) => void;
58
64
  reset: () => void;
59
- patch: (value: Partial<S>, options?: MutationsOneOptions) => void;
65
+ patch: (value: Partial<S>, options?: ModelOneCommitOptions) => void;
60
66
  }
61
- export interface MutationsMany<S extends Shape> {
67
+ export interface ModelManyCommit<S extends Shape> {
62
68
  set: (value: S[]) => void;
63
69
  reset: () => void;
64
- patch: (value: Partial<S> | Partial<S>[], options?: MutationsManyOptions) => void;
65
- remove: (value: S | S[], options?: MutationsManyOptions) => void;
66
- add: (value: S | S[], options?: MutationsManyOptions) => void;
70
+ patch: (value: Partial<S> | Partial<S>[], options?: ModelManyCommitOptions) => void;
71
+ remove: (value: S | S[], options?: ModelManyCommitOptions) => void;
72
+ add: (value: S | S[], options?: ModelManyCommitOptions) => void;
67
73
  }
68
- export type Mutations<M extends Model> = {
69
- [K in keyof M]: M[K] extends ModelOneDefinition<infer S> ? MutationsOne<S> : M[K] extends ModelManyDefinition<infer S> ? MutationsMany<S> : never;
74
+ export type ModelOneCall<S extends Shape> = ModelOneCommit<S> & {
75
+ commit(mode: string, value?: unknown, options?: unknown): void;
76
+ aliases(): Record<string, string>;
77
+ };
78
+ export type ModelManyCall<S extends Shape> = ModelManyCommit<S> & {
79
+ commit(mode: string, value?: unknown, options?: unknown): void;
80
+ aliases(): Record<string, string>;
81
+ };
82
+ export type ModelCall<S extends Shape> = ModelOneCall<S> | ModelManyCall<S>;
83
+ export type StoreModel<MD extends ModelDefinitions> = {
84
+ [K in keyof MD]: MD[K] extends ModelOneDefinition<infer S> ? ModelOneCall<S> : MD[K] extends ModelManyDefinition<infer S> ? ModelManyCall<S> : never;
70
85
  };
@@ -3,3 +3,17 @@ export var ModelKind = /* @__PURE__ */ ((ModelKind2) => {
3
3
  ModelKind2["ARRAY"] = "array";
4
4
  return ModelKind2;
5
5
  })(ModelKind || {});
6
+ export var ModelOneMode = /* @__PURE__ */ ((ModelOneMode2) => {
7
+ ModelOneMode2["SET"] = "set";
8
+ ModelOneMode2["RESET"] = "reset";
9
+ ModelOneMode2["PATCH"] = "patch";
10
+ return ModelOneMode2;
11
+ })(ModelOneMode || {});
12
+ export var ModelManyMode = /* @__PURE__ */ ((ModelManyMode2) => {
13
+ ModelManyMode2["SET"] = "set";
14
+ ModelManyMode2["RESET"] = "reset";
15
+ ModelManyMode2["PATCH"] = "patch";
16
+ ModelManyMode2["REMOVE"] = "remove";
17
+ ModelManyMode2["ADD"] = "add";
18
+ return ModelManyMode2;
19
+ })(ModelManyMode || {});
@@ -1,12 +1,37 @@
1
1
  import type { z } from "zod";
2
- export type ShapeDefinition = z.ZodObject<z.ZodRawShape>;
2
+ export type Shape = Record<string, unknown>;
3
+ export type ShapeRawDefinition = z.ZodRawShape;
4
+ export type ShapeDefinition = z.ZodObject<ShapeRawDefinition>;
3
5
  export type ShapeType<S> = z.ZodType<S>;
4
6
  export type ShapeInfer<T extends z.ZodType<any>> = z.infer<T>;
5
- export type Shape = Record<string, unknown>;
6
- export interface ShapeMeta {
7
- identifier?: string;
7
+ export type ShapeCall<T extends ShapeRawDefinition> = z.ZodObject<T> & {
8
+ defaults: (overrides?: Partial<z.infer<z.ZodObject<T>>>) => z.infer<z.ZodObject<T>>;
9
+ };
10
+ export interface ShapeResolved<T extends ShapeDefinition = ShapeDefinition> {
11
+ identifier?: keyof T["shape"] & string;
8
12
  defaults: Record<string, unknown>;
9
- fields: string[];
13
+ fields: (keyof T["shape"] & string)[];
14
+ aliases: Record<string, string>;
15
+ }
16
+ export interface ShapeFieldDefinition {
17
+ meta?: {
18
+ identifier?: boolean;
19
+ alias?: string;
20
+ };
21
+ defaultValue?: unknown;
22
+ }
23
+ export interface ZodFieldDefinition {
24
+ type?: string;
25
+ shape?: Record<string, z.ZodType>;
26
+ element?: z.ZodType;
27
+ items?: z.ZodType[];
28
+ entries?: Record<string, string | number>;
29
+ values?: unknown[];
30
+ options?: z.ZodType[];
31
+ innerType?: z.ZodType;
32
+ defaultValue?: unknown;
33
+ keyType?: z.ZodType;
34
+ valueType?: z.ZodType;
10
35
  }
11
36
  export interface ShapeFactory {
12
37
  string: typeof z.string;
@@ -0,0 +1,14 @@
1
+ import type { ModelDefinitions, ModelFactory, StoreModel } from "./model.js";
2
+ import type { ViewDefinitions, ViewFactory, StoreView } from "./view.js";
3
+ import type { ActionDefinition, ActionDefinitions, ActionFactory, StoreAction } from "./action.js";
4
+ export interface StoreConfig<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>, _AD extends ActionDefinitions<MD, VD>> {
5
+ name: string;
6
+ model: (factory: ModelFactory) => MD;
7
+ view: (factory: ViewFactory<MD>) => VD;
8
+ action: (factory: ActionFactory<MD, VD>) => Record<string, ActionDefinition<MD, VD>>;
9
+ }
10
+ export interface Store<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>, AD extends ActionDefinitions<MD, VD>> {
11
+ model: StoreModel<MD>;
12
+ view: StoreView<MD, VD>;
13
+ action: StoreAction<MD, VD, AD>;
14
+ }
File without changes
@@ -1,29 +1,40 @@
1
- import type { ConsolaInstance } from "consola";
2
- import type { Model, ModelInstance } from "./model.js";
1
+ import type { ComputedRef } from "vue";
2
+ import type { BaseDefinition } from "./base.js";
3
+ import type { ModelDefinitions, ModelDefinitionInfer, ModelDefinitionInferTuple } from "./model.js";
4
+ export declare enum ViewClone {
5
+ SHALLOW = "shallow",
6
+ DEEP = "deep"
7
+ }
3
8
  export interface RuntimeViewConfig {
9
+ clone?: ViewClone;
4
10
  }
5
- export type ViewFromResolver<M extends Model, K extends keyof M, R> = (model: ModelInstance<M, K>) => R;
6
- export type ModelInstanceTuple<M extends Model, K extends readonly (keyof M)[]> = {
7
- [I in keyof K]: K[I] extends keyof M ? ModelInstance<M, K[I]> : never;
8
- };
9
- export type ViewMergeResolver<M extends Model, K extends readonly (keyof M)[], R> = (...values: [...ModelInstanceTuple<M, K>]) => R;
10
- export interface ViewFromDefinition<M extends Model, K extends keyof M, R = ModelInstance<M, K>> {
11
- sources: readonly [K];
12
- resolver?: ViewFromResolver<M, K, R>;
13
- logger?: ConsolaInstance;
11
+ export interface ViewDefinitionOptions {
12
+ clone?: ViewClone;
14
13
  }
15
- export interface ViewMergeDefinition<M extends Model, K extends readonly (keyof M)[], R> {
16
- sources: K;
17
- resolver: ViewMergeResolver<M, K, R>;
18
- logger?: ConsolaInstance;
14
+ export type ViewFromDefinitionResolver<MD extends ModelDefinitions, K extends keyof MD, R> = (model: ModelDefinitionInfer<MD, K>) => R;
15
+ export type ViewMergeDefinitionResolver<MD extends ModelDefinitions, K extends readonly (keyof MD)[], R> = (...values: [...ModelDefinitionInferTuple<MD, K>]) => R;
16
+ export interface ViewFromDefinition<MD extends ModelDefinitions, K extends keyof MD, R = ModelDefinitionInfer<MD, K>> extends BaseDefinition {
17
+ model: readonly [K];
18
+ resolver?: ViewFromDefinitionResolver<MD, K, R>;
19
+ options?: ViewDefinitionOptions;
19
20
  }
20
- export type ViewDefinition<M extends Model> = ViewFromDefinition<M, keyof M, unknown> | ViewMergeDefinition<M, readonly (keyof M)[], unknown>;
21
- export type ViewDefinitions<M extends Model> = Record<string, ViewDefinition<M>>;
22
- export type ViewResult<M extends Model, VD extends ViewDefinition<M>> = VD extends ViewFromDefinition<M, infer _K, infer R> ? R : VD extends ViewMergeDefinition<M, infer _K, infer R> ? R : never;
23
- export interface ViewFactory<M extends Model> {
24
- from<K extends keyof M>(source: K): ViewFromDefinition<M, K, ModelInstance<M, K>>;
25
- from<K extends keyof M, R>(source: K, resolver: ViewFromResolver<M, K, R>): ViewFromDefinition<M, K, R>;
26
- merge<K1 extends keyof M, K2 extends keyof M, R>(sources: readonly [K1, K2], resolver: (v1: ModelInstance<M, K1>, v2: ModelInstance<M, K2>) => R): ViewMergeDefinition<M, readonly [K1, K2], R>;
27
- merge<K1 extends keyof M, K2 extends keyof M, K3 extends keyof M, R>(sources: readonly [K1, K2, K3], resolver: (v1: ModelInstance<M, K1>, v2: ModelInstance<M, K2>, v3: ModelInstance<M, K3>) => R): ViewMergeDefinition<M, readonly [K1, K2, K3], R>;
28
- merge<K extends readonly (keyof M)[], R>(sources: K, resolver: ViewMergeResolver<M, K, R>): ViewMergeDefinition<M, K, R>;
21
+ export interface ViewMergeDefinition<MD extends ModelDefinitions, K extends readonly (keyof MD)[], R> extends BaseDefinition {
22
+ models: K;
23
+ resolver: ViewMergeDefinitionResolver<MD, K, R>;
24
+ options?: ViewDefinitionOptions;
29
25
  }
26
+ export type ViewDefinition<MD extends ModelDefinitions> = ViewFromDefinition<MD, keyof MD, unknown> | ViewMergeDefinition<MD, readonly (keyof MD)[], unknown>;
27
+ export type ViewDefinitions<MD extends ModelDefinitions> = Record<string, ViewDefinition<MD>>;
28
+ export type ViewDefinitionInfer<MD extends ModelDefinitions, VD extends ViewDefinition<MD>> = VD extends ViewFromDefinition<MD, infer _K, infer R> ? R : VD extends ViewMergeDefinition<MD, infer _K, infer R> ? R : never;
29
+ export interface ViewFactory<MD extends ModelDefinitions> {
30
+ from<K extends keyof MD>(model: K): ViewFromDefinition<MD, K, ModelDefinitionInfer<MD, K>>;
31
+ from<K extends keyof MD, R>(model: K, resolver: ViewFromDefinitionResolver<MD, K, R>, options?: ViewDefinitionOptions): ViewFromDefinition<MD, K, R>;
32
+ merge<MK1 extends keyof MD, MK2 extends keyof MD, R>(models: readonly [MK1, MK2], resolver: (mv1: ModelDefinitionInfer<MD, MK1>, mv2: ModelDefinitionInfer<MD, MK2>) => R, options?: ViewDefinitionOptions): ViewMergeDefinition<MD, readonly [MK1, MK2], R>;
33
+ merge<MK1 extends keyof MD, MK2 extends keyof MD, MK3 extends keyof MD, R>(models: readonly [MK1, MK2, MK3], resolver: (mv1: ModelDefinitionInfer<MD, MK1>, mv2: ModelDefinitionInfer<MD, MK2>, mv3: ModelDefinitionInfer<MD, MK3>) => R, options?: ViewDefinitionOptions): ViewMergeDefinition<MD, readonly [MK1, MK2, MK3], R>;
34
+ merge<MK1 extends keyof MD, MK2 extends keyof MD, MK3 extends keyof MD, MK4 extends keyof MD, R>(models: readonly [MK1, MK2, MK3, MK4], resolver: (mv1: ModelDefinitionInfer<MD, MK1>, mv2: ModelDefinitionInfer<MD, MK2>, mv3: ModelDefinitionInfer<MD, MK3>, mv4: ModelDefinitionInfer<MD, MK4>) => R, options?: ViewDefinitionOptions): ViewMergeDefinition<MD, readonly [MK1, MK2, MK3, MK4], R>;
35
+ merge<MK1 extends keyof MD, MK2 extends keyof MD, MK3 extends keyof MD, MK4 extends keyof MD, MK5 extends keyof MD, R>(models: readonly [MK1, MK2, MK3, MK4, MK5], resolver: (mv1: ModelDefinitionInfer<MD, MK1>, mv2: ModelDefinitionInfer<MD, MK2>, mv3: ModelDefinitionInfer<MD, MK3>, mv4: ModelDefinitionInfer<MD, MK4>, mv5: ModelDefinitionInfer<MD, MK5>) => R, options?: ViewDefinitionOptions): ViewMergeDefinition<MD, readonly [MK1, MK2, MK3, MK4, MK5], R>;
36
+ }
37
+ export type ViewCall<R = unknown> = ComputedRef<R>;
38
+ export type StoreView<MD extends ModelDefinitions, VD extends ViewDefinitions<MD>> = {
39
+ readonly [K in keyof VD]: ViewCall<ViewDefinitionInfer<MD, VD[K]>>;
40
+ };
@@ -0,0 +1,5 @@
1
+ export var ViewClone = /* @__PURE__ */ ((ViewClone2) => {
2
+ ViewClone2["SHALLOW"] = "shallow";
3
+ ViewClone2["DEEP"] = "deep";
4
+ return ViewClone2;
5
+ })(ViewClone || {});