@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,130 @@
|
|
|
1
|
+
import { computed, unref } from "vue";
|
|
2
|
+
import { asPlainObject } from "../support/scopeHelpers.js";
|
|
3
|
+
import {
|
|
4
|
+
normalizeRouteParamName,
|
|
5
|
+
resolveRouteParamsSource,
|
|
6
|
+
resolveScopedRoutePathname,
|
|
7
|
+
resolveRouteTemplateLocation,
|
|
8
|
+
toRouteParamValue
|
|
9
|
+
} from "../support/routeTemplateHelpers.js";
|
|
10
|
+
|
|
11
|
+
function toResolvedRecordId({ routeParams, recordIdParam, routeRecordId }) {
|
|
12
|
+
const explicitRecordId = toRouteParamValue(
|
|
13
|
+
typeof routeRecordId === "function" ? routeRecordId() : unref(routeRecordId)
|
|
14
|
+
);
|
|
15
|
+
if (explicitRecordId) {
|
|
16
|
+
return explicitRecordId;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return toRouteParamValue(routeParams[recordIdParam]);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function resolveSavedRecordId(payload, saveRecordIdSelector) {
|
|
23
|
+
const sourcePayload = asPlainObject(payload);
|
|
24
|
+
if (typeof saveRecordIdSelector === "function") {
|
|
25
|
+
return toRouteParamValue(saveRecordIdSelector(sourcePayload));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return toRouteParamValue(sourcePayload.id);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function createAddEditUiRuntime({
|
|
32
|
+
recordIdParam = "recordId",
|
|
33
|
+
routeParams = null,
|
|
34
|
+
routeParamNames = null,
|
|
35
|
+
routePath = "",
|
|
36
|
+
routeRecordId = null,
|
|
37
|
+
apiUrlTemplate = "",
|
|
38
|
+
viewUrlTemplate = "",
|
|
39
|
+
listUrlTemplate = "",
|
|
40
|
+
saveRecordIdSelector = null
|
|
41
|
+
} = {}) {
|
|
42
|
+
const normalizedRecordIdParam = normalizeRouteParamName(recordIdParam, {
|
|
43
|
+
context: "useAddEdit recordIdParam"
|
|
44
|
+
});
|
|
45
|
+
const normalizedApiUrlTemplate = String(apiUrlTemplate || "").trim();
|
|
46
|
+
const normalizedViewUrlTemplate = String(viewUrlTemplate || "").trim();
|
|
47
|
+
const normalizedListUrlTemplate = String(listUrlTemplate || "").trim();
|
|
48
|
+
|
|
49
|
+
function resolveTemplatePath(urlTemplate = "", extraParams = {}) {
|
|
50
|
+
const normalizedTemplate = String(urlTemplate || "").trim();
|
|
51
|
+
if (!normalizedTemplate) {
|
|
52
|
+
return "";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const currentRouteParams = resolveRouteParamsSource(routeParams);
|
|
56
|
+
const sourceParams = {
|
|
57
|
+
...currentRouteParams,
|
|
58
|
+
...asPlainObject(extraParams)
|
|
59
|
+
};
|
|
60
|
+
const currentRouteRecordId = toResolvedRecordId({
|
|
61
|
+
routeParams: currentRouteParams,
|
|
62
|
+
recordIdParam: normalizedRecordIdParam,
|
|
63
|
+
routeRecordId
|
|
64
|
+
});
|
|
65
|
+
const resolvedRecordId = toRouteParamValue(sourceParams[normalizedRecordIdParam]) ||
|
|
66
|
+
currentRouteRecordId;
|
|
67
|
+
sourceParams[normalizedRecordIdParam] = resolvedRecordId;
|
|
68
|
+
const currentPathname = resolveScopedRoutePathname({
|
|
69
|
+
currentPathname: routePath,
|
|
70
|
+
params: currentRouteParams,
|
|
71
|
+
orderedParamNames: routeParamNames,
|
|
72
|
+
anchorParamName: normalizedRecordIdParam,
|
|
73
|
+
anchorParamValue: currentRouteRecordId,
|
|
74
|
+
anchorMode: "after"
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return resolveRouteTemplateLocation(normalizedTemplate, {
|
|
78
|
+
params: sourceParams,
|
|
79
|
+
currentPathname
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const recordId = computed(() =>
|
|
84
|
+
toResolvedRecordId({
|
|
85
|
+
routeParams: resolveRouteParamsSource(routeParams),
|
|
86
|
+
recordIdParam: normalizedRecordIdParam,
|
|
87
|
+
routeRecordId
|
|
88
|
+
})
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const apiSuffix = computed(() => resolveTemplatePath(normalizedApiUrlTemplate));
|
|
92
|
+
const listUrl = computed(() => resolveTemplatePath(normalizedListUrlTemplate));
|
|
93
|
+
const cancelUrl = computed(() => resolveTemplatePath(normalizedViewUrlTemplate) || listUrl.value);
|
|
94
|
+
|
|
95
|
+
function resolveParams(urlTemplate = "", extraParams = {}) {
|
|
96
|
+
return resolveTemplatePath(urlTemplate, extraParams);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function resolveViewUrl(recordIdLike = "") {
|
|
100
|
+
const targetRecordId = toRouteParamValue(recordIdLike);
|
|
101
|
+
if (!targetRecordId) {
|
|
102
|
+
return "";
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return resolveTemplatePath(normalizedViewUrlTemplate, {
|
|
106
|
+
[normalizedRecordIdParam]: targetRecordId
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function resolveSavedViewUrl(payload = {}) {
|
|
111
|
+
const targetRecordId = resolveSavedRecordId(payload, saveRecordIdSelector);
|
|
112
|
+
if (!targetRecordId) {
|
|
113
|
+
return "";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return resolveViewUrl(targetRecordId);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return Object.freeze({
|
|
120
|
+
recordId,
|
|
121
|
+
apiSuffix,
|
|
122
|
+
listUrl,
|
|
123
|
+
cancelUrl,
|
|
124
|
+
resolveParams,
|
|
125
|
+
resolveViewUrl,
|
|
126
|
+
resolveSavedViewUrl
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export { createAddEditUiRuntime };
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { computed } from "vue";
|
|
2
|
+
import { asPlainObject } from "../support/scopeHelpers.js";
|
|
3
|
+
import {
|
|
4
|
+
normalizeRouteParamName,
|
|
5
|
+
toRouteParamValue,
|
|
6
|
+
resolveRouteParamsSource,
|
|
7
|
+
resolveScopedRoutePathname,
|
|
8
|
+
resolveRouteTemplateLocation
|
|
9
|
+
} from "../support/routeTemplateHelpers.js";
|
|
10
|
+
|
|
11
|
+
function resolveRecordId(record, recordIdSelector) {
|
|
12
|
+
const item = asPlainObject(record);
|
|
13
|
+
if (typeof recordIdSelector === "function") {
|
|
14
|
+
return toRouteParamValue(recordIdSelector(item));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return toRouteParamValue(item.id);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function createListUiRuntime({
|
|
21
|
+
items,
|
|
22
|
+
isInitialLoading,
|
|
23
|
+
recordIdParam = "recordId",
|
|
24
|
+
recordIdSelector = null,
|
|
25
|
+
routeParams = null,
|
|
26
|
+
routeParamNames = null,
|
|
27
|
+
routePath = "",
|
|
28
|
+
viewUrlTemplate = "",
|
|
29
|
+
editUrlTemplate = ""
|
|
30
|
+
} = {}) {
|
|
31
|
+
const normalizedRecordIdParam = normalizeRouteParamName(recordIdParam, {
|
|
32
|
+
context: "useList recordIdParam"
|
|
33
|
+
});
|
|
34
|
+
const normalizedViewUrlTemplate = String(viewUrlTemplate || "").trim();
|
|
35
|
+
const normalizedEditUrlTemplate = String(editUrlTemplate || "").trim();
|
|
36
|
+
const hasViewUrl = Boolean(normalizedViewUrlTemplate);
|
|
37
|
+
const hasEditUrl = Boolean(normalizedEditUrlTemplate);
|
|
38
|
+
const actionColumnCount = (hasViewUrl ? 1 : 0) + (hasEditUrl ? 1 : 0);
|
|
39
|
+
const normalizedItems = computed(() => (Array.isArray(items?.value) ? items.value : []));
|
|
40
|
+
const showListSkeleton = computed(() => Boolean(isInitialLoading?.value && normalizedItems.value.length < 1));
|
|
41
|
+
|
|
42
|
+
function resolveTemplatePath(urlTemplate = "", extraParams = {}) {
|
|
43
|
+
const normalizedTemplate = String(urlTemplate || "").trim();
|
|
44
|
+
if (!normalizedTemplate) {
|
|
45
|
+
return "";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const currentRouteParams = resolveRouteParamsSource(routeParams);
|
|
49
|
+
const sourceParams = {
|
|
50
|
+
...currentRouteParams,
|
|
51
|
+
...asPlainObject(extraParams)
|
|
52
|
+
};
|
|
53
|
+
const currentPathname = resolveScopedRoutePathname({
|
|
54
|
+
currentPathname: routePath,
|
|
55
|
+
params: currentRouteParams,
|
|
56
|
+
orderedParamNames: routeParamNames,
|
|
57
|
+
anchorParamName: normalizedRecordIdParam,
|
|
58
|
+
anchorParamValue: currentRouteParams[normalizedRecordIdParam],
|
|
59
|
+
anchorMode: "before"
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
return resolveRouteTemplateLocation(normalizedTemplate, {
|
|
63
|
+
params: sourceParams,
|
|
64
|
+
currentPathname
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function resolveParams(urlTemplate = "", extraParams = {}) {
|
|
69
|
+
return resolveTemplatePath(urlTemplate, extraParams);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function resolveRowKey(record, index) {
|
|
73
|
+
const recordId = resolveRecordId(record, recordIdSelector);
|
|
74
|
+
if (recordId) {
|
|
75
|
+
return recordId;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return `row-${index}`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function resolveViewUrl(record) {
|
|
82
|
+
if (!hasViewUrl) {
|
|
83
|
+
return "";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const recordId = resolveRecordId(record, recordIdSelector);
|
|
87
|
+
if (!recordId) {
|
|
88
|
+
return "";
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return resolveTemplatePath(normalizedViewUrlTemplate, {
|
|
92
|
+
[normalizedRecordIdParam]: recordId
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function resolveEditUrl(record) {
|
|
97
|
+
if (!hasEditUrl) {
|
|
98
|
+
return "";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const recordId = resolveRecordId(record, recordIdSelector);
|
|
102
|
+
if (!recordId) {
|
|
103
|
+
return "";
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return resolveTemplatePath(normalizedEditUrlTemplate, {
|
|
107
|
+
[normalizedRecordIdParam]: recordId
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return Object.freeze({
|
|
112
|
+
hasViewUrl,
|
|
113
|
+
hasEditUrl,
|
|
114
|
+
actionColumnCount,
|
|
115
|
+
showListSkeleton,
|
|
116
|
+
resolveParams,
|
|
117
|
+
resolveRowKey,
|
|
118
|
+
resolveViewUrl,
|
|
119
|
+
resolveEditUrl
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export { createListUiRuntime };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { watch } from "vue";
|
|
2
|
+
|
|
3
|
+
function isObjectLike(value) {
|
|
4
|
+
return value !== null && typeof value === "object";
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function deepClone(value) {
|
|
8
|
+
try {
|
|
9
|
+
return JSON.parse(JSON.stringify(value));
|
|
10
|
+
} catch {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function captureModelSnapshot(model) {
|
|
16
|
+
if (!isObjectLike(model)) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return deepClone(model);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function restoreModelSnapshot(model, snapshot) {
|
|
24
|
+
if (!isObjectLike(model) || !isObjectLike(snapshot)) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (Array.isArray(model) && Array.isArray(snapshot)) {
|
|
29
|
+
model.splice(0, model.length, ...snapshot);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (Array.isArray(model) || Array.isArray(snapshot)) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
for (const key of Object.keys(model)) {
|
|
38
|
+
if (!Object.prototype.hasOwnProperty.call(snapshot, key)) {
|
|
39
|
+
delete model[key];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
for (const [key, value] of Object.entries(snapshot)) {
|
|
44
|
+
model[key] = value;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function watchResourceModelState({
|
|
49
|
+
resource,
|
|
50
|
+
model,
|
|
51
|
+
mapLoadedToModel,
|
|
52
|
+
resolveMapContext = null
|
|
53
|
+
} = {}) {
|
|
54
|
+
const modelSnapshot = captureModelSnapshot(model);
|
|
55
|
+
|
|
56
|
+
watch(
|
|
57
|
+
() => resource?.query?.isPending?.value,
|
|
58
|
+
(isPending) => {
|
|
59
|
+
if (!isPending || !modelSnapshot) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
restoreModelSnapshot(model, modelSnapshot);
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
immediate: true
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
watch(
|
|
71
|
+
() => resource?.data?.value,
|
|
72
|
+
(payload) => {
|
|
73
|
+
if (!payload || typeof mapLoadedToModel !== "function") {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const context = typeof resolveMapContext === "function" ? resolveMapContext() : {};
|
|
78
|
+
mapLoadedToModel(model, payload, context);
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
immediate: true
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export {
|
|
87
|
+
captureModelSnapshot,
|
|
88
|
+
restoreModelSnapshot,
|
|
89
|
+
watchResourceModelState
|
|
90
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { useOperationScope } from "../internal/useOperationScope.js";
|
|
2
|
+
|
|
3
|
+
const WEB_OPERATION_ADAPTER_ID = "http-web-default";
|
|
4
|
+
|
|
5
|
+
function createWebOperationAdapter() {
|
|
6
|
+
return Object.freeze({
|
|
7
|
+
id: WEB_OPERATION_ADAPTER_ID,
|
|
8
|
+
useOperationScope(options = {}) {
|
|
9
|
+
return useOperationScope(options);
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const defaultWebOperationAdapter = createWebOperationAdapter();
|
|
15
|
+
|
|
16
|
+
function resolveOperationAdapter(adapter, { context = "http-web operation adapter" } = {}) {
|
|
17
|
+
if (adapter == null) {
|
|
18
|
+
return defaultWebOperationAdapter;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!adapter || typeof adapter !== "object" || Array.isArray(adapter)) {
|
|
22
|
+
throw new TypeError(`${context} must be an object when provided.`);
|
|
23
|
+
}
|
|
24
|
+
if (typeof adapter.useOperationScope !== "function") {
|
|
25
|
+
throw new TypeError(`${context} must expose useOperationScope(options).`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return adapter;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
createWebOperationAdapter,
|
|
33
|
+
resolveOperationAdapter
|
|
34
|
+
};
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { watch } from "vue";
|
|
2
|
+
import { useShellWebErrorRuntime } from "@jskit-ai/shell-web/client/error";
|
|
3
|
+
|
|
4
|
+
function normalizeMessage(value) {
|
|
5
|
+
return String(value || "").trim();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function setupOperationErrorReporting({
|
|
9
|
+
enabled = true,
|
|
10
|
+
source = "http-web.operation",
|
|
11
|
+
loadError = null,
|
|
12
|
+
notFoundError = null,
|
|
13
|
+
loadActionFactory = null,
|
|
14
|
+
notFoundActionFactory = null,
|
|
15
|
+
loadChannel = "",
|
|
16
|
+
notFoundChannel = "",
|
|
17
|
+
loadSeverity = "error",
|
|
18
|
+
notFoundSeverity = "warning",
|
|
19
|
+
dedupeWindowMs = 2000
|
|
20
|
+
} = {}) {
|
|
21
|
+
if (!enabled) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const runtime = useShellWebErrorRuntime();
|
|
26
|
+
const normalizedSource = normalizeMessage(source) || "http-web.operation";
|
|
27
|
+
|
|
28
|
+
function watchMessage(value, {
|
|
29
|
+
kind = "load",
|
|
30
|
+
channel = "",
|
|
31
|
+
intent = "resource-load",
|
|
32
|
+
severity = "error",
|
|
33
|
+
actionFactory = null
|
|
34
|
+
} = {}) {
|
|
35
|
+
let lastMessage = "";
|
|
36
|
+
let lastPresentationId = "";
|
|
37
|
+
|
|
38
|
+
watch(
|
|
39
|
+
() => normalizeMessage(value?.value),
|
|
40
|
+
(nextMessage) => {
|
|
41
|
+
if (!nextMessage) {
|
|
42
|
+
lastMessage = "";
|
|
43
|
+
if (lastPresentationId) {
|
|
44
|
+
runtime.dismiss(lastPresentationId);
|
|
45
|
+
lastPresentationId = "";
|
|
46
|
+
}
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (nextMessage === lastMessage) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
lastMessage = nextMessage;
|
|
55
|
+
const action = typeof actionFactory === "function"
|
|
56
|
+
? actionFactory({
|
|
57
|
+
message: nextMessage,
|
|
58
|
+
kind
|
|
59
|
+
})
|
|
60
|
+
: null;
|
|
61
|
+
const reportResult = runtime.report({
|
|
62
|
+
source: normalizedSource,
|
|
63
|
+
message: nextMessage,
|
|
64
|
+
intent,
|
|
65
|
+
severity,
|
|
66
|
+
channel,
|
|
67
|
+
action,
|
|
68
|
+
dedupeKey: `${normalizedSource}:${kind}:${nextMessage}`,
|
|
69
|
+
dedupeWindowMs
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const nextPresentationId = String(reportResult?.presentationId || "").trim();
|
|
73
|
+
if (nextPresentationId) {
|
|
74
|
+
if (lastPresentationId && lastPresentationId !== nextPresentationId) {
|
|
75
|
+
runtime.dismiss(lastPresentationId);
|
|
76
|
+
}
|
|
77
|
+
lastPresentationId = nextPresentationId;
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
{ immediate: true }
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (loadError) {
|
|
85
|
+
watchMessage(loadError, {
|
|
86
|
+
kind: "load",
|
|
87
|
+
channel: loadChannel,
|
|
88
|
+
intent: "resource-load",
|
|
89
|
+
severity: loadSeverity,
|
|
90
|
+
actionFactory: loadActionFactory
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (notFoundError) {
|
|
95
|
+
watchMessage(notFoundError, {
|
|
96
|
+
kind: "not-found",
|
|
97
|
+
channel: notFoundChannel,
|
|
98
|
+
intent: "resource-load",
|
|
99
|
+
severity: notFoundSeverity,
|
|
100
|
+
actionFactory: notFoundActionFactory
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function setupRouteChangeCleanup({
|
|
106
|
+
enabled = true,
|
|
107
|
+
route = null,
|
|
108
|
+
feedback = null,
|
|
109
|
+
fieldBag = null
|
|
110
|
+
} = {}) {
|
|
111
|
+
if (!enabled) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
watch(
|
|
116
|
+
() => route?.fullPath,
|
|
117
|
+
() => {
|
|
118
|
+
feedback?.clear?.();
|
|
119
|
+
fieldBag?.clear?.();
|
|
120
|
+
}
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export { setupRouteChangeCleanup };
|
|
125
|
+
export { setupOperationErrorReporting };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createValidationFailure,
|
|
3
|
+
resolveFieldErrors
|
|
4
|
+
} from "@jskit-ai/http-runtime/client";
|
|
5
|
+
import { validateSchemaPayload } from "@jskit-ai/kernel/shared/validators";
|
|
6
|
+
|
|
7
|
+
function validateOperationInput({
|
|
8
|
+
input,
|
|
9
|
+
rawPayload = {},
|
|
10
|
+
fieldBag = null,
|
|
11
|
+
feedback = null,
|
|
12
|
+
validationMessage = "Validation failed."
|
|
13
|
+
} = {}) {
|
|
14
|
+
if (!input) {
|
|
15
|
+
return {
|
|
16
|
+
ok: true,
|
|
17
|
+
parsedInput: rawPayload
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
const parsedInput = validateSchemaPayload(input, rawPayload, {
|
|
23
|
+
phase: "input",
|
|
24
|
+
context: "operation input"
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
ok: true,
|
|
29
|
+
parsedInput
|
|
30
|
+
};
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (!error?.fieldErrors || typeof error.fieldErrors !== "object") {
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const failure = createValidationFailure({
|
|
37
|
+
error: String(validationMessage || "Validation failed."),
|
|
38
|
+
code: "validation_failed",
|
|
39
|
+
fieldErrors: error?.fieldErrors
|
|
40
|
+
});
|
|
41
|
+
fieldBag?.apply?.(resolveFieldErrors(failure));
|
|
42
|
+
feedback?.error?.(failure, failure.error);
|
|
43
|
+
return {
|
|
44
|
+
ok: false,
|
|
45
|
+
failure,
|
|
46
|
+
parsedInput: null
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { validateOperationInput };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { useQueryClient } from "@tanstack/vue-query";
|
|
2
|
+
import { resolveFieldErrors } from "@jskit-ai/http-runtime/client";
|
|
3
|
+
import { validateOperationInput } from "./operationValidationHelpers.js";
|
|
4
|
+
import { watchResourceModelState } from "./modelStateHelpers.js";
|
|
5
|
+
|
|
6
|
+
function useAddEditCore({
|
|
7
|
+
model,
|
|
8
|
+
resource,
|
|
9
|
+
queryKey,
|
|
10
|
+
canSave,
|
|
11
|
+
fieldBag,
|
|
12
|
+
feedback,
|
|
13
|
+
input,
|
|
14
|
+
mapLoadedToModel,
|
|
15
|
+
buildRawPayload,
|
|
16
|
+
buildSavePayload,
|
|
17
|
+
onSaveSuccess,
|
|
18
|
+
messages = {}
|
|
19
|
+
} = {}) {
|
|
20
|
+
const queryClient = useQueryClient();
|
|
21
|
+
watchResourceModelState({
|
|
22
|
+
resource,
|
|
23
|
+
model,
|
|
24
|
+
mapLoadedToModel,
|
|
25
|
+
resolveMapContext() {
|
|
26
|
+
return {
|
|
27
|
+
queryClient,
|
|
28
|
+
resource
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const saving = resource?.isSaving;
|
|
34
|
+
const fieldErrors = fieldBag?.errors;
|
|
35
|
+
const message = feedback?.message;
|
|
36
|
+
const messageType = feedback?.messageType;
|
|
37
|
+
|
|
38
|
+
async function submit() {
|
|
39
|
+
if (!canSave?.value || saving?.value) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
feedback?.clear?.();
|
|
44
|
+
fieldBag?.clear?.();
|
|
45
|
+
|
|
46
|
+
const rawPayload = typeof buildRawPayload === "function" ? buildRawPayload(model, {
|
|
47
|
+
queryClient,
|
|
48
|
+
resource
|
|
49
|
+
}) : {};
|
|
50
|
+
|
|
51
|
+
const validationResult = validateOperationInput({
|
|
52
|
+
input,
|
|
53
|
+
rawPayload,
|
|
54
|
+
fieldBag,
|
|
55
|
+
feedback,
|
|
56
|
+
validationMessage: String(messages.validation || "Validation failed.")
|
|
57
|
+
});
|
|
58
|
+
if (!validationResult.ok) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const { parsedInput } = validationResult;
|
|
63
|
+
const savePayload = typeof buildSavePayload === "function"
|
|
64
|
+
? buildSavePayload(parsedInput, {
|
|
65
|
+
rawPayload,
|
|
66
|
+
queryClient,
|
|
67
|
+
resource
|
|
68
|
+
})
|
|
69
|
+
: parsedInput;
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
const queryKeySnapshot = queryKey?.value;
|
|
73
|
+
const payload = await resource.save(savePayload);
|
|
74
|
+
|
|
75
|
+
if (typeof mapLoadedToModel === "function") {
|
|
76
|
+
mapLoadedToModel(model, payload, {
|
|
77
|
+
queryClient,
|
|
78
|
+
resource
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (queryKeySnapshot !== undefined) {
|
|
83
|
+
queryClient.setQueryData(queryKeySnapshot, payload);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (typeof onSaveSuccess === "function") {
|
|
87
|
+
await onSaveSuccess(payload, {
|
|
88
|
+
queryClient,
|
|
89
|
+
parsed: parsedInput,
|
|
90
|
+
rawPayload,
|
|
91
|
+
savePayload,
|
|
92
|
+
resource
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
feedback?.success?.(String(messages.saveSuccess || "Saved."));
|
|
97
|
+
} catch (error) {
|
|
98
|
+
fieldBag?.apply?.(resolveFieldErrors(error));
|
|
99
|
+
feedback?.error?.(error, String(messages.saveError || "Unable to save."));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return Object.freeze({
|
|
104
|
+
saving,
|
|
105
|
+
fieldErrors,
|
|
106
|
+
message,
|
|
107
|
+
messageType,
|
|
108
|
+
submit
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export { useAddEditCore };
|