@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.
Files changed (110) hide show
  1. package/fixtures/crud-list-date-filters/App.vue +107 -0
  2. package/fixtures/crud-list-date-filters/index.html +12 -0
  3. package/fixtures/crud-list-date-filters/main.js +24 -0
  4. package/fixtures/crud-list-date-filters/vite.config.mjs +18 -0
  5. package/package.json +150 -0
  6. package/src/client/bulkActions.js +47 -0
  7. package/src/client/components/CrudAddEditScreen.vue +186 -0
  8. package/src/client/components/CrudDeleteAction.vue +93 -0
  9. package/src/client/components/CrudListBulkActionSurface.vue +126 -0
  10. package/src/client/components/CrudListDateFilterControl.vue +188 -0
  11. package/src/client/components/CrudListFilterSurface.vue +388 -0
  12. package/src/client/components/CrudListRecordActionMenu.vue +120 -0
  13. package/src/client/components/CrudListScreen.vue +501 -0
  14. package/src/client/components/CrudViewScreen.vue +197 -0
  15. package/src/client/composables/crud/crudBindingSupport.js +75 -0
  16. package/src/client/composables/crud/crudHttpClientSupport.js +57 -0
  17. package/src/client/composables/crud/crudJsonApiTransportSupport.js +145 -0
  18. package/src/client/composables/crud/crudLookupFieldLabelSupport.js +151 -0
  19. package/src/client/composables/crud/crudLookupFieldRuntime.js +251 -0
  20. package/src/client/composables/crud/crudSchemaFormHelpers.js +464 -0
  21. package/src/client/composables/internal/crudListFilterLookupSupport.js +161 -0
  22. package/src/client/composables/internal/crudListParentTitleSupport.js +171 -0
  23. package/src/client/composables/internal/useOperationScope.js +144 -0
  24. package/src/client/composables/records/useAddEdit.js +224 -0
  25. package/src/client/composables/records/useCrudAddEdit.js +227 -0
  26. package/src/client/composables/records/useCrudList.js +101 -0
  27. package/src/client/composables/records/useCrudView.js +47 -0
  28. package/src/client/composables/records/useList.js +500 -0
  29. package/src/client/composables/records/useView.js +164 -0
  30. package/src/client/composables/runtime/addEditUiRuntime.js +130 -0
  31. package/src/client/composables/runtime/listUiRuntime.js +123 -0
  32. package/src/client/composables/runtime/modelStateHelpers.js +90 -0
  33. package/src/client/composables/runtime/operationAdapters.js +34 -0
  34. package/src/client/composables/runtime/operationUiHelpers.js +125 -0
  35. package/src/client/composables/runtime/operationValidationHelpers.js +51 -0
  36. package/src/client/composables/runtime/useAddEditCore.js +112 -0
  37. package/src/client/composables/runtime/useCommandCore.js +123 -0
  38. package/src/client/composables/runtime/useEndpointResource.js +199 -0
  39. package/src/client/composables/runtime/useFieldErrorBag.js +61 -0
  40. package/src/client/composables/runtime/useListCore.js +112 -0
  41. package/src/client/composables/runtime/useUiFeedback.js +98 -0
  42. package/src/client/composables/runtime/useViewCore.js +93 -0
  43. package/src/client/composables/runtime/viewUiRuntime.js +96 -0
  44. package/src/client/composables/support/errorMessageHelpers.js +66 -0
  45. package/src/client/composables/support/listQueryParamSupport.js +473 -0
  46. package/src/client/composables/support/listSearchSupport.js +70 -0
  47. package/src/client/composables/support/refValueHelpers.js +19 -0
  48. package/src/client/composables/support/requestQueryRuntimeSupport.js +141 -0
  49. package/src/client/composables/support/resourceLoadStateHelpers.js +208 -0
  50. package/src/client/composables/support/routeTemplateHelpers.js +285 -0
  51. package/src/client/composables/support/scopeHelpers.js +145 -0
  52. package/src/client/composables/useAccess.js +109 -0
  53. package/src/client/composables/useCommand.js +118 -0
  54. package/src/client/composables/useCrudAddEditScreen.js +88 -0
  55. package/src/client/composables/useCrudDeleteAction.js +160 -0
  56. package/src/client/composables/useCrudListBulkActions.js +147 -0
  57. package/src/client/composables/useCrudListFilterLookups.js +146 -0
  58. package/src/client/composables/useCrudListFilters.js +339 -0
  59. package/src/client/composables/useCrudListParentTitle.js +151 -0
  60. package/src/client/composables/useCrudListRowActions.js +144 -0
  61. package/src/client/composables/useCrudListScreen.js +237 -0
  62. package/src/client/composables/useCrudViewScreen.js +77 -0
  63. package/src/client/composables/usePagedCollection.js +181 -0
  64. package/src/client/composables/useRealtimeQueryInvalidation.js +156 -0
  65. package/src/client/composables/useScopeRuntime.js +135 -0
  66. package/src/client/filters.js +15 -0
  67. package/src/client/index.js +24 -0
  68. package/src/client/lib/httpClient.js +47 -0
  69. package/src/client/lib/permissions.js +34 -0
  70. package/src/client/rowActions.js +47 -0
  71. package/src/client/support/contractGuards.js +34 -0
  72. package/src/client/support/crudListDateFilterSupport.js +91 -0
  73. package/test/addEditUiRuntime.test.js +110 -0
  74. package/test/crudBindingSupport.test.js +110 -0
  75. package/test/crudHttpClientSupport.test.js +77 -0
  76. package/test/crudJsonApiTransportSupport.test.js +179 -0
  77. package/test/crudListBulkActionSurface.test.js +27 -0
  78. package/test/crudListDateFilterSupport.test.js +92 -0
  79. package/test/crudListDateFilterSurface.browser.test.js +182 -0
  80. package/test/crudListFilterSurface.test.js +90 -0
  81. package/test/crudLookupFieldRuntime.test.js +264 -0
  82. package/test/crudScreenComponents.test.js +110 -0
  83. package/test/errorIntentContract.test.js +31 -0
  84. package/test/errorMessageHelpers.test.js +28 -0
  85. package/test/exportsContract.test.js +68 -0
  86. package/test/listQueryParamSupport.test.js +223 -0
  87. package/test/listUiRuntime.test.js +102 -0
  88. package/test/operationValidationHelpers.test.js +64 -0
  89. package/test/packageBoundary.test.js +31 -0
  90. package/test/permissions.test.js +35 -0
  91. package/test/refValueHelpers.test.js +14 -0
  92. package/test/requestTransportOptions.test.js +457 -0
  93. package/test/resourceLoadStateHelpers.test.js +189 -0
  94. package/test/routeTemplateHelpers.test.js +85 -0
  95. package/test/scopeHelpers.test.js +57 -0
  96. package/test/useAddEditCore.test.js +124 -0
  97. package/test/useAddEditRequestQueryParams.test.js +190 -0
  98. package/test/useCrudAddEdit.test.js +415 -0
  99. package/test/useCrudDeleteAction.test.js +139 -0
  100. package/test/useCrudListBulkActions.test.js +65 -0
  101. package/test/useCrudListFilterLookups.test.js +114 -0
  102. package/test/useCrudListFilters.test.js +293 -0
  103. package/test/useCrudListParentTitle.test.js +262 -0
  104. package/test/useCrudListRowActions.test.js +77 -0
  105. package/test/useListRouteInitialization.test.js +337 -0
  106. package/test/useListSearchSupport.test.js +61 -0
  107. package/test/usePagedCollection.test.js +53 -0
  108. package/test/useViewRequestQueryParams.test.js +20 -0
  109. package/test/viewCoreLoading.test.js +44 -0
  110. package/test/viewUiRuntime.test.js +95 -0
