@jay-framework/stack-client-runtime 0.8.0 → 0.10.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/dist/index.cjs +206 -20
- package/dist/index.d.ts +68 -2
- package/dist/index.js +207 -21
- package/package.json +7 -6
package/dist/index.cjs
CHANGED
|
@@ -1,38 +1,128 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
|
+
var __publicField = (obj, key, value) => {
|
|
5
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
+
return value;
|
|
7
|
+
};
|
|
2
8
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
9
|
const component = require("@jay-framework/component");
|
|
4
|
-
function
|
|
5
|
-
|
|
6
|
-
|
|
10
|
+
function deepMergeViewStates(base, overlay, trackByMap, path = "") {
|
|
11
|
+
if (!base && !overlay)
|
|
12
|
+
return {};
|
|
13
|
+
if (!base)
|
|
14
|
+
return overlay || {};
|
|
15
|
+
if (!overlay)
|
|
16
|
+
return base || {};
|
|
17
|
+
const result = {};
|
|
18
|
+
const allKeys = /* @__PURE__ */ new Set([...Object.keys(base), ...Object.keys(overlay)]);
|
|
19
|
+
for (const key of allKeys) {
|
|
20
|
+
const baseValue = base[key];
|
|
21
|
+
const overlayValue = overlay[key];
|
|
22
|
+
const currentPath = path ? `${path}.${key}` : key;
|
|
23
|
+
if (overlayValue === void 0) {
|
|
24
|
+
result[key] = baseValue;
|
|
25
|
+
} else if (baseValue === void 0) {
|
|
26
|
+
result[key] = overlayValue;
|
|
27
|
+
} else if (Array.isArray(baseValue) && Array.isArray(overlayValue)) {
|
|
28
|
+
const trackByField = trackByMap[currentPath];
|
|
29
|
+
if (trackByField) {
|
|
30
|
+
result[key] = mergeArraysByTrackBy(
|
|
31
|
+
baseValue,
|
|
32
|
+
overlayValue,
|
|
33
|
+
trackByField,
|
|
34
|
+
trackByMap,
|
|
35
|
+
currentPath
|
|
36
|
+
);
|
|
37
|
+
} else {
|
|
38
|
+
result[key] = overlayValue;
|
|
39
|
+
}
|
|
40
|
+
} else if (typeof baseValue === "object" && baseValue !== null && typeof overlayValue === "object" && overlayValue !== null && !Array.isArray(baseValue) && !Array.isArray(overlayValue)) {
|
|
41
|
+
result[key] = deepMergeViewStates(baseValue, overlayValue, trackByMap, currentPath);
|
|
42
|
+
} else {
|
|
43
|
+
result[key] = overlayValue;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
function mergeArraysByTrackBy(baseArray, overlayArray, trackByField, trackByMap, arrayPath) {
|
|
49
|
+
const baseByKey = /* @__PURE__ */ new Map();
|
|
50
|
+
for (const item of baseArray) {
|
|
51
|
+
const key = item[trackByField];
|
|
52
|
+
if (key !== void 0 && key !== null) {
|
|
53
|
+
if (baseByKey.has(key)) {
|
|
54
|
+
console.warn(
|
|
55
|
+
`Duplicate trackBy key [${key}] in base array at path [${arrayPath}]. This may cause incorrect merging.`
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
baseByKey.set(key, item);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const overlayByKey = /* @__PURE__ */ new Map();
|
|
62
|
+
for (const item of overlayArray) {
|
|
63
|
+
const key = item[trackByField];
|
|
64
|
+
if (key !== void 0 && key !== null) {
|
|
65
|
+
overlayByKey.set(key, item);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return baseArray.map((baseItem) => {
|
|
69
|
+
const key = baseItem[trackByField];
|
|
70
|
+
if (key === void 0 || key === null) {
|
|
71
|
+
return baseItem;
|
|
72
|
+
}
|
|
73
|
+
const overlayItem = overlayByKey.get(key);
|
|
74
|
+
if (overlayItem) {
|
|
75
|
+
return deepMergeViewStates(baseItem, overlayItem, trackByMap, arrayPath);
|
|
76
|
+
} else {
|
|
77
|
+
return baseItem;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function makeSignals(obj) {
|
|
82
|
+
return Object.keys(obj).reduce((signals, key) => {
|
|
83
|
+
signals[key] = component.createSignal(obj[key]);
|
|
7
84
|
return signals;
|
|
8
85
|
}, {});
|
|
9
86
|
}
|
|
10
|
-
function makeCompositeJayComponent(preRender, defaultViewState, fastCarryForward, parts) {
|
|
87
|
+
function makeCompositeJayComponent(preRender, defaultViewState, fastCarryForward, parts, trackByMap = {}) {
|
|
88
|
+
const hasFastRendering = defaultViewState !== null && defaultViewState !== void 0;
|
|
11
89
|
const comp = (props, refs, ...contexts) => {
|
|
12
90
|
const instances = parts.map((part) => {
|
|
13
91
|
const partRefs = part.key ? refs[part.key] : refs;
|
|
14
|
-
let
|
|
15
|
-
if (
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
partCarryForward =
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
92
|
+
let partContexts;
|
|
93
|
+
if (hasFastRendering) {
|
|
94
|
+
const partViewState = part.key ? defaultViewState?.[part.key] : defaultViewState;
|
|
95
|
+
const partFastViewState = partViewState ? makeSignals(partViewState) : void 0;
|
|
96
|
+
const partCarryForward = part.key ? fastCarryForward?.[part.key] : fastCarryForward;
|
|
97
|
+
partContexts = [
|
|
98
|
+
partFastViewState,
|
|
99
|
+
partCarryForward,
|
|
100
|
+
...contexts.splice(0, part.contextMarkers.length)
|
|
101
|
+
];
|
|
102
|
+
} else {
|
|
103
|
+
partContexts = [...contexts.splice(0, part.contextMarkers.length)];
|
|
104
|
+
}
|
|
23
105
|
return [part.key, part.comp(props, partRefs, ...partContexts)];
|
|
24
106
|
});
|
|
25
107
|
return {
|
|
26
108
|
render: () => {
|
|
27
109
|
let viewState = defaultViewState;
|
|
28
110
|
instances.forEach(([key, instance]) => {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
111
|
+
const rendered = component.materializeViewState(instance.render());
|
|
112
|
+
if (key) {
|
|
113
|
+
viewState[key] = deepMergeViewStates(
|
|
114
|
+
defaultViewState[key],
|
|
115
|
+
rendered,
|
|
116
|
+
trackByMap,
|
|
117
|
+
key
|
|
118
|
+
);
|
|
119
|
+
} else {
|
|
120
|
+
viewState = deepMergeViewStates(
|
|
121
|
+
viewState,
|
|
122
|
+
rendered,
|
|
123
|
+
trackByMap
|
|
124
|
+
);
|
|
125
|
+
}
|
|
36
126
|
});
|
|
37
127
|
return viewState;
|
|
38
128
|
}
|
|
@@ -47,4 +137,100 @@ function makeCompositeJayComponent(preRender, defaultViewState, fastCarryForward
|
|
|
47
137
|
...contextMarkers
|
|
48
138
|
);
|
|
49
139
|
}
|
|
140
|
+
const ACTION_ENDPOINT_BASE = "/_jay/actions";
|
|
141
|
+
class ActionError extends Error {
|
|
142
|
+
constructor(code, message) {
|
|
143
|
+
super(message);
|
|
144
|
+
__publicField(this, "name", "ActionError");
|
|
145
|
+
this.code = code;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
let globalOptions = {};
|
|
149
|
+
function setActionCallerOptions(options) {
|
|
150
|
+
globalOptions = { ...globalOptions, ...options };
|
|
151
|
+
}
|
|
152
|
+
function createActionCaller(actionName, method = "POST") {
|
|
153
|
+
return async (input) => {
|
|
154
|
+
const baseUrl = globalOptions.baseUrl ?? "";
|
|
155
|
+
const url = buildActionUrl(baseUrl, actionName, method, input);
|
|
156
|
+
const fetchOptions = {
|
|
157
|
+
method,
|
|
158
|
+
headers: {
|
|
159
|
+
"Content-Type": "application/json",
|
|
160
|
+
...globalOptions.headers
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
if (method !== "GET") {
|
|
164
|
+
fetchOptions.body = JSON.stringify(input);
|
|
165
|
+
}
|
|
166
|
+
const timeout = globalOptions.timeout ?? 3e4;
|
|
167
|
+
const controller = new AbortController();
|
|
168
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
169
|
+
fetchOptions.signal = controller.signal;
|
|
170
|
+
try {
|
|
171
|
+
const response = await fetch(url, fetchOptions);
|
|
172
|
+
clearTimeout(timeoutId);
|
|
173
|
+
const data = await response.json();
|
|
174
|
+
if (data.success) {
|
|
175
|
+
return data.data;
|
|
176
|
+
} else {
|
|
177
|
+
if (data.error) {
|
|
178
|
+
throw new ActionError(data.error.code, data.error.message);
|
|
179
|
+
}
|
|
180
|
+
throw new ActionError("UNKNOWN_ERROR", "Unknown error occurred");
|
|
181
|
+
}
|
|
182
|
+
} catch (error) {
|
|
183
|
+
clearTimeout(timeoutId);
|
|
184
|
+
if (error instanceof ActionError) {
|
|
185
|
+
throw error;
|
|
186
|
+
}
|
|
187
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
188
|
+
throw new ActionError(
|
|
189
|
+
"TIMEOUT",
|
|
190
|
+
`Action '${actionName}' timed out after ${timeout}ms`
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
if (error instanceof TypeError) {
|
|
194
|
+
throw new ActionError(
|
|
195
|
+
"NETWORK_ERROR",
|
|
196
|
+
`Network error calling '${actionName}': ${error.message}`
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function buildActionUrl(baseUrl, actionName, method, input) {
|
|
204
|
+
const path = `${ACTION_ENDPOINT_BASE}/${actionName}`;
|
|
205
|
+
const fullUrl = `${baseUrl}${path}`;
|
|
206
|
+
if (method === "GET" && input !== void 0 && input !== null) {
|
|
207
|
+
const params = new URLSearchParams();
|
|
208
|
+
if (isSimpleObject(input)) {
|
|
209
|
+
for (const [key, value] of Object.entries(input)) {
|
|
210
|
+
if (value !== void 0) {
|
|
211
|
+
params.append(key, String(value));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
} else {
|
|
215
|
+
params.append("_input", JSON.stringify(input));
|
|
216
|
+
}
|
|
217
|
+
const queryString = params.toString();
|
|
218
|
+
return queryString ? `${fullUrl}?${queryString}` : fullUrl;
|
|
219
|
+
}
|
|
220
|
+
return fullUrl;
|
|
221
|
+
}
|
|
222
|
+
function isSimpleObject(obj) {
|
|
223
|
+
if (typeof obj !== "object" || obj === null) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
for (const value of Object.values(obj)) {
|
|
227
|
+
if (typeof value === "object" && value !== null) {
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
exports.ActionError = ActionError;
|
|
234
|
+
exports.createActionCaller = createActionCaller;
|
|
50
235
|
exports.makeCompositeJayComponent = makeCompositeJayComponent;
|
|
236
|
+
exports.setActionCallerOptions = setActionCallerOptions;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as _jay_framework_component from '@jay-framework/component';
|
|
2
2
|
import { ComponentConstructor, ContextMarkers, JayComponentCore } from '@jay-framework/component';
|
|
3
3
|
import { JayElement, PreRenderElement } from '@jay-framework/runtime';
|
|
4
|
+
import { TrackByMap } from '@jay-framework/view-state-merge';
|
|
4
5
|
|
|
5
6
|
interface CompositePart {
|
|
6
7
|
comp: ComponentConstructor<any, any, any, any, any>;
|
|
@@ -8,6 +9,71 @@ interface CompositePart {
|
|
|
8
9
|
key?: string;
|
|
9
10
|
}
|
|
10
11
|
|
|
11
|
-
declare function makeCompositeJayComponent<PropsT extends object, ViewState extends object, Refs extends object, JayElementT extends JayElement<ViewState, Refs>, CompCore extends JayComponentCore<PropsT, ViewState>>(preRender: PreRenderElement<ViewState, Refs, JayElementT>, defaultViewState: ViewState, fastCarryForward: object, parts: Array<CompositePart
|
|
12
|
+
declare function makeCompositeJayComponent<PropsT extends object, ViewState extends object, Refs extends object, JayElementT extends JayElement<ViewState, Refs>, CompCore extends JayComponentCore<PropsT, ViewState>>(preRender: PreRenderElement<ViewState, Refs, JayElementT>, defaultViewState: ViewState, fastCarryForward: object, parts: Array<CompositePart>, trackByMap?: TrackByMap): (props: PropsT) => _jay_framework_component.ConcreteJayComponent<PropsT, ViewState, Refs, CompCore, JayElementT>;
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Client-side action caller for Jay Stack.
|
|
16
|
+
*
|
|
17
|
+
* This module provides the client-side implementation for calling server actions.
|
|
18
|
+
* It replaces the server-side action handler when the action is imported in client code.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* HTTP method types supported by actions.
|
|
22
|
+
*/
|
|
23
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
24
|
+
/**
|
|
25
|
+
* Error thrown when an action fails on the server.
|
|
26
|
+
* Recreated on the client from the error response.
|
|
27
|
+
*/
|
|
28
|
+
declare class ActionError extends Error {
|
|
29
|
+
readonly code: string;
|
|
30
|
+
readonly name = "ActionError";
|
|
31
|
+
constructor(code: string, message: string);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Options for configuring the action caller.
|
|
35
|
+
*/
|
|
36
|
+
interface ActionCallerOptions {
|
|
37
|
+
/** Base URL for action endpoints (default: '') */
|
|
38
|
+
baseUrl?: string;
|
|
39
|
+
/** Custom headers to include with requests */
|
|
40
|
+
headers?: Record<string, string>;
|
|
41
|
+
/** Request timeout in milliseconds (default: 30000) */
|
|
42
|
+
timeout?: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Sets global options for all action callers.
|
|
46
|
+
*
|
|
47
|
+
* @param options - Options to apply globally
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* setActionCallerOptions({
|
|
52
|
+
* baseUrl: 'https://api.example.com',
|
|
53
|
+
* headers: { 'X-Custom-Header': 'value' },
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare function setActionCallerOptions(options: ActionCallerOptions): void;
|
|
58
|
+
/**
|
|
59
|
+
* Creates a client-side action caller that makes HTTP requests to the server.
|
|
60
|
+
*
|
|
61
|
+
* This function is used by the build transform to replace server-side action handlers
|
|
62
|
+
* with client-side HTTP callers.
|
|
63
|
+
*
|
|
64
|
+
* @param actionName - The unique action name (matches server registration)
|
|
65
|
+
* @param method - The HTTP method (default: POST)
|
|
66
|
+
* @returns A callable function that makes the HTTP request
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* // Build transform replaces:
|
|
71
|
+
* import { addToCart } from './actions/cart.actions';
|
|
72
|
+
*
|
|
73
|
+
* // With:
|
|
74
|
+
* const addToCart = createActionCaller<{productId: string}, {cartCount: number}>('cart.addToCart', 'POST');
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
declare function createActionCaller<Input, Output>(actionName: string, method?: HttpMethod): (input: Input) => Promise<Output>;
|
|
78
|
+
|
|
79
|
+
export { type ActionCallerOptions, ActionError, type CompositePart, type HttpMethod, createActionCaller, makeCompositeJayComponent, setActionCallerOptions };
|
package/dist/index.js
CHANGED
|
@@ -1,36 +1,126 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => {
|
|
4
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
return value;
|
|
6
|
+
};
|
|
1
7
|
import { makeJayComponent, materializeViewState, createSignal } from "@jay-framework/component";
|
|
2
|
-
function
|
|
3
|
-
|
|
4
|
-
|
|
8
|
+
function deepMergeViewStates(base, overlay, trackByMap, path = "") {
|
|
9
|
+
if (!base && !overlay)
|
|
10
|
+
return {};
|
|
11
|
+
if (!base)
|
|
12
|
+
return overlay || {};
|
|
13
|
+
if (!overlay)
|
|
14
|
+
return base || {};
|
|
15
|
+
const result = {};
|
|
16
|
+
const allKeys = /* @__PURE__ */ new Set([...Object.keys(base), ...Object.keys(overlay)]);
|
|
17
|
+
for (const key of allKeys) {
|
|
18
|
+
const baseValue = base[key];
|
|
19
|
+
const overlayValue = overlay[key];
|
|
20
|
+
const currentPath = path ? `${path}.${key}` : key;
|
|
21
|
+
if (overlayValue === void 0) {
|
|
22
|
+
result[key] = baseValue;
|
|
23
|
+
} else if (baseValue === void 0) {
|
|
24
|
+
result[key] = overlayValue;
|
|
25
|
+
} else if (Array.isArray(baseValue) && Array.isArray(overlayValue)) {
|
|
26
|
+
const trackByField = trackByMap[currentPath];
|
|
27
|
+
if (trackByField) {
|
|
28
|
+
result[key] = mergeArraysByTrackBy(
|
|
29
|
+
baseValue,
|
|
30
|
+
overlayValue,
|
|
31
|
+
trackByField,
|
|
32
|
+
trackByMap,
|
|
33
|
+
currentPath
|
|
34
|
+
);
|
|
35
|
+
} else {
|
|
36
|
+
result[key] = overlayValue;
|
|
37
|
+
}
|
|
38
|
+
} else if (typeof baseValue === "object" && baseValue !== null && typeof overlayValue === "object" && overlayValue !== null && !Array.isArray(baseValue) && !Array.isArray(overlayValue)) {
|
|
39
|
+
result[key] = deepMergeViewStates(baseValue, overlayValue, trackByMap, currentPath);
|
|
40
|
+
} else {
|
|
41
|
+
result[key] = overlayValue;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
function mergeArraysByTrackBy(baseArray, overlayArray, trackByField, trackByMap, arrayPath) {
|
|
47
|
+
const baseByKey = /* @__PURE__ */ new Map();
|
|
48
|
+
for (const item of baseArray) {
|
|
49
|
+
const key = item[trackByField];
|
|
50
|
+
if (key !== void 0 && key !== null) {
|
|
51
|
+
if (baseByKey.has(key)) {
|
|
52
|
+
console.warn(
|
|
53
|
+
`Duplicate trackBy key [${key}] in base array at path [${arrayPath}]. This may cause incorrect merging.`
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
baseByKey.set(key, item);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const overlayByKey = /* @__PURE__ */ new Map();
|
|
60
|
+
for (const item of overlayArray) {
|
|
61
|
+
const key = item[trackByField];
|
|
62
|
+
if (key !== void 0 && key !== null) {
|
|
63
|
+
overlayByKey.set(key, item);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return baseArray.map((baseItem) => {
|
|
67
|
+
const key = baseItem[trackByField];
|
|
68
|
+
if (key === void 0 || key === null) {
|
|
69
|
+
return baseItem;
|
|
70
|
+
}
|
|
71
|
+
const overlayItem = overlayByKey.get(key);
|
|
72
|
+
if (overlayItem) {
|
|
73
|
+
return deepMergeViewStates(baseItem, overlayItem, trackByMap, arrayPath);
|
|
74
|
+
} else {
|
|
75
|
+
return baseItem;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function makeSignals(obj) {
|
|
80
|
+
return Object.keys(obj).reduce((signals, key) => {
|
|
81
|
+
signals[key] = createSignal(obj[key]);
|
|
5
82
|
return signals;
|
|
6
83
|
}, {});
|
|
7
84
|
}
|
|
8
|
-
function makeCompositeJayComponent(preRender, defaultViewState, fastCarryForward, parts) {
|
|
85
|
+
function makeCompositeJayComponent(preRender, defaultViewState, fastCarryForward, parts, trackByMap = {}) {
|
|
86
|
+
const hasFastRendering = defaultViewState !== null && defaultViewState !== void 0;
|
|
9
87
|
const comp = (props, refs, ...contexts) => {
|
|
10
88
|
const instances = parts.map((part) => {
|
|
11
89
|
const partRefs = part.key ? refs[part.key] : refs;
|
|
12
|
-
let
|
|
13
|
-
if (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
partCarryForward =
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
90
|
+
let partContexts;
|
|
91
|
+
if (hasFastRendering) {
|
|
92
|
+
const partViewState = part.key ? defaultViewState?.[part.key] : defaultViewState;
|
|
93
|
+
const partFastViewState = partViewState ? makeSignals(partViewState) : void 0;
|
|
94
|
+
const partCarryForward = part.key ? fastCarryForward?.[part.key] : fastCarryForward;
|
|
95
|
+
partContexts = [
|
|
96
|
+
partFastViewState,
|
|
97
|
+
partCarryForward,
|
|
98
|
+
...contexts.splice(0, part.contextMarkers.length)
|
|
99
|
+
];
|
|
100
|
+
} else {
|
|
101
|
+
partContexts = [...contexts.splice(0, part.contextMarkers.length)];
|
|
102
|
+
}
|
|
21
103
|
return [part.key, part.comp(props, partRefs, ...partContexts)];
|
|
22
104
|
});
|
|
23
105
|
return {
|
|
24
106
|
render: () => {
|
|
25
107
|
let viewState = defaultViewState;
|
|
26
108
|
instances.forEach(([key, instance]) => {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
109
|
+
const rendered = materializeViewState(instance.render());
|
|
110
|
+
if (key) {
|
|
111
|
+
viewState[key] = deepMergeViewStates(
|
|
112
|
+
defaultViewState[key],
|
|
113
|
+
rendered,
|
|
114
|
+
trackByMap,
|
|
115
|
+
key
|
|
116
|
+
);
|
|
117
|
+
} else {
|
|
118
|
+
viewState = deepMergeViewStates(
|
|
119
|
+
viewState,
|
|
120
|
+
rendered,
|
|
121
|
+
trackByMap
|
|
122
|
+
);
|
|
123
|
+
}
|
|
34
124
|
});
|
|
35
125
|
return viewState;
|
|
36
126
|
}
|
|
@@ -45,6 +135,102 @@ function makeCompositeJayComponent(preRender, defaultViewState, fastCarryForward
|
|
|
45
135
|
...contextMarkers
|
|
46
136
|
);
|
|
47
137
|
}
|
|
138
|
+
const ACTION_ENDPOINT_BASE = "/_jay/actions";
|
|
139
|
+
class ActionError extends Error {
|
|
140
|
+
constructor(code, message) {
|
|
141
|
+
super(message);
|
|
142
|
+
__publicField(this, "name", "ActionError");
|
|
143
|
+
this.code = code;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
let globalOptions = {};
|
|
147
|
+
function setActionCallerOptions(options) {
|
|
148
|
+
globalOptions = { ...globalOptions, ...options };
|
|
149
|
+
}
|
|
150
|
+
function createActionCaller(actionName, method = "POST") {
|
|
151
|
+
return async (input) => {
|
|
152
|
+
const baseUrl = globalOptions.baseUrl ?? "";
|
|
153
|
+
const url = buildActionUrl(baseUrl, actionName, method, input);
|
|
154
|
+
const fetchOptions = {
|
|
155
|
+
method,
|
|
156
|
+
headers: {
|
|
157
|
+
"Content-Type": "application/json",
|
|
158
|
+
...globalOptions.headers
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
if (method !== "GET") {
|
|
162
|
+
fetchOptions.body = JSON.stringify(input);
|
|
163
|
+
}
|
|
164
|
+
const timeout = globalOptions.timeout ?? 3e4;
|
|
165
|
+
const controller = new AbortController();
|
|
166
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
167
|
+
fetchOptions.signal = controller.signal;
|
|
168
|
+
try {
|
|
169
|
+
const response = await fetch(url, fetchOptions);
|
|
170
|
+
clearTimeout(timeoutId);
|
|
171
|
+
const data = await response.json();
|
|
172
|
+
if (data.success) {
|
|
173
|
+
return data.data;
|
|
174
|
+
} else {
|
|
175
|
+
if (data.error) {
|
|
176
|
+
throw new ActionError(data.error.code, data.error.message);
|
|
177
|
+
}
|
|
178
|
+
throw new ActionError("UNKNOWN_ERROR", "Unknown error occurred");
|
|
179
|
+
}
|
|
180
|
+
} catch (error) {
|
|
181
|
+
clearTimeout(timeoutId);
|
|
182
|
+
if (error instanceof ActionError) {
|
|
183
|
+
throw error;
|
|
184
|
+
}
|
|
185
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
186
|
+
throw new ActionError(
|
|
187
|
+
"TIMEOUT",
|
|
188
|
+
`Action '${actionName}' timed out after ${timeout}ms`
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
if (error instanceof TypeError) {
|
|
192
|
+
throw new ActionError(
|
|
193
|
+
"NETWORK_ERROR",
|
|
194
|
+
`Network error calling '${actionName}': ${error.message}`
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function buildActionUrl(baseUrl, actionName, method, input) {
|
|
202
|
+
const path = `${ACTION_ENDPOINT_BASE}/${actionName}`;
|
|
203
|
+
const fullUrl = `${baseUrl}${path}`;
|
|
204
|
+
if (method === "GET" && input !== void 0 && input !== null) {
|
|
205
|
+
const params = new URLSearchParams();
|
|
206
|
+
if (isSimpleObject(input)) {
|
|
207
|
+
for (const [key, value] of Object.entries(input)) {
|
|
208
|
+
if (value !== void 0) {
|
|
209
|
+
params.append(key, String(value));
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
} else {
|
|
213
|
+
params.append("_input", JSON.stringify(input));
|
|
214
|
+
}
|
|
215
|
+
const queryString = params.toString();
|
|
216
|
+
return queryString ? `${fullUrl}?${queryString}` : fullUrl;
|
|
217
|
+
}
|
|
218
|
+
return fullUrl;
|
|
219
|
+
}
|
|
220
|
+
function isSimpleObject(obj) {
|
|
221
|
+
if (typeof obj !== "object" || obj === null) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
for (const value of Object.values(obj)) {
|
|
225
|
+
if (typeof value === "object" && value !== null) {
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
48
231
|
export {
|
|
49
|
-
|
|
232
|
+
ActionError,
|
|
233
|
+
createActionCaller,
|
|
234
|
+
makeCompositeJayComponent,
|
|
235
|
+
setActionCallerOptions
|
|
50
236
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jay-framework/stack-client-runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -27,13 +27,14 @@
|
|
|
27
27
|
"test:watch": "vitest"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@jay-framework/component": "^0.
|
|
31
|
-
"@jay-framework/fullstack-component": "^0.
|
|
32
|
-
"@jay-framework/runtime": "^0.
|
|
30
|
+
"@jay-framework/component": "^0.10.0",
|
|
31
|
+
"@jay-framework/fullstack-component": "^0.10.0",
|
|
32
|
+
"@jay-framework/runtime": "^0.10.0",
|
|
33
|
+
"@jay-framework/view-state-merge": "^0.10.0"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
|
-
"@jay-framework/dev-environment": "^0.
|
|
36
|
-
"@jay-framework/jay-cli": "^0.
|
|
36
|
+
"@jay-framework/dev-environment": "^0.10.0",
|
|
37
|
+
"@jay-framework/jay-cli": "^0.10.0",
|
|
37
38
|
"@types/express": "^5.0.2",
|
|
38
39
|
"@types/node": "^22.15.21",
|
|
39
40
|
"nodemon": "^3.0.3",
|