@jskit-ai/http-web 0.1.1
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/fixtures/crud-list-date-filters/App.vue +107 -0
- package/fixtures/crud-list-date-filters/index.html +12 -0
- package/fixtures/crud-list-date-filters/main.js +24 -0
- package/fixtures/crud-list-date-filters/vite.config.mjs +18 -0
- package/package.json +150 -0
- package/src/client/bulkActions.js +47 -0
- package/src/client/components/CrudAddEditScreen.vue +186 -0
- package/src/client/components/CrudDeleteAction.vue +93 -0
- package/src/client/components/CrudListBulkActionSurface.vue +126 -0
- package/src/client/components/CrudListDateFilterControl.vue +188 -0
- package/src/client/components/CrudListFilterSurface.vue +388 -0
- package/src/client/components/CrudListRecordActionMenu.vue +120 -0
- package/src/client/components/CrudListScreen.vue +501 -0
- package/src/client/components/CrudViewScreen.vue +197 -0
- package/src/client/composables/crud/crudBindingSupport.js +75 -0
- package/src/client/composables/crud/crudHttpClientSupport.js +57 -0
- package/src/client/composables/crud/crudJsonApiTransportSupport.js +145 -0
- package/src/client/composables/crud/crudLookupFieldLabelSupport.js +151 -0
- package/src/client/composables/crud/crudLookupFieldRuntime.js +251 -0
- package/src/client/composables/crud/crudSchemaFormHelpers.js +464 -0
- package/src/client/composables/internal/crudListFilterLookupSupport.js +161 -0
- package/src/client/composables/internal/crudListParentTitleSupport.js +171 -0
- package/src/client/composables/internal/useOperationScope.js +144 -0
- package/src/client/composables/records/useAddEdit.js +224 -0
- package/src/client/composables/records/useCrudAddEdit.js +227 -0
- package/src/client/composables/records/useCrudList.js +101 -0
- package/src/client/composables/records/useCrudView.js +47 -0
- package/src/client/composables/records/useList.js +500 -0
- package/src/client/composables/records/useView.js +164 -0
- package/src/client/composables/runtime/addEditUiRuntime.js +130 -0
- package/src/client/composables/runtime/listUiRuntime.js +123 -0
- package/src/client/composables/runtime/modelStateHelpers.js +90 -0
- package/src/client/composables/runtime/operationAdapters.js +34 -0
- package/src/client/composables/runtime/operationUiHelpers.js +125 -0
- package/src/client/composables/runtime/operationValidationHelpers.js +51 -0
- package/src/client/composables/runtime/useAddEditCore.js +112 -0
- package/src/client/composables/runtime/useCommandCore.js +123 -0
- package/src/client/composables/runtime/useEndpointResource.js +199 -0
- package/src/client/composables/runtime/useFieldErrorBag.js +61 -0
- package/src/client/composables/runtime/useListCore.js +112 -0
- package/src/client/composables/runtime/useUiFeedback.js +98 -0
- package/src/client/composables/runtime/useViewCore.js +93 -0
- package/src/client/composables/runtime/viewUiRuntime.js +96 -0
- package/src/client/composables/support/errorMessageHelpers.js +66 -0
- package/src/client/composables/support/listQueryParamSupport.js +473 -0
- package/src/client/composables/support/listSearchSupport.js +70 -0
- package/src/client/composables/support/refValueHelpers.js +19 -0
- package/src/client/composables/support/requestQueryRuntimeSupport.js +141 -0
- package/src/client/composables/support/resourceLoadStateHelpers.js +208 -0
- package/src/client/composables/support/routeTemplateHelpers.js +285 -0
- package/src/client/composables/support/scopeHelpers.js +145 -0
- package/src/client/composables/useAccess.js +109 -0
- package/src/client/composables/useCommand.js +118 -0
- package/src/client/composables/useCrudAddEditScreen.js +88 -0
- package/src/client/composables/useCrudDeleteAction.js +160 -0
- package/src/client/composables/useCrudListBulkActions.js +147 -0
- package/src/client/composables/useCrudListFilterLookups.js +146 -0
- package/src/client/composables/useCrudListFilters.js +339 -0
- package/src/client/composables/useCrudListParentTitle.js +151 -0
- package/src/client/composables/useCrudListRowActions.js +144 -0
- package/src/client/composables/useCrudListScreen.js +237 -0
- package/src/client/composables/useCrudViewScreen.js +77 -0
- package/src/client/composables/usePagedCollection.js +181 -0
- package/src/client/composables/useRealtimeQueryInvalidation.js +156 -0
- package/src/client/composables/useScopeRuntime.js +135 -0
- package/src/client/filters.js +15 -0
- package/src/client/index.js +24 -0
- package/src/client/lib/httpClient.js +47 -0
- package/src/client/lib/permissions.js +34 -0
- package/src/client/rowActions.js +47 -0
- package/src/client/support/contractGuards.js +34 -0
- package/src/client/support/crudListDateFilterSupport.js +91 -0
- package/test/addEditUiRuntime.test.js +110 -0
- package/test/crudBindingSupport.test.js +110 -0
- package/test/crudHttpClientSupport.test.js +77 -0
- package/test/crudJsonApiTransportSupport.test.js +179 -0
- package/test/crudListBulkActionSurface.test.js +27 -0
- package/test/crudListDateFilterSupport.test.js +92 -0
- package/test/crudListDateFilterSurface.browser.test.js +182 -0
- package/test/crudListFilterSurface.test.js +90 -0
- package/test/crudLookupFieldRuntime.test.js +264 -0
- package/test/crudScreenComponents.test.js +110 -0
- package/test/errorIntentContract.test.js +31 -0
- package/test/errorMessageHelpers.test.js +28 -0
- package/test/exportsContract.test.js +68 -0
- package/test/listQueryParamSupport.test.js +223 -0
- package/test/listUiRuntime.test.js +102 -0
- package/test/operationValidationHelpers.test.js +64 -0
- package/test/packageBoundary.test.js +31 -0
- package/test/permissions.test.js +35 -0
- package/test/refValueHelpers.test.js +14 -0
- package/test/requestTransportOptions.test.js +457 -0
- package/test/resourceLoadStateHelpers.test.js +189 -0
- package/test/routeTemplateHelpers.test.js +85 -0
- package/test/scopeHelpers.test.js +57 -0
- package/test/useAddEditCore.test.js +124 -0
- package/test/useAddEditRequestQueryParams.test.js +190 -0
- package/test/useCrudAddEdit.test.js +415 -0
- package/test/useCrudDeleteAction.test.js +139 -0
- package/test/useCrudListBulkActions.test.js +65 -0
- package/test/useCrudListFilterLookups.test.js +114 -0
- package/test/useCrudListFilters.test.js +293 -0
- package/test/useCrudListParentTitle.test.js +262 -0
- package/test/useCrudListRowActions.test.js +77 -0
- package/test/useListRouteInitialization.test.js +337 -0
- package/test/useListSearchSupport.test.js +61 -0
- package/test/usePagedCollection.test.js +53 -0
- package/test/useViewRequestQueryParams.test.js +20 -0
- package/test/viewCoreLoading.test.js +44 -0
- package/test/viewUiRuntime.test.js +95 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { useQueryClient } from "@tanstack/vue-query";
|
|
2
|
+
import { resolveFieldErrors } from "@jskit-ai/http-runtime/client";
|
|
3
|
+
import { validateOperationInput } from "./operationValidationHelpers.js";
|
|
4
|
+
|
|
5
|
+
function useCommandCore({
|
|
6
|
+
model,
|
|
7
|
+
resource,
|
|
8
|
+
writeMethod = "POST",
|
|
9
|
+
canRun,
|
|
10
|
+
fieldBag,
|
|
11
|
+
feedback,
|
|
12
|
+
input,
|
|
13
|
+
buildRawPayload,
|
|
14
|
+
buildCommandPayload,
|
|
15
|
+
buildCommandOptions,
|
|
16
|
+
onRunSuccess,
|
|
17
|
+
onRunError,
|
|
18
|
+
suppressSuccessMessage = false,
|
|
19
|
+
messages = {}
|
|
20
|
+
} = {}) {
|
|
21
|
+
const queryClient = useQueryClient();
|
|
22
|
+
const normalizedWriteMethod = String(writeMethod || "POST").trim().toUpperCase();
|
|
23
|
+
|
|
24
|
+
const running = resource?.isSaving;
|
|
25
|
+
const fieldErrors = fieldBag?.errors;
|
|
26
|
+
const message = feedback?.message;
|
|
27
|
+
const messageType = feedback?.messageType;
|
|
28
|
+
|
|
29
|
+
async function run(context = {}) {
|
|
30
|
+
if (!canRun?.value || running?.value) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
feedback?.clear?.();
|
|
35
|
+
fieldBag?.clear?.();
|
|
36
|
+
|
|
37
|
+
const rawPayload = typeof buildRawPayload === "function"
|
|
38
|
+
? buildRawPayload(model, {
|
|
39
|
+
queryClient,
|
|
40
|
+
resource,
|
|
41
|
+
context
|
|
42
|
+
})
|
|
43
|
+
: {};
|
|
44
|
+
|
|
45
|
+
const validationResult = validateOperationInput({
|
|
46
|
+
input,
|
|
47
|
+
rawPayload,
|
|
48
|
+
fieldBag,
|
|
49
|
+
feedback,
|
|
50
|
+
validationMessage: String(messages.validation || "Validation failed.")
|
|
51
|
+
});
|
|
52
|
+
if (!validationResult.ok) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const { parsedInput } = validationResult;
|
|
57
|
+
const payload = typeof buildCommandPayload === "function"
|
|
58
|
+
? buildCommandPayload(parsedInput, {
|
|
59
|
+
rawPayload,
|
|
60
|
+
queryClient,
|
|
61
|
+
resource,
|
|
62
|
+
context
|
|
63
|
+
})
|
|
64
|
+
: (normalizedWriteMethod === "DELETE" ? undefined : parsedInput);
|
|
65
|
+
|
|
66
|
+
const options = typeof buildCommandOptions === "function"
|
|
67
|
+
? buildCommandOptions(parsedInput, {
|
|
68
|
+
rawPayload,
|
|
69
|
+
queryClient,
|
|
70
|
+
resource,
|
|
71
|
+
context
|
|
72
|
+
})
|
|
73
|
+
: {};
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const response = await resource.save(payload, options);
|
|
77
|
+
|
|
78
|
+
if (typeof onRunSuccess === "function") {
|
|
79
|
+
await onRunSuccess(response, {
|
|
80
|
+
parsed: parsedInput,
|
|
81
|
+
rawPayload,
|
|
82
|
+
payload,
|
|
83
|
+
options,
|
|
84
|
+
queryClient,
|
|
85
|
+
resource,
|
|
86
|
+
context
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (!suppressSuccessMessage) {
|
|
91
|
+
feedback?.success?.(String(messages.success || "Completed."));
|
|
92
|
+
}
|
|
93
|
+
return response;
|
|
94
|
+
} catch (error) {
|
|
95
|
+
fieldBag?.apply?.(resolveFieldErrors(error));
|
|
96
|
+
|
|
97
|
+
if (typeof onRunError === "function") {
|
|
98
|
+
await onRunError(error, {
|
|
99
|
+
parsed: parsedInput,
|
|
100
|
+
rawPayload,
|
|
101
|
+
payload,
|
|
102
|
+
options,
|
|
103
|
+
queryClient,
|
|
104
|
+
resource,
|
|
105
|
+
context
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
feedback?.error?.(error, String(messages.error || "Unable to complete action."));
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return Object.freeze({
|
|
115
|
+
running,
|
|
116
|
+
fieldErrors,
|
|
117
|
+
message,
|
|
118
|
+
messageType,
|
|
119
|
+
run
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export { useCommandCore };
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { computed, unref } from "vue";
|
|
2
|
+
import { useMutation, useQuery } from "@tanstack/vue-query";
|
|
3
|
+
import { getHttpWebClient } from "../../lib/httpClient.js";
|
|
4
|
+
import { asPlainObject } from "../support/scopeHelpers.js";
|
|
5
|
+
import { resolveEnabledRef, resolveTextRef } from "../support/refValueHelpers.js";
|
|
6
|
+
import { toQueryErrorMessage } from "../support/errorMessageHelpers.js";
|
|
7
|
+
import { useOperationRealtime } from "../useRealtimeQueryInvalidation.js";
|
|
8
|
+
import {
|
|
9
|
+
hasResolvedQueryData,
|
|
10
|
+
mergeQueryMeta,
|
|
11
|
+
mergeRequestRecoveryQueryMeta
|
|
12
|
+
} from "../support/resourceLoadStateHelpers.js";
|
|
13
|
+
|
|
14
|
+
function buildEndpointReadRequestOptions({
|
|
15
|
+
method = "GET",
|
|
16
|
+
query = null,
|
|
17
|
+
transport = null
|
|
18
|
+
} = {}) {
|
|
19
|
+
const requestOptions = {
|
|
20
|
+
method: String(method || "GET").toUpperCase()
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const normalizedQuery = resolveRequestQuery(query);
|
|
24
|
+
if (normalizedQuery) {
|
|
25
|
+
requestOptions.query = normalizedQuery;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (transport && typeof transport === "object") {
|
|
29
|
+
requestOptions.transport = transport;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return requestOptions;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function resolveRequestQuery(value = null) {
|
|
36
|
+
const source = unref(value);
|
|
37
|
+
if (!source || typeof source !== "object" || Array.isArray(source)) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return source;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function buildEndpointWriteRequestOptions({
|
|
45
|
+
method = "PATCH",
|
|
46
|
+
body = undefined,
|
|
47
|
+
options = null,
|
|
48
|
+
transport = null
|
|
49
|
+
} = {}) {
|
|
50
|
+
const requestOptions = {
|
|
51
|
+
method: String(method || "PATCH").toUpperCase(),
|
|
52
|
+
...(asPlainObject(options))
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
if (body !== undefined) {
|
|
56
|
+
requestOptions.body = body;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (transport && typeof transport === "object") {
|
|
60
|
+
requestOptions.transport = transport;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return requestOptions;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function useEndpointResource({
|
|
67
|
+
queryKey,
|
|
68
|
+
path = "",
|
|
69
|
+
enabled = true,
|
|
70
|
+
client = null,
|
|
71
|
+
readMethod = "GET",
|
|
72
|
+
writeMethod = "PATCH",
|
|
73
|
+
readQuery = null,
|
|
74
|
+
transport = null,
|
|
75
|
+
refreshOnPull = false,
|
|
76
|
+
realtime = null,
|
|
77
|
+
queryOptions = null,
|
|
78
|
+
mutationOptions = null,
|
|
79
|
+
requestRecovery = null,
|
|
80
|
+
requestRecoveryLabel = "Request",
|
|
81
|
+
fallbackLoadError = "Unable to load resource.",
|
|
82
|
+
fallbackSaveError = "Unable to save resource."
|
|
83
|
+
} = {}) {
|
|
84
|
+
const activeClient = client || getHttpWebClient();
|
|
85
|
+
if (!activeClient || typeof activeClient.request !== "function") {
|
|
86
|
+
throw new TypeError("useEndpointResource requires a client with request().");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const normalizedPath = computed(() => resolveTextRef(path));
|
|
90
|
+
const queryEnabled = computed(() => resolveEnabledRef(enabled) && Boolean(normalizedPath.value));
|
|
91
|
+
|
|
92
|
+
const query = useQuery({
|
|
93
|
+
queryKey,
|
|
94
|
+
queryFn: () => {
|
|
95
|
+
const requestPath = normalizedPath.value;
|
|
96
|
+
if (!requestPath) {
|
|
97
|
+
throw new Error("Resource path is required.");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return activeClient.request(requestPath, {
|
|
101
|
+
...buildEndpointReadRequestOptions({
|
|
102
|
+
method: readMethod,
|
|
103
|
+
query: readQuery,
|
|
104
|
+
transport
|
|
105
|
+
})
|
|
106
|
+
});
|
|
107
|
+
},
|
|
108
|
+
enabled: queryEnabled,
|
|
109
|
+
...mergeRequestRecoveryQueryMeta(
|
|
110
|
+
refreshOnPull
|
|
111
|
+
? mergeQueryMeta(asPlainObject(queryOptions), {
|
|
112
|
+
jskit: {
|
|
113
|
+
refreshOnPull: true
|
|
114
|
+
}
|
|
115
|
+
})
|
|
116
|
+
: asPlainObject(queryOptions),
|
|
117
|
+
requestRecovery,
|
|
118
|
+
{
|
|
119
|
+
label: requestRecoveryLabel,
|
|
120
|
+
method: readMethod
|
|
121
|
+
}
|
|
122
|
+
)
|
|
123
|
+
});
|
|
124
|
+
const realtimeBinding = useOperationRealtime({
|
|
125
|
+
realtime,
|
|
126
|
+
queryKey,
|
|
127
|
+
enabled: queryEnabled
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const mutation = useMutation({
|
|
131
|
+
mutationFn: async (request = {}) => {
|
|
132
|
+
const options = asPlainObject(request);
|
|
133
|
+
const requestPath = resolveTextRef(options.path || normalizedPath.value);
|
|
134
|
+
if (!requestPath) {
|
|
135
|
+
throw new Error("Resource path is required.");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const method = String(options.method || writeMethod || "PATCH").toUpperCase();
|
|
139
|
+
const hasBody = Object.hasOwn(options, "body");
|
|
140
|
+
const body = hasBody ? options.body : options.payload;
|
|
141
|
+
return activeClient.request(
|
|
142
|
+
requestPath,
|
|
143
|
+
buildEndpointWriteRequestOptions({
|
|
144
|
+
method,
|
|
145
|
+
body,
|
|
146
|
+
options: options.options,
|
|
147
|
+
transport
|
|
148
|
+
})
|
|
149
|
+
);
|
|
150
|
+
},
|
|
151
|
+
...(asPlainObject(mutationOptions))
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
const data = computed(() => query.data.value);
|
|
155
|
+
const hasResolvedData = computed(() => hasResolvedQueryData({
|
|
156
|
+
query,
|
|
157
|
+
data
|
|
158
|
+
}));
|
|
159
|
+
const isInitialLoading = computed(() => Boolean(queryEnabled.value && query.isPending.value && !hasResolvedData.value));
|
|
160
|
+
const isFetching = computed(() => Boolean(queryEnabled.value && query.isFetching.value));
|
|
161
|
+
const isRefetching = computed(() => Boolean(isFetching.value && !isInitialLoading.value));
|
|
162
|
+
const isLoading = computed(() => Boolean(isInitialLoading.value || isFetching.value));
|
|
163
|
+
const isSaving = computed(() => Boolean(mutation.isPending.value));
|
|
164
|
+
const loadError = computed(() => toQueryErrorMessage(query.error.value, fallbackLoadError, "Request failed."));
|
|
165
|
+
const saveError = computed(() => toQueryErrorMessage(mutation.error.value, fallbackSaveError, "Request failed."));
|
|
166
|
+
|
|
167
|
+
async function reload() {
|
|
168
|
+
return query.refetch();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async function save(payload, options = {}) {
|
|
172
|
+
return mutation.mutateAsync({
|
|
173
|
+
...asPlainObject(options),
|
|
174
|
+
payload
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return Object.freeze({
|
|
179
|
+
query,
|
|
180
|
+
mutation,
|
|
181
|
+
data,
|
|
182
|
+
isInitialLoading,
|
|
183
|
+
isFetching,
|
|
184
|
+
isRefetching,
|
|
185
|
+
isLoading,
|
|
186
|
+
isSaving,
|
|
187
|
+
loadError,
|
|
188
|
+
saveError,
|
|
189
|
+
realtime: realtimeBinding,
|
|
190
|
+
reload,
|
|
191
|
+
save
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export {
|
|
196
|
+
buildEndpointReadRequestOptions,
|
|
197
|
+
buildEndpointWriteRequestOptions,
|
|
198
|
+
useEndpointResource
|
|
199
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { reactive } from "vue";
|
|
2
|
+
|
|
3
|
+
function normalizeKeys(keys) {
|
|
4
|
+
return Array.isArray(keys)
|
|
5
|
+
? keys.map((entry) => String(entry || "").trim()).filter(Boolean)
|
|
6
|
+
: [];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function useFieldErrorBag(keys = []) {
|
|
10
|
+
const normalizedKeys = normalizeKeys(keys);
|
|
11
|
+
const hasFixedKeys = normalizedKeys.length > 0;
|
|
12
|
+
const errors = reactive(
|
|
13
|
+
Object.fromEntries(normalizedKeys.map((key) => [key, ""]))
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
function clear() {
|
|
17
|
+
const keysToClear = hasFixedKeys ? normalizedKeys : Object.keys(errors);
|
|
18
|
+
for (const key of keysToClear) {
|
|
19
|
+
errors[key] = "";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function apply(source = {}) {
|
|
24
|
+
const fieldErrorMap = source && typeof source === "object" ? source : {};
|
|
25
|
+
if (hasFixedKeys) {
|
|
26
|
+
for (const key of normalizedKeys) {
|
|
27
|
+
errors[key] = String(fieldErrorMap[key] || "");
|
|
28
|
+
}
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
clear();
|
|
33
|
+
for (const [field, message] of Object.entries(fieldErrorMap)) {
|
|
34
|
+
const key = String(field || "").trim();
|
|
35
|
+
if (!key) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
errors[key] = String(message || "");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function set(field, message) {
|
|
43
|
+
const key = String(field || "").trim();
|
|
44
|
+
if (!key) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (hasFixedKeys && !(key in errors)) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
errors[key] = String(message || "");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return Object.freeze({
|
|
54
|
+
errors,
|
|
55
|
+
clear,
|
|
56
|
+
apply,
|
|
57
|
+
set
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { useFieldErrorBag };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { computed, unref } from "vue";
|
|
2
|
+
import { getHttpWebClient } from "../../lib/httpClient.js";
|
|
3
|
+
import { asPlainObject } from "../support/scopeHelpers.js";
|
|
4
|
+
import { resolveEnabledRef, resolveTextRef } from "../support/refValueHelpers.js";
|
|
5
|
+
import { usePagedCollection } from "../usePagedCollection.js";
|
|
6
|
+
import { mergeRequestRecoveryQueryMeta } from "../support/resourceLoadStateHelpers.js";
|
|
7
|
+
|
|
8
|
+
const DEFAULT_LIST_LIMIT = 20;
|
|
9
|
+
|
|
10
|
+
function buildListRequestOptions({
|
|
11
|
+
requestOptions = null,
|
|
12
|
+
transport = null,
|
|
13
|
+
pageParam = null
|
|
14
|
+
} = {}) {
|
|
15
|
+
const resolvedOptions = {
|
|
16
|
+
method: "GET",
|
|
17
|
+
...(resolveRequestOptionsObject(requestOptions))
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const sourceQuery =
|
|
21
|
+
resolvedOptions.query && typeof resolvedOptions.query === "object" && !Array.isArray(resolvedOptions.query)
|
|
22
|
+
? { ...resolvedOptions.query }
|
|
23
|
+
: {};
|
|
24
|
+
if (
|
|
25
|
+
!Object.hasOwn(sourceQuery, "limit") &&
|
|
26
|
+
!Object.hasOwn(sourceQuery, "page[limit]")
|
|
27
|
+
) {
|
|
28
|
+
sourceQuery.limit = DEFAULT_LIST_LIMIT;
|
|
29
|
+
}
|
|
30
|
+
if (pageParam !== null && pageParam !== undefined && pageParam !== "") {
|
|
31
|
+
sourceQuery.cursor = String(pageParam);
|
|
32
|
+
}
|
|
33
|
+
if (Object.keys(sourceQuery).length > 0) {
|
|
34
|
+
resolvedOptions.query = sourceQuery;
|
|
35
|
+
} else {
|
|
36
|
+
delete resolvedOptions.query;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (transport && typeof transport === "object") {
|
|
40
|
+
resolvedOptions.transport = transport;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return resolvedOptions;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function resolveRequestOptionsObject(value = null) {
|
|
47
|
+
const source = unref(value);
|
|
48
|
+
return asPlainObject(source);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function resolveListRequestMethod(requestOptions = null) {
|
|
52
|
+
return String(resolveRequestOptionsObject(requestOptions).method || "GET").toUpperCase();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function useListCore({
|
|
56
|
+
queryKey,
|
|
57
|
+
path = "",
|
|
58
|
+
enabled = true,
|
|
59
|
+
client = null,
|
|
60
|
+
transport = null,
|
|
61
|
+
initialPageParam = null,
|
|
62
|
+
getNextPageParam,
|
|
63
|
+
selectItems,
|
|
64
|
+
requestOptions = null,
|
|
65
|
+
queryOptions = null,
|
|
66
|
+
requestRecovery = null,
|
|
67
|
+
requestRecoveryLabel = "List",
|
|
68
|
+
fallbackLoadError = "Unable to load list."
|
|
69
|
+
} = {}) {
|
|
70
|
+
const activeClient = client || getHttpWebClient();
|
|
71
|
+
if (!activeClient || typeof activeClient.request !== "function") {
|
|
72
|
+
throw new TypeError("useListCore requires a client with request().");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const normalizedPath = computed(() => resolveTextRef(path));
|
|
76
|
+
const queryEnabled = computed(() => resolveEnabledRef(enabled) && Boolean(normalizedPath.value));
|
|
77
|
+
|
|
78
|
+
const collection = usePagedCollection({
|
|
79
|
+
queryKey,
|
|
80
|
+
initialPageParam,
|
|
81
|
+
enabled: queryEnabled,
|
|
82
|
+
queryFn: async ({ pageParam }) => {
|
|
83
|
+
const requestPath = normalizedPath.value;
|
|
84
|
+
if (!requestPath) {
|
|
85
|
+
throw new Error("List path is required.");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return activeClient.request(
|
|
89
|
+
requestPath,
|
|
90
|
+
buildListRequestOptions({
|
|
91
|
+
requestOptions,
|
|
92
|
+
transport,
|
|
93
|
+
pageParam
|
|
94
|
+
})
|
|
95
|
+
);
|
|
96
|
+
},
|
|
97
|
+
getNextPageParam,
|
|
98
|
+
selectItems,
|
|
99
|
+
queryOptions: mergeRequestRecoveryQueryMeta(queryOptions, requestRecovery, {
|
|
100
|
+
label: requestRecoveryLabel,
|
|
101
|
+
method: resolveListRequestMethod(requestOptions)
|
|
102
|
+
}),
|
|
103
|
+
fallbackLoadError
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
return collection;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export {
|
|
110
|
+
buildListRequestOptions,
|
|
111
|
+
useListCore
|
|
112
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { ref } from "vue";
|
|
2
|
+
import { useShellWebErrorRuntime } from "@jskit-ai/shell-web/client/error";
|
|
3
|
+
import { toUiErrorMessage } from "../support/errorMessageHelpers.js";
|
|
4
|
+
|
|
5
|
+
function useUiFeedback({
|
|
6
|
+
initialType = "success",
|
|
7
|
+
source = "http-web.ui-feedback",
|
|
8
|
+
successChannel = "",
|
|
9
|
+
errorChannel = "",
|
|
10
|
+
dedupeWindowMs = 2000
|
|
11
|
+
} = {}) {
|
|
12
|
+
const message = ref("");
|
|
13
|
+
const messageType = ref(String(initialType || "success"));
|
|
14
|
+
const errorRuntime = useShellWebErrorRuntime();
|
|
15
|
+
const normalizedSource = String(source || "").trim() || "http-web.ui-feedback";
|
|
16
|
+
let lastErrorPresentation = null;
|
|
17
|
+
|
|
18
|
+
function rememberErrorPresentation(reportResult = null) {
|
|
19
|
+
const presentationId = String(reportResult?.presentationId || "").trim();
|
|
20
|
+
const presenterId = String(reportResult?.decision?.presenterId || "").trim();
|
|
21
|
+
if (!presentationId || !presenterId) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
lastErrorPresentation = Object.freeze({
|
|
26
|
+
presentationId,
|
|
27
|
+
presenterId
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function dismissLastErrorPresentation() {
|
|
32
|
+
if (!lastErrorPresentation) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
errorRuntime.dismiss(lastErrorPresentation.presentationId, {
|
|
37
|
+
presenterId: lastErrorPresentation.presenterId
|
|
38
|
+
});
|
|
39
|
+
lastErrorPresentation = null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function clear() {
|
|
43
|
+
message.value = "";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function success(nextMessage = "") {
|
|
47
|
+
messageType.value = "success";
|
|
48
|
+
const normalizedMessage = String(nextMessage || "").trim();
|
|
49
|
+
message.value = normalizedMessage;
|
|
50
|
+
dismissLastErrorPresentation();
|
|
51
|
+
if (!normalizedMessage) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
errorRuntime.report({
|
|
56
|
+
source: normalizedSource,
|
|
57
|
+
message: normalizedMessage,
|
|
58
|
+
intent: "action-feedback",
|
|
59
|
+
severity: "success",
|
|
60
|
+
channel: successChannel,
|
|
61
|
+
dedupeKey: `${normalizedSource}:success:${normalizedMessage}`,
|
|
62
|
+
dedupeWindowMs
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function error(errorValue, fallbackMessage = "") {
|
|
67
|
+
messageType.value = "error";
|
|
68
|
+
message.value = toUiErrorMessage(errorValue, fallbackMessage, "Request failed.");
|
|
69
|
+
if (!message.value) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const reportResult = errorRuntime.report({
|
|
74
|
+
source: normalizedSource,
|
|
75
|
+
message: message.value,
|
|
76
|
+
cause: errorValue || null,
|
|
77
|
+
intent: "action-feedback",
|
|
78
|
+
severity: "error",
|
|
79
|
+
channel: errorChannel,
|
|
80
|
+
dedupeKey: `${normalizedSource}:error:${message.value}`,
|
|
81
|
+
dedupeWindowMs
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
if (!reportResult?.skipped) {
|
|
85
|
+
rememberErrorPresentation(reportResult);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return Object.freeze({
|
|
90
|
+
message,
|
|
91
|
+
messageType,
|
|
92
|
+
clear,
|
|
93
|
+
success,
|
|
94
|
+
error
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export { useUiFeedback };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { computed } from "vue";
|
|
2
|
+
import { watchResourceModelState } from "./modelStateHelpers.js";
|
|
3
|
+
|
|
4
|
+
function normalizeStatusList(value) {
|
|
5
|
+
if (Array.isArray(value)) {
|
|
6
|
+
return value.map((entry) => Number(entry)).filter((entry) => Number.isInteger(entry) && entry > 0);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
return [404];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function useViewCore({
|
|
13
|
+
resource,
|
|
14
|
+
model,
|
|
15
|
+
canView,
|
|
16
|
+
mapLoadedToModel,
|
|
17
|
+
notFoundStatuses = [404],
|
|
18
|
+
notFoundMessage = "Record not found."
|
|
19
|
+
} = {}) {
|
|
20
|
+
const statusList = normalizeStatusList(notFoundStatuses);
|
|
21
|
+
watchResourceModelState({
|
|
22
|
+
resource,
|
|
23
|
+
model,
|
|
24
|
+
mapLoadedToModel,
|
|
25
|
+
resolveMapContext() {
|
|
26
|
+
return {
|
|
27
|
+
resource
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const data = resource?.data;
|
|
33
|
+
const record = computed(() => (model !== undefined ? model : data?.value));
|
|
34
|
+
const isLoading = computed(() => {
|
|
35
|
+
if (resource?.isInitialLoading?.value !== undefined) {
|
|
36
|
+
return Boolean(resource.isInitialLoading.value);
|
|
37
|
+
}
|
|
38
|
+
return Boolean(resource?.query?.isPending?.value);
|
|
39
|
+
});
|
|
40
|
+
const isFetching = computed(() => {
|
|
41
|
+
if (resource?.isFetching?.value !== undefined) {
|
|
42
|
+
return Boolean(resource.isFetching.value);
|
|
43
|
+
}
|
|
44
|
+
return Boolean(resource?.query?.isFetching?.value);
|
|
45
|
+
});
|
|
46
|
+
const error = computed(() => resource?.query?.error?.value || null);
|
|
47
|
+
|
|
48
|
+
const isNotFound = computed(() => {
|
|
49
|
+
const status = Number(error.value?.status || 0);
|
|
50
|
+
return statusList.includes(status);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const notFoundError = computed(() => {
|
|
54
|
+
if (!isNotFound.value) {
|
|
55
|
+
return "";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return String(notFoundMessage || "Record not found.");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const loadError = computed(() => {
|
|
62
|
+
if (isNotFound.value) {
|
|
63
|
+
return "";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return String(resource?.loadError?.value || "").trim();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const resolvedCanView = computed(() => {
|
|
70
|
+
if (canView === undefined) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return Boolean(canView?.value);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
async function refresh() {
|
|
78
|
+
return resource?.reload?.();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return Object.freeze({
|
|
82
|
+
record,
|
|
83
|
+
isLoading,
|
|
84
|
+
isFetching,
|
|
85
|
+
isNotFound,
|
|
86
|
+
notFoundError,
|
|
87
|
+
loadError,
|
|
88
|
+
canView: resolvedCanView,
|
|
89
|
+
refresh
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export { useViewCore };
|