nuxt-outfit 1.7.0 → 1.9.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/dist/module.json +1 -1
- package/dist/runtime/composables/useDataFetch.d.ts +11 -0
- package/dist/runtime/composables/useDataFetch.mjs +68 -0
- package/dist/runtime/composables/useFile.d.ts +1 -1
- package/dist/runtime/composables/useFile.mjs +1 -1
- package/dist/runtime/composables/useForm.mjs +3 -3
- package/dist/runtime/composables/useQuery.d.ts +6 -0
- package/dist/runtime/composables/useQuery.mjs +29 -0
- package/package.json +1 -1
package/dist/module.json
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const useDataFetch: (name: any, path: any, options?: {}) => Promise<{
|
|
2
|
+
pending: any;
|
|
3
|
+
refresh: any;
|
|
4
|
+
data: any;
|
|
5
|
+
filters: any;
|
|
6
|
+
currentPage: import("vue").WritableComputedRef<any>;
|
|
7
|
+
changePage: (page: any) => Promise<void>;
|
|
8
|
+
changePerPage: (count: any) => Promise<void>;
|
|
9
|
+
filter: () => Promise<void>;
|
|
10
|
+
clearFilter: () => Promise<void>;
|
|
11
|
+
}>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { useRouter, useNuxtApp, useQuery, useAsyncData } from "#imports";
|
|
2
|
+
import { reactive, computed, watch } from "vue";
|
|
3
|
+
import { klona as deepClone } from "klona/full";
|
|
4
|
+
import { flatten } from "flat";
|
|
5
|
+
export const useDataFetch = async (name, path, options = {}) => {
|
|
6
|
+
const { $spaFetch } = useNuxtApp();
|
|
7
|
+
const { push } = useRouter();
|
|
8
|
+
const { getBindingQueryValues } = useQuery();
|
|
9
|
+
const { filters: filtersQuery, other: otherQuery } = getBindingQueryValues();
|
|
10
|
+
const filters = reactive({ ...deepClone(options?.filters), ...options?.isSilent ? {} : filtersQuery });
|
|
11
|
+
const other = reactive({ page: 1, ...options?.isSilent ? {} : otherQuery });
|
|
12
|
+
const currentPage = computed({
|
|
13
|
+
get() {
|
|
14
|
+
return other.page;
|
|
15
|
+
},
|
|
16
|
+
set(value) {
|
|
17
|
+
other.page = value;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
const preparedFilters = computed(() => {
|
|
21
|
+
const inLine = flatten(filters);
|
|
22
|
+
return Object.keys(inLine).filter((key) => inLine[key] !== null).reduce((result, key) => {
|
|
23
|
+
result[`filter[${key}]`] = inLine[key];
|
|
24
|
+
return result;
|
|
25
|
+
}, {});
|
|
26
|
+
});
|
|
27
|
+
const validQuery = computed(() => ({ ...other, ...preparedFilters.value }));
|
|
28
|
+
const { data, refresh, pending } = await useAsyncData(name, () => $spaFetch(path, {
|
|
29
|
+
query: validQuery.value,
|
|
30
|
+
...options?.fetch ?? {}
|
|
31
|
+
}));
|
|
32
|
+
const make = async () => {
|
|
33
|
+
await refresh();
|
|
34
|
+
if (options?.isSilent !== true) {
|
|
35
|
+
push({ query: validQuery.value });
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const changePage = async (page) => {
|
|
39
|
+
currentPage.value = page;
|
|
40
|
+
await make();
|
|
41
|
+
};
|
|
42
|
+
const changePerPage = async (count) => {
|
|
43
|
+
other.per_page = count;
|
|
44
|
+
await make();
|
|
45
|
+
};
|
|
46
|
+
const filter = async () => {
|
|
47
|
+
currentPage.value = 1;
|
|
48
|
+
await make();
|
|
49
|
+
};
|
|
50
|
+
const clearFilter = async () => {
|
|
51
|
+
Object.assign(filters, deepClone(options?.filters));
|
|
52
|
+
await make();
|
|
53
|
+
};
|
|
54
|
+
watch(filters, async () => {
|
|
55
|
+
await filter();
|
|
56
|
+
}, { deep: true });
|
|
57
|
+
return {
|
|
58
|
+
pending,
|
|
59
|
+
refresh,
|
|
60
|
+
data,
|
|
61
|
+
filters,
|
|
62
|
+
currentPage,
|
|
63
|
+
changePage,
|
|
64
|
+
changePerPage,
|
|
65
|
+
filter,
|
|
66
|
+
clearFilter
|
|
67
|
+
};
|
|
68
|
+
};
|
|
@@ -8,7 +8,7 @@ export const useFile = () => {
|
|
|
8
8
|
}
|
|
9
9
|
return bytes.buffer;
|
|
10
10
|
};
|
|
11
|
-
const createFileFromBase64 = (base64, name
|
|
11
|
+
const createFileFromBase64 = (base64, name) => {
|
|
12
12
|
return new File([new Blob([createArrayBufferFromBase64(base64)])], name);
|
|
13
13
|
};
|
|
14
14
|
return {
|
|
@@ -84,11 +84,11 @@ export const useForm = (opts = {}) => {
|
|
|
84
84
|
};
|
|
85
85
|
const toFormData = (fields2, form = null, namespace = null) => {
|
|
86
86
|
const formData = form || new FormData();
|
|
87
|
-
for (
|
|
88
|
-
if (!
|
|
87
|
+
for (const property in fields2) {
|
|
88
|
+
if (!Object.prototype.hasOwnProperty.call(fields2, property)) {
|
|
89
89
|
continue;
|
|
90
90
|
}
|
|
91
|
-
|
|
91
|
+
const formKey = namespace ? `${namespace}[${property}]` : property;
|
|
92
92
|
if (fields2[property] instanceof FileList || Array.isArray(fields2[property])) {
|
|
93
93
|
fields2[property].forEach((element, index) => {
|
|
94
94
|
if (element instanceof File) {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { useRoute } from "#imports";
|
|
2
|
+
export const useQuery = () => {
|
|
3
|
+
const { query } = useRoute();
|
|
4
|
+
const convertValue = (value) => {
|
|
5
|
+
if (!isNaN(value)) {
|
|
6
|
+
return parseFloat(value);
|
|
7
|
+
} else if (value === "true" || value === "false") {
|
|
8
|
+
return value === "true";
|
|
9
|
+
} else if (value.includes(",")) {
|
|
10
|
+
return value.split(",").map((item) => convertValue(item.trim()));
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
};
|
|
14
|
+
const getBindingQueryValues = () => {
|
|
15
|
+
return Object.keys(query).reduce((params, key) => {
|
|
16
|
+
const match = key.match(/^filter\[(.+)\]$/);
|
|
17
|
+
const value = convertValue(query[key]);
|
|
18
|
+
if (match) {
|
|
19
|
+
params.filters[match[1]] = value;
|
|
20
|
+
} else {
|
|
21
|
+
params.other[key] = value;
|
|
22
|
+
}
|
|
23
|
+
return params;
|
|
24
|
+
}, { filters: {}, other: {} });
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
getBindingQueryValues
|
|
28
|
+
};
|
|
29
|
+
};
|