@diphyx/harlemify 5.2.0 → 5.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.
- package/README.md +95 -31
- package/dist/module.json +2 -2
- package/dist/module.mjs +5 -3
- package/dist/runtime/composables/action.d.ts +4 -4
- package/dist/runtime/composables/compose.d.ts +9 -0
- package/dist/runtime/composables/compose.js +10 -0
- package/dist/runtime/composables/model.d.ts +3 -3
- package/dist/runtime/composables/view.d.ts +3 -4
- package/dist/runtime/composables/view.js +2 -14
- package/dist/runtime/core/layers/model.js +36 -7
- package/dist/runtime/core/store.d.ts +2 -1
- package/dist/runtime/core/store.js +41 -24
- package/dist/runtime/core/types/action.d.ts +43 -30
- package/dist/runtime/core/types/action.js +5 -0
- package/dist/runtime/core/types/base.d.ts +1 -2
- package/dist/runtime/core/types/compose.d.ts +18 -0
- package/dist/runtime/core/types/compose.js +0 -0
- package/dist/runtime/core/types/model.d.ts +61 -30
- package/dist/runtime/core/types/model.js +15 -5
- package/dist/runtime/core/types/shape.d.ts +0 -6
- package/dist/runtime/core/types/store.d.ts +6 -2
- package/dist/runtime/core/utils/action.js +4 -1
- package/dist/runtime/core/utils/base.d.ts +2 -1
- package/dist/runtime/core/utils/base.js +4 -5
- package/dist/runtime/core/utils/compose.d.ts +3 -0
- package/dist/runtime/core/utils/compose.js +37 -0
- package/dist/runtime/core/utils/model.js +174 -165
- package/dist/runtime/core/utils/shape.d.ts +4 -3
- package/dist/runtime/core/utils/shape.js +84 -124
- package/dist/runtime/core/utils/store.d.ts +1 -1
- package/dist/runtime/core/utils/store.js +5 -6
- package/dist/runtime/core/utils/view.js +1 -1
- package/dist/runtime/index.d.ts +5 -2
- package/dist/runtime/index.js +3 -2
- package/dist/runtime/plugin.js +3 -17
- package/dist/runtime/plugins/ssr.d.ts +9 -0
- package/dist/runtime/plugins/ssr.js +53 -0
- package/package.json +17 -18
|
@@ -1,211 +1,220 @@
|
|
|
1
1
|
import { defu } from "defu";
|
|
2
|
-
import {
|
|
2
|
+
import { ensureArray } from "./base.js";
|
|
3
|
+
import { resolveShapeAliases } from "./shape.js";
|
|
3
4
|
import {
|
|
4
|
-
|
|
5
|
+
ModelType,
|
|
6
|
+
ModelManyKind,
|
|
5
7
|
ModelOneMode,
|
|
6
|
-
ModelManyMode
|
|
8
|
+
ModelManyMode,
|
|
9
|
+
ModelSilent
|
|
7
10
|
} from "../types/model.js";
|
|
8
|
-
function
|
|
9
|
-
if (
|
|
10
|
-
return
|
|
11
|
+
function callHook(definition, hook, silent) {
|
|
12
|
+
if (silent === true || silent === hook) {
|
|
13
|
+
return;
|
|
11
14
|
}
|
|
12
|
-
|
|
15
|
+
try {
|
|
16
|
+
definition.options?.[hook]?.();
|
|
17
|
+
} catch (error) {
|
|
18
|
+
definition.logger?.error(`Model ${hook} hook error`, {
|
|
19
|
+
model: definition.key,
|
|
20
|
+
error
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function wrapOperation(definition, mutation, operation, silent) {
|
|
25
|
+
definition.logger?.debug("Model mutation", {
|
|
26
|
+
model: definition.key,
|
|
27
|
+
mutation
|
|
28
|
+
});
|
|
29
|
+
callHook(definition, ModelSilent.PRE, silent);
|
|
30
|
+
operation();
|
|
31
|
+
callHook(definition, ModelSilent.POST, silent);
|
|
13
32
|
}
|
|
14
33
|
function createOneCommit(definition, source) {
|
|
15
|
-
const setOperation = source.mutation(`${definition.key}:set`, (state,
|
|
16
|
-
state[definition.key] =
|
|
34
|
+
const setOperation = source.mutation(`${definition.key}:set`, (state, { payload }) => {
|
|
35
|
+
state[definition.key] = payload;
|
|
17
36
|
});
|
|
18
37
|
const resetOperation = source.mutation(`${definition.key}:reset`, (state) => {
|
|
19
|
-
state[definition.key] = definition.
|
|
38
|
+
state[definition.key] = definition.default();
|
|
20
39
|
});
|
|
21
|
-
const patchOperation = source.mutation(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
if (payload.options?.deep) {
|
|
28
|
-
state[definition.key] = defu(payload.value, state[definition.key]);
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
state[definition.key] = {
|
|
32
|
-
...state[definition.key],
|
|
33
|
-
...payload.value
|
|
34
|
-
};
|
|
40
|
+
const patchOperation = source.mutation(`${definition.key}:patch`, (state, { payload, options }) => {
|
|
41
|
+
if (options?.deep) {
|
|
42
|
+
state[definition.key] = defu(payload, state[definition.key]);
|
|
43
|
+
return;
|
|
35
44
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
});
|
|
42
|
-
setOperation(value);
|
|
43
|
-
}
|
|
44
|
-
function reset() {
|
|
45
|
-
definition.logger?.debug("Model mutation", {
|
|
46
|
-
model: definition.key,
|
|
47
|
-
mutation: "reset"
|
|
48
|
-
});
|
|
49
|
-
resetOperation();
|
|
50
|
-
}
|
|
51
|
-
function patch(value, options) {
|
|
52
|
-
definition.logger?.debug("Model mutation", {
|
|
53
|
-
model: definition.key,
|
|
54
|
-
mutation: "patch"
|
|
55
|
-
});
|
|
56
|
-
patchOperation({
|
|
57
|
-
value,
|
|
58
|
-
options
|
|
59
|
-
});
|
|
60
|
-
}
|
|
45
|
+
state[definition.key] = {
|
|
46
|
+
...state[definition.key],
|
|
47
|
+
...payload
|
|
48
|
+
};
|
|
49
|
+
});
|
|
61
50
|
return {
|
|
62
|
-
set,
|
|
63
|
-
|
|
64
|
-
|
|
51
|
+
set(payload, options) {
|
|
52
|
+
wrapOperation(definition, "set", () => setOperation({ payload }), options?.silent);
|
|
53
|
+
},
|
|
54
|
+
reset(options) {
|
|
55
|
+
wrapOperation(definition, "reset", () => resetOperation(), options?.silent);
|
|
56
|
+
},
|
|
57
|
+
patch(payload, options) {
|
|
58
|
+
wrapOperation(definition, "patch", () => patchOperation({ payload, options }), options?.silent);
|
|
59
|
+
}
|
|
65
60
|
};
|
|
66
61
|
}
|
|
67
|
-
function
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
state[definition.key] = value;
|
|
62
|
+
function createManyListCommit(definition, source) {
|
|
63
|
+
const setOperation = source.mutation(`${definition.key}:set`, (state, { payload }) => {
|
|
64
|
+
state[definition.key] = payload;
|
|
71
65
|
});
|
|
72
66
|
const resetOperation = source.mutation(`${definition.key}:reset`, (state) => {
|
|
73
|
-
state[definition.key] = definition.
|
|
74
|
-
});
|
|
75
|
-
const patchOperation = source.mutation(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
67
|
+
state[definition.key] = definition.default();
|
|
68
|
+
});
|
|
69
|
+
const patchOperation = source.mutation(`${definition.key}:patch`, (state, { payload, options }) => {
|
|
70
|
+
const items = ensureArray(payload);
|
|
71
|
+
const by = options?.by ?? definition.identifier;
|
|
72
|
+
state[definition.key] = state[definition.key].map((item) => {
|
|
73
|
+
const found = items.find((partial) => {
|
|
74
|
+
return partial[by] === item[by];
|
|
75
|
+
});
|
|
76
|
+
if (!found) {
|
|
77
|
+
return item;
|
|
78
|
+
}
|
|
79
|
+
if (options?.deep) {
|
|
80
|
+
return defu(found, item);
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
...item,
|
|
84
|
+
...found
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
const removeOperation = source.mutation(`${definition.key}:remove`, (state, { payload }) => {
|
|
89
|
+
const items = ensureArray(payload);
|
|
90
|
+
state[definition.key] = state[definition.key].filter((item) => {
|
|
91
|
+
return !items.some((match) => {
|
|
92
|
+
let keys = Object.keys(match);
|
|
93
|
+
if (definition.identifier in match) {
|
|
94
|
+
keys = [definition.identifier];
|
|
89
95
|
}
|
|
90
|
-
return {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
};
|
|
96
|
+
return keys.every((key) => {
|
|
97
|
+
return item[key] === match[key];
|
|
98
|
+
});
|
|
94
99
|
});
|
|
95
|
-
}
|
|
96
|
-
);
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
(
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
items.map((item) => {
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
const addOperation = source.mutation(`${definition.key}:add`, (state, { payload, options }) => {
|
|
103
|
+
let items = ensureArray(payload);
|
|
104
|
+
if (options?.unique) {
|
|
105
|
+
const by = options.by ?? definition.identifier;
|
|
106
|
+
const existingIds = new Set(
|
|
107
|
+
state[definition.key].map((item) => {
|
|
104
108
|
return item[by];
|
|
105
109
|
})
|
|
106
110
|
);
|
|
107
|
-
|
|
108
|
-
return !
|
|
111
|
+
items = items.filter((item) => {
|
|
112
|
+
return !existingIds.has(item[by]);
|
|
109
113
|
});
|
|
110
114
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
115
|
+
if (options?.prepend) {
|
|
116
|
+
state[definition.key] = [...items, ...state[definition.key]];
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
state[definition.key] = [...state[definition.key], ...items];
|
|
120
|
+
});
|
|
121
|
+
return {
|
|
122
|
+
set(payload, options) {
|
|
123
|
+
wrapOperation(definition, "set", () => setOperation({ payload }), options?.silent);
|
|
124
|
+
},
|
|
125
|
+
reset(options) {
|
|
126
|
+
wrapOperation(definition, "reset", () => resetOperation(), options?.silent);
|
|
127
|
+
},
|
|
128
|
+
patch(payload, options) {
|
|
129
|
+
wrapOperation(definition, "patch", () => patchOperation({ payload, options }), options?.silent);
|
|
130
|
+
},
|
|
131
|
+
remove(payload, options) {
|
|
132
|
+
wrapOperation(definition, "remove", () => removeOperation({ payload }), options?.silent);
|
|
133
|
+
},
|
|
134
|
+
add(payload, options) {
|
|
135
|
+
wrapOperation(definition, "add", () => addOperation({ payload, options }), options?.silent);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function createManyRecordCommit(definition, source) {
|
|
140
|
+
const setOperation = source.mutation(`${definition.key}:set`, (state, { payload }) => {
|
|
141
|
+
state[definition.key] = payload;
|
|
142
|
+
});
|
|
143
|
+
const resetOperation = source.mutation(`${definition.key}:reset`, (state) => {
|
|
144
|
+
state[definition.key] = definition.default();
|
|
145
|
+
});
|
|
146
|
+
const patchOperation = source.mutation(`${definition.key}:patch`, (state, { payload, options }) => {
|
|
147
|
+
if (options?.deep) {
|
|
148
|
+
state[definition.key] = defu(payload, state[definition.key]);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
state[definition.key] = {
|
|
152
|
+
...state[definition.key],
|
|
153
|
+
...payload
|
|
154
|
+
};
|
|
155
|
+
});
|
|
156
|
+
const removeOperation = source.mutation(`${definition.key}:remove`, (state, { payload }) => {
|
|
157
|
+
const record = {};
|
|
158
|
+
for (const entry of Object.keys(state[definition.key])) {
|
|
159
|
+
if (entry !== payload) {
|
|
160
|
+
record[entry] = state[definition.key][entry];
|
|
130
161
|
}
|
|
131
|
-
state[definition.key] = [...state[definition.key], ...items];
|
|
132
162
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
function reset() {
|
|
142
|
-
definition.logger?.debug("Model mutation", {
|
|
143
|
-
model: definition.key,
|
|
144
|
-
mutation: "reset"
|
|
145
|
-
});
|
|
146
|
-
resetOperation();
|
|
147
|
-
}
|
|
148
|
-
function patch(value, options) {
|
|
149
|
-
definition.logger?.debug("Model mutation", {
|
|
150
|
-
model: definition.key,
|
|
151
|
-
mutation: "patch"
|
|
152
|
-
});
|
|
153
|
-
patchOperation({
|
|
154
|
-
value,
|
|
155
|
-
options
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
function remove(value, options) {
|
|
159
|
-
definition.logger?.debug("Model mutation", {
|
|
160
|
-
model: definition.key,
|
|
161
|
-
mutation: "remove"
|
|
162
|
-
});
|
|
163
|
-
removeOperation({
|
|
164
|
-
value,
|
|
165
|
-
options
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
function add(value, options) {
|
|
169
|
-
definition.logger?.debug("Model mutation", {
|
|
170
|
-
model: definition.key,
|
|
171
|
-
mutation: "add"
|
|
172
|
-
});
|
|
173
|
-
addOperation({
|
|
174
|
-
value,
|
|
175
|
-
options
|
|
176
|
-
});
|
|
177
|
-
}
|
|
163
|
+
state[definition.key] = record;
|
|
164
|
+
});
|
|
165
|
+
const addOperation = source.mutation(`${definition.key}:add`, (state, { payload }) => {
|
|
166
|
+
state[definition.key] = {
|
|
167
|
+
...state[definition.key],
|
|
168
|
+
[payload.key]: payload.value
|
|
169
|
+
};
|
|
170
|
+
});
|
|
178
171
|
return {
|
|
179
|
-
set,
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
172
|
+
set(payload, options) {
|
|
173
|
+
wrapOperation(definition, "set", () => setOperation({ payload }), options?.silent);
|
|
174
|
+
},
|
|
175
|
+
reset(options) {
|
|
176
|
+
wrapOperation(definition, "reset", () => resetOperation(), options?.silent);
|
|
177
|
+
},
|
|
178
|
+
patch(payload, options) {
|
|
179
|
+
wrapOperation(definition, "patch", () => patchOperation({ payload, options }), options?.silent);
|
|
180
|
+
},
|
|
181
|
+
remove(payload, options) {
|
|
182
|
+
wrapOperation(definition, "remove", () => removeOperation({ payload }), options?.silent);
|
|
183
|
+
},
|
|
184
|
+
add(payload, options) {
|
|
185
|
+
wrapOperation(definition, "add", () => addOperation({ payload }), options?.silent);
|
|
186
|
+
}
|
|
184
187
|
};
|
|
185
188
|
}
|
|
186
189
|
function isOneDefinition(definition) {
|
|
187
|
-
return definition.
|
|
190
|
+
return definition.type === ModelType.ONE;
|
|
188
191
|
}
|
|
189
|
-
function
|
|
192
|
+
function isManyRecordDefinition(definition) {
|
|
193
|
+
return definition.kind === ModelManyKind.RECORD;
|
|
194
|
+
}
|
|
195
|
+
function resolveCommit(definition, source) {
|
|
190
196
|
if (isOneDefinition(definition)) {
|
|
191
197
|
return createOneCommit(definition, source);
|
|
192
198
|
}
|
|
193
|
-
|
|
199
|
+
if (isManyRecordDefinition(definition)) {
|
|
200
|
+
return createManyRecordCommit(definition, source);
|
|
201
|
+
}
|
|
202
|
+
return createManyListCommit(definition, source);
|
|
194
203
|
}
|
|
195
204
|
export function createModel(definition, source) {
|
|
196
205
|
definition.logger?.debug("Registering model", {
|
|
197
206
|
model: definition.key,
|
|
198
|
-
|
|
207
|
+
type: definition.type
|
|
199
208
|
});
|
|
200
|
-
const
|
|
201
|
-
const
|
|
209
|
+
const commit = resolveCommit(definition, source);
|
|
210
|
+
const aliases = resolveShapeAliases(definition.shape);
|
|
202
211
|
const model = Object.assign(commit, {
|
|
203
212
|
commit(mode, value, options) {
|
|
204
213
|
const handler = commit[mode];
|
|
205
214
|
switch (mode) {
|
|
206
215
|
case ModelOneMode.RESET:
|
|
207
216
|
case ModelManyMode.RESET: {
|
|
208
|
-
handler();
|
|
217
|
+
handler(options);
|
|
209
218
|
break;
|
|
210
219
|
}
|
|
211
220
|
default: {
|
|
@@ -214,7 +223,7 @@ export function createModel(definition, source) {
|
|
|
214
223
|
}
|
|
215
224
|
},
|
|
216
225
|
aliases() {
|
|
217
|
-
return
|
|
226
|
+
return aliases;
|
|
218
227
|
}
|
|
219
228
|
});
|
|
220
229
|
return model;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type { ShapeCall, ShapeDefinition, ShapeInfer,
|
|
2
|
-
export declare function resolveShape(shape: ShapeDefinition): ShapeResolved;
|
|
1
|
+
import type { ShapeCall, ShapeDefinition, ShapeInfer, ShapeRawDefinition, ShapeType } from "../types/shape.js";
|
|
3
2
|
export declare function resolveAliasInbound<T = unknown>(data: T, aliases?: Record<string, string>): T;
|
|
4
3
|
export declare function resolveAliasOutbound<T = unknown>(data: T, aliases?: Record<string, string>): T;
|
|
5
|
-
export declare function
|
|
4
|
+
export declare function resolveShapeIdentifier(shape: ShapeType<unknown>, ...overrides: (string | undefined)[]): string;
|
|
5
|
+
export declare function resolveShapeAliases(shape: ShapeType<unknown>): Record<string, string>;
|
|
6
|
+
export declare function resolveZeroValues<T extends ShapeDefinition>(shape: T): ShapeInfer<T>;
|
|
6
7
|
export declare function createShape<T extends ShapeRawDefinition>(definition: T): ShapeCall<T>;
|
|
@@ -1,77 +1,14 @@
|
|
|
1
1
|
import { defu } from "defu";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { isPlainObject, isEmptyRecord } from "./base.js";
|
|
4
|
-
function
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function resolveNumberZeroValue() {
|
|
8
|
-
return 0;
|
|
9
|
-
}
|
|
10
|
-
function resolveBooleanZeroValue() {
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
|
-
function resolveBigintZeroValue() {
|
|
14
|
-
return BigInt(0);
|
|
15
|
-
}
|
|
16
|
-
function resolveDateZeroValue() {
|
|
17
|
-
return /* @__PURE__ */ new Date(0);
|
|
18
|
-
}
|
|
19
|
-
function resolveArrayZeroValue() {
|
|
20
|
-
return [];
|
|
21
|
-
}
|
|
22
|
-
function resolveRecordZeroValue() {
|
|
23
|
-
return {};
|
|
24
|
-
}
|
|
25
|
-
function resolveMapZeroValue() {
|
|
26
|
-
return /* @__PURE__ */ new Map();
|
|
27
|
-
}
|
|
28
|
-
function resolveSetZeroValue() {
|
|
29
|
-
return /* @__PURE__ */ new Set();
|
|
30
|
-
}
|
|
31
|
-
function resolveObjectZeroValue(definition) {
|
|
32
|
-
const output = {};
|
|
33
|
-
if (definition.shape) {
|
|
34
|
-
for (const [key, value] of Object.entries(definition.shape)) {
|
|
35
|
-
output[key] = resolveZeroValue(value);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return output;
|
|
39
|
-
}
|
|
40
|
-
function resolveEnumZeroValue(definition) {
|
|
41
|
-
if (definition.entries) {
|
|
42
|
-
return Object.values(definition.entries)[0];
|
|
43
|
-
}
|
|
44
|
-
return void 0;
|
|
45
|
-
}
|
|
46
|
-
function resolveLiteralZeroValue(definition) {
|
|
47
|
-
if (definition.values) {
|
|
48
|
-
return definition.values[0];
|
|
49
|
-
}
|
|
50
|
-
return void 0;
|
|
51
|
-
}
|
|
52
|
-
function resolveTupleZeroValue(definition) {
|
|
53
|
-
if (definition.items) {
|
|
54
|
-
return definition.items.map(resolveZeroValue);
|
|
55
|
-
}
|
|
56
|
-
return [];
|
|
57
|
-
}
|
|
58
|
-
function resolveUnionZeroValue(definition) {
|
|
59
|
-
if (definition.options?.[0]) {
|
|
60
|
-
return resolveZeroValue(definition.options[0]);
|
|
61
|
-
}
|
|
62
|
-
return void 0;
|
|
63
|
-
}
|
|
64
|
-
function resolveDefaultZeroValue(definition) {
|
|
65
|
-
if (typeof definition.defaultValue === "function") {
|
|
66
|
-
return definition.defaultValue();
|
|
4
|
+
function resolveShapeFields(shape) {
|
|
5
|
+
if (!("shape" in shape) || typeof shape.shape !== "object" || !shape.shape) {
|
|
6
|
+
return void 0;
|
|
67
7
|
}
|
|
68
|
-
return
|
|
8
|
+
return shape.shape;
|
|
69
9
|
}
|
|
70
|
-
function
|
|
71
|
-
|
|
72
|
-
return resolveZeroValue(definition.innerType);
|
|
73
|
-
}
|
|
74
|
-
return void 0;
|
|
10
|
+
function resolveFieldMeta(field) {
|
|
11
|
+
return field?.meta?.();
|
|
75
12
|
}
|
|
76
13
|
function resolveZeroValue(field) {
|
|
77
14
|
const definition = field.def;
|
|
@@ -80,93 +17,83 @@ function resolveZeroValue(field) {
|
|
|
80
17
|
}
|
|
81
18
|
switch (definition.type) {
|
|
82
19
|
case "string": {
|
|
83
|
-
return
|
|
20
|
+
return "";
|
|
84
21
|
}
|
|
85
22
|
case "number": {
|
|
86
|
-
return
|
|
23
|
+
return 0;
|
|
87
24
|
}
|
|
88
25
|
case "boolean": {
|
|
89
|
-
return
|
|
26
|
+
return false;
|
|
90
27
|
}
|
|
91
28
|
case "bigint": {
|
|
92
|
-
return
|
|
29
|
+
return BigInt(0);
|
|
93
30
|
}
|
|
94
31
|
case "date": {
|
|
95
|
-
return
|
|
32
|
+
return /* @__PURE__ */ new Date(0);
|
|
96
33
|
}
|
|
97
34
|
case "array": {
|
|
98
|
-
return
|
|
35
|
+
return [];
|
|
99
36
|
}
|
|
100
37
|
case "record": {
|
|
101
|
-
return
|
|
38
|
+
return {};
|
|
102
39
|
}
|
|
103
40
|
case "map": {
|
|
104
|
-
return
|
|
41
|
+
return /* @__PURE__ */ new Map();
|
|
105
42
|
}
|
|
106
43
|
case "set": {
|
|
107
|
-
return
|
|
44
|
+
return /* @__PURE__ */ new Set();
|
|
108
45
|
}
|
|
109
46
|
case "object": {
|
|
110
|
-
|
|
47
|
+
const output = {};
|
|
48
|
+
if (definition.shape) {
|
|
49
|
+
for (const [key, value] of Object.entries(definition.shape)) {
|
|
50
|
+
output[key] = resolveZeroValue(value);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return output;
|
|
111
54
|
}
|
|
112
55
|
case "enum": {
|
|
113
|
-
|
|
56
|
+
if (definition.entries) {
|
|
57
|
+
return Object.values(definition.entries)[0];
|
|
58
|
+
}
|
|
59
|
+
return void 0;
|
|
114
60
|
}
|
|
115
61
|
case "literal": {
|
|
116
|
-
|
|
62
|
+
if (definition.values) {
|
|
63
|
+
return definition.values[0];
|
|
64
|
+
}
|
|
65
|
+
return void 0;
|
|
117
66
|
}
|
|
118
67
|
case "tuple": {
|
|
119
|
-
|
|
68
|
+
if (definition.items) {
|
|
69
|
+
return definition.items.map(resolveZeroValue);
|
|
70
|
+
}
|
|
71
|
+
return [];
|
|
120
72
|
}
|
|
121
73
|
case "union": {
|
|
122
|
-
|
|
74
|
+
if (definition.options?.[0]) {
|
|
75
|
+
return resolveZeroValue(definition.options[0]);
|
|
76
|
+
}
|
|
77
|
+
return void 0;
|
|
123
78
|
}
|
|
124
79
|
case "default": {
|
|
125
|
-
|
|
80
|
+
if (typeof definition.defaultValue === "function") {
|
|
81
|
+
return definition.defaultValue();
|
|
82
|
+
}
|
|
83
|
+
return definition.defaultValue;
|
|
126
84
|
}
|
|
127
85
|
case "optional":
|
|
128
86
|
case "nullable": {
|
|
129
|
-
|
|
87
|
+
if (definition.innerType) {
|
|
88
|
+
return resolveZeroValue(definition.innerType);
|
|
89
|
+
}
|
|
90
|
+
return void 0;
|
|
130
91
|
}
|
|
131
92
|
default: {
|
|
132
93
|
return void 0;
|
|
133
94
|
}
|
|
134
95
|
}
|
|
135
96
|
}
|
|
136
|
-
export function resolveShape(shape) {
|
|
137
|
-
const resolved = {
|
|
138
|
-
identifier: void 0,
|
|
139
|
-
defaults: {},
|
|
140
|
-
fields: [],
|
|
141
|
-
aliases: {}
|
|
142
|
-
};
|
|
143
|
-
for (const [key, field] of Object.entries(shape.shape)) {
|
|
144
|
-
resolved.fields.push(key);
|
|
145
|
-
const fieldMeta = field.meta();
|
|
146
|
-
if (fieldMeta?.identifier) {
|
|
147
|
-
resolved.identifier = key;
|
|
148
|
-
}
|
|
149
|
-
if (fieldMeta?.alias) {
|
|
150
|
-
resolved.aliases[key] = fieldMeta.alias;
|
|
151
|
-
}
|
|
152
|
-
const fieldDefinition = field.def;
|
|
153
|
-
if (fieldDefinition?.defaultValue !== void 0) {
|
|
154
|
-
if (typeof fieldDefinition.defaultValue === "function") {
|
|
155
|
-
resolved.defaults[key] = fieldDefinition.defaultValue();
|
|
156
|
-
continue;
|
|
157
|
-
}
|
|
158
|
-
resolved.defaults[key] = fieldDefinition.defaultValue;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
if (!resolved.identifier) {
|
|
162
|
-
if (resolved.fields.includes("id")) {
|
|
163
|
-
resolved.identifier = "id";
|
|
164
|
-
} else if (resolved.fields.includes("_id")) {
|
|
165
|
-
resolved.identifier = "_id";
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
return resolved;
|
|
169
|
-
}
|
|
170
97
|
function resolveAliasObject(data, mapping) {
|
|
171
98
|
const output = {};
|
|
172
99
|
for (const [key, value] of Object.entries(data)) {
|
|
@@ -212,21 +139,54 @@ export function resolveAliasOutbound(data, aliases) {
|
|
|
212
139
|
}
|
|
213
140
|
return data;
|
|
214
141
|
}
|
|
215
|
-
export function
|
|
142
|
+
export function resolveShapeIdentifier(shape, ...overrides) {
|
|
143
|
+
for (const key of overrides) {
|
|
144
|
+
if (key) {
|
|
145
|
+
return key;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const fields = resolveShapeFields(shape);
|
|
149
|
+
if (!fields) {
|
|
150
|
+
return "id";
|
|
151
|
+
}
|
|
152
|
+
for (const [key, field] of Object.entries(fields)) {
|
|
153
|
+
const meta = resolveFieldMeta(field);
|
|
154
|
+
if (meta?.identifier) {
|
|
155
|
+
return key;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return "id";
|
|
159
|
+
}
|
|
160
|
+
export function resolveShapeAliases(shape) {
|
|
161
|
+
const output = {};
|
|
162
|
+
const fields = resolveShapeFields(shape);
|
|
163
|
+
if (!fields) {
|
|
164
|
+
return output;
|
|
165
|
+
}
|
|
166
|
+
for (const [key, field] of Object.entries(fields)) {
|
|
167
|
+
const meta = resolveFieldMeta(field);
|
|
168
|
+
if (meta?.alias) {
|
|
169
|
+
output[key] = meta.alias;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return output;
|
|
173
|
+
}
|
|
174
|
+
export function resolveZeroValues(shape) {
|
|
216
175
|
const output = {};
|
|
217
176
|
for (const [key, field] of Object.entries(shape.shape)) {
|
|
218
177
|
output[key] = resolveZeroValue(field);
|
|
219
178
|
}
|
|
220
|
-
if (overrides) {
|
|
221
|
-
return defu(overrides, output);
|
|
222
|
-
}
|
|
223
179
|
return output;
|
|
224
180
|
}
|
|
225
181
|
export function createShape(definition) {
|
|
226
182
|
const object = z.object(definition);
|
|
227
183
|
const shape = Object.assign(object, {
|
|
228
184
|
defaults(overrides) {
|
|
229
|
-
|
|
185
|
+
const zero = resolveZeroValues(object);
|
|
186
|
+
if (overrides) {
|
|
187
|
+
return defu(overrides, zero);
|
|
188
|
+
}
|
|
189
|
+
return zero;
|
|
230
190
|
}
|
|
231
191
|
});
|
|
232
192
|
return shape;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { createStore as createSourceStore } from "@harlem/core";
|
|
2
|
-
import {
|
|
2
|
+
import type { ModelDefinitions, ModelDefinitionsInfer, StoreModel } from "../types/model.js";
|
|
3
3
|
import type { ViewDefinitions, StoreView } from "../types/view.js";
|
|
4
4
|
import type { ActionDefinition, ActionDefinitions, StoreAction } from "../types/action.js";
|
|
5
5
|
export declare function createStoreState<MD extends ModelDefinitions>(modelDefinitions: MD): ModelDefinitionsInfer<MD>;
|