@ciwergrp/nuxid 1.17.4 → 1.17.8
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/module.json
CHANGED
|
@@ -5,9 +5,9 @@ export async function useCursorHttp(url, options) {
|
|
|
5
5
|
if (!obj || typeof obj !== "object") {
|
|
6
6
|
return sources;
|
|
7
7
|
}
|
|
8
|
-
for (const
|
|
9
|
-
if (Object.prototype.hasOwnProperty.call(obj,
|
|
10
|
-
const value = obj[
|
|
8
|
+
for (const key2 in obj) {
|
|
9
|
+
if (Object.prototype.hasOwnProperty.call(obj, key2)) {
|
|
10
|
+
const value = obj[key2];
|
|
11
11
|
if (isRef(value)) {
|
|
12
12
|
sources.push(value);
|
|
13
13
|
} else if (typeof value === "object") {
|
|
@@ -22,15 +22,15 @@ export async function useCursorHttp(url, options) {
|
|
|
22
22
|
return obj;
|
|
23
23
|
}
|
|
24
24
|
const unwrapped = Array.isArray(obj) ? [] : {};
|
|
25
|
-
for (const
|
|
26
|
-
if (Object.prototype.hasOwnProperty.call(obj,
|
|
27
|
-
const value = obj[
|
|
25
|
+
for (const key2 in obj) {
|
|
26
|
+
if (Object.prototype.hasOwnProperty.call(obj, key2)) {
|
|
27
|
+
const value = obj[key2];
|
|
28
28
|
if (isRef(value)) {
|
|
29
|
-
unwrapped[
|
|
29
|
+
unwrapped[key2] = value.value;
|
|
30
30
|
} else if (typeof value === "object") {
|
|
31
|
-
unwrapped[
|
|
31
|
+
unwrapped[key2] = unwrapReactiveObject(value);
|
|
32
32
|
} else {
|
|
33
|
-
unwrapped[
|
|
33
|
+
unwrapped[key2] = value;
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
}
|
|
@@ -56,9 +56,11 @@ export async function useCursorHttp(url, options) {
|
|
|
56
56
|
pollInterval,
|
|
57
57
|
fetcher,
|
|
58
58
|
fetchOptions,
|
|
59
|
+
query,
|
|
59
60
|
meta,
|
|
60
61
|
itemKey = "id",
|
|
61
|
-
cursorParam = "cursor"
|
|
62
|
+
cursorParam = "cursor",
|
|
63
|
+
key
|
|
62
64
|
} = options ?? {};
|
|
63
65
|
const initialUrl = url;
|
|
64
66
|
const data = ref();
|
|
@@ -70,6 +72,24 @@ export async function useCursorHttp(url, options) {
|
|
|
70
72
|
const currentParams = shallowRef(
|
|
71
73
|
unwrapReactiveObject(fetchOptions) ?? {}
|
|
72
74
|
);
|
|
75
|
+
const normalizeStateKey = (value, fallback) => {
|
|
76
|
+
if (typeof value !== "string") {
|
|
77
|
+
return fallback;
|
|
78
|
+
}
|
|
79
|
+
const trimmed = value.trim();
|
|
80
|
+
return trimmed.length > 0 ? trimmed : fallback;
|
|
81
|
+
};
|
|
82
|
+
const stateKey = normalizeStateKey(key, (() => {
|
|
83
|
+
try {
|
|
84
|
+
const queryValue = query ?? currentParams.value?.query ?? {};
|
|
85
|
+
return `${String(url)}:${JSON.stringify(queryValue)}`;
|
|
86
|
+
} catch {
|
|
87
|
+
return String(url);
|
|
88
|
+
}
|
|
89
|
+
})());
|
|
90
|
+
const { useNuxtApp, useState } = await import("#app");
|
|
91
|
+
const nuxtApp = useNuxtApp();
|
|
92
|
+
const initialResponseState = useState(stateKey, () => null);
|
|
73
93
|
let pollTimer = null;
|
|
74
94
|
let fetchPromise = null;
|
|
75
95
|
const fetcherFn = fetcher ?? globalThis.$fetch;
|
|
@@ -97,6 +117,18 @@ export async function useCursorHttp(url, options) {
|
|
|
97
117
|
}
|
|
98
118
|
return String(value);
|
|
99
119
|
};
|
|
120
|
+
const cloneResponse = (response) => ({
|
|
121
|
+
...response,
|
|
122
|
+
data: [...response.data]
|
|
123
|
+
});
|
|
124
|
+
const applyResponse = (response, cacheInitial = false) => {
|
|
125
|
+
data.value = cloneResponse(response);
|
|
126
|
+
nextCursor.value = normalizeCursorValue(response.meta?.[cursorKey]);
|
|
127
|
+
hasNextPage.value = response.meta?.[hasMoreKey] ?? false;
|
|
128
|
+
if (cacheInitial) {
|
|
129
|
+
initialResponseState.value = cloneResponse(response);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
100
132
|
const fetchData = async (fetchUrl, params) => {
|
|
101
133
|
if (loading.value) {
|
|
102
134
|
return fetchPromise ?? Promise.resolve();
|
|
@@ -130,7 +162,36 @@ export async function useCursorHttp(url, options) {
|
|
|
130
162
|
}
|
|
131
163
|
}
|
|
132
164
|
};
|
|
133
|
-
const
|
|
165
|
+
const fetchInitialData = async (params, useCache = true) => {
|
|
166
|
+
if (loading.value) {
|
|
167
|
+
return fetchPromise ?? Promise.resolve();
|
|
168
|
+
}
|
|
169
|
+
const canUseHydrationCache = import.meta.client && nuxtApp.isHydrating && nuxtApp.payload.serverRendered;
|
|
170
|
+
if (useCache && canUseHydrationCache && initialResponseState.value) {
|
|
171
|
+
applyResponse(initialResponseState.value);
|
|
172
|
+
return Promise.resolve();
|
|
173
|
+
}
|
|
174
|
+
loading.value = true;
|
|
175
|
+
error.value = null;
|
|
176
|
+
const promise = (async () => {
|
|
177
|
+
try {
|
|
178
|
+
const response = await fetcherFn(initialUrl, params ?? currentParams.value);
|
|
179
|
+
applyResponse(response, true);
|
|
180
|
+
} catch (e) {
|
|
181
|
+
error.value = normalizeFetchError(e);
|
|
182
|
+
} finally {
|
|
183
|
+
loading.value = false;
|
|
184
|
+
}
|
|
185
|
+
})();
|
|
186
|
+
fetchPromise = promise;
|
|
187
|
+
try {
|
|
188
|
+
await promise;
|
|
189
|
+
} finally {
|
|
190
|
+
if (fetchPromise === promise) {
|
|
191
|
+
fetchPromise = null;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
};
|
|
134
195
|
const pollData = async () => {
|
|
135
196
|
try {
|
|
136
197
|
const response = await fetcherFn(initialUrl, currentParams.value);
|
|
@@ -145,9 +206,7 @@ export async function useCursorHttp(url, options) {
|
|
|
145
206
|
data.value.data.unshift(...newItems);
|
|
146
207
|
}
|
|
147
208
|
} else {
|
|
148
|
-
|
|
149
|
-
nextCursor.value = normalizeCursorValue(response.meta?.[cursorKey]);
|
|
150
|
-
hasNextPage.value = response.meta?.[hasMoreKey] ?? false;
|
|
209
|
+
applyResponse(response, true);
|
|
151
210
|
}
|
|
152
211
|
} catch (e) {
|
|
153
212
|
console.error("Polling error:", e);
|
|
@@ -173,13 +232,13 @@ export async function useCursorHttp(url, options) {
|
|
|
173
232
|
nextCursor.value = null;
|
|
174
233
|
hasNextPage.value = true;
|
|
175
234
|
isLoadMoreTriggered.value = false;
|
|
176
|
-
await
|
|
235
|
+
await fetchInitialData(currentParams.value, false);
|
|
177
236
|
};
|
|
178
237
|
const init = async () => {
|
|
179
238
|
if (data.value) {
|
|
180
239
|
return;
|
|
181
240
|
}
|
|
182
|
-
await
|
|
241
|
+
await fetchInitialData(currentParams.value);
|
|
183
242
|
};
|
|
184
243
|
const reactiveSources = findReactiveSources(fetchOptions);
|
|
185
244
|
if (reactiveSources.length > 0) {
|
|
@@ -203,10 +262,10 @@ export async function useCursorHttp(url, options) {
|
|
|
203
262
|
if (immediate) {
|
|
204
263
|
if (import.meta.client && lazy) {
|
|
205
264
|
onBeforeMount(() => {
|
|
206
|
-
void
|
|
265
|
+
void fetchInitialData();
|
|
207
266
|
});
|
|
208
267
|
} else {
|
|
209
|
-
await
|
|
268
|
+
await fetchInitialData();
|
|
210
269
|
}
|
|
211
270
|
}
|
|
212
271
|
return {
|