@adminforth/import-export 1.6.1 → 1.7.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/build.log CHANGED
@@ -11,5 +11,5 @@ custom/package.json
11
11
  custom/pnpm-lock.yaml
12
12
  custom/tsconfig.json
13
13
 
14
- sent 17,878 bytes received 134 bytes 36,024.00 bytes/sec
15
- total size is 17,389 speedup is 0.97
14
+ sent 21,367 bytes received 134 bytes 43,002.00 bytes/sec
15
+ total size is 20,878 speedup is 0.97
@@ -1,33 +1,127 @@
1
1
  <template>
2
- <div class="cursor-pointer flex gap-2 items-center">
3
- {{$t("Export")}} {{ meta.select === 'all' ? $t('All'): $t('Filtered') }} {{$t('to CSV')}}
2
+ <div ref="rootRef" class="relative" @click.stop>
3
+ <div class="cursor-pointer flex gap-2 items-center justify-between -mx-4 -my-2 px-4 py-2" @click="toggle">
4
+ <span class="flex gap-2 items-center">
5
+ {{ $t('Export to CSV') }}
4
6
 
5
- <svg v-if="inProgress"
6
- aria-hidden="true" class="w-4 h-4 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/><path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/></svg>
7
+ <svg v-if="inProgress"
8
+ aria-hidden="true" class="w-4 h-4 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/><path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/></svg>
9
+ </span>
10
+ <svg class="w-2 h-2 shrink-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 6 10">
11
+ <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 1 1 5l4 4"/>
12
+ </svg>
13
+ </div>
14
+
15
+ <div v-if="open"
16
+ class="absolute right-full top-0 mr-5 z-40 w-max bg-lightThreeDotsMenuBodyBackground dark:bg-darkThreeDotsMenuBodyBackground rounded-lg shadow border border-gray-100 dark:border-gray-600">
17
+ <ul class="py-2 text-sm text-lightThreeDotsMenuBodyText dark:text-darkThreeDotsMenuBodyText">
18
+ <li v-for="option in options" :key="option.select">
19
+ <div
20
+ class="px-4 py-1.5 text-sm whitespace-nowrap"
21
+ :class="isDisabled(option)
22
+ ? 'opacity-50 cursor-not-allowed'
23
+ : 'cursor-pointer hover:text-lightThreeDotsMenuBodyTextHover hover:bg-lightThreeDotsMenuBodyBackgroundHover dark:hover:bg-darkThreeDotsMenuBodyBackgroundHover dark:hover:text-darkThreeDotsMenuBodyTextHover'"
24
+ @click="!isDisabled(option) && run(option.select)"
25
+ >
26
+ {{ option.label() }}
27
+ </div>
28
+ </li>
29
+ </ul>
30
+ </div>
7
31
  </div>
8
32
  </template>
9
33
 
10
34
  <script setup lang="ts">
11
- import { ref } from 'vue';
35
+ import { ref, onMounted, onUnmounted } from 'vue';
12
36
  import { useCoreStore } from '@/stores/core';
13
37
  import { callAdminForthApi } from '@/utils';
14
38
  import { useFiltersStore } from '@/stores/filters';
15
39
  import adminforth from '@/adminforth';
16
40
  import Papa from 'papaparse';
41
+ import { useI18n } from 'vue-i18n';
17
42
 
43
+ const { t } = useI18n();
18
44
  const filtersStore = useFiltersStore();
19
45
  const coreStore = useCoreStore();
20
46
  const inProgress = ref(false);
47
+ const open = ref(false);
48
+ const rootRef = ref<HTMLElement | null>(null);
49
+ const allCount = ref<number | null>(null);
50
+ const filteredCount = ref<number | null>(null);
21
51
 
22
52
  defineExpose({
23
- click,
53
+ click: () => { toggle(); },
24
54
  });
25
55
 
26
56
  const props = defineProps({
27
57
  meta: Object,
28
58
  record: Object,
59
+ checkboxes: Array,
29
60
  });
