@adminforth/import-export 1.6.0 → 1.6.2
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 +29 -18
- package/index.ts +27 -14
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ 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,
|
|
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({
|
|
@@ -121,11 +121,9 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
121
121
|
server.endpoint({
|
|
122
122
|
method: 'POST',
|
|
123
123
|
path: `/plugin/${this.pluginInstanceId}/export-csv`,
|
|
124
|
+
request_schema: exportCsvBodySchema,
|
|
124
125
|
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, headers, response }) {
|
|
125
|
-
const
|
|
126
|
-
if ('error' in parsed)
|
|
127
|
-
return parsed.error;
|
|
128
|
-
const payload = parsed.data;
|
|
126
|
+
const payload = body;
|
|
129
127
|
const { filters, sort } = payload;
|
|
130
128
|
if (!filters || !sort) {
|
|
131
129
|
return { ok: false, error: 'Missing filters or sort in request body' };
|
|
@@ -158,7 +156,14 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
158
156
|
});
|
|
159
157
|
const fields = columns.map((col) => col.name);
|
|
160
158
|
const rows = data.data.map((row) => {
|
|
161
|
-
return columns.map((col) =>
|
|
159
|
+
return columns.map((col) => {
|
|
160
|
+
var _a;
|
|
161
|
+
const value = row[col.name];
|
|
162
|
+
if (col.type === AdminForthDataTypes.JSON || ((_a = col.isArray) === null || _a === void 0 ? void 0 : _a.enabled)) {
|
|
163
|
+
return value == null ? value : JSON.stringify(value);
|
|
164
|
+
}
|
|
165
|
+
return value;
|
|
166
|
+
});
|
|
162
167
|
});
|
|
163
168
|
this.tryToAuditLogAction('export', `Export CSV with filters: ${JSON.stringify(filters)} and sort: ${JSON.stringify(sort)}. Total records: ${rows.length}`, adminUser, headers);
|
|
164
169
|
return {
|
|
@@ -172,11 +177,9 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
172
177
|
server.endpoint({
|
|
173
178
|
method: 'POST',
|
|
174
179
|
path: `/plugin/${this.pluginInstanceId}/import-csv`,
|
|
180
|
+
request_schema: importCsvBodySchema,
|
|
175
181
|
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, query, headers, cookies, requestUrl, response }) {
|
|
176
|
-
const
|
|
177
|
-
if ('error' in parsed)
|
|
178
|
-
return parsed.error;
|
|
179
|
-
const payload = parsed.data;
|
|
182
|
+
const payload = body;
|
|
180
183
|
const { data } = payload;
|
|
181
184
|
if (!data || typeof data !== 'object') {
|
|
182
185
|
return { ok: false, error: 'Invalid data format. Expected an object with column names as keys and arrays of values as values.' };
|
|
@@ -248,11 +251,9 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
248
251
|
server.endpoint({
|
|
249
252
|
method: 'POST',
|
|
250
253
|
path: `/plugin/${this.pluginInstanceId}/import-csv-new-only`,
|
|
254
|
+
request_schema: importCsvBodySchema,
|
|
251
255
|
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, query, headers, cookies, requestUrl, response }) {
|
|
252
|
-
const
|
|
253
|
-
if ('error' in parsed)
|
|
254
|
-
return parsed.error;
|
|
255
|
-
const payload = parsed.data;
|
|
256
|
+
const payload = body;
|
|
256
257
|
const { data } = payload;
|
|
257
258
|
if (!data || typeof data !== 'object') {
|
|
258
259
|
return { ok: false, error: 'Invalid data format. Expected an object with column names as keys and arrays of values as values.' };
|
|
@@ -304,11 +305,9 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
304
305
|
server.endpoint({
|
|
305
306
|
method: 'POST',
|
|
306
307
|
path: `/plugin/${this.pluginInstanceId}/check-records`,
|
|
308
|
+
request_schema: importCsvBodySchema,
|
|
307
309
|
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, response }) {
|
|
308
|
-
const
|
|
309
|
-
if ('error' in parsed)
|
|
310
|
-
return parsed.error;
|
|
311
|
-
const payload = parsed.data;
|
|
310
|
+
const payload = body;
|
|
312
311
|
const access = yield this.ensureAnyAllowed(adminUser, [
|
|
313
312
|
{ source: ActionCheckSource.ListRequest, action: AllowedActionsEnum.list },
|
|
314
313
|
{ source: ActionCheckSource.ShowRequest, action: AllowedActionsEnum.show },
|
|
@@ -379,6 +378,7 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
379
378
|
return rows;
|
|
380
379
|
}
|
|
381
380
|
coerceValue(resourceCol, val) {
|
|
381
|
+
var _a;
|
|
382
382
|
if (!resourceCol || val === '') {
|
|
383
383
|
return val;
|
|
384
384
|
}
|
|
@@ -392,6 +392,17 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
392
392
|
}
|
|
393
393
|
return val === 1 || val === true;
|
|
394
394
|
}
|
|
395
|
+
if (resourceCol.type === AdminForthDataTypes.JSON || ((_a = resourceCol.isArray) === null || _a === void 0 ? void 0 : _a.enabled)) {
|
|
396
|
+
if (typeof val === 'string') {
|
|
397
|
+
try {
|
|
398
|
+
return JSON.parse(val);
|
|
399
|
+
}
|
|
400
|
+
catch (_b) {
|
|
401
|
+
return val;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return val;
|
|
405
|
+
}
|
|
395
406
|
return val;
|
|
396
407
|
}
|
|
397
408
|
}
|
package/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AdminForthPlugin,
|
|
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';
|
|
@@ -137,10 +137,9 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
137
137
|
server.endpoint({
|
|
138
138
|
method: 'POST',
|
|
139
139
|
path: `/plugin/${this.pluginInstanceId}/export-csv`,
|
|
140
|
+
request_schema: exportCsvBodySchema,
|
|
140
141
|
handler: async ({ body, adminUser, headers, response }) => {
|
|
141
|
-
const
|
|
142
|
-
if ('error' in parsed) return parsed.error;
|
|
143
|
-
const payload = parsed.data;
|
|
142
|
+
const payload = body as z.infer<typeof exportCsvBodySchema>;
|
|
144
143
|
const { filters, sort } = payload;
|
|
145
144
|
if (!filters || !sort) {
|
|
146
145
|
return { ok: false, error: 'Missing filters or sort in request body' };
|
|
@@ -181,7 +180,13 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
181
180
|
const fields = columns.map((col) => col.name);
|
|
182
181
|
|
|
183
182
|
const rows = data.data.map((row) => {
|
|
184
|
-
return columns.map((col) =>
|
|
183
|
+
return columns.map((col) => {
|
|
184
|
+
const value = row[col.name];
|
|
185
|
+
if (col.type === AdminForthDataTypes.JSON || col.isArray?.enabled) {
|
|
186
|
+
return value == null ? value : JSON.stringify(value);
|
|
187
|
+
}
|
|
188
|
+
return value;
|
|
189
|
+
});
|
|
185
190
|
});
|
|
186
191
|
|
|
187
192
|
this.tryToAuditLogAction('export', `Export CSV with filters: ${JSON.stringify(filters)} and sort: ${JSON.stringify(sort)}. Total records: ${rows.length}`, adminUser, headers);
|
|
@@ -198,10 +203,9 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
198
203
|
server.endpoint({
|
|
199
204
|
method: 'POST',
|
|
200
205
|
path: `/plugin/${this.pluginInstanceId}/import-csv`,
|
|
206
|
+
request_schema: importCsvBodySchema,
|
|
201
207
|
handler: async ({ body, adminUser, query, headers, cookies, requestUrl, response }) => {
|
|
202
|
-
const
|
|
203
|
-
if ('error' in parsed) return parsed.error;
|
|
204
|
-
const payload = parsed.data;
|
|
208
|
+
const payload = body as z.infer<typeof importCsvBodySchema>;
|
|
205
209
|
const { data } = payload;
|
|
206
210
|
if (!data || typeof data !== 'object') {
|
|
207
211
|
return { ok: false, error: 'Invalid data format. Expected an object with column names as keys and arrays of values as values.' };
|
|
@@ -283,10 +287,9 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
283
287
|
server.endpoint({
|
|
284
288
|
method: 'POST',
|
|
285
289
|
path: `/plugin/${this.pluginInstanceId}/import-csv-new-only`,
|
|
290
|
+
request_schema: importCsvBodySchema,
|
|
286
291
|
handler: async ({ body, adminUser, query, headers, cookies, requestUrl, response }) => {
|
|
287
|
-
const
|
|
288
|
-
if ('error' in parsed) return parsed.error;
|
|
289
|
-
const payload = parsed.data;
|
|
292
|
+
const payload = body as z.infer<typeof importCsvBodySchema>;
|
|
290
293
|
const { data } = payload;
|
|
291
294
|
if (!data || typeof data !== 'object') {
|
|
292
295
|
return { ok: false, error: 'Invalid data format. Expected an object with column names as keys and arrays of values as values.' };
|
|
@@ -347,10 +350,9 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
347
350
|
server.endpoint({
|
|
348
351
|
method: 'POST',
|
|
349
352
|
path: `/plugin/${this.pluginInstanceId}/check-records`,
|
|
353
|
+
request_schema: importCsvBodySchema,
|
|
350
354
|
handler: async ({ body, adminUser, response }) => {
|
|
351
|
-
const
|
|
352
|
-
if ('error' in parsed) return parsed.error;
|
|
353
|
-
const payload = parsed.data;
|
|
355
|
+
const payload = body as z.infer<typeof importCsvBodySchema>;
|
|
354
356
|
const access = await this.ensureAnyAllowed(
|
|
355
357
|
adminUser,
|
|
356
358
|
[
|
|
@@ -468,6 +470,17 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
468
470
|
return val === 1 || val === true;
|
|
469
471
|
}
|
|
470
472
|
|
|
473
|
+
if (resourceCol.type === AdminForthDataTypes.JSON || resourceCol.isArray?.enabled) {
|
|
474
|
+
if (typeof val === 'string') {
|
|
475
|
+
try {
|
|
476
|
+
return JSON.parse(val);
|
|
477
|
+
} catch {
|
|
478
|
+
return val;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
return val;
|
|
482
|
+
}
|
|
483
|
+
|
|
471
484
|
return val;
|
|
472
485
|
}
|
|
473
486
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adminforth/import-export",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.2",
|
|
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.8.2"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/node": "^22.10.7",
|
|
32
|
-
"adminforth": "^3.
|
|
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"
|