@@ -0,0 +1,145 @@
1
+ import { resolveEnabledRef, resolveTextRef } from "./refValueHelpers.js";
2
+ import {
3
+ ROUTE_VISIBILITY_TOKENS,
4
+ ROUTE_VISIBILITY_WORKSPACE,
5
+ ROUTE_VISIBILITY_WORKSPACE_USER
6
+ } from "@jskit-ai/kernel/shared/support/visibility";
7
+
8
+ const OWNERSHIP_FILTER_VALUES = ROUTE_VISIBILITY_TOKENS;
9
+ const SCOPED_OWNERSHIP_FILTER_SET = new Set([
10
+ ROUTE_VISIBILITY_WORKSPACE,
11
+ ROUTE_VISIBILITY_WORKSPACE_USER
12
+ ]);
13
+ const ACCESS_MODE_VALUES = Object.freeze(["auto", "always", "never"]);
14
+
15
+ function asPlainObject(value) {
16
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
17
+ return {};
18
+ }
19
+
20
+ return value;
21
+ }
22
+
23
+ function normalizePermissions(value) {
24
+ if (Array.isArray(value)) {
25
+ return value.map((entry) => String(entry || "").trim()).filter(Boolean);
26
+ }
27
+
28
+ const one = String(value || "").trim();
29
+ return one ? [one] : [];
30
+ }
31
+
32
+ function resolvePermissionAccess(access, normalizedPermissions = []) {
33
+ if (normalizedPermissions.length < 1) {
34
+ return true;
35
+ }
36
+
37
+ return access.canAny(normalizedPermissions);
38
+ }
39
+
40
+ function normalizeAccessMode(value = "auto") {
41
+ const normalized = String(value || "auto").trim().toLowerCase();
42
+ if (ACCESS_MODE_VALUES.includes(normalized)) {
43
+ return normalized;
44
+ }
45
+
46
+ throw new TypeError(
47
+ `access must be one of: ${ACCESS_MODE_VALUES.join(", ")}. Received: ${String(value || "") || "(empty)"}`
48
+ );
49
+ }
50
+
51
+ function resolveAccessModeEnabled(accessMode = "auto", { hasPermissionRequirements = false } = {}) {
52
+ const normalizedMode = normalizeAccessMode(accessMode);
53
+ if (normalizedMode === "always") {
54
+ return true;
55
+ }
56
+ if (normalizedMode === "never") {
57
+ return false;
58
+ }
59
+
60
+ return hasPermissionRequirements === true;
61
+ }
62
+
63
+ function ensureAccessModeCompatibility({
64
+ accessMode = "auto",
65
+ hasPermissionRequirements = false,
66
+ caller = "http-web"
67
+ } = {}) {
68
+ const normalizedMode = normalizeAccessMode(accessMode);
69
+ if (normalizedMode === "never" && hasPermissionRequirements) {
70
+ throw new TypeError(`${caller} cannot use access:\"never\" when permission requirements are configured.`);
71
+ }
72
+
73
+ return normalizedMode;
74
+ }
75
+
76
+ function resolveApiSuffix(apiSuffix, context = {}) {
77
+ if (typeof apiSuffix === "function") {
78
+ return resolveTextRef(apiSuffix(context));
79
+ }
80
+
81
+ return resolveTextRef(apiSuffix);
82
+ }
83
+
84
+ function resolveEnabled(value, context = {}) {
85
+ if (typeof value === "function") {
86
+ return Boolean(value(context));
87
+ }
88
+
89
+ return resolveEnabledRef(value);
90
+ }
91
+
92
+ function normalizeOwnershipFilter(value = ROUTE_VISIBILITY_WORKSPACE) {
93
+ const normalized = String(value || ROUTE_VISIBILITY_WORKSPACE).trim().toLowerCase();
94
+ if (OWNERSHIP_FILTER_VALUES.includes(normalized)) {
95
+ return normalized;
96
+ }
97
+
98
+ throw new TypeError(
99
+ `ownershipFilter must be one of: ${OWNERSHIP_FILTER_VALUES.join(", ")}. Received: ${String(value || "") || "(empty)"}`
100
+ );
101
+ }
102
+
103
+ function isScopedOwnershipFilter(ownershipFilter) {
104
+ return SCOPED_OWNERSHIP_FILTER_SET.has(ownershipFilter);
105
+ }
106
+
107
+ function resolveQueryKey(
108
+ queryKeyFactory,
109
+ { surfaceId = "", scopeParamValue = "", ownershipFilter = ROUTE_VISIBILITY_WORKSPACE } = {}
110
+ ) {
111
+ if (typeof queryKeyFactory !== "function") {
112
+ throw new TypeError("queryKeyFactory is required.");
113
+ }
114
+
115
+ if (isScopedOwnershipFilter(ownershipFilter)) {
116
+ return queryKeyFactory(surfaceId, scopeParamValue, ownershipFilter);
117
+ }
118
+
119
+ return queryKeyFactory(surfaceId, ownershipFilter);
120
+ }
121
+
122
+ function resolveResourceMessages(resource, defaults = {}) {
123
+ const defaultMessages = asPlainObject(defaults);
124
+ const resourceMessages = asPlainObject(asPlainObject(resource).messages);
125
+
126
+ return {
127
+ ...defaultMessages,
128
+ ...resourceMessages
129
+ };
130
+ }
131
+
132
+ export {
133
+ asPlainObject,
134
+ normalizePermissions,
135
+ resolvePermissionAccess,
136
+ normalizeAccessMode,
137
+ resolveAccessModeEnabled,
138
+ ensureAccessModeCompatibility,
139
+ resolveApiSuffix,
140
+ resolveEnabled,
141
+ normalizeOwnershipFilter,
142
+ isScopedOwnershipFilter,
143
+ resolveQueryKey,
144
+ resolveResourceMessages
145
+ };
@@ -0,0 +1,109 @@
1
+ import { computed } from "vue";
2
+ import { hasPermission, normalizePermissionList } from "../lib/permissions.js";
3
+ import { resolveEnabledRef, resolveTextRef } from "./support/refValueHelpers.js";
4
+ import {
5
+ normalizeAccessMode,
6
+ resolveAccessModeEnabled
7
+ } from "./support/scopeHelpers.js";
8
+ import { useWebPlacementContext } from "@jskit-ai/shell-web/client/placement";
9
+
10
+ function asPermissionList(value) {
11
+ if (Array.isArray(value)) {
12
+ return value;
13
+ }
14
+
15
+ if (value === undefined || value === null || value === "") {
16
+ return [];
17
+ }
18
+
19
+ return [value];
20
+ }
21
+
22
+ function useAccess({
23
+ scopeParamValue = "",
24
+ enabled = true,
25
+ access = "always",
26
+ hasPermissionRequirements = false
27
+ } = {}) {
28
+ const normalizedAccessMode = normalizeAccessMode(access);
29
+ const accessRequired = resolveAccessModeEnabled(normalizedAccessMode, {
30
+ hasPermissionRequirements: hasPermissionRequirements === true
31
+ });
32
+ const { context: placementContext } = useWebPlacementContext();
33
+ const normalizedScopeParamValue = computed(() => resolveTextRef(scopeParamValue));
34
+ const queryEnabled = computed(() => resolveEnabledRef(enabled) && accessRequired);
35
+ const hasPlacementBootstrapPermissions = computed(() => {
36
+ const source = placementContext.value;
37
+ if (!source || typeof source !== "object") {
38
+ return false;
39
+ }
40
+ return Object.hasOwn(source, "permissions");
41
+ });
42
+ const placementPermissions = computed(() => normalizePermissionList(placementContext.value?.permissions));
43
+ const permissions = computed(() => {
44
+ if (!queryEnabled.value || !hasPlacementBootstrapPermissions.value) {
45
+ return [];
46
+ }
47
+ return placementPermissions.value;
48
+ });
49
+ const bootstrapError = computed(() => {
50
+ if (!queryEnabled.value || hasPlacementBootstrapPermissions.value) {
51
+ return "";
52
+ }
53
+ return "Permissions are unavailable in placement context.";
54
+ });
55
+ const isBootstrapping = computed(() => queryEnabled.value && !hasPlacementBootstrapPermissions.value);
56
+
57
+ function can(permission) {
58
+ return hasPermission(permissions.value, permission);
59
+ }
60
+
61
+ function canAny(requiredPermissions) {
62
+ const list = asPermissionList(requiredPermissions);
63
+ if (list.length < 1) {
64
+ return true;
65
+ }
66
+
67
+ for (const entry of list) {
68
+ if (can(entry)) {
69
+ return true;
70
+ }
71
+ }
72
+
73
+ return false;
74
+ }
75
+
76
+ function canAll(requiredPermissions) {
77
+ const list = asPermissionList(requiredPermissions);
78
+ if (list.length < 1) {
79
+ return true;
80
+ }
81
+
82
+ for (const entry of list) {
83
+ if (!can(entry)) {
84
+ return false;
85
+ }
86
+ }
87
+
88
+ return true;
89
+ }
90
+
91
+ async function refreshBootstrap() {
92
+ return null;
93
+ }
94
+
95
+ return Object.freeze({
96
+ accessMode: normalizedAccessMode,
97
+ accessRequired,
98
+ scopeParamValue: normalizedScopeParamValue,
99
+ permissions,
100
+ bootstrapError,
101
+ isBootstrapping,
102
+ can,
103
+ canAny,
104
+ canAll,
105
+ refreshBootstrap
106
+ });
107
+ }
108
+
109
+ export { useAccess };
@@ -0,0 +1,118 @@
1
+ import { proxyRefs } from "vue";
2
+ import { ROUTE_VISIBILITY_WORKSPACE } from "@jskit-ai/kernel/shared/support/visibility";
3
+ import { useCommandCore } from "./runtime/useCommandCore.js";
4
+ import { useEndpointResource } from "./runtime/useEndpointResource.js";
5
+ import { useOperationScope } from "./internal/useOperationScope.js";
6
+ import { useUiFeedback } from "./runtime/useUiFeedback.js";
7
+ import { useFieldErrorBag } from "./runtime/useFieldErrorBag.js";
8
+ import {
9
+ setupRouteChangeCleanup,
10
+ setupOperationErrorReporting
11
+ } from "./runtime/operationUiHelpers.js";
12
+ import { resolveCrudHttpClient } from "./crud/crudHttpClientSupport.js";
13
+
14
+ function useCommand({
15
+ ownershipFilter = ROUTE_VISIBILITY_WORKSPACE,
16
+ surfaceId = "",
17
+ access = "auto",
18
+ apiSuffix = "",
19
+ runPermissions = [],
20
+ writeMethod = "POST",
21
+ client = null,
22
+ resource: commandResource = null,
23
+ transport = null,
24
+ placementSource = "http-web.command",
25
+ fallbackRunError = "Unable to complete action.",
26
+ fieldErrorKeys = [],
27
+ clearOnRouteChange = true,
28
+ model,
29
+ input,
30
+ buildRawPayload,
31
+ buildCommandPayload,
32
+ buildCommandOptions,
33
+ onRunSuccess,
34
+ onRunError,
35
+ suppressSuccessMessage = false,
36
+ messages = {},
37
+ realtime = null
38
+ } = {}) {
39
+ const operationScope = useOperationScope({
40
+ ownershipFilter,
41
+ surfaceId,
42
+ access,
43
+ placementSource,
44
+ apiSuffix,
45
+ model,
46
+ readEnabled: false,
47
+ permissionSets: {
48
+ run: runPermissions
49
+ },
50
+ realtime
51
+ });
52
+ const routeContext = operationScope.routeContext;
53
+ const canRun = operationScope.permissionGate("run");
54
+
55
+ const endpointResource = useEndpointResource({
56
+ path: operationScope.apiPath,
57
+ enabled: false,
58
+ client: resolveCrudHttpClient(commandResource, { client }),
59
+ writeMethod,
60
+ transport,
61
+ fallbackSaveError: fallbackRunError
62
+ });
63
+
64
+ const feedback = useUiFeedback({
65
+ source: `${placementSource}.feedback`
66
+ });
67
+ const fieldBag = useFieldErrorBag(fieldErrorKeys);
68
+
69
+ const command = useCommandCore({
70
+ model,
71
+ resource: endpointResource,
72
+ writeMethod,
73
+ canRun,
74
+ fieldBag,
75
+ feedback,
76
+ input,
77
+ buildRawPayload,
78
+ buildCommandPayload,
79
+ buildCommandOptions,
80
+ onRunSuccess,
81
+ onRunError,
82
+ suppressSuccessMessage: Boolean(suppressSuccessMessage),
83
+ messages: {
84
+ validation: "Fix invalid values and try again.",
85
+ success: "Completed.",
86
+ error: String(fallbackRunError || "Unable to complete action."),
87
+ ...(messages && typeof messages === "object" ? messages : {})
88
+ }
89
+ });
90
+
91
+ setupRouteChangeCleanup({
92
+ enabled: clearOnRouteChange,
93
+ route: routeContext.route,
94
+ feedback,
95
+ fieldBag
96
+ });
97
+
98
+ const loadError = operationScope.loadError();
99
+ const isLoading = operationScope.isLoading();
100
+ setupOperationErrorReporting({
101
+ source: `${placementSource}.load`,
102
+ loadError
103
+ });
104
+
105
+ return proxyRefs({
106
+ canRun,
107
+ isLoading,
108
+ loadError,
109
+ isRunning: command.running,
110
+ fieldErrors: command.fieldErrors,
111
+ message: command.message,
112
+ messageType: command.messageType,
113
+ run: command.run,
114
+ resource: endpointResource
115
+ });
116
+ }
117
+
118
+ export { useCommand };
@@ -0,0 +1,88 @@
1
+ import { computed, unref } from "vue";
2
+ import { useRoute } from "vue-router";
3
+ import { useCrudAddEdit } from "./records/useCrudAddEdit.js";
4
+
5
+ function normalizeProvidedScreen(screen = null) {
6
+ return screen && typeof screen === "object" && !Array.isArray(screen)
7
+ ? screen
8
+ : null;
9
+ }
10
+
11
+ function useCrudAddEditScreen({
12
+ screen = null,
13
+ mode = "new",
14
+ title = "",
15
+ subtitle = "",
16
+ saveLabel = "Save",
17
+ cancelTo = "",
18
+ resource = null,
19
+ operationName = "",
20
+ formFields = [],
21
+ addEditOptions = {},
22
+ saveSuccess = {},
23
+ fieldBinding = null,
24
+ createModel = null,
25
+ buildPayload = null,
26
+ mapPayloadToModel = null,
27
+ input = null,
28
+ preserveCancelQuery = false
29
+ } = {}) {
30
+ const providedScreen = normalizeProvidedScreen(screen);
31
+ if (providedScreen) {
32
+ return providedScreen;
33
+ }
34
+
35
+ const route = useRoute();
36
+ const formRuntime = useCrudAddEdit({
37
+ resource,
38
+ operationName,
39
+ formFields,
40
+ addEditOptions,
41
+ saveSuccess,
42
+ fieldBinding,
43
+ createModel,
44
+ buildPayload,
45
+ mapPayloadToModel,
46
+ input
47
+ });
48
+ const resolvedMode = computed(() => String(unref(mode) || "new").trim() || "new");
49
+ const resolvedTitle = computed(() => String(unref(title) || "").trim());
50
+ const resolvedSubtitle = computed(() => String(unref(subtitle) || "").trim());
51
+ const resolvedSaveLabel = computed(() => String(unref(saveLabel) || "Save").trim() || "Save");
52
+ const resolvedCancelTo = computed(() => unref(cancelTo));
53
+
54
+ function resolveCancelTo(target = resolvedCancelTo.value) {
55
+ const resolvedTarget = unref(target);
56
+ if (!resolvedTarget) {
57
+ return "";
58
+ }
59
+
60
+ if (typeof resolvedTarget === "string") {
61
+ const resolvedPath = formRuntime.addEdit.resolveParams(resolvedTarget);
62
+ if (!preserveCancelQuery || !resolvedPath) {
63
+ return resolvedPath;
64
+ }
65
+ return {
66
+ path: resolvedPath,
67
+ query: route.query
68
+ };
69
+ }
70
+
71
+ return resolvedTarget;
72
+ }
73
+
74
+ return Object.freeze({
75
+ mode: resolvedMode,
76
+ title: resolvedTitle,
77
+ subtitle: resolvedSubtitle,
78
+ saveLabel: resolvedSaveLabel,
79
+ cancelTo: resolvedCancelTo,
80
+ formRuntime,
81
+ addEdit: formRuntime.addEdit,
82
+ formState: formRuntime.form,
83
+ resolveFieldErrors: formRuntime.resolveFieldErrors,
84
+ resolveCancelTo
85
+ });
86
+ }
87
+
88
+ export { useCrudAddEditScreen };
@@ -0,0 +1,160 @@
1
+ import { computed, proxyRefs, ref, unref } from "vue";
2
+ import { useRouter } from "vue-router";
3
+ import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
4
+ import { resolveCrudJsonApiTransport } from "./crud/crudJsonApiTransportSupport.js";
5
+ import { toQueryErrorMessage } from "./support/errorMessageHelpers.js";
6
+ import { useCommand } from "./useCommand.js";
7
+
8
+ function requireCrudDeleteOperation(resource = null) {
9
+ const operation = resource?.operations?.delete;
10
+ if (!operation || typeof operation !== "object" || Array.isArray(operation)) {
11
+ throw new TypeError("useCrudDeleteAction requires resource.operations.delete.");
12
+ }
13
+ if (normalizeText(operation.method).toUpperCase() !== "DELETE") {
14
+ throw new TypeError("useCrudDeleteAction requires resource.operations.delete.method to be DELETE.");
15
+ }
16
+ return operation;
17
+ }
18
+
19
+ function requireCrudViewScreen(screen = null) {
20
+ if (!screen || typeof screen !== "object" || typeof screen?.view?.resolveParams !== "function") {
21
+ throw new TypeError("useCrudDeleteAction requires a useCrudViewScreen() result.");
22
+ }
23
+ return screen;
24
+ }
25
+
26
+ function resolveDeleteApiSuffix(screen, apiUrlTemplate = "") {
27
+ const template = normalizeText(unref(apiUrlTemplate));
28
+ return template ? normalizeText(screen.view.resolveParams(template)) : "";
29
+ }
30
+
31
+ function resolveListLocation(screen) {
32
+ return unref(screen?.listLocation) || null;
33
+ }
34
+
35
+ function useCrudDeleteAction({
36
+ screen = null,
37
+ resource = null,
38
+ resourceNamespace = "",
39
+ apiUrlTemplate = "",
40
+ access = "auto",
41
+ client = null,
42
+ router: routerOverride = null,
43
+ fallbackDeleteError = "Unable to delete record."
44
+ } = {}) {
45
+ const resolvedScreen = requireCrudViewScreen(screen);
46
+ const deleteOperation = requireCrudDeleteOperation(resource);
47
+ const namespace = normalizeText(resourceNamespace, {
48
+ fallback: resource?.namespace
49
+ });
50
+ if (!namespace) {
51
+ throw new TypeError("useCrudDeleteAction requires resourceNamespace or resource.namespace.");
52
+ }
53
+
54
+ const activeRouter = routerOverride || useRouter();
55
+ if (!activeRouter || typeof activeRouter.push !== "function") {
56
+ throw new TypeError("useCrudDeleteAction requires an installed Vue Router.");
57
+ }
58
+
59
+ const isOpen = ref(false);
60
+ const error = ref("");
61
+ const deleteApiSuffix = computed(() => resolveDeleteApiSuffix(resolvedScreen, apiUrlTemplate));
62
+
63
+ async function handleDeleteSuccess(_response, context = {}) {
64
+ await context?.queryClient?.invalidateQueries?.({
65
+ queryKey: ["ui-generator", namespace]
66
+ });
67
+
68
+ const listLocation = resolveListLocation(resolvedScreen);
69
+ if (!listLocation) {
70
+ throw new Error("Deleted the record, but the generated list route is unavailable.");
71
+ }
72
+
73
+ isOpen.value = false;
74
+ await activeRouter.push(listLocation);
75
+ }
76
+
77
+ function handleDeleteError(cause) {
78
+ error.value = toQueryErrorMessage(
79
+ cause,
80
+ fallbackDeleteError,
81
+ "Unable to delete record."
82
+ );
83
+ }
84
+
85
+ const command = useCommand({
86
+ access,
87
+ apiSuffix: deleteApiSuffix,
88
+ writeMethod: deleteOperation.method,
89
+ client,
90
+ transport: resolveCrudJsonApiTransport(undefined, resource, {
91
+ mode: "delete"
92
+ }),
93
+ placementSource: `ui-generator.${namespace}.view.delete`,
94
+ fallbackRunError: fallbackDeleteError,
95
+ onRunSuccess: handleDeleteSuccess,
96
+ onRunError: handleDeleteError,
97
+ suppressSuccessMessage: true
98
+ });
99
+
100
+ const isDeleting = computed(() => Boolean(command.isRunning));
101
+ const canDelete = computed(() => {
102
+ const view = resolvedScreen.view;
103
+ return Boolean(
104
+ command.canRun &&
105
+ deleteApiSuffix.value &&
106
+ resolveListLocation(resolvedScreen) &&
107
+ unref(view.recordId) &&
108
+ unref(view.record) &&
109
+ !unref(view.isLoading) &&
110
+ !unref(view.isNotFound)
111
+ );
112
+ });
113
+
114
+ function request() {
115
+ if (!canDelete.value) {
116
+ return false;
117
+ }
118
+ error.value = "";
119
+ isOpen.value = true;
120
+ return true;
121
+ }
122
+
123
+ function cancel() {
124
+ if (isDeleting.value) {
125
+ return false;
126
+ }
127
+ error.value = "";
128
+ isOpen.value = false;
129
+ return true;
130
+ }
131
+
132
+ async function confirm() {
133
+ if (!isOpen.value || !canDelete.value || isDeleting.value) {
134
+ return null;
135
+ }
136
+
137
+ error.value = "";
138
+ try {
139
+ return await command.run();
140
+ } catch {
141
+ return null;
142
+ }
143
+ }
144
+
145
+ return proxyRefs({
146
+ isOpen,
147
+ isDeleting,
148
+ canDelete,
149
+ error,
150
+ request,
151
+ cancel,
152
+ confirm
153
+ });
154
+ }
155
+
156
+ export {
157
+ requireCrudDeleteOperation,
158
+ resolveDeleteApiSuffix,
159
+ useCrudDeleteAction
160
+ };