@svadmin/core 0.21.2 → 0.22.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/package.json +1 -1
- package/src/query-hooks.svelte.ts +66 -109
package/package.json
CHANGED
|
@@ -25,13 +25,16 @@ function extendQuery<Q extends object, E extends Record<string, unknown>>(
|
|
|
25
25
|
extensions: () => E,
|
|
26
26
|
): Q & E {
|
|
27
27
|
return new Proxy(query, {
|
|
28
|
-
get(target, prop
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
get(target, prop) {
|
|
29
|
+
if (typeof prop === 'string') {
|
|
30
|
+
const ext = extensions();
|
|
31
|
+
if (prop in ext) return ext[prop as keyof E];
|
|
32
|
+
}
|
|
33
|
+
return target[prop as keyof Q];
|
|
32
34
|
},
|
|
33
35
|
has(target, prop) {
|
|
34
|
-
|
|
36
|
+
if (typeof prop === 'string' && prop in extensions()) return true;
|
|
37
|
+
return prop in target;
|
|
35
38
|
},
|
|
36
39
|
}) as Q & E;
|
|
37
40
|
}
|
|
@@ -56,53 +59,48 @@ export interface UseListOptions<TData extends BaseRecord = BaseRecord, TError =
|
|
|
56
59
|
|
|
57
60
|
export function useList<TData extends BaseRecord = BaseRecord, TError = HttpError>(options: UseListOptions<TData, TError> = {}) {
|
|
58
61
|
const parsed = useParsed();
|
|
62
|
+
const resource = options.resource ?? parsed.resource ?? '';
|
|
59
63
|
const adminOptions = getAdminOptions();
|
|
64
|
+
const provider = getDataProviderForResource(resource, options.dataProviderName);
|
|
60
65
|
|
|
61
|
-
const
|
|
62
|
-
const resource = options.resource ?? parsed.resource ?? '';
|
|
63
|
-
const queryOptions = options.queryOptions;
|
|
64
|
-
const provider = getDataProviderForResource(resource, options.dataProviderName);
|
|
66
|
+
const queryOptions = options.queryOptions;
|
|
65
67
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
68
|
+
const query = createQuery<GetListResult<TData>, TError>(() => ({
|
|
69
|
+
queryKey: [resource, 'list', options.pagination, options.sorters, options.filters, options.meta],
|
|
70
|
+
queryFn: async () => {
|
|
71
|
+
const result = await provider.getList<TData>({
|
|
69
72
|
resource,
|
|
70
73
|
pagination: options.pagination,
|
|
71
74
|
sorters: options.sorters,
|
|
72
75
|
filters: options.filters,
|
|
73
76
|
meta: options.meta,
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
77
|
+
});
|
|
78
|
+
return result;
|
|
79
|
+
},
|
|
80
|
+
enabled: queryOptions?.enabled ?? true,
|
|
81
|
+
staleTime: queryOptions?.staleTime ?? adminOptions.reactQuery?.staleTime,
|
|
82
|
+
gcTime: queryOptions?.gcTime ?? adminOptions.reactQuery?.gcTime,
|
|
83
|
+
refetchOnWindowFocus: queryOptions?.refetchOnWindowFocus ?? adminOptions.reactQuery?.refetchOnWindowFocus,
|
|
84
|
+
}));
|
|
81
85
|
|
|
82
86
|
const overtime = createOvertimeTracker(() => query.isLoading, options.overtimeOptions ?? adminOptions.overtime);
|
|
83
87
|
|
|
84
|
-
createLiveSubscription(() => ({
|
|
85
|
-
resource
|
|
88
|
+
createLiveSubscription((): LiveSubscriptionParams => ({
|
|
89
|
+
resource,
|
|
86
90
|
liveProvider: getLiveProvider(),
|
|
87
91
|
liveMode: options.liveMode ?? adminOptions.liveMode,
|
|
88
|
-
onLiveEvent: (e) => {
|
|
92
|
+
onLiveEvent: (e: LiveEvent) => {
|
|
89
93
|
options.onLiveEvent?.(e);
|
|
90
94
|
adminOptions.onLiveEvent?.(e);
|
|
91
95
|
},
|
|
92
96
|
liveParams: options.liveParams,
|
|
93
|
-
enabled:
|
|
97
|
+
enabled: queryOptions?.enabled ?? true,
|
|
94
98
|
}));
|
|
95
99
|
|
|
96
|
-
let lastSuccess = $state(0);
|
|
97
|
-
let lastError = $state(0);
|
|
98
|
-
|
|
99
100
|
$effect(() => {
|
|
100
|
-
if (query.isSuccess && options.successNotification
|
|
101
|
-
lastSuccess = query.dataUpdatedAt;
|
|
102
|
-
const resource = options.resource ?? parsed.resource ?? '';
|
|
101
|
+
if (query.isSuccess && options.successNotification) {
|
|
103
102
|
fireSuccessNotification(options.successNotification, '', query.data, undefined, resource);
|
|
104
|
-
} else if (query.isError
|
|
105
|
-
lastError = query.errorUpdatedAt;
|
|
103
|
+
} else if (query.isError) {
|
|
106
104
|
fireErrorNotification(options.errorNotification, 'Fetch failed', query.error);
|
|
107
105
|
}
|
|
108
106
|
});
|
|
@@ -128,54 +126,35 @@ export interface UseOneOptions<TData extends BaseRecord = BaseRecord, TError = H
|
|
|
128
126
|
|
|
129
127
|
export function useOne<TData extends BaseRecord = BaseRecord, TError = HttpError>(options: UseOneOptions<TData, TError> = {}) {
|
|
130
128
|
const parsed = useParsed();
|
|
129
|
+
const resource = options.resource ?? parsed.resource ?? '';
|
|
130
|
+
const id = options.id ?? parsed.id;
|
|
131
131
|
const adminOptions = getAdminOptions();
|
|
132
|
+
const provider = getDataProviderForResource(resource, options.dataProviderName);
|
|
132
133
|
|
|
133
|
-
const
|
|
134
|
-
const resource = options.resource ?? parsed.resource ?? '';
|
|
135
|
-
const id = options.id ?? parsed.id;
|
|
136
|
-
const queryOptions = options.queryOptions;
|
|
137
|
-
const provider = getDataProviderForResource(resource, options.dataProviderName);
|
|
138
|
-
|
|
139
|
-
return {
|
|
140
|
-
queryKey: [resource, 'one', id, options.meta],
|
|
141
|
-
queryFn: async () => {
|
|
142
|
-
if (id == null) throw new Error('useOne requires an id');
|
|
143
|
-
return provider.getOne<TData>({
|
|
144
|
-
resource,
|
|
145
|
-
id,
|
|
146
|
-
meta: options.meta,
|
|
147
|
-
});
|
|
148
|
-
},
|
|
149
|
-
enabled: (queryOptions?.enabled ?? true) && id != null,
|
|
150
|
-
staleTime: queryOptions?.staleTime ?? adminOptions.reactQuery?.staleTime,
|
|
151
|
-
gcTime: queryOptions?.gcTime ?? adminOptions.reactQuery?.gcTime,
|
|
152
|
-
};
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
const overtime = createOvertimeTracker(() => query.isLoading, options.overtimeOptions ?? adminOptions.overtime);
|
|
134
|
+
const queryOptions = options.queryOptions;
|
|
156
135
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
136
|
+
const query = createQuery<GetOneResult<TData>, TError>(() => ({
|
|
137
|
+
queryKey: [resource, 'one', id, options.meta],
|
|
138
|
+
queryFn: async () => {
|
|
139
|
+
if (id == null) throw new Error('useOne requires an id');
|
|
140
|
+
const result = await provider.getOne<TData>({
|
|
141
|
+
resource,
|
|
142
|
+
id,
|
|
143
|
+
meta: options.meta,
|
|
144
|
+
});
|
|
145
|
+
return result;
|
|
164
146
|
},
|
|
165
|
-
|
|
166
|
-
|
|
147
|
+
enabled: (queryOptions?.enabled ?? true) && id != null,
|
|
148
|
+
staleTime: queryOptions?.staleTime ?? adminOptions.reactQuery?.staleTime,
|
|
149
|
+
gcTime: queryOptions?.gcTime ?? adminOptions.reactQuery?.gcTime,
|
|
167
150
|
}));
|
|
168
151
|
|
|
169
|
-
|
|
170
|
-
let lastError = $state(0);
|
|
152
|
+
const overtime = createOvertimeTracker(() => query.isLoading, options.overtimeOptions ?? adminOptions.overtime);
|
|
171
153
|
|
|
172
154
|
$effect(() => {
|
|
173
|
-
if (query.isSuccess && options.successNotification
|
|
174
|
-
lastSuccess = query.dataUpdatedAt;
|
|
175
|
-
const resource = options.resource ?? parsed.resource ?? '';
|
|
155
|
+
if (query.isSuccess && options.successNotification) {
|
|
176
156
|
fireSuccessNotification(options.successNotification, '', query.data, undefined, resource);
|
|
177
|
-
} else if (query.isError
|
|
178
|
-
lastError = query.errorUpdatedAt;
|
|
157
|
+
} else if (query.isError) {
|
|
179
158
|
fireErrorNotification(options.errorNotification, 'Fetch failed', query.error);
|
|
180
159
|
}
|
|
181
160
|
});
|
|
@@ -217,51 +196,29 @@ export interface UseManyOptions<TData extends BaseRecord = BaseRecord, TError =
|
|
|
217
196
|
}
|
|
218
197
|
|
|
219
198
|
export function useMany<TData extends BaseRecord = BaseRecord, TError = HttpError>(options: UseManyOptions<TData, TError>) {
|
|
199
|
+
const { resource, ids, meta, dataProviderName, queryOptions } = options;
|
|
220
200
|
const adminOptions = getAdminOptions();
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
const results = await Promise.all(ids.map(id => provider.getOne<TData>({ resource, id, meta })));
|
|
233
|
-
return { data: results.map(r => r.data) };
|
|
234
|
-
},
|
|
235
|
-
enabled: (queryOptions?.enabled ?? true) && ids.length > 0,
|
|
236
|
-
staleTime: queryOptions?.staleTime ?? adminOptions.reactQuery?.staleTime,
|
|
237
|
-
};
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
const overtime = createOvertimeTracker(() => query.isLoading, options.overtimeOptions ?? adminOptions.overtime);
|
|
241
|
-
|
|
242
|
-
createLiveSubscription(() => ({
|
|
243
|
-
resource: options.resource,
|
|
244
|
-
liveProvider: getLiveProvider(),
|
|
245
|
-
liveMode: options.liveMode ?? adminOptions.liveMode,
|
|
246
|
-
onLiveEvent: (e) => {
|
|
247
|
-
options.onLiveEvent?.(e);
|
|
248
|
-
adminOptions.onLiveEvent?.(e);
|
|
201
|
+
const provider = getDataProviderForResource(resource, dataProviderName);
|
|
202
|
+
|
|
203
|
+
const query = createQuery<GetManyResult<TData>, TError>(() => ({
|
|
204
|
+
queryKey: [resource, 'many', ids, meta],
|
|
205
|
+
queryFn: async () => {
|
|
206
|
+
if (!ids.length) return { data: [] };
|
|
207
|
+
if (provider.getMany) {
|
|
208
|
+
return provider.getMany<TData>({ resource, ids, meta });
|
|
209
|
+
}
|
|
210
|
+
const results = await Promise.all(ids.map(id => provider.getOne<TData>({ resource, id, meta })));
|
|
211
|
+
return { data: results.map(r => r.data) };
|
|
249
212
|
},
|
|
250
|
-
|
|
251
|
-
|
|
213
|
+
enabled: (queryOptions?.enabled ?? true) && ids.length > 0,
|
|
214
|
+
staleTime: queryOptions?.staleTime ?? adminOptions.reactQuery?.staleTime,
|
|
252
215
|
}));
|
|
253
216
|
|
|
254
|
-
|
|
255
|
-
let lastError = $state(0);
|
|
217
|
+
const overtime = createOvertimeTracker(() => query.isLoading, options.overtimeOptions ?? adminOptions.overtime);
|
|
256
218
|
|
|
257
219
|
$effect(() => {
|
|
258
|
-
if (query.isSuccess && options.successNotification
|
|
259
|
-
|
|
260
|
-
fireSuccessNotification(options.successNotification, '', query.data, undefined, options.resource);
|
|
261
|
-
} else if (query.isError && query.errorUpdatedAt > lastError) {
|
|
262
|
-
lastError = query.errorUpdatedAt;
|
|
263
|
-
fireErrorNotification(options.errorNotification, 'Fetch failed', query.error);
|
|
264
|
-
}
|
|
220
|
+
if (query.isSuccess && options.successNotification) fireSuccessNotification(options.successNotification, '', query.data, undefined, resource);
|
|
221
|
+
else if (query.isError) fireErrorNotification(options.errorNotification, 'Fetch failed', query.error);
|
|
265
222
|
});
|
|
266
223
|
|
|
267
224
|
return extendQuery(query, () => ({ overtime }));
|