@adminforth/import-export 1.5.3 → 1.6.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/index.js +31 -7
- package/index.ts +29 -7
- package/package.json +5 -4
package/dist/index.js
CHANGED
|
@@ -7,8 +7,16 @@ 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, suggestIfTypo, AdminForthFilterOperators, Filters, AdminForthDataTypes, rejectApiRawFilters, interpretResource, ActionCheckSource, AllowedActionsEnum } from "adminforth";
|
|
10
|
+
import { AdminForthPlugin, parseBody, suggestIfTypo, AdminForthFilterOperators, Filters, AdminForthDataTypes, rejectApiRawFilters, interpretResource, ActionCheckSource, AllowedActionsEnum } from "adminforth";
|
|
11
11
|
import pLimit from 'p-limit';
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
const exportCsvBodySchema = z.object({
|
|
14
|
+
filters: z.any(),
|
|
15
|
+
sort: z.any(),
|
|
16
|
+
}).strict();
|
|
17
|
+
const importCsvBodySchema = z.object({
|
|
18
|
+
data: z.record(z.string(), z.array(z.unknown())),
|
|
19
|
+
}).strict();
|
|
12
20
|
export default class ImportExport extends AdminForthPlugin {
|
|
13
21
|
constructor(options) {
|
|
14
22
|
super(options, import.meta.url);
|
|
@@ -113,8 +121,12 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
113
121
|
server.endpoint({
|
|
114
122
|
method: 'POST',
|
|
115
123
|
path: `/plugin/${this.pluginInstanceId}/export-csv`,
|
|
116
|
-
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, headers }) {
|
|
117
|
-
const
|
|
124
|
+
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;
|
|
118
130
|
if (!filters || !sort) {
|
|
119
131
|
return { ok: false, error: 'Missing filters or sort in request body' };
|
|
120
132
|
}
|
|
@@ -161,7 +173,11 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
161
173
|
method: 'POST',
|
|
162
174
|
path: `/plugin/${this.pluginInstanceId}/import-csv`,
|
|
163
175
|
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, query, headers, cookies, requestUrl, response }) {
|
|
164
|
-
const
|
|
176
|
+
const parsed = parseBody(importCsvBodySchema, body, response);
|
|
177
|
+
if ('error' in parsed)
|
|
178
|
+
return parsed.error;
|
|
179
|
+
const payload = parsed.data;
|
|
180
|
+
const { data } = payload;
|
|
165
181
|
if (!data || typeof data !== 'object') {
|
|
166
182
|
return { ok: false, error: 'Invalid data format. Expected an object with column names as keys and arrays of values as values.' };
|
|
167
183
|
}
|
|
@@ -233,7 +249,11 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
233
249
|
method: 'POST',
|
|
234
250
|
path: `/plugin/${this.pluginInstanceId}/import-csv-new-only`,
|
|
235
251
|
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, query, headers, cookies, requestUrl, response }) {
|
|
236
|
-
const
|
|
252
|
+
const parsed = parseBody(importCsvBodySchema, body, response);
|
|
253
|
+
if ('error' in parsed)
|
|
254
|
+
return parsed.error;
|
|
255
|
+
const payload = parsed.data;
|
|
256
|
+
const { data } = payload;
|
|
237
257
|
if (!data || typeof data !== 'object') {
|
|
238
258
|
return { ok: false, error: 'Invalid data format. Expected an object with column names as keys and arrays of values as values.' };
|
|
239
259
|
}
|
|
@@ -284,7 +304,11 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
284
304
|
server.endpoint({
|
|
285
305
|
method: 'POST',
|
|
286
306
|
path: `/plugin/${this.pluginInstanceId}/check-records`,
|
|
287
|
-
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser }) {
|
|
307
|
+
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, response }) {
|
|
308
|
+
const parsed = parseBody(importCsvBodySchema, body, response);
|
|
309
|
+
if ('error' in parsed)
|
|
310
|
+
return parsed.error;
|
|
311
|
+
const payload = parsed.data;
|
|
288
312
|
const access = yield this.ensureAnyAllowed(adminUser, [
|
|
289
313
|
{ source: ActionCheckSource.ListRequest, action: AllowedActionsEnum.list },
|
|
290
314
|
{ source: ActionCheckSource.ShowRequest, action: AllowedActionsEnum.show },
|
|
@@ -292,7 +316,7 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
292
316
|
if (!access.ok) {
|
|
293
317
|
return { ok: false, error: access.error };
|
|
294
318
|
}
|
|
295
|
-
const { data } =
|
|
319
|
+
const { data } = payload;
|
|
296
320
|
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
|
|
297
321
|
const columns = this.getColumnNames(data);
|
|
298
322
|
const rows = this.buildRowsFromData(data, columns, undefined, { coerceTypes: false });
|
package/index.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
import { AdminForthPlugin, suggestIfTypo, AdminForthFilterOperators, Filters, AdminForthDataTypes, rejectApiRawFilters, interpretResource, ActionCheckSource, AllowedActionsEnum } from "adminforth";
|
|
1
|
+
import { AdminForthPlugin, parseBody, 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';
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
|
|
7
|
+
const exportCsvBodySchema = z.object({
|
|
8
|
+
filters: z.any(),
|
|
9
|
+
sort: z.any(),
|
|
10
|
+
}).strict();
|
|
11
|
+
|
|
12
|
+
const importCsvBodySchema = z.object({
|
|
13
|
+
data: z.record(z.string(), z.array(z.unknown())),
|
|
14
|
+
}).strict();
|
|
5
15
|
|
|
6
16
|
export default class ImportExport extends AdminForthPlugin {
|
|
7
17
|
options: PluginOptions;
|
|
@@ -127,8 +137,11 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
127
137
|
server.endpoint({
|
|
128
138
|
method: 'POST',
|
|
129
139
|
path: `/plugin/${this.pluginInstanceId}/export-csv`,
|
|
130
|
-
handler: async ({ body, adminUser, headers }) => {
|
|
131
|
-
const
|
|
140
|
+
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;
|
|
132
145
|
if (!filters || !sort) {
|
|
133
146
|
return { ok: false, error: 'Missing filters or sort in request body' };
|
|
134
147
|
}
|
|
@@ -186,7 +199,10 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
186
199
|
method: 'POST',
|
|
187
200
|
path: `/plugin/${this.pluginInstanceId}/import-csv`,
|
|
188
201
|
handler: async ({ body, adminUser, query, headers, cookies, requestUrl, response }) => {
|
|
189
|
-
const
|
|
202
|
+
const parsed = parseBody(importCsvBodySchema, body, response);
|
|
203
|
+
if ('error' in parsed) return parsed.error;
|
|
204
|
+
const payload = parsed.data;
|
|
205
|
+
const { data } = payload;
|
|
190
206
|
if (!data || typeof data !== 'object') {
|
|
191
207
|
return { ok: false, error: 'Invalid data format. Expected an object with column names as keys and arrays of values as values.' };
|
|
192
208
|
}
|
|
@@ -268,7 +284,10 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
268
284
|
method: 'POST',
|
|
269
285
|
path: `/plugin/${this.pluginInstanceId}/import-csv-new-only`,
|
|
270
286
|
handler: async ({ body, adminUser, query, headers, cookies, requestUrl, response }) => {
|
|
271
|
-
const
|
|
287
|
+
const parsed = parseBody(importCsvBodySchema, body, response);
|
|
288
|
+
if ('error' in parsed) return parsed.error;
|
|
289
|
+
const payload = parsed.data;
|
|
290
|
+
const { data } = payload;
|
|
272
291
|
if (!data || typeof data !== 'object') {
|
|
273
292
|
return { ok: false, error: 'Invalid data format. Expected an object with column names as keys and arrays of values as values.' };
|
|
274
293
|
}
|
|
@@ -328,7 +347,10 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
328
347
|
server.endpoint({
|
|
329
348
|
method: 'POST',
|
|
330
349
|
path: `/plugin/${this.pluginInstanceId}/check-records`,
|
|
331
|
-
handler: async ({ body, adminUser }) => {
|
|
350
|
+
handler: async ({ body, adminUser, response }) => {
|
|
351
|
+
const parsed = parseBody(importCsvBodySchema, body, response);
|
|
352
|
+
if ('error' in parsed) return parsed.error;
|
|
353
|
+
const payload = parsed.data;
|
|
332
354
|
const access = await this.ensureAnyAllowed(
|
|
333
355
|
adminUser,
|
|
334
356
|
[
|
|
@@ -340,7 +362,7 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
340
362
|
if (!access.ok) {
|
|
341
363
|
return { ok: false, error: access.error };
|
|
342
364
|
}
|
|
343
|
-
const { data } =
|
|
365
|
+
const { data } = payload;
|
|
344
366
|
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
|
|
345
367
|
const columns = this.getColumnNames(data);
|
|
346
368
|
const rows = this.buildRowsFromData(data, columns, undefined, { coerceTypes: false });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adminforth/import-export",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.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.
|
|
28
|
+
"adminforth": "^3.7.1"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/node": "^22.10.7",
|
|
32
|
-
"adminforth": "^3.
|
|
32
|
+
"adminforth": "^3.7.1",
|
|
33
33
|
"semantic-release": "^24.2.1",
|
|
34
34
|
"semantic-release-slack-bot": "^4.0.2",
|
|
35
35
|
"typescript": "^5.7.3"
|
|
@@ -60,6 +60,7 @@
|
|
|
60
60
|
]
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"p-limit": "^7.3.0"
|
|
63
|
+
"p-limit": "^7.3.0",
|
|
64
|
+
"zod": "^4.3.6"
|
|
64
65
|
}
|
|
65
66
|
}
|