30
61
 
62
+ function formatCount(count: number) {
63
+ if (count < 1000) return `${count}`;
64
+ if (count < 1_000_000) return `${Math.floor(count / 1000)}k+`;
65
+ return `${Math.floor(count / 1_000_000)}M+`;
66
+ }
67
+
68
+ function withCount(label: string, count: number | null) {
69
+ return count === null ? `${label} (…)` : `${label} (${formatCount(count)})`;
70
+ }
71
+
72
+ const options = [
73
+ { select: 'all', label: () => withCount(t('All'), allCount.value) },
74
+ { select: 'filtered', label: () => withCount(t('Filtered'), filteredCount.value) },
75
+ { select: 'selected', label: () => `${t('Selected')} (${props.checkboxes?.length || 0})` },
76
+ ];
77
+
78
+ function isDisabled(option: { select: string }) {
79
+ return option.select === 'selected' && !(props.checkboxes?.length);
80
+ }
81
+
82
+ function handleClickOutside(e: MouseEvent) {
83
+ if (open.value && rootRef.value && !rootRef.value.contains(e.target as Node)) {
84
+ open.value = false;
85
+ }
86
+ }
87
+
88
+ onMounted(() => document.addEventListener('mousedown', handleClickOutside));
89
+ onUnmounted(() => document.removeEventListener('mousedown', handleClickOutside));
90
+
91
+ function toggle() {
92
+ open.value = !open.value;
93
+ if (open.value) {
94
+ fetchCounts();
95
+ }
96
+ }
97
+
98
+ async function fetchCounts() {
99
+ const resourceId = coreStore.resource?.resourceId;
100
+ if (!resourceId) {
101
+ return;
102
+ }
103
+ const countOnly = (filters: any) => callAdminForthApi({
104
+ path: '/get_resource_data',
105
+ method: 'POST',
106
+ body: { source: 'list', resourceId, limit: 1, offset: 0, filters, sort: [] },
107
+ });
108
+ try {
109
+ const [allResp, filteredResp] = await Promise.all([
110
+ countOnly([]),
111
+ countOnly(filtersStore.getFilters()),
112
+ ]);
113
+ allCount.value = allResp?.total ?? null;
114
+ filteredCount.value = filteredResp?.total ?? null;
115
+ } catch (e) {
116
+ // leave counts as null (renders "…") on failure
117
+ }
118
+ }
119
+
120
+ function run(select: string) {
121
+ open.value = false;
122
+ exportCsv(select);
123
+ }
124
+
31
125
  function downloadFile(data: string, filename: string) {
32
126
  const blob = new Blob([data], { type: 'text/csv' });
33
127
  const url = window.URL.createObjectURL(blob);
@@ -38,15 +132,16 @@ function downloadFile(data: string, filename: string) {
38
132
  window.URL.revokeObjectURL(url);
39
133
  }
40
134
 
41
- async function exportCsv() {
135
+ async function exportCsv(select: string) {
42
136
  inProgress.value = true;
43
137
  try {
44
138
  const resp = await callAdminForthApi({
45
- path: `/plugin/${props.meta.pluginInstanceId}/export-csv`,
139
+ path: `/plugin/${props.meta?.pluginInstanceId}/export-csv`,
46
140
  method: 'POST',
47
141
  body: {
48
- filters: props.meta.select === 'all' ? [] : filtersStore.getFilters(),
142
+ filters: select === 'filtered' ? filtersStore.getFilters() : [],
49
143
  sort: filtersStore.getSort(),
144
+ selectedIds: select === 'selected' ? props.checkboxes : undefined,
50
145
  }
51
146
  });
52
147
 
@@ -55,14 +150,14 @@ async function exportCsv() {
55
150
  }
56
151
 
57
152
  // Generate properly formatted CSV
58
- const csvContent = '\ufeff' + Papa.unparse(resp.data, {
153
+ const csvContent = '' + Papa.unparse(resp.data, {
59
154
  quotes: resp.columnsToForceQuote, // Force quotes only certain columns (!!!not all this breaks BI/Excel tasks)
60
155
  quoteChar: '"',
61
156
  escapeChar: '"',
62
157
  });
63
158
 
64
159
  // Download the file
65
- const filename = `export-${coreStore.resource.resourceId}-${new Date().toISOString()}.csv`;
160
+ const filename = `export-${coreStore.resource?.resourceId}-${new Date().toISOString()}.csv`;
66
161
  downloadFile(csvContent, filename);
67
162
 
68
163
  adminforth.alert({
@@ -78,8 +173,4 @@ async function exportCsv() {
78
173
  adminforth.list.closeThreeDotsDropdown();
79
174
  }
80
175
  }
81
-
82
- function click() {
83
- exportCsv();
84
- }
85
- </script>
176
+ </script>
@@ -1,33 +1,127 @@
1
1
  <template>
2
- <div class="cursor-pointer flex gap-2 items-center">
3
- {{$t("Export")}} {{ meta.select === 'all' ? $t('All'): $t('Filtered') }} {{$t('to CSV')}}
2
+ <div ref="rootRef" class="relative" @click.stop>
3
+ <div class="cursor-pointer flex gap-2 items-center justify-between -mx-4 -my-2 px-4 py-2" @click="toggle">
4
+ <span class="flex gap-2 items-center">
5
+ {{ $t('Export to CSV') }}
4
6
 
5
- <svg v-if="inProgress"
6
- aria-hidden="true" class="w-4 h-4 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/><path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/></svg>
7
+ <svg v-if="inProgress"
8
+ aria-hidden="true" class="w-4 h-4 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/><path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/></svg>
9
+ </span>
10
+ <svg class="w-2 h-2 shrink-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 6 10">
11
+ <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 1 1 5l4 4"/>
12
+ </svg>
13
+ </div>
14
+
15
+ <div v-if="open"
16
+ class="absolute right-full top-0 mr-5 z-40 w-max bg-lightThreeDotsMenuBodyBackground dark:bg-darkThreeDotsMenuBodyBackground rounded-lg shadow border border-gray-100 dark:border-gray-600">
17
+ <ul class="py-2 text-sm text-lightThreeDotsMenuBodyText dark:text-darkThreeDotsMenuBodyText">
18
+ <li v-for="option in options" :key="option.select">
19
+ <div
20
+ class="px-4 py-1.5 text-sm whitespace-nowrap"
21
+ :class="isDisabled(option)
22
+ ? 'opacity-50 cursor-not-allowed'
23
+ : 'cursor-pointer hover:text-lightThreeDotsMenuBodyTextHover hover:bg-lightThreeDotsMenuBodyBackgroundHover dark:hover:bg-darkThreeDotsMenuBodyBackgroundHover dark:hover:text-darkThreeDotsMenuBodyTextHover'"
24
+ @click="!isDisabled(option) && run(option.select)"
25
+ >
26
+ {{ option.label() }}
27
+ </div>
28
+ </li>
29
+ </ul>
30
+ </div>
7
31
  </div>
8
32
  </template>
9
33
 
10
34
  <script setup lang="ts">
11
- import { ref } from 'vue';
35
+ import { ref, onMounted, onUnmounted } from 'vue';
12
36
  import { useCoreStore } from '@/stores/core';
13
37
  import { callAdminForthApi } from '@/utils';
14
38
  import { useFiltersStore } from '@/stores/filters';
15
39
  import adminforth from '@/adminforth';
16
40
  import Papa from 'papaparse';
41
+ import { useI18n } from 'vue-i18n';
17
42
 
43
+ const { t } = useI18n();
18
44
  const filtersStore = useFiltersStore();
19
45
  const coreStore = useCoreStore();
20
46
  const inProgress = ref(false);
47
+ const open = ref(false);
48
+ const rootRef = ref<HTMLElement | null>(null);
49
+ const allCount = ref<number | null>(null);
50
+ const filteredCount = ref<number | null>(null);
21
51
 
22
52
  defineExpose({
23
- click,
53
+ click: () => { toggle(); },
24
54
  });
25
55
 
26
56
  const props = defineProps({
27
57
  meta: Object,
28
58
  record: Object,
59
+ checkboxes: Array,
29
60
  });
30
61
 
62
+ function formatCount(count: number) {
63
+ if (count < 1000) return `${count}`;
64
+ if (count < 1_000_000) return `${Math.floor(count / 1000)}k+`;
65
+ return `${Math.floor(count / 1_000_000)}M+`;
66
+ }
67
+
68
+ function withCount(label: string, count: number | null) {
69
+ return count === null ? `${label} (…)` : `${label} (${formatCount(count)})`;
70
+ }
71
+
72
+ const options = [
73
+ { select: 'all', label: () => withCount(t('All'), allCount.value) },
74
+ { select: 'filtered', label: () => withCount(t('Filtered'), filteredCount.value) },
75
+ { select: 'selected', label: () => `${t('Selected')} (${props.checkboxes?.length || 0})` },
76
+ ];
77
+
78
+ function isDisabled(option: { select: string }) {
79
+ return option.select === 'selected' && !(props.checkboxes?.length);
80
+ }
81
+
82
+ function handleClickOutside(e: MouseEvent) {
83
+ if (open.value && rootRef.value && !rootRef.value.contains(e.target as Node)) {
84
+ open.value = false;
85
+ }
86
+ }
87
+
88
+ onMounted(() => document.addEventListener('mousedown', handleClickOutside));
89
+ onUnmounted(() => document.removeEventListener('mousedown', handleClickOutside));
90
+
91
+ function toggle() {
92
+ open.value = !open.value;
93
+ if (open.value) {
94
+ fetchCounts();
95
+ }
96
+ }
97
+
98
+ async function fetchCounts() {
99
+ const resourceId = coreStore.resource?.resourceId;
100
+ if (!resourceId) {
101
+ return;
102
+ }
103
+ const countOnly = (filters: any) => callAdminForthApi({
104
+ path: '/get_resource_data',
105
+ method: 'POST',
106
+ body: { source: 'list', resourceId, limit: 1, offset: 0, filters, sort: [] },
107
+ });
108
+ try {
109
+ const [allResp, filteredResp] = await Promise.all([
110
+ countOnly([]),
111
+ countOnly(filtersStore.getFilters()),
112
+ ]);
113
+ allCount.value = allResp?.total ?? null;
114
+ filteredCount.value = filteredResp?.total ?? null;
115
+ } catch (e) {
116
+ // leave counts as null (renders "…") on failure
117
+ }
118
+ }
119
+
120
+ function run(select: string) {
121
+ open.value = false;
122
+ exportCsv(select);
123
+ }
124
+
31
125
  function downloadFile(data: string, filename: string) {
32
126
  const blob = new Blob([data], { type: 'text/csv' });
33
127
  const url = window.URL.createObjectURL(blob);
@@ -38,15 +132,16 @@ function downloadFile(data: string, filename: string) {
38
132
  window.URL.revokeObjectURL(url);
39
133
  }
40
134
 
41
- async function exportCsv() {
135
+ async function exportCsv(select: string) {
42
136
  inProgress.value = true;
43
137
  try {
44
138
  const resp = await callAdminForthApi({
45
- path: `/plugin/${props.meta.pluginInstanceId}/export-csv`,
139
+ path: `/plugin/${props.meta?.pluginInstanceId}/export-csv`,
46
140
  method: 'POST',
47
141
  body: {
48
- filters: props.meta.select === 'all' ? [] : filtersStore.getFilters(),
142
+ filters: select === 'filtered' ? filtersStore.getFilters() : [],
49
143
  sort: filtersStore.getSort(),
144
+ selectedIds: select === 'selected' ? props.checkboxes : undefined,
50
145
  }
51
146
  });
52
147
 
@@ -55,14 +150,14 @@ async function exportCsv() {
55
150
  }
56
151
 
57
152
  // Generate properly formatted CSV
58
- const csvContent = '\ufeff' + Papa.unparse(resp.data, {
153
+ const csvContent = '' + Papa.unparse(resp.data, {
59
154
  quotes: resp.columnsToForceQuote, // Force quotes only certain columns (!!!not all this breaks BI/Excel tasks)
60
155
  quoteChar: '"',
61
156
  escapeChar: '"',
62
157
  });
63
158
 
64
159
  // Download the file
65
- const filename = `export-${coreStore.resource.resourceId}-${new Date().toISOString()}.csv`;
160
+ const filename = `export-${coreStore.resource?.resourceId}-${new Date().toISOString()}.csv`;
66
161
  downloadFile(csvContent, filename);
67
162
 
68
163
  adminforth.alert({
@@ -78,8 +173,4 @@ async function exportCsv() {
78
173
  adminforth.list.closeThreeDotsDropdown();
79
174
  }
80
175
  }
81
-
82
- function click() {
83
- exportCsv();
84
- }
85
- </script>
176
+ </script>
package/dist/index.js CHANGED
@@ -7,12 +7,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { AdminForthPlugin, parseBody, suggestIfTypo, AdminForthFilterOperators, Filters, AdminForthDataTypes, rejectApiRawFilters, interpretResource, ActionCheckSource, AllowedActionsEnum } from "adminforth";
10
+ import { AdminForthPlugin, suggestIfTypo, AdminForthFilterOperators, Filters, AdminForthDataTypes, rejectApiRawFilters, interpretResource, ActionCheckSource, AllowedActionsEnum } from "adminforth";
11
11
  import pLimit from 'p-limit';
12
12
  import { z } from "zod";
13
13
  const exportCsvBodySchema = z.object({
14
14
  filters: z.any(),
15
15
  sort: z.any(),
16
+ selectedIds: z.array(z.any()).optional(),
16
17
  }).strict();
17
18
  const importCsvBodySchema = z.object({
18
19
  data: z.record(z.string(), z.array(z.unknown())),
@@ -92,10 +93,7 @@ export default class ImportExport extends AdminForthPlugin {
92
93
  }
93
94
  resourceConfig.options.pageInjections.list.threeDotsDropdownItems.push({
94
95
  file: this.componentPath('ExportCsv.vue'),
95
- meta: { pluginInstanceId: this.pluginInstanceId, select: 'all' }
96
- }, {
97
- file: this.componentPath('ExportCsv.vue'),
98
- meta: { pluginInstanceId: this.pluginInstanceId, select: 'filtered' }
96
+ meta: { pluginInstanceId: this.pluginInstanceId }
99
97
  }, {
100
98
  file: this.componentPath('ImportCsv.vue'),
101
99
  meta: { pluginInstanceId: this.pluginInstanceId }
@@ -121,12 +119,10 @@ export default class ImportExport extends AdminForthPlugin {
121
119
  server.endpoint({
122
120
  method: 'POST',
123
121
  path: `/plugin/${this.pluginInstanceId}/export-csv`,
122
+ request_schema: exportCsvBodySchema,
124
123
  handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, headers, response }) {
125
- const parsed = parseBody(exportCsvBodySchema, body, response);
126
- if ('error' in parsed)
127
- return parsed.error;
128
- const payload = parsed.data;
129
- const { filters, sort } = payload;
124
+ const payload = body;
125
+ const { filters, sort, selectedIds } = payload;
130
126
  if (!filters || !sort) {
131
127
  return { ok: false, error: 'Missing filters or sort in request body' };
132
128
  }
@@ -141,11 +137,23 @@ export default class ImportExport extends AdminForthPlugin {
141
137
  if (rawFilterError) {
142
138
  return rawFilterError;
143
139
  }
140
+ let effectiveFilters = filters;
141
+ if (Array.isArray(selectedIds) && selectedIds.length > 0) {
142
+ const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
143
+ if (!primaryKeyColumn) {
144
+ return { ok: false, error: 'Cannot export selected records: resource has no primary key' };
145
+ }
146
+ effectiveFilters = [{
147
+ field: primaryKeyColumn.name,
148
+ operator: AdminForthFilterOperators.IN,
149
+ value: selectedIds,
150
+ }];
151
+ }
144
152
  const data = yield this.adminforth.connectors[this.resourceConfig.dataSource].getData({
145
153
  resource: this.resourceConfig,
146
154
  limit: 1e6,
147
155
  offset: 0,
148
- filters: this.adminforth.connectors[this.resourceConfig.dataSource].validateAndNormalizeInputFilters(filters),
156
+ filters: this.adminforth.connectors[this.resourceConfig.dataSource].validateAndNormalizeInputFilters(effectiveFilters),
149
157
  sort,
150
158
  getTotals: true,
151
159
  });
@@ -167,7 +175,7 @@ export default class ImportExport extends AdminForthPlugin {
167
175
  return value;
168
176
  });
169
177
  });
170
- this.tryToAuditLogAction('export', `Export CSV with filters: ${JSON.stringify(filters)} and sort: ${JSON.stringify(sort)}. Total records: ${rows.length}`, adminUser, headers);
178
+ this.tryToAuditLogAction('export', `Export CSV with filters: ${JSON.stringify(effectiveFilters)} and sort: ${JSON.stringify(sort)}. Total records: ${rows.length}`, adminUser, headers);
171
179
  return {
172
180
  data: { fields, data: rows },
173
181
  columnsToForceQuote,
@@ -179,11 +187,9 @@ export default class ImportExport extends AdminForthPlugin {
179
187
  server.endpoint({
180
188
  method: 'POST',
181
189
  path: `/plugin/${this.pluginInstanceId}/import-csv`,
190
+ request_schema: importCsvBodySchema,
182
191
  handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, query, headers, cookies, requestUrl, response }) {
183
- const parsed = parseBody(importCsvBodySchema, body, response);
184
- if ('error' in parsed)
185
- return parsed.error;
186
- const payload = parsed.data;
192
+ const payload = body;
187
193
  const { data } = payload;
188
194
  if (!data || typeof data !== 'object') {
189
195
  return { ok: false, error: 'Invalid data format. Expected an object with column names as keys and arrays of values as values.' };
@@ -255,11 +261,9 @@ export default class ImportExport extends AdminForthPlugin {
255
261
  server.endpoint({
256
262
  method: 'POST',
257
263
  path: `/plugin/${this.pluginInstanceId}/import-csv-new-only`,
264
+ request_schema: importCsvBodySchema,
258
265
  handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, query, headers, cookies, requestUrl, response }) {
259
- const parsed = parseBody(importCsvBodySchema, body, response);
260
- if ('error' in parsed)
261
- return parsed.error;
262
- const payload = parsed.data;
266
+ const payload = body;
263
267
  const { data } = payload;
264
268
  if (!data || typeof data !== 'object') {
265
269
  return { ok: false, error: 'Invalid data format. Expected an object with column names as keys and arrays of values as values.' };
@@ -311,11 +315,9 @@ export default class ImportExport extends AdminForthPlugin {
311
315
  server.endpoint({
312
316
  method: 'POST',
313
317
  path: `/plugin/${this.pluginInstanceId}/check-records`,
318
+ request_schema: importCsvBodySchema,
314
319
  handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, response }) {
315
- const parsed = parseBody(importCsvBodySchema, body, response);
316
- if ('error' in parsed)
317
- return parsed.error;
318
- const payload = parsed.data;
320
+ const payload = body;
319
321
  const access = yield this.ensureAnyAllowed(adminUser, [
320
322
  { source: ActionCheckSource.ListRequest, action: AllowedActionsEnum.list },
321
323
  { source: ActionCheckSource.ShowRequest, action: AllowedActionsEnum.show },
package/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AdminForthPlugin, parseBody, suggestIfTypo, AdminForthFilterOperators, Filters, AdminForthDataTypes, rejectApiRawFilters, interpretResource, ActionCheckSource, AllowedActionsEnum } from "adminforth";
1
+ import { AdminForthPlugin, suggestIfTypo, AdminForthFilterOperators, Filters, AdminForthDataTypes, rejectApiRawFilters, interpretResource, ActionCheckSource, AllowedActionsEnum } from "adminforth";
2
2
  import type { IAdminForth, IHttpServer, AdminForthResourceColumn, AdminForthComponentDeclaration, AdminForthResource, AdminUser } from "adminforth";
3
3
  import type { PluginOptions } from './types.js';
4
4
  import pLimit from 'p-limit';
@@ -7,6 +7,7 @@ import { z } from "zod";
7
7
  const exportCsvBodySchema = z.object({
8
8
  filters: z.any(),
9
9
  sort: z.any(),
10
+ selectedIds: z.array(z.any()).optional(),
10
11
  }).strict();
11
12
 
12
13
  const importCsvBodySchema = z.object({
@@ -105,10 +106,7 @@ export default class ImportExport extends AdminForthPlugin {
105
106
  }
106
107
  (resourceConfig.options.pageInjections.list.threeDotsDropdownItems as AdminForthComponentDeclaration[]).push({
107
108
  file: this.componentPath('ExportCsv.vue'),
108
- meta: { pluginInstanceId: this.pluginInstanceId, select: 'all' }
109
- }, {
110
- file: this.componentPath('ExportCsv.vue'),
111
- meta: { pluginInstanceId: this.pluginInstanceId, select: 'filtered' }
109
+ meta: { pluginInstanceId: this.pluginInstanceId }
112
110
  }, {
113
111
  file: this.componentPath('ImportCsv.vue'),
114
112
  meta: { pluginInstanceId: this.pluginInstanceId }
@@ -137,11 +135,10 @@ export default class ImportExport extends AdminForthPlugin {
137
135
  server.endpoint({
138
136
  method: 'POST',
139
137
  path: `/plugin/${this.pluginInstanceId}/export-csv`,
138
+ request_schema: exportCsvBodySchema,
140
139
  handler: async ({ body, adminUser, headers, response }) => {
141
- const parsed = parseBody(exportCsvBodySchema, body, response);
142
- if ('error' in parsed) return parsed.error;
143
- const payload = parsed.data;
144
- const { filters, sort } = payload;
140
+ const payload = body as z.infer<typeof exportCsvBodySchema>;
141
+ const { filters, sort, selectedIds } = payload;
145
142
  if (!filters || !sort) {
146
143
  return { ok: false, error: 'Missing filters or sort in request body' };
147
144
  }
@@ -160,11 +157,25 @@ export default class ImportExport extends AdminForthPlugin {
160
157
  if (rawFilterError) {
161
158
  return rawFilterError;
162
159
  }
160
+
161
+ let effectiveFilters = filters;
162
+ if (Array.isArray(selectedIds) && selectedIds.length > 0) {
163
+ const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
164
+ if (!primaryKeyColumn) {
165
+ return { ok: false, error: 'Cannot export selected records: resource has no primary key' };
166
+ }
167
+ effectiveFilters = [{
168
+ field: primaryKeyColumn.name,
169
+ operator: AdminForthFilterOperators.IN,
170
+ value: selectedIds,
171
+ }];
172
+ }
173
+
163
174
  const data = await this.adminforth.connectors[this.resourceConfig.dataSource].getData({
164
175
  resource: this.resourceConfig,
165
176
  limit: 1e6,
166
177
  offset: 0,
167
- filters: this.adminforth.connectors[this.resourceConfig.dataSource].validateAndNormalizeInputFilters(filters),
178
+ filters: this.adminforth.connectors[this.resourceConfig.dataSource].validateAndNormalizeInputFilters(effectiveFilters),
168
179
  sort,
169
180
  getTotals: true,
170
181
  });
@@ -190,7 +201,7 @@ export default class ImportExport extends AdminForthPlugin {
190
201
  });
191
202
  });
192
203
 
193
- this.tryToAuditLogAction('export', `Export CSV with filters: ${JSON.stringify(filters)} and sort: ${JSON.stringify(sort)}. Total records: ${rows.length}`, adminUser, headers);
204
+ this.tryToAuditLogAction('export', `Export CSV with filters: ${JSON.stringify(effectiveFilters)} and sort: ${JSON.stringify(sort)}. Total records: ${rows.length}`, adminUser, headers);
194
205
 
195
206
  return {
196
207
  data: { fields, data: rows },
@@ -204,10 +215,9 @@ export default class ImportExport extends AdminForthPlugin {
204
215
  server.endpoint({
205
216
  method: 'POST',
206
217
  path: `/plugin/${this.pluginInstanceId}/import-csv`,
218
+ request_schema: importCsvBodySchema,
207
219
  handler: async ({ body, adminUser, query, headers, cookies, requestUrl, response }) => {
208
- const parsed = parseBody(importCsvBodySchema, body, response);
209
- if ('error' in parsed) return parsed.error;
210
- const payload = parsed.data;
220
+ const payload = body as z.infer<typeof importCsvBodySchema>;
211
221
  const { data } = payload;
212
222
  if (!data || typeof data !== 'object') {
213
223
  return { ok: false, error: 'Invalid data format. Expected an object with column names as keys and arrays of values as values.' };
@@ -289,10 +299,9 @@ export default class ImportExport extends AdminForthPlugin {
289
299
  server.endpoint({
290
300
  method: 'POST',
291
301
  path: `/plugin/${this.pluginInstanceId}/import-csv-new-only`,
302
+ request_schema: importCsvBodySchema,
292
303
  handler: async ({ body, adminUser, query, headers, cookies, requestUrl, response }) => {
293
- const parsed = parseBody(importCsvBodySchema, body, response);
294
- if ('error' in parsed) return parsed.error;
295
- const payload = parsed.data;
304
+ const payload = body as z.infer<typeof importCsvBodySchema>;
296
305
  const { data } = payload;
297
306
  if (!data || typeof data !== 'object') {
298
307
  return { ok: false, error: 'Invalid data format. Expected an object with column names as keys and arrays of values as values.' };
@@ -353,10 +362,9 @@ export default class ImportExport extends AdminForthPlugin {
353
362
  server.endpoint({
354
363
  method: 'POST',
355
364
  path: `/plugin/${this.pluginInstanceId}/check-records`,
365
+ request_schema: importCsvBodySchema,
356
366
  handler: async ({ body, adminUser, response }) => {
357
- const parsed = parseBody(importCsvBodySchema, body, response);
358
- if ('error' in parsed) return parsed.error;
359
- const payload = parsed.data;
367
+ const payload = body as z.infer<typeof importCsvBodySchema>;
360
368
  const access = await this.ensureAnyAllowed(
361
369
  adminUser,
362
370
  [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/import-export",
3
- "version": "1.6.1",
3
+ "version": "1.7.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -25,11 +25,11 @@
25
25
  "license": "MIT",
26
26
  "description": "CSV import/export plugin for adminforth",
27
27
  "peerDependencies": {
28
- "adminforth": "^3.7.1"
28
+ "adminforth": "^3.8.2"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/node": "^22.10.7",
32
- "adminforth": "^3.7.1",
32
+ "adminforth": "^3.8.2",
33
33
  "semantic-release": "^24.2.1",
34
34
  "semantic-release-slack-bot": "^4.0.2",
35
35
  "typescript": "^5.7.3"