@weconstudio/nuxt 0.0.0-stage → 1.72.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/LICENSE +1 -0
- package/README.md +115 -2
- package/dist/module.d.mts +7 -0
- package/dist/module.json +12 -0
- package/dist/module.mjs +250 -0
- package/dist/runtime/components/We/Autocomplete.d.vue.ts +13 -0
- package/dist/runtime/components/We/Autocomplete.vue +91 -0
- package/dist/runtime/components/We/Autocomplete.vue.d.ts +13 -0
- package/dist/runtime/components/We/BoldText.d.vue.ts +17 -0
- package/dist/runtime/components/We/BoldText.vue +29 -0
- package/dist/runtime/components/We/BoldText.vue.d.ts +17 -0
- package/dist/runtime/components/We/ColorPicker.d.vue.ts +11 -0
- package/dist/runtime/components/We/ColorPicker.vue +199 -0
- package/dist/runtime/components/We/ColorPicker.vue.d.ts +11 -0
- package/dist/runtime/components/We/ConfirmDialog.d.vue.ts +104 -0
- package/dist/runtime/components/We/ConfirmDialog.vue +143 -0
- package/dist/runtime/components/We/ConfirmDialog.vue.d.ts +104 -0
- package/dist/runtime/components/We/Datatable.d.vue.ts +40 -0
- package/dist/runtime/components/We/Datatable.vue +87 -0
- package/dist/runtime/components/We/Datatable.vue.d.ts +40 -0
- package/dist/runtime/components/We/DatatableInternal.d.vue.ts +194 -0
- package/dist/runtime/components/We/DatatableInternal.vue +638 -0
- package/dist/runtime/components/We/DatatableInternal.vue.d.ts +194 -0
- package/dist/runtime/components/We/DatePicker.d.vue.ts +51 -0
- package/dist/runtime/components/We/DatePicker.vue +901 -0
- package/dist/runtime/components/We/DatePicker.vue.d.ts +51 -0
- package/dist/runtime/components/We/Dt/Actions.d.vue.ts +51 -0
- package/dist/runtime/components/We/Dt/Actions.vue +106 -0
- package/dist/runtime/components/We/Dt/Actions.vue.d.ts +51 -0
- package/dist/runtime/components/We/Dt/BooleanFormatter.d.vue.ts +8 -0
- package/dist/runtime/components/We/Dt/BooleanFormatter.vue +26 -0
- package/dist/runtime/components/We/Dt/BooleanFormatter.vue.d.ts +8 -0
- package/dist/runtime/components/We/Dt/CardFilterContainer.d.vue.ts +17 -0
- package/dist/runtime/components/We/Dt/CardFilterContainer.vue +60 -0
- package/dist/runtime/components/We/Dt/CardFilterContainer.vue.d.ts +17 -0
- package/dist/runtime/components/We/Dt/CheckboxFilter.d.vue.ts +16 -0
- package/dist/runtime/components/We/Dt/CheckboxFilter.vue +47 -0
- package/dist/runtime/components/We/Dt/CheckboxFilter.vue.d.ts +16 -0
- package/dist/runtime/components/We/Dt/ChipGroupSelect.d.vue.ts +7 -0
- package/dist/runtime/components/We/Dt/ChipGroupSelect.vue +35 -0
- package/dist/runtime/components/We/Dt/ChipGroupSelect.vue.d.ts +7 -0
- package/dist/runtime/components/We/Dt/ClusterFilter.d.vue.ts +18 -0
- package/dist/runtime/components/We/Dt/ClusterFilter.vue +68 -0
- package/dist/runtime/components/We/Dt/ClusterFilter.vue.d.ts +18 -0
- package/dist/runtime/components/We/Dt/DatasetFormatter.d.vue.ts +19 -0
- package/dist/runtime/components/We/Dt/DatasetFormatter.vue +65 -0
- package/dist/runtime/components/We/Dt/DatasetFormatter.vue.d.ts +19 -0
- package/dist/runtime/components/We/Dt/DaterangeFilter.d.vue.ts +28 -0
- package/dist/runtime/components/We/Dt/DaterangeFilter.vue +140 -0
- package/dist/runtime/components/We/Dt/DaterangeFilter.vue.d.ts +28 -0
- package/dist/runtime/components/We/Dt/Filter.d.vue.ts +7 -0
- package/dist/runtime/components/We/Dt/Filter.vue +234 -0
- package/dist/runtime/components/We/Dt/Filter.vue.d.ts +7 -0
- package/dist/runtime/components/We/Dt/Filters.d.vue.ts +6 -0
- package/dist/runtime/components/We/Dt/Filters.vue +24 -0
- package/dist/runtime/components/We/Dt/Filters.vue.d.ts +6 -0
- package/dist/runtime/components/We/Dt/LuxonFormatter.d.vue.ts +12 -0
- package/dist/runtime/components/We/Dt/LuxonFormatter.vue +23 -0
- package/dist/runtime/components/We/Dt/LuxonFormatter.vue.d.ts +12 -0
- package/dist/runtime/components/We/Dt/SelectFilter.d.vue.ts +60 -0
- package/dist/runtime/components/We/Dt/SelectFilter.vue +333 -0
- package/dist/runtime/components/We/Dt/SelectFilter.vue.d.ts +60 -0
- package/dist/runtime/components/We/Dt/SelectResourceFilter.d.vue.ts +43 -0
- package/dist/runtime/components/We/Dt/SelectResourceFilter.vue +127 -0
- package/dist/runtime/components/We/Dt/SelectResourceFilter.vue.d.ts +43 -0
- package/dist/runtime/components/We/Dt/dt.types.d.ts +8 -0
- package/dist/runtime/components/We/EndpointSwitcher.d.vue.ts +12 -0
- package/dist/runtime/components/We/EndpointSwitcher.vue +256 -0
- package/dist/runtime/components/We/EndpointSwitcher.vue.d.ts +12 -0
- package/dist/runtime/components/We/Import.d.vue.ts +118 -0
- package/dist/runtime/components/We/Import.vue +730 -0
- package/dist/runtime/components/We/Import.vue.d.ts +118 -0
- package/dist/runtime/components/We/ImportDefaultValues.d.vue.ts +3666 -0
- package/dist/runtime/components/We/ImportDefaultValues.vue +115 -0
- package/dist/runtime/components/We/ImportDefaultValues.vue.d.ts +3666 -0
- package/dist/runtime/components/We/ImportMatchColumns.d.vue.ts +66 -0
- package/dist/runtime/components/We/ImportMatchColumns.vue +172 -0
- package/dist/runtime/components/We/ImportMatchColumns.vue.d.ts +66 -0
- package/dist/runtime/components/We/ImportPreviewResults.d.vue.ts +38 -0
- package/dist/runtime/components/We/ImportPreviewResults.vue +165 -0
- package/dist/runtime/components/We/ImportPreviewResults.vue.d.ts +38 -0
- package/dist/runtime/components/We/ImportSelectStart.d.vue.ts +47 -0
- package/dist/runtime/components/We/ImportSelectStart.vue +106 -0
- package/dist/runtime/components/We/ImportSelectStart.vue.d.ts +47 -0
- package/dist/runtime/components/We/Notification.d.vue.ts +7 -0
- package/dist/runtime/components/We/Notification.vue +49 -0
- package/dist/runtime/components/We/Notification.vue.d.ts +7 -0
- package/dist/runtime/components/We/S3Dropzone.d.vue.ts +17 -0
- package/dist/runtime/components/We/S3Dropzone.vue +527 -0
- package/dist/runtime/components/We/S3Dropzone.vue.d.ts +17 -0
- package/dist/runtime/components/We/Select.d.vue.ts +39 -0
- package/dist/runtime/components/We/Select.vue +354 -0
- package/dist/runtime/components/We/Select.vue.d.ts +39 -0
- package/dist/runtime/components/We/TipTap.d.vue.ts +26 -0
- package/dist/runtime/components/We/TipTap.vue +362 -0
- package/dist/runtime/components/We/TipTap.vue.d.ts +26 -0
- package/dist/runtime/components/We/datatable.props.d.ts +57 -0
- package/dist/runtime/components/We/datatable.props.js +60 -0
- package/dist/runtime/composables/datatable/useAutoFillHeight.d.ts +31 -0
- package/dist/runtime/composables/datatable/useAutoFillHeight.js +57 -0
- package/dist/runtime/composables/datatable/useDataTable.d.ts +33 -0
- package/dist/runtime/composables/datatable/useDataTable.js +161 -0
- package/dist/runtime/composables/datatable/useDatatableColumns.d.ts +12 -0
- package/dist/runtime/composables/datatable/useDatatableColumns.js +96 -0
- package/dist/runtime/composables/useApi.d.ts +56 -0
- package/dist/runtime/composables/useApi.js +230 -0
- package/dist/runtime/composables/useApiFetch.d.ts +2 -0
- package/dist/runtime/composables/useApiFetch.js +9 -0
- package/dist/runtime/composables/useApiUrl.d.ts +10 -0
- package/dist/runtime/composables/useApiUrl.js +37 -0
- package/dist/runtime/composables/useAuth.d.ts +9 -0
- package/dist/runtime/composables/useAuth.js +22 -0
- package/dist/runtime/composables/useBus.d.ts +8 -0
- package/dist/runtime/composables/useBus.js +18 -0
- package/dist/runtime/composables/useDateRangePresets.d.ts +21 -0
- package/dist/runtime/composables/useDateRangePresets.js +90 -0
- package/dist/runtime/composables/useForm.d.ts +29 -0
- package/dist/runtime/composables/useForm.js +74 -0
- package/dist/runtime/composables/useFormLock.d.ts +11 -0
- package/dist/runtime/composables/useFormLock.js +8 -0
- package/dist/runtime/composables/useFormWrapper.d.ts +16 -0
- package/dist/runtime/composables/useFormWrapper.js +36 -0
- package/dist/runtime/composables/useImportJob.d.ts +55 -0
- package/dist/runtime/composables/useImportJob.js +94 -0
- package/dist/runtime/composables/useLux.d.ts +1 -0
- package/dist/runtime/composables/useLux.js +8 -0
- package/dist/runtime/composables/useNotify.d.ts +12 -0
- package/dist/runtime/composables/useNotify.js +24 -0
- package/dist/runtime/composables/usePaginatedFetch.d.ts +24 -0
- package/dist/runtime/composables/usePaginatedFetch.js +154 -0
- package/dist/runtime/composables/useServerErrors.d.ts +16 -0
- package/dist/runtime/composables/useServerErrors.js +60 -0
- package/dist/runtime/composables/useValidators.d.ts +18 -0
- package/dist/runtime/composables/useValidators.js +56 -0
- package/dist/runtime/generator/code/dt.d.ts +8 -0
- package/dist/runtime/generator/code/dt.js +97 -0
- package/dist/runtime/generator/code/edit.d.ts +4 -0
- package/dist/runtime/generator/code/edit.js +41 -0
- package/dist/runtime/generator/code/form.d.ts +6 -0
- package/dist/runtime/generator/code/form.js +229 -0
- package/dist/runtime/generator/code/i18n.d.ts +5 -0
- package/dist/runtime/generator/code/i18n.js +43 -0
- package/dist/runtime/generator/code/index.d.ts +11 -0
- package/dist/runtime/generator/code/index.js +90 -0
- package/dist/runtime/generator/components/CodeSnippet.d.vue.ts +14 -0
- package/dist/runtime/generator/components/CodeSnippet.vue +49 -0
- package/dist/runtime/generator/components/CodeSnippet.vue.d.ts +14 -0
- package/dist/runtime/generator/components/Configuration.d.vue.ts +16 -0
- package/dist/runtime/generator/components/Configuration.vue +154 -0
- package/dist/runtime/generator/components/Configuration.vue.d.ts +16 -0
- package/dist/runtime/generator/components/GeneratorPage.d.vue.ts +50 -0
- package/dist/runtime/generator/components/GeneratorPage.vue +226 -0
- package/dist/runtime/generator/components/GeneratorPage.vue.d.ts +50 -0
- package/dist/runtime/generator/fs.d.ts +16 -0
- package/dist/runtime/generator/fs.js +79 -0
- package/dist/runtime/generator/index.d.vue.ts +52 -0
- package/dist/runtime/generator/index.vue +17 -0
- package/dist/runtime/generator/index.vue.d.ts +52 -0
- package/dist/runtime/internals/api.d.ts +3 -0
- package/dist/runtime/internals/api.js +16 -0
- package/dist/runtime/internals/bus.d.ts +1 -0
- package/dist/runtime/internals/bus.js +2 -0
- package/dist/runtime/internals/dt.d.ts +24 -0
- package/dist/runtime/internals/dt.js +57 -0
- package/dist/runtime/lang/en.json +211 -0
- package/dist/runtime/lang/it.json +211 -0
- package/dist/runtime/mixins/formValidator.d.ts +38 -0
- package/dist/runtime/mixins/formValidator.js +118 -0
- package/dist/runtime/plugins/api-legacy.d.ts +76 -0
- package/dist/runtime/plugins/api-legacy.js +11 -0
- package/dist/runtime/plugins/auth-legacy.d.ts +22 -0
- package/dist/runtime/plugins/auth-legacy.js +14 -0
- package/dist/runtime/plugins/bus-legacy.d.ts +6 -0
- package/dist/runtime/plugins/bus-legacy.js +9 -0
- package/dist/runtime/plugins/notify-legacy.d.ts +25 -0
- package/dist/runtime/plugins/notify-legacy.js +35 -0
- package/dist/runtime/plugins/portal.d.ts +2 -0
- package/dist/runtime/plugins/portal.js +5 -0
- package/dist/runtime/plugins/update-sanctum-app-config.d.ts +2 -0
- package/dist/runtime/plugins/update-sanctum-app-config.js +44 -0
- package/dist/runtime/plugins/vuetify-nuxt-icon.d.ts +4 -0
- package/dist/runtime/plugins/vuetify-nuxt-icon.js +30 -0
- package/dist/runtime/types/app-config.d.ts +24 -0
- package/dist/runtime/types/app-config.js +1 -0
- package/dist/runtime/types/icons.d.ts +82 -0
- package/dist/runtime/types/icons.js +84 -0
- package/dist/runtime/types/index.d.ts +2 -0
- package/dist/runtime/types/index.js +2 -0
- package/dist/runtime/types/module-options.d.ts +29 -0
- package/dist/runtime/types/module-options.js +0 -0
- package/dist/runtime/types/runtime-hooks.d.ts +17 -0
- package/dist/runtime/types/runtime-hooks.js +1 -0
- package/dist/runtime/utils/color.d.ts +10 -0
- package/dist/runtime/utils/color.js +38 -0
- package/dist/runtime/utils/file.d.ts +17 -0
- package/dist/runtime/utils/file.js +108 -0
- package/dist/runtime/utils/format.d.ts +1 -0
- package/dist/runtime/utils/format.js +14 -0
- package/dist/runtime/utils/make-enum.d.ts +15 -0
- package/dist/runtime/utils/make-enum.js +27 -0
- package/dist/runtime/utils/mergeDefaults.d.ts +1 -0
- package/dist/runtime/utils/mergeDefaults.js +18 -0
- package/dist/runtime/utils/object-to-formdata.d.ts +10 -0
- package/dist/runtime/utils/object-to-formdata.js +82 -0
- package/dist/runtime/utils/uuid.d.ts +1 -0
- package/dist/runtime/utils/uuid.js +9 -0
- package/dist/types.d.mts +9 -0
- package/package.json +92 -4
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { computed, ref, toRaw, watch } from "vue";
|
|
2
|
+
import { isFilterComplete } from "../../internals/dt.js";
|
|
3
|
+
import { useApi } from "../useApi.js";
|
|
4
|
+
const ABORT_REASON = "ABORT-PREV";
|
|
5
|
+
export function useDataTable(dtOptions) {
|
|
6
|
+
const configuration = ref();
|
|
7
|
+
const totalItems = ref(0);
|
|
8
|
+
const items = ref([]);
|
|
9
|
+
const loading = ref(false);
|
|
10
|
+
const error = ref();
|
|
11
|
+
const aggregates = ref();
|
|
12
|
+
const prevOptions = ref();
|
|
13
|
+
let abortController;
|
|
14
|
+
const allHeaders = computed(() => {
|
|
15
|
+
if (!configuration.value) {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
const val = toRaw(configuration.value.datatable.headers);
|
|
19
|
+
return Object.entries(val).map(([key, { label, ...value }]) => ({
|
|
20
|
+
title: label,
|
|
21
|
+
key,
|
|
22
|
+
...value,
|
|
23
|
+
field: configuration.value.fields[key]
|
|
24
|
+
}));
|
|
25
|
+
});
|
|
26
|
+
const optionalHeaders = computed(() => allHeaders.value.filter((h) => h.optional));
|
|
27
|
+
const defaultColumns = computed(() => optionalHeaders.value.filter((h) => h.optional_visible).map((h) => h.key));
|
|
28
|
+
const visibleColumns = computed(() => dtOptions.columns?.value ?? defaultColumns.value);
|
|
29
|
+
const headers = computed(() => {
|
|
30
|
+
return allHeaders.value.filter((h) => h.optional ? visibleColumns.value.includes(h.key) : !h.hidden);
|
|
31
|
+
});
|
|
32
|
+
const columnsParam = computed(() => dtOptions.columns?.value ? { columns: dtOptions.columns?.value } : {});
|
|
33
|
+
const pagination = computed(() => configuration.value?.datatable.pagination);
|
|
34
|
+
const availableFilters = computed(() => allHeaders.value.filter((h) => h.filter?.advanced && (!h.optional || visibleColumns.value.includes(h.key))));
|
|
35
|
+
const selectionKey = computed(() => configuration.value?.datatable.selection?.id ?? null);
|
|
36
|
+
const actions = computed(() => configuration.value?.actions ?? []);
|
|
37
|
+
const selectedItems = ref([]);
|
|
38
|
+
const loadingExecuteAction = ref(false);
|
|
39
|
+
function clearSelection() {
|
|
40
|
+
selectedItems.value = [];
|
|
41
|
+
}
|
|
42
|
+
async function executeServerAction(action, params, fetchOptions) {
|
|
43
|
+
const body = {
|
|
44
|
+
action,
|
|
45
|
+
...params
|
|
46
|
+
};
|
|
47
|
+
return useApi().post(`${dtOptions.resource.value}/datatables`, body, fetchOptions);
|
|
48
|
+
}
|
|
49
|
+
const filtersValue = computed(() => {
|
|
50
|
+
return Object.entries(dtOptions.filters.value).map(([id, f]) => ({
|
|
51
|
+
id,
|
|
52
|
+
value: f?.value ?? null,
|
|
53
|
+
operator: f?.operator
|
|
54
|
+
})).filter(isFilterComplete);
|
|
55
|
+
});
|
|
56
|
+
async function executeAction(action, extraParams = {}, reset = true, keepSelection = false) {
|
|
57
|
+
loadingExecuteAction.value = true;
|
|
58
|
+
try {
|
|
59
|
+
const res = await executeServerAction(action, {
|
|
60
|
+
filters: filtersValue.value,
|
|
61
|
+
...prevOptions.value,
|
|
62
|
+
selection: selectedItems.value,
|
|
63
|
+
...columnsParam.value,
|
|
64
|
+
...dtOptions.requestParams.value,
|
|
65
|
+
...extraParams
|
|
66
|
+
});
|
|
67
|
+
switch (res?.action) {
|
|
68
|
+
case "redirect":
|
|
69
|
+
window.location = res.url;
|
|
70
|
+
break;
|
|
71
|
+
case "refresh":
|
|
72
|
+
default:
|
|
73
|
+
if (reset) {
|
|
74
|
+
reloadData(false, keepSelection);
|
|
75
|
+
}
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
if (reset && !keepSelection) {
|
|
79
|
+
clearSelection();
|
|
80
|
+
}
|
|
81
|
+
return res;
|
|
82
|
+
} finally {
|
|
83
|
+
loadingExecuteAction.value = false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const requestKey = computed(() => JSON.stringify([filtersValue.value, dtOptions.requestParams.value, dtOptions.columns?.value]));
|
|
87
|
+
watch([dtOptions.resource, requestKey], ([newRes], [oldRes]) => {
|
|
88
|
+
const reloadConfig = newRes !== oldRes;
|
|
89
|
+
reloadData(reloadConfig);
|
|
90
|
+
});
|
|
91
|
+
const load = async (options, reloadConfig = false, keepSelection = false) => {
|
|
92
|
+
error.value = void 0;
|
|
93
|
+
abortController?.abort(ABORT_REASON);
|
|
94
|
+
abortController = new AbortController();
|
|
95
|
+
const signal = abortController.signal;
|
|
96
|
+
loading.value = true;
|
|
97
|
+
try {
|
|
98
|
+
const params = {
|
|
99
|
+
search: options.search,
|
|
100
|
+
sorts: options.sortBy.map(({ key, order }) => ({
|
|
101
|
+
id: key,
|
|
102
|
+
desc: order === "desc"
|
|
103
|
+
})),
|
|
104
|
+
filters: filtersValue.value,
|
|
105
|
+
conf: !configuration.value || reloadConfig || dtOptions.loadConfiguration?.value === "always",
|
|
106
|
+
page: options.page,
|
|
107
|
+
limit: options.itemsPerPage,
|
|
108
|
+
...columnsParam.value,
|
|
109
|
+
...dtOptions.requestParams.value
|
|
110
|
+
};
|
|
111
|
+
prevOptions.value = options;
|
|
112
|
+
const { conf, data, meta, aggregates: apiAggregates } = await executeServerAction("read", params, { notify: false, signal });
|
|
113
|
+
if (conf) {
|
|
114
|
+
configuration.value = conf;
|
|
115
|
+
}
|
|
116
|
+
aggregates.value = typeof apiAggregates === "object" && !Array.isArray(apiAggregates) ? apiAggregates : {};
|
|
117
|
+
totalItems.value = pagination.value ? meta.total : data.length;
|
|
118
|
+
items.value = data;
|
|
119
|
+
if (!keepSelection) {
|
|
120
|
+
clearSelection();
|
|
121
|
+
}
|
|
122
|
+
return { data, meta };
|
|
123
|
+
} catch (e) {
|
|
124
|
+
if (e.cause === ABORT_REASON) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
error.value = e;
|
|
128
|
+
console.error(e);
|
|
129
|
+
} finally {
|
|
130
|
+
if (!signal.aborted) {
|
|
131
|
+
loading.value = false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
const reloadData = (reloadConfig = false, keepSelection = false) => {
|
|
136
|
+
if (prevOptions.value) {
|
|
137
|
+
load(prevOptions.value, reloadConfig, keepSelection);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
return {
|
|
141
|
+
error,
|
|
142
|
+
totalItems,
|
|
143
|
+
aggregates,
|
|
144
|
+
items,
|
|
145
|
+
headers,
|
|
146
|
+
pagination,
|
|
147
|
+
actions,
|
|
148
|
+
selectedItems,
|
|
149
|
+
loadingExecuteAction,
|
|
150
|
+
clearSelection,
|
|
151
|
+
selectionKey,
|
|
152
|
+
load,
|
|
153
|
+
loading,
|
|
154
|
+
reloadData,
|
|
155
|
+
executeAction,
|
|
156
|
+
availableFilters,
|
|
157
|
+
optionalHeaders,
|
|
158
|
+
visibleColumns,
|
|
159
|
+
defaultColumns
|
|
160
|
+
};
|
|
161
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Ref } from 'vue';
|
|
2
|
+
import type { Header } from '~/types';
|
|
3
|
+
export declare function useDatatableColumns(headers: Ref<Header[]>, extraHeaders: Ref<Header[]>, actionsHeader: Ref<Header | boolean>): {
|
|
4
|
+
allHeaders: import("vue").ComputedRef<any[]>;
|
|
5
|
+
autoSlots: import("vue").ComputedRef<(false | {
|
|
6
|
+
slot: string;
|
|
7
|
+
component: any;
|
|
8
|
+
field: any;
|
|
9
|
+
key: any;
|
|
10
|
+
})[]>;
|
|
11
|
+
userDefinedSlots: import("vue").ComputedRef<string[]>;
|
|
12
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { useAppConfig } from "#app";
|
|
2
|
+
import { computed, useSlots } from "vue";
|
|
3
|
+
import BooleanFormatter from "../../components/We/Dt/BooleanFormatter.vue";
|
|
4
|
+
import DatasetFormatter from "../../components/We/Dt/DatasetFormatter.vue";
|
|
5
|
+
import LuxonFormatter from "../../components/We/Dt/LuxonFormatter.vue";
|
|
6
|
+
import { addClass } from "../../internals/dt.js";
|
|
7
|
+
const defaultFormatters = {
|
|
8
|
+
date: LuxonFormatter,
|
|
9
|
+
datetime: LuxonFormatter,
|
|
10
|
+
boolean: BooleanFormatter,
|
|
11
|
+
dataset: DatasetFormatter
|
|
12
|
+
};
|
|
13
|
+
export function useDatatableColumns(headers, extraHeaders, actionsHeader) {
|
|
14
|
+
const slots = useSlots();
|
|
15
|
+
const userDefinedSlots = computed(() => {
|
|
16
|
+
return Object.keys(slots).filter((s) => s !== "default");
|
|
17
|
+
});
|
|
18
|
+
const formatters = {
|
|
19
|
+
...defaultFormatters,
|
|
20
|
+
...useAppConfig().we?.datatables?.formatters
|
|
21
|
+
};
|
|
22
|
+
const autoSlots = computed(
|
|
23
|
+
() => headers.value.map((h) => {
|
|
24
|
+
const formatter = h.field?.formatter?.value ?? h.field.type;
|
|
25
|
+
const key = h.key;
|
|
26
|
+
const slot = `item.${key}`;
|
|
27
|
+
if (userDefinedSlots.value.includes(slot)) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
const component = formatters[formatter];
|
|
31
|
+
if (!component) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
slot: `item.${key}`,
|
|
36
|
+
component,
|
|
37
|
+
field: { ...h.field, filter: h.filter },
|
|
38
|
+
key
|
|
39
|
+
};
|
|
40
|
+
}).filter(Boolean)
|
|
41
|
+
);
|
|
42
|
+
const allExtraHeaders = computed(() => {
|
|
43
|
+
const h = [...extraHeaders.value];
|
|
44
|
+
const actionSlotName = actionsHeader.value?.id ?? "actions";
|
|
45
|
+
const hasActionHeader = actionsHeader.value && !extraHeaders.value.find((h2) => [h2.id, h2.value].includes(actionSlotName)) && userDefinedSlots.value.includes(`item.${actionSlotName}`);
|
|
46
|
+
if (hasActionHeader) {
|
|
47
|
+
const actionH = {
|
|
48
|
+
key: actionSlotName,
|
|
49
|
+
align: "end",
|
|
50
|
+
fixed: "end",
|
|
51
|
+
...actionsHeader.value
|
|
52
|
+
};
|
|
53
|
+
if (!actionH.width) {
|
|
54
|
+
actionH.headerProps = { style: "width: 1px", ...actionH.headerProps };
|
|
55
|
+
actionH.cellProps = { style: "width: 1px; white-space: nowrap", ...actionH.cellProps };
|
|
56
|
+
}
|
|
57
|
+
h.push(actionH);
|
|
58
|
+
}
|
|
59
|
+
return h;
|
|
60
|
+
});
|
|
61
|
+
const allHeaders = computed(() => {
|
|
62
|
+
return [...headers.value, ...allExtraHeaders.value].map((h) => {
|
|
63
|
+
h = {
|
|
64
|
+
...h,
|
|
65
|
+
key: h.key ?? h.id ?? h.value,
|
|
66
|
+
title: h.title ?? h.label ?? h.text,
|
|
67
|
+
align: alignHeader(h),
|
|
68
|
+
sortable: h.sortable?.value ?? h.sortable ?? false,
|
|
69
|
+
maxWidth: h.width,
|
|
70
|
+
minWidth: h.width
|
|
71
|
+
};
|
|
72
|
+
const classes = [].filter(Boolean);
|
|
73
|
+
h.headerProps ??= {};
|
|
74
|
+
h.cellProps ??= {};
|
|
75
|
+
h.headerProps.class = addClass(h.class, classes);
|
|
76
|
+
h.cellProps.class = addClass(h.cellClass, classes);
|
|
77
|
+
return h;
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
function alignHeader({ align }) {
|
|
81
|
+
switch (align) {
|
|
82
|
+
case "right":
|
|
83
|
+
case "end":
|
|
84
|
+
return "end";
|
|
85
|
+
case "center":
|
|
86
|
+
return "center";
|
|
87
|
+
default:
|
|
88
|
+
return "start";
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
allHeaders,
|
|
93
|
+
autoSlots,
|
|
94
|
+
userDefinedSlots
|
|
95
|
+
};
|
|
96
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { $Fetch, FetchOptions as _FetchOptions } from 'ofetch';
|
|
2
|
+
import type { Options as ObjectToFormDataOptions } from '../utils/object-to-formdata.js';
|
|
3
|
+
export type BodyRequest = RequestInit['body'] | Record<string, any> | undefined;
|
|
4
|
+
export type FetchOptions = _FetchOptions & WeNuxtFetchOptions;
|
|
5
|
+
export type InputFormData = any;
|
|
6
|
+
interface WeNuxtFetchOptions {
|
|
7
|
+
notify?: boolean;
|
|
8
|
+
logout_on_401?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface LaravelResponse<T> {
|
|
11
|
+
data: T;
|
|
12
|
+
meta?: {
|
|
13
|
+
current_page: number;
|
|
14
|
+
last_page: number;
|
|
15
|
+
total: number;
|
|
16
|
+
per_page: number;
|
|
17
|
+
from: number;
|
|
18
|
+
to: number;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export declare function useApi(): {
|
|
22
|
+
fetch: $Fetch;
|
|
23
|
+
request<T>(options: FetchOptions & {
|
|
24
|
+
url: string;
|
|
25
|
+
data?: BodyRequest;
|
|
26
|
+
}): Promise<LaravelResponse<T>>;
|
|
27
|
+
get<T>(url: string, options?: FetchOptions): Promise<LaravelResponse<T>>;
|
|
28
|
+
getLazy<T>(url: string, options?: FetchOptions): Promise<any>;
|
|
29
|
+
post<T>(url: string, body?: BodyRequest, options?: FetchOptions): Promise<LaravelResponse<T>>;
|
|
30
|
+
put<T>(url: string, body?: BodyRequest, options?: FetchOptions): Promise<LaravelResponse<T>>;
|
|
31
|
+
patch<T>(url: string, body?: BodyRequest, options?: FetchOptions): Promise<LaravelResponse<T>>;
|
|
32
|
+
delete<T>(url: string, body?: BodyRequest, options?: FetchOptions): Promise<LaravelResponse<T>>;
|
|
33
|
+
postForm<T>(url: string, data?: InputFormData, options?: FetchOptions & ObjectToFormDataOptions): Promise<LaravelResponse<T>>;
|
|
34
|
+
putForm<T>(url: string, data?: InputFormData, options?: FetchOptions & ObjectToFormDataOptions): Promise<LaravelResponse<T>>;
|
|
35
|
+
patchForm<T>(url: string, data: InputFormData, options?: FetchOptions & ObjectToFormDataOptions): Promise<LaravelResponse<T>>;
|
|
36
|
+
$request<T>(options: FetchOptions & {
|
|
37
|
+
url: string;
|
|
38
|
+
data?: BodyRequest;
|
|
39
|
+
}): Promise<T>;
|
|
40
|
+
$get<T>(url: string, options?: FetchOptions): Promise<T>;
|
|
41
|
+
$post<T>(url: string, body?: BodyRequest, options?: FetchOptions): Promise<T>;
|
|
42
|
+
$put<T>(url: string, body?: BodyRequest, options?: FetchOptions): Promise<T>;
|
|
43
|
+
$patch<T>(url: string, body?: BodyRequest, options?: FetchOptions): Promise<T>;
|
|
44
|
+
$delete<T>(url: string, body?: BodyRequest, options?: FetchOptions): Promise<T>;
|
|
45
|
+
$postForm<T>(url: string, data?: InputFormData, options?: FetchOptions & ObjectToFormDataOptions): Promise<T>;
|
|
46
|
+
$putForm<T>(url: string, data?: InputFormData, options?: FetchOptions & ObjectToFormDataOptions): Promise<T>;
|
|
47
|
+
$patchForm<T>(url: string, data: InputFormData, options?: FetchOptions & ObjectToFormDataOptions): Promise<T>;
|
|
48
|
+
buildFormData(data?: InputFormData, options?: ObjectToFormDataOptions): FormData;
|
|
49
|
+
$getLazy<T>(url: string, options?: FetchOptions): Promise<T>;
|
|
50
|
+
$getLazyInvalidate(key?: string): void;
|
|
51
|
+
getSignedURL(file: File, options?: FetchOptions, signedUrl?: string): Promise<import("ofetch").FetchResponse<any>>;
|
|
52
|
+
uploadFile(file: File, options?: FetchOptions, signedUrl?: string): Promise<any>;
|
|
53
|
+
download(url: string, filename?: string, options?: FetchOptions): Promise<import("ofetch").FetchResponse<Blob>>;
|
|
54
|
+
changeBaseUrl(baseURL: string, reload?: boolean): void;
|
|
55
|
+
};
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { useNuxtApp } from "#app";
|
|
2
|
+
import { useApiUrl, useAuth } from "#imports";
|
|
3
|
+
import { saveAs } from "file-saver-es";
|
|
4
|
+
import { LRUCache } from "lru-cache";
|
|
5
|
+
import { ofetch } from "ofetch";
|
|
6
|
+
import { getRequestKey } from "../internals/api.js";
|
|
7
|
+
import { objectToFormData } from "../utils/object-to-formdata.js";
|
|
8
|
+
import { useNotify } from "./useNotify.js";
|
|
9
|
+
function adaptParams(params) {
|
|
10
|
+
return Object.keys(params).reduce((acc, key) => {
|
|
11
|
+
if (Array.isArray(params[key]) && !key.endsWith("[]")) {
|
|
12
|
+
acc[`${key}[]`] = params[key];
|
|
13
|
+
} else {
|
|
14
|
+
acc[key] = params[key];
|
|
15
|
+
}
|
|
16
|
+
return acc;
|
|
17
|
+
}, {});
|
|
18
|
+
}
|
|
19
|
+
const cache = new LRUCache({
|
|
20
|
+
max: 100,
|
|
21
|
+
ttl: 1e3 * 60 * 60
|
|
22
|
+
// One hour
|
|
23
|
+
});
|
|
24
|
+
const callPool = {};
|
|
25
|
+
export function useApi() {
|
|
26
|
+
function cachedGet(url, options, fetchFn) {
|
|
27
|
+
const key = getRequestKey(url, options);
|
|
28
|
+
if (callPool[key]) {
|
|
29
|
+
return callPool[key];
|
|
30
|
+
}
|
|
31
|
+
if (cache.has(key)) {
|
|
32
|
+
return Promise.resolve(cache.get(key));
|
|
33
|
+
}
|
|
34
|
+
callPool[key] = fetchFn();
|
|
35
|
+
return callPool[key].finally(() => {
|
|
36
|
+
delete callPool[key];
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
class Api {
|
|
40
|
+
fetch;
|
|
41
|
+
constructor(fetch2) {
|
|
42
|
+
this.fetch = fetch2;
|
|
43
|
+
}
|
|
44
|
+
request(options) {
|
|
45
|
+
if (!options.body && options.data) {
|
|
46
|
+
options.body = options.data;
|
|
47
|
+
}
|
|
48
|
+
if (options.params) {
|
|
49
|
+
options.params = adaptParams(options.params);
|
|
50
|
+
}
|
|
51
|
+
if (options.query) {
|
|
52
|
+
options.query = adaptParams(options.query);
|
|
53
|
+
}
|
|
54
|
+
return this.fetch(options.url, options);
|
|
55
|
+
}
|
|
56
|
+
get(url, options) {
|
|
57
|
+
return this.request({ ...options, url });
|
|
58
|
+
}
|
|
59
|
+
getLazy(url, options) {
|
|
60
|
+
return cachedGet(url, options, async () => {
|
|
61
|
+
const res = await this.get(url, options);
|
|
62
|
+
cache.set(getRequestKey(url, options), res);
|
|
63
|
+
return res;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
post(url, body, options) {
|
|
67
|
+
return this.request({ method: "post", body, ...options, url });
|
|
68
|
+
}
|
|
69
|
+
put(url, body, options) {
|
|
70
|
+
return this.request({ method: "put", body, ...options, url });
|
|
71
|
+
}
|
|
72
|
+
patch(url, body, options) {
|
|
73
|
+
return this.request({ method: "patch", body, ...options, url });
|
|
74
|
+
}
|
|
75
|
+
delete(url, body, options) {
|
|
76
|
+
return this.request({ method: "delete", body, ...options, url });
|
|
77
|
+
}
|
|
78
|
+
postForm(url, data, options) {
|
|
79
|
+
return this.post(url, this.buildFormData(data, options), options);
|
|
80
|
+
}
|
|
81
|
+
putForm(url, data, options) {
|
|
82
|
+
return this.postForm(url, { _method: "put", ...data }, options);
|
|
83
|
+
}
|
|
84
|
+
patchForm(url, data, options) {
|
|
85
|
+
return this.postForm(url, { _method: "patch", ...data }, options);
|
|
86
|
+
}
|
|
87
|
+
$request(options) {
|
|
88
|
+
return this.request(options).then((res) => res?.data);
|
|
89
|
+
}
|
|
90
|
+
$get(url, options) {
|
|
91
|
+
return this.$request({ ...options, url });
|
|
92
|
+
}
|
|
93
|
+
$post(url, body, options) {
|
|
94
|
+
return this.$request({ method: "post", body, ...options, url });
|
|
95
|
+
}
|
|
96
|
+
$put(url, body, options) {
|
|
97
|
+
return this.$request({ method: "put", body, ...options, url });
|
|
98
|
+
}
|
|
99
|
+
$patch(url, body, options) {
|
|
100
|
+
return this.$request({ method: "patch", body, ...options, url });
|
|
101
|
+
}
|
|
102
|
+
$delete(url, body, options) {
|
|
103
|
+
return this.$request({ method: "delete", body, ...options, url });
|
|
104
|
+
}
|
|
105
|
+
$postForm(url, data, options) {
|
|
106
|
+
return this.$post(url, this.buildFormData(data, options), options);
|
|
107
|
+
}
|
|
108
|
+
$putForm(url, data, options) {
|
|
109
|
+
return this.$postForm(url, { _method: "put", ...data }, options);
|
|
110
|
+
}
|
|
111
|
+
$patchForm(url, data, options) {
|
|
112
|
+
return this.$postForm(url, { _method: "patch", ...data }, options);
|
|
113
|
+
}
|
|
114
|
+
buildFormData(data, options = {}) {
|
|
115
|
+
const defaultOptions = {
|
|
116
|
+
nullsAsUndefineds: false,
|
|
117
|
+
booleansAsIntegers: true,
|
|
118
|
+
indices: true
|
|
119
|
+
};
|
|
120
|
+
return objectToFormData(data ?? {}, { ...defaultOptions, ...options });
|
|
121
|
+
}
|
|
122
|
+
$getLazy(url, options) {
|
|
123
|
+
return this.getLazy(url, options).then((res) => res?.data);
|
|
124
|
+
}
|
|
125
|
+
$getLazyInvalidate(key) {
|
|
126
|
+
if (typeof key === "undefined") {
|
|
127
|
+
cache.clear();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
for (const k of cache.keys()) {
|
|
131
|
+
if (k.includes(key)) {
|
|
132
|
+
cache.delete(k);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async getSignedURL(file, options = {}, signedUrl) {
|
|
137
|
+
const url = signedUrl ?? "storage/signed_url";
|
|
138
|
+
return fetch.raw(url, {
|
|
139
|
+
method: "post",
|
|
140
|
+
...options,
|
|
141
|
+
headers: {
|
|
142
|
+
ContentType: file.type
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
async uploadFile(file, options = {}, signedUrl) {
|
|
147
|
+
const raw = await this.getSignedURL(file, options, signedUrl);
|
|
148
|
+
const { _data: data, ...response } = raw;
|
|
149
|
+
const url = data.url;
|
|
150
|
+
await ofetch(url, {
|
|
151
|
+
method: "put",
|
|
152
|
+
body: file,
|
|
153
|
+
...options
|
|
154
|
+
});
|
|
155
|
+
const { name, size, type: mime_type } = file;
|
|
156
|
+
const type = mime_type.split("/")[0];
|
|
157
|
+
return { ...response, ...data, name, size, mime_type, type };
|
|
158
|
+
}
|
|
159
|
+
async download(url, filename, options) {
|
|
160
|
+
if (options?.params) {
|
|
161
|
+
options.params = adaptParams(options.params);
|
|
162
|
+
}
|
|
163
|
+
if (options?.query) {
|
|
164
|
+
options.query = adaptParams(options.query);
|
|
165
|
+
}
|
|
166
|
+
return this.fetch.raw(url, {
|
|
167
|
+
method: "get",
|
|
168
|
+
...options,
|
|
169
|
+
responseType: "blob"
|
|
170
|
+
}).then((response) => {
|
|
171
|
+
const contentDisposition = response.headers.get("content-disposition");
|
|
172
|
+
const name = filename || contentDisposition?.split("filename=")[1];
|
|
173
|
+
if (response._data instanceof Blob) {
|
|
174
|
+
saveAs(response._data, name);
|
|
175
|
+
}
|
|
176
|
+
return response;
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
changeBaseUrl(baseURL, reload = true) {
|
|
180
|
+
useApiUrl().changeBaseUrl(baseURL, reload);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const fetch = ofetch.create({
|
|
184
|
+
baseURL: useApiUrl().apiUrl.value,
|
|
185
|
+
onRequest: async (context) => {
|
|
186
|
+
context.options.headers.append("Accept", "application/json");
|
|
187
|
+
const token = await useAuth().getUserToken();
|
|
188
|
+
if (token) {
|
|
189
|
+
context.options.headers.append("Authorization", `Bearer ${token}`);
|
|
190
|
+
}
|
|
191
|
+
context.options.query = {
|
|
192
|
+
...context.options.query,
|
|
193
|
+
XDEBUG_SESSION_START: useApiUrl().xDebug.value ?? void 0
|
|
194
|
+
};
|
|
195
|
+
useNuxtApp().hooks.callHook("we-api:onRequest", context);
|
|
196
|
+
},
|
|
197
|
+
onRequestError: async (context) => {
|
|
198
|
+
useNuxtApp().hooks.callHook("we-api:onRequestError", context);
|
|
199
|
+
},
|
|
200
|
+
onResponse: async (context) => {
|
|
201
|
+
useNuxtApp().hooks.callHook("we-api:onResponse", context);
|
|
202
|
+
},
|
|
203
|
+
onResponseError: async (context) => {
|
|
204
|
+
const options = context.options;
|
|
205
|
+
const notify = options?.notify !== false;
|
|
206
|
+
const logout_on_401 = options?.logout_on_401 !== false;
|
|
207
|
+
if (context?.response?._data instanceof Blob) {
|
|
208
|
+
context.response._data = JSON.parse(await context.response._data.text());
|
|
209
|
+
}
|
|
210
|
+
const status = context?.response?.status;
|
|
211
|
+
switch (status) {
|
|
212
|
+
case 401:
|
|
213
|
+
if (logout_on_401) {
|
|
214
|
+
useAuth().logout();
|
|
215
|
+
}
|
|
216
|
+
break;
|
|
217
|
+
case 404:
|
|
218
|
+
case 422:
|
|
219
|
+
break;
|
|
220
|
+
default:
|
|
221
|
+
if (notify) {
|
|
222
|
+
useNotify().error(context);
|
|
223
|
+
}
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
useNuxtApp().hooks.callHook("we-api:onResponseError", context);
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
return new Api(fetch);
|
|
230
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare function useApiUrl(): {
|
|
2
|
+
apiUrl: import("vue").ComputedRef<string>;
|
|
3
|
+
baseUrl: import("vue").Ref<string, string>;
|
|
4
|
+
apiBaseUrl: import("vue").ComputedRef<any>;
|
|
5
|
+
endpoints: import("vue").ComputedRef<any>;
|
|
6
|
+
conf: any;
|
|
7
|
+
changeBaseUrl: (url: string, reload?: boolean) => void;
|
|
8
|
+
xDebug: import("vue").Ref<string | null, string | null>;
|
|
9
|
+
changeXDebug: (value: boolean, key?: string, reload?: boolean) => void;
|
|
10
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useRuntimeConfig, useState } from "#app";
|
|
2
|
+
import { computed } from "vue";
|
|
3
|
+
export function useApiUrl() {
|
|
4
|
+
const conf = useRuntimeConfig().public.we.api;
|
|
5
|
+
const storageKeyBaseUrl = "we_nuxt_$api_baseUrl";
|
|
6
|
+
const storageKeyXDebug = "we_nuxt_$api_xDebug";
|
|
7
|
+
const baseUrl = useState(storageKeyBaseUrl, () => localStorage.getItem(storageKeyBaseUrl) || "");
|
|
8
|
+
const xDebug = useState(storageKeyXDebug, () => localStorage.getItem(storageKeyXDebug));
|
|
9
|
+
const apiBaseUrl = computed(() => {
|
|
10
|
+
return baseUrl.value || conf.baseUrl || conf.endpoints[0];
|
|
11
|
+
});
|
|
12
|
+
const apiUrl = computed(() => {
|
|
13
|
+
return `${apiBaseUrl.value}/${conf.apiSuffix}`;
|
|
14
|
+
});
|
|
15
|
+
const endpoints = computed(() => {
|
|
16
|
+
return conf.endpoints;
|
|
17
|
+
});
|
|
18
|
+
function changeBaseUrl(url, reload = true) {
|
|
19
|
+
baseUrl.value = url;
|
|
20
|
+
localStorage.setItem(storageKeyBaseUrl, url);
|
|
21
|
+
if (reload) {
|
|
22
|
+
location.reload();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function changeXDebug(value, key = "weconstudio", reload = false) {
|
|
26
|
+
xDebug.value = value ? key : null;
|
|
27
|
+
if (value) {
|
|
28
|
+
localStorage.setItem(storageKeyXDebug, key);
|
|
29
|
+
} else {
|
|
30
|
+
localStorage.removeItem(storageKeyXDebug);
|
|
31
|
+
}
|
|
32
|
+
if (reload) {
|
|
33
|
+
location.reload();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return { apiUrl, baseUrl, apiBaseUrl, endpoints, conf, changeBaseUrl, xDebug, changeXDebug };
|
|
37
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare function useAuth(): {
|
|
2
|
+
login: (credentials: Record<string, unknown>, fetchIdentity?: boolean) => Promise<unknown>;
|
|
3
|
+
logout: () => Promise<void>;
|
|
4
|
+
loggedIn: import("vue").ComputedRef<boolean>;
|
|
5
|
+
fetchUser: () => Promise<void>;
|
|
6
|
+
user: import("vue").ComputedRef<any>;
|
|
7
|
+
setUserToken: (token: string) => Promise<void>;
|
|
8
|
+
getUserToken: () => Promise<string | undefined> | undefined;
|
|
9
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useNuxtApp } from "#app";
|
|
2
|
+
import { useSanctumAppConfig, useSanctumAuth } from "#imports";
|
|
3
|
+
import { computed } from "vue";
|
|
4
|
+
export function useAuth() {
|
|
5
|
+
const {
|
|
6
|
+
login,
|
|
7
|
+
logout,
|
|
8
|
+
isAuthenticated: loggedIn,
|
|
9
|
+
refreshIdentity: fetchUser,
|
|
10
|
+
user: sanctumUser
|
|
11
|
+
} = useSanctumAuth();
|
|
12
|
+
const user = computed(() => {
|
|
13
|
+
return sanctumUser.value?.data ?? void 0;
|
|
14
|
+
});
|
|
15
|
+
async function setUserToken(token) {
|
|
16
|
+
await useSanctumAppConfig().tokenStorage?.set(useNuxtApp(), token);
|
|
17
|
+
}
|
|
18
|
+
function getUserToken() {
|
|
19
|
+
return useSanctumAppConfig().tokenStorage?.get(useNuxtApp());
|
|
20
|
+
}
|
|
21
|
+
return { login, logout, loggedIn, fetchUser, user, setUserToken, getUserToken };
|
|
22
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { EventType, Handler } from 'mitt';
|
|
2
|
+
export declare function useBus<Payload = unknown | undefined>(type: EventType): {
|
|
3
|
+
on: (handler: Handler<Payload>) => void;
|
|
4
|
+
off: (handler: Handler<Payload>) => void;
|
|
5
|
+
emit: (payload: Payload) => void;
|
|
6
|
+
all: import("mitt").EventHandlerMap<Record<EventType, Payload>>;
|
|
7
|
+
listeners: import("vue").ComputedRef<import("mitt").EventHandlerList<Payload> | import("mitt").WildCardEventHandlerList<Record<EventType, Payload>>>;
|
|
8
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { computed, onBeforeUnmount } from "vue";
|
|
2
|
+
import { busInstance } from "../internals/bus.js";
|
|
3
|
+
export function useBus(type) {
|
|
4
|
+
const $bus = busInstance;
|
|
5
|
+
function on(handler) {
|
|
6
|
+
$bus.on(type, handler);
|
|
7
|
+
const _off = () => $bus.off(type, handler);
|
|
8
|
+
onBeforeUnmount(() => _off());
|
|
9
|
+
}
|
|
10
|
+
function off(handler) {
|
|
11
|
+
$bus.off(type, handler);
|
|
12
|
+
}
|
|
13
|
+
function emit(payload) {
|
|
14
|
+
$bus.emit(type, payload);
|
|
15
|
+
}
|
|
16
|
+
const listeners = computed(() => $bus.all.get(type) ?? []);
|
|
17
|
+
return { on, off, emit, all: $bus.all, listeners };
|
|
18
|
+
}
|