jtcsv 3.0.0 → 3.1.1
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/README.md +205 -146
- package/bin/jtcsv.ts +280 -202
- package/browser.d.ts +142 -0
- package/dist/benchmark.js +446 -0
- package/dist/benchmark.js.map +1 -0
- package/dist/bin/jtcsv.js +1940 -0
- package/dist/bin/jtcsv.js.map +1 -0
- package/dist/csv-to-json.js +1261 -0
- package/dist/csv-to-json.js.map +1 -0
- package/dist/errors.js +291 -0
- package/dist/errors.js.map +1 -0
- package/dist/eslint.config.js +147 -0
- package/dist/eslint.config.js.map +1 -0
- package/dist/index-core.js +95 -0
- package/dist/index-core.js.map +1 -0
- package/dist/index.js +93 -0
- package/dist/index.js.map +1 -0
- package/dist/json-save.js +229 -0
- package/dist/json-save.js.map +1 -0
- package/dist/json-to-csv.js +576 -0
- package/dist/json-to-csv.js.map +1 -0
- package/dist/jtcsv-core.cjs.js +336 -7
- package/dist/jtcsv-core.cjs.js.map +1 -1
- package/dist/jtcsv-core.esm.js +336 -7
- package/dist/jtcsv-core.esm.js.map +1 -1
- package/dist/jtcsv-core.umd.js +336 -7
- package/dist/jtcsv-core.umd.js.map +1 -1
- package/dist/jtcsv-full.cjs.js +336 -7
- package/dist/jtcsv-full.cjs.js.map +1 -1
- package/dist/jtcsv-full.esm.js +336 -7
- package/dist/jtcsv-full.esm.js.map +1 -1
- package/dist/jtcsv-full.umd.js +336 -7
- package/dist/jtcsv-full.umd.js.map +1 -1
- package/dist/jtcsv-workers.esm.js +9 -0
- package/dist/jtcsv-workers.esm.js.map +1 -1
- package/dist/jtcsv-workers.umd.js +9 -0
- package/dist/jtcsv-workers.umd.js.map +1 -1
- package/dist/jtcsv.cjs.js +1998 -2092
- package/dist/jtcsv.cjs.js.map +1 -1
- package/dist/jtcsv.esm.js +1994 -2092
- package/dist/jtcsv.esm.js.map +1 -1
- package/dist/jtcsv.umd.js +2157 -2251
- package/dist/jtcsv.umd.js.map +1 -1
- package/dist/plugins/express-middleware/index.js +350 -0
- package/dist/plugins/express-middleware/index.js.map +1 -0
- package/dist/plugins/fastify-plugin/index.js +315 -0
- package/dist/plugins/fastify-plugin/index.js.map +1 -0
- package/dist/plugins/hono/index.js +111 -0
- package/dist/plugins/hono/index.js.map +1 -0
- package/dist/plugins/nestjs/index.js +192 -0
- package/dist/plugins/nestjs/index.js.map +1 -0
- package/dist/plugins/nuxt/index.js +53 -0
- package/dist/plugins/nuxt/index.js.map +1 -0
- package/dist/plugins/remix/index.js +133 -0
- package/dist/plugins/remix/index.js.map +1 -0
- package/dist/plugins/sveltekit/index.js +155 -0
- package/dist/plugins/sveltekit/index.js.map +1 -0
- package/dist/plugins/trpc/index.js +136 -0
- package/dist/plugins/trpc/index.js.map +1 -0
- package/dist/run-demo.js +49 -0
- package/dist/run-demo.js.map +1 -0
- package/dist/src/browser/browser-functions.js +193 -0
- package/dist/src/browser/browser-functions.js.map +1 -0
- package/dist/src/browser/core.js +123 -0
- package/dist/src/browser/core.js.map +1 -0
- package/dist/src/browser/csv-to-json-browser.js +353 -0
- package/dist/src/browser/csv-to-json-browser.js.map +1 -0
- package/dist/src/browser/errors-browser.js +219 -0
- package/dist/src/browser/errors-browser.js.map +1 -0
- package/dist/src/browser/extensions/plugins.js +106 -0
- package/dist/src/browser/extensions/plugins.js.map +1 -0
- package/dist/src/browser/extensions/workers.js +66 -0
- package/dist/src/browser/extensions/workers.js.map +1 -0
- package/dist/src/browser/index.js +140 -0
- package/dist/src/browser/index.js.map +1 -0
- package/dist/src/browser/json-to-csv-browser.js +225 -0
- package/dist/src/browser/json-to-csv-browser.js.map +1 -0
- package/dist/src/browser/streams.js +340 -0
- package/dist/src/browser/streams.js.map +1 -0
- package/dist/src/browser/workers/csv-parser.worker.js +264 -0
- package/dist/src/browser/workers/csv-parser.worker.js.map +1 -0
- package/dist/src/browser/workers/worker-pool.js +338 -0
- package/dist/src/browser/workers/worker-pool.js.map +1 -0
- package/dist/src/core/delimiter-cache.js +196 -0
- package/dist/src/core/delimiter-cache.js.map +1 -0
- package/dist/src/core/node-optimizations.js +279 -0
- package/dist/src/core/node-optimizations.js.map +1 -0
- package/dist/src/core/plugin-system.js +399 -0
- package/dist/src/core/plugin-system.js.map +1 -0
- package/dist/src/core/transform-hooks.js +348 -0
- package/dist/src/core/transform-hooks.js.map +1 -0
- package/dist/src/engines/fast-path-engine-new.js +262 -0
- package/dist/src/engines/fast-path-engine-new.js.map +1 -0
- package/dist/src/engines/fast-path-engine.js +671 -0
- package/dist/src/engines/fast-path-engine.js.map +1 -0
- package/dist/src/errors.js +18 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/formats/ndjson-parser.js +332 -0
- package/dist/src/formats/ndjson-parser.js.map +1 -0
- package/dist/src/formats/tsv-parser.js +230 -0
- package/dist/src/formats/tsv-parser.js.map +1 -0
- package/dist/src/index-with-plugins.js +259 -0
- package/dist/src/index-with-plugins.js.map +1 -0
- package/dist/src/types/index.js +3 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/utils/bom-utils.js +267 -0
- package/dist/src/utils/bom-utils.js.map +1 -0
- package/dist/src/utils/encoding-support.js +77 -0
- package/dist/src/utils/encoding-support.js.map +1 -0
- package/dist/src/utils/schema-validator.js +609 -0
- package/dist/src/utils/schema-validator.js.map +1 -0
- package/dist/src/utils/transform-loader.js +281 -0
- package/dist/src/utils/transform-loader.js.map +1 -0
- package/dist/src/utils/validators.js +40 -0
- package/dist/src/utils/validators.js.map +1 -0
- package/dist/src/utils/zod-adapter.js +144 -0
- package/dist/src/utils/zod-adapter.js.map +1 -0
- package/{src → dist/src}/web-server/index.js +251 -286
- package/dist/src/web-server/index.js.map +1 -0
- package/dist/src/workers/csv-multithreaded.js +211 -0
- package/dist/src/workers/csv-multithreaded.js.map +1 -0
- package/dist/src/workers/csv-parser.worker.js +179 -0
- package/dist/src/workers/csv-parser.worker.js.map +1 -0
- package/dist/src/workers/worker-pool.js +228 -0
- package/dist/src/workers/worker-pool.js.map +1 -0
- package/dist/stream-csv-to-json.js +664 -0
- package/dist/stream-csv-to-json.js.map +1 -0
- package/dist/stream-json-to-csv.js +389 -0
- package/dist/stream-json-to-csv.js.map +1 -0
- package/examples/advanced/conditional-transformations.ts +2 -2
- package/examples/advanced/performance-optimization.ts +2 -2
- package/examples/cli-advanced-usage.md +2 -0
- package/examples/cli-tool.ts +1 -1
- package/examples/large-dataset-example.ts +2 -2
- package/examples/simple-usage.ts +2 -2
- package/examples/streaming-example.ts +1 -1
- package/index.d.ts +186 -15
- package/package.json +243 -305
- package/plugins.d.ts +37 -0
- package/schema.d.ts +103 -0
- package/src/browser/csv-to-json-browser.ts +233 -3
- package/src/browser/errors-browser.ts +45 -28
- package/src/browser/json-to-csv-browser.ts +81 -5
- package/src/browser/streams.ts +73 -6
- package/src/core/delimiter-cache.ts +21 -11
- package/src/core/plugin-system.ts +343 -155
- package/src/core/transform-hooks.ts +20 -12
- package/src/engines/fast-path-engine.ts +48 -32
- package/src/errors.ts +1 -72
- package/src/formats/ndjson-parser.ts +6 -0
- package/src/formats/tsv-parser.ts +6 -0
- package/src/types/index.ts +21 -1
- package/src/utils/validators.ts +35 -0
- package/src/web-server/index.ts +1 -1
- package/bin/jtcsv.js +0 -2532
- package/csv-to-json.js +0 -711
- package/errors.js +0 -394
- package/examples/advanced/conditional-transformations.js +0 -446
- package/examples/advanced/csv-parser.worker.js +0 -89
- package/examples/advanced/nested-objects-example.js +0 -306
- package/examples/advanced/performance-optimization.js +0 -504
- package/examples/advanced/run-demo-server.js +0 -116
- package/examples/cli-batch-processing.js +0 -38
- package/examples/cli-tool.js +0 -183
- package/examples/error-handling.js +0 -338
- package/examples/express-api.js +0 -164
- package/examples/large-dataset-example.js +0 -182
- package/examples/ndjson-processing.js +0 -434
- package/examples/plugin-excel-exporter.js +0 -406
- package/examples/schema-validation.js +0 -640
- package/examples/simple-usage.js +0 -282
- package/examples/streaming-example.js +0 -418
- package/examples/web-workers-advanced.js +0 -28
- package/index.js +0 -82
- package/json-save.js +0 -255
- package/json-to-csv.js +0 -668
- package/plugins/README.md +0 -91
- package/plugins/express-middleware/README.md +0 -83
- package/plugins/express-middleware/example.js +0 -135
- package/plugins/express-middleware/example.ts +0 -135
- package/plugins/express-middleware/index.d.ts +0 -114
- package/plugins/express-middleware/index.js +0 -512
- package/plugins/express-middleware/index.ts +0 -557
- package/plugins/express-middleware/package.json +0 -52
- package/plugins/fastify-plugin/index.js +0 -404
- package/plugins/fastify-plugin/index.ts +0 -443
- package/plugins/fastify-plugin/package.json +0 -55
- package/plugins/hono/README.md +0 -28
- package/plugins/hono/index.d.ts +0 -12
- package/plugins/hono/index.js +0 -36
- package/plugins/hono/index.ts +0 -226
- package/plugins/hono/package.json +0 -35
- package/plugins/nestjs/README.md +0 -35
- package/plugins/nestjs/index.d.ts +0 -25
- package/plugins/nestjs/index.js +0 -77
- package/plugins/nestjs/index.ts +0 -201
- package/plugins/nestjs/package.json +0 -37
- package/plugins/nextjs-api/README.md +0 -57
- package/plugins/nextjs-api/examples/ConverterComponent.jsx +0 -386
- package/plugins/nextjs-api/examples/ConverterComponent.tsx +0 -386
- package/plugins/nextjs-api/examples/api-convert.js +0 -67
- package/plugins/nextjs-api/examples/api-convert.ts +0 -67
- package/plugins/nextjs-api/index.js +0 -387
- package/plugins/nextjs-api/index.tsx +0 -339
- package/plugins/nextjs-api/package.json +0 -63
- package/plugins/nextjs-api/route.js +0 -370
- package/plugins/nextjs-api/route.ts +0 -370
- package/plugins/nuxt/README.md +0 -24
- package/plugins/nuxt/index.js +0 -21
- package/plugins/nuxt/index.ts +0 -94
- package/plugins/nuxt/package.json +0 -35
- package/plugins/nuxt/runtime/composables/useJtcsv.js +0 -6
- package/plugins/nuxt/runtime/composables/useJtcsv.ts +0 -100
- package/plugins/nuxt/runtime/plugin.js +0 -6
- package/plugins/nuxt/runtime/plugin.ts +0 -71
- package/plugins/remix/README.md +0 -26
- package/plugins/remix/index.d.ts +0 -16
- package/plugins/remix/index.js +0 -62
- package/plugins/remix/index.ts +0 -260
- package/plugins/remix/package.json +0 -35
- package/plugins/sveltekit/README.md +0 -28
- package/plugins/sveltekit/index.d.ts +0 -17
- package/plugins/sveltekit/index.js +0 -54
- package/plugins/sveltekit/index.ts +0 -301
- package/plugins/sveltekit/package.json +0 -33
- package/plugins/trpc/README.md +0 -25
- package/plugins/trpc/index.d.ts +0 -7
- package/plugins/trpc/index.js +0 -32
- package/plugins/trpc/index.ts +0 -267
- package/plugins/trpc/package.json +0 -34
- package/src/browser/browser-functions.js +0 -219
- package/src/browser/core.js +0 -92
- package/src/browser/csv-to-json-browser.js +0 -722
- package/src/browser/errors-browser.js +0 -212
- package/src/browser/extensions/plugins.js +0 -92
- package/src/browser/extensions/workers.js +0 -39
- package/src/browser/index.js +0 -113
- package/src/browser/json-to-csv-browser.js +0 -319
- package/src/browser/streams.js +0 -403
- package/src/browser/workers/csv-parser.worker.js +0 -377
- package/src/browser/workers/worker-pool.js +0 -527
- package/src/core/delimiter-cache.js +0 -200
- package/src/core/node-optimizations.js +0 -408
- package/src/core/plugin-system.js +0 -494
- package/src/core/transform-hooks.js +0 -350
- package/src/engines/fast-path-engine-new.js +0 -338
- package/src/engines/fast-path-engine.js +0 -844
- package/src/errors.js +0 -26
- package/src/formats/ndjson-parser.js +0 -467
- package/src/formats/tsv-parser.js +0 -339
- package/src/index-with-plugins.js +0 -378
- package/src/utils/bom-utils.js +0 -259
- package/src/utils/encoding-support.js +0 -124
- package/src/utils/schema-validator.js +0 -594
- package/src/utils/transform-loader.js +0 -205
- package/src/utils/zod-adapter.js +0 -170
- package/stream-csv-to-json.js +0 -560
- package/stream-json-to-csv.js +0 -465
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SvelteKit plugin for jtcsv
|
|
3
|
-
* Provides utilities for CSV parsing and generation in SvelteKit applications
|
|
4
|
-
* @module plugins/sveltekit
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { csvToJson, jsonToCsv } from '../../index-core';
|
|
8
|
-
import type { CsvToJsonOptions, JsonToCsvOptions } from '../../src/types';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* SvelteKit Request type (simplified)
|
|
12
|
-
*/
|
|
13
|
-
interface SvelteKitRequest {
|
|
14
|
-
text(): Promise<string>;
|
|
15
|
-
formData(): Promise<FormData>;
|
|
16
|
-
headers?: {
|
|
17
|
-
get(name: string): string | null;
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Options for CSV parsing from request
|
|
23
|
-
*/
|
|
24
|
-
export interface ParseCsvOptions extends CsvToJsonOptions {
|
|
25
|
-
/** Field name containing the CSV file (default: 'file') */
|
|
26
|
-
fieldName?: string;
|
|
27
|
-
/** Force form data parsing (default: auto-detect) */
|
|
28
|
-
formData?: boolean;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Options for CSV response generation
|
|
33
|
-
*/
|
|
34
|
-
export interface GenerateCsvOptions extends JsonToCsvOptions {
|
|
35
|
-
// Additional options specific to CSV response
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Normalize filename for CSV download
|
|
40
|
-
*/
|
|
41
|
-
function normalizeFilename(filename?: string): string {
|
|
42
|
-
if (!filename || typeof filename !== 'string') {
|
|
43
|
-
return 'export.csv';
|
|
44
|
-
}
|
|
45
|
-
return filename.includes('.') ? filename : `${filename}.csv`;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Extract CSV text from FormData
|
|
50
|
-
*/
|
|
51
|
-
async function extractCsvText(formData: FormData, fieldName: string): Promise<string | null> {
|
|
52
|
-
if (formData.has(fieldName)) {
|
|
53
|
-
const value = formData.get(fieldName);
|
|
54
|
-
if (value && typeof (value as any).text === 'function') {
|
|
55
|
-
return await (value as any).text();
|
|
56
|
-
}
|
|
57
|
-
if (value !== null) {
|
|
58
|
-
return String(value);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
for (const value of formData.values()) {
|
|
63
|
-
if (value && typeof (value as any).text === 'function') {
|
|
64
|
-
return await (value as any).text();
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return null;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Parse CSV from SvelteKit request
|
|
73
|
-
*
|
|
74
|
-
* @example
|
|
75
|
-
* // In a SvelteKit endpoint:
|
|
76
|
-
* export async function POST({ request }) {
|
|
77
|
-
* const data = await parseCsv(request, { delimiter: ',' });
|
|
78
|
-
* return json({ success: true, data });
|
|
79
|
-
* }
|
|
80
|
-
*/
|
|
81
|
-
export async function parseCsv(
|
|
82
|
-
request: SvelteKitRequest,
|
|
83
|
-
options: ParseCsvOptions = {}
|
|
84
|
-
): Promise<any[]> {
|
|
85
|
-
if (!request || typeof request.text !== 'function') {
|
|
86
|
-
throw new Error('parseCsv expects a Request instance');
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const { fieldName = 'file', formData: forceFormData, ...csvOptions } = options;
|
|
90
|
-
const contentType = request.headers?.get?.('content-type') || '';
|
|
91
|
-
let csvText: string | null = null;
|
|
92
|
-
|
|
93
|
-
if (forceFormData || contentType.includes('multipart/form-data')) {
|
|
94
|
-
const formData = await request.formData();
|
|
95
|
-
csvText = await extractCsvText(formData, fieldName);
|
|
96
|
-
} else {
|
|
97
|
-
csvText = await request.text();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (!csvText) {
|
|
101
|
-
throw new Error('No CSV payload found in request');
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return await csvToJson(csvText, csvOptions);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Async version of parseCsv with better error handling
|
|
109
|
-
*/
|
|
110
|
-
export async function parseCsvAsync(
|
|
111
|
-
request: SvelteKitRequest,
|
|
112
|
-
options: ParseCsvOptions = {}
|
|
113
|
-
): Promise<any[]> {
|
|
114
|
-
try {
|
|
115
|
-
return await parseCsv(request, options);
|
|
116
|
-
} catch (error) {
|
|
117
|
-
throw new Error(`Failed to parse CSV from request: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Generate a CSV response for SvelteKit
|
|
123
|
-
*
|
|
124
|
-
* @example
|
|
125
|
-
* // In a SvelteKit endpoint:
|
|
126
|
-
* export async function GET() {
|
|
127
|
-
* const data = await getData();
|
|
128
|
-
* return generateCsv(data, 'export.csv');
|
|
129
|
-
* }
|
|
130
|
-
*/
|
|
131
|
-
export function generateCsv(
|
|
132
|
-
data: any,
|
|
133
|
-
filename: string = 'export.csv',
|
|
134
|
-
options: GenerateCsvOptions = {}
|
|
135
|
-
): Response {
|
|
136
|
-
const safeName = normalizeFilename(filename);
|
|
137
|
-
const rows = Array.isArray(data) ? data : [data];
|
|
138
|
-
const csv = jsonToCsv(rows, options);
|
|
139
|
-
|
|
140
|
-
return new Response(csv, {
|
|
141
|
-
headers: {
|
|
142
|
-
'Content-Type': 'text/csv; charset=utf-8',
|
|
143
|
-
'Content-Disposition': `attachment; filename="${safeName}"`
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Async version of generateCsv
|
|
150
|
-
*/
|
|
151
|
-
export async function generateAsyncCsv(
|
|
152
|
-
data: any,
|
|
153
|
-
filename: string = 'export.csv',
|
|
154
|
-
options: GenerateCsvOptions = {}
|
|
155
|
-
): Promise<Response> {
|
|
156
|
-
const safeName = normalizeFilename(filename);
|
|
157
|
-
const rows = Array.isArray(data) ? data : [data];
|
|
158
|
-
const csv = await jsonToCsv(rows, options);
|
|
159
|
-
|
|
160
|
-
return new Response(csv, {
|
|
161
|
-
headers: {
|
|
162
|
-
'Content-Type': 'text/csv; charset=utf-8',
|
|
163
|
-
'Content-Disposition': `attachment; filename="${safeName}"`
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* CSV loader helper for SvelteKit
|
|
170
|
-
* Creates a loader that returns CSV data
|
|
171
|
-
*
|
|
172
|
-
* @example
|
|
173
|
-
* export const loader = createCsvLoader(async () => {
|
|
174
|
-
* return await getData();
|
|
175
|
-
* }, 'data.csv');
|
|
176
|
-
*/
|
|
177
|
-
export function createCsvLoader(
|
|
178
|
-
dataLoader: () => Promise<any> | any,
|
|
179
|
-
filename: string = 'export.csv',
|
|
180
|
-
options: GenerateCsvOptions = {}
|
|
181
|
-
): () => Promise<Response> {
|
|
182
|
-
return async () => {
|
|
183
|
-
try {
|
|
184
|
-
const data = await (typeof dataLoader === 'function' ? dataLoader() : dataLoader);
|
|
185
|
-
return generateCsv(data, filename, options);
|
|
186
|
-
} catch (error) {
|
|
187
|
-
return new Response(
|
|
188
|
-
JSON.stringify({ error: error instanceof Error ? error.message : 'Unknown error' }),
|
|
189
|
-
{ status: 500, headers: { 'Content-Type': 'application/json' } }
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* CSV action helper for SvelteKit
|
|
197
|
-
* Creates an action that parses CSV from form data
|
|
198
|
-
*
|
|
199
|
-
* @example
|
|
200
|
-
* export const action = createCsvAction({ delimiter: ',' });
|
|
201
|
-
*/
|
|
202
|
-
export function createCsvAction(
|
|
203
|
-
options: ParseCsvOptions = {}
|
|
204
|
-
): (args: { request: SvelteKitRequest }) => Promise<Response> {
|
|
205
|
-
return async ({ request }) => {
|
|
206
|
-
try {
|
|
207
|
-
const data = await parseCsv(request, options);
|
|
208
|
-
return new Response(
|
|
209
|
-
JSON.stringify({ success: true, data }),
|
|
210
|
-
{ headers: { 'Content-Type': 'application/json' } }
|
|
211
|
-
);
|
|
212
|
-
} catch (error) {
|
|
213
|
-
return new Response(
|
|
214
|
-
JSON.stringify({
|
|
215
|
-
success: false,
|
|
216
|
-
error: error instanceof Error ? error.message : 'Unknown error'
|
|
217
|
-
}),
|
|
218
|
-
{ status: 400, headers: { 'Content-Type': 'application/json' } }
|
|
219
|
-
);
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Multi-part form data CSV parser
|
|
226
|
-
* Handles multiple CSV files in a single request
|
|
227
|
-
*/
|
|
228
|
-
export async function parseMultiPartCsv(
|
|
229
|
-
request: SvelteKitRequest,
|
|
230
|
-
options: ParseCsvOptions & { multiple?: boolean } = {}
|
|
231
|
-
): Promise<any[] | any[][]> {
|
|
232
|
-
const { multiple = false, ...csvOptions } = options;
|
|
233
|
-
const formData = await request.formData();
|
|
234
|
-
const results: any[][] = [];
|
|
235
|
-
|
|
236
|
-
for (const [fieldName, value] of formData.entries()) {
|
|
237
|
-
if (value && typeof (value as any).text === 'function') {
|
|
238
|
-
const csvText = await (value as any).text();
|
|
239
|
-
if (csvText) {
|
|
240
|
-
const parsed = await csvToJson(csvText, csvOptions);
|
|
241
|
-
results.push(parsed);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
if (!multiple && results.length > 0) {
|
|
247
|
-
return results[0];
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
return results;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* CSV export utility for SvelteKit routes
|
|
255
|
-
*/
|
|
256
|
-
export function csvExport(
|
|
257
|
-
data: any,
|
|
258
|
-
filename?: string,
|
|
259
|
-
options?: GenerateCsvOptions
|
|
260
|
-
): () => Promise<Response> {
|
|
261
|
-
return () => generateAsyncCsv(data, filename, options);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* SvelteKit server hook for CSV processing
|
|
266
|
-
* Can be used in hooks.server.js/ts
|
|
267
|
-
*/
|
|
268
|
-
export function createCsvHook(options: {
|
|
269
|
-
parseOptions?: ParseCsvOptions;
|
|
270
|
-
generateOptions?: GenerateCsvOptions;
|
|
271
|
-
} = {}) {
|
|
272
|
-
return {
|
|
273
|
-
async handle({ event, resolve }: { event: any; resolve: any }) {
|
|
274
|
-
// Add CSV utilities to event.locals
|
|
275
|
-
event.locals.csv = {
|
|
276
|
-
parse: (request: SvelteKitRequest, opts?: ParseCsvOptions) =>
|
|
277
|
-
parseCsv(request, { ...options.parseOptions, ...opts }),
|
|
278
|
-
parseAsync: (request: SvelteKitRequest, opts?: ParseCsvOptions) =>
|
|
279
|
-
parseCsvAsync(request, { ...options.parseOptions, ...opts }),
|
|
280
|
-
generate: (data: any, filename?: string, opts?: GenerateCsvOptions) =>
|
|
281
|
-
generateCsv(data, filename, { ...options.generateOptions, ...opts }),
|
|
282
|
-
generateAsync: (data: any, filename?: string, opts?: GenerateCsvOptions) =>
|
|
283
|
-
generateAsyncCsv(data, filename, { ...options.generateOptions, ...opts }),
|
|
284
|
-
};
|
|
285
|
-
|
|
286
|
-
return resolve(event);
|
|
287
|
-
}
|
|
288
|
-
};
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
export default {
|
|
292
|
-
parseCsv,
|
|
293
|
-
parseCsvAsync,
|
|
294
|
-
generateCsv,
|
|
295
|
-
generateAsyncCsv,
|
|
296
|
-
createCsvLoader,
|
|
297
|
-
createCsvAction,
|
|
298
|
-
parseMultiPartCsv,
|
|
299
|
-
csvExport,
|
|
300
|
-
createCsvHook,
|
|
301
|
-
};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@jtcsv/sveltekit",
|
|
3
|
-
"version": "1.0.0",
|
|
4
|
-
"description": "SvelteKit helpers for JTCSV",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"types": "index.d.ts",
|
|
7
|
-
"keywords": [
|
|
8
|
-
"sveltekit",
|
|
9
|
-
"csv",
|
|
10
|
-
"json",
|
|
11
|
-
"converter",
|
|
12
|
-
"jtcsv"
|
|
13
|
-
],
|
|
14
|
-
"author": "Ruslan Fomenko",
|
|
15
|
-
"license": "MIT",
|
|
16
|
-
"repository": {
|
|
17
|
-
"type": "git",
|
|
18
|
-
"url": "git+https://github.com/Linol-Hamelton/jtcsv.git",
|
|
19
|
-
"directory": "plugins/sveltekit"
|
|
20
|
-
},
|
|
21
|
-
"bugs": {
|
|
22
|
-
"url": "https://github.com/Linol-Hamelton/jtcsv/issues"
|
|
23
|
-
},
|
|
24
|
-
"homepage": "https://github.com/Linol-Hamelton/jtcsv/tree/main/plugins/sveltekit#readme",
|
|
25
|
-
"peerDependencies": {
|
|
26
|
-
"jtcsv": "^2.1.3"
|
|
27
|
-
},
|
|
28
|
-
"files": [
|
|
29
|
-
"index.js",
|
|
30
|
-
"index.d.ts",
|
|
31
|
-
"README.md"
|
|
32
|
-
]
|
|
33
|
-
}
|
package/plugins/trpc/README.md
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
# @jtcsv/trpc
|
|
2
|
-
|
|
3
|
-
Middleware helper that parses CSV input for tRPC procedures.
|
|
4
|
-
|
|
5
|
-
## Install
|
|
6
|
-
```bash
|
|
7
|
-
npm install @jtcsv/trpc jtcsv
|
|
8
|
-
```
|
|
9
|
-
|
|
10
|
-
## Usage
|
|
11
|
-
```javascript
|
|
12
|
-
import { initTRPC } from '@trpc/server';
|
|
13
|
-
import { z } from 'zod';
|
|
14
|
-
import { createCsvProcedure } from '@jtcsv/trpc';
|
|
15
|
-
|
|
16
|
-
const t = initTRPC.create();
|
|
17
|
-
|
|
18
|
-
export const router = t.router({
|
|
19
|
-
parseCsv: createCsvProcedure(t, z.string())
|
|
20
|
-
.mutation(async ({ input }) => ({ rows: input }))
|
|
21
|
-
});
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## Exports
|
|
25
|
-
- createCsvProcedure
|
package/plugins/trpc/index.d.ts
DELETED
package/plugins/trpc/index.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
const jtcsv = require('jtcsv');
|
|
2
|
-
|
|
3
|
-
function extractCsvText(input) {
|
|
4
|
-
if (typeof input === 'string') {
|
|
5
|
-
return input;
|
|
6
|
-
}
|
|
7
|
-
if (input && typeof input === 'object' && typeof input.csv === 'string') {
|
|
8
|
-
return input.csv;
|
|
9
|
-
}
|
|
10
|
-
return null;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function createCsvProcedure(t, schema, options = {}) {
|
|
14
|
-
if (!t || !t.procedure) {
|
|
15
|
-
throw new Error('createCsvProcedure expects initTRPC instance');
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return t.procedure
|
|
19
|
-
.input(schema)
|
|
20
|
-
.use(async ({ input, next }) => {
|
|
21
|
-
const csvText = extractCsvText(input);
|
|
22
|
-
if (!csvText) {
|
|
23
|
-
throw new Error('CSV input must be a string or { csv: string }');
|
|
24
|
-
}
|
|
25
|
-
const parsed = jtcsv.csvToJson(csvText, options);
|
|
26
|
-
return next({ input: parsed });
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
module.exports = {
|
|
31
|
-
createCsvProcedure
|
|
32
|
-
};
|
package/plugins/trpc/index.ts
DELETED
|
@@ -1,267 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* tRPC plugin for jtcsv
|
|
3
|
-
* Provides utilities for CSV parsing and generation in tRPC applications
|
|
4
|
-
* @module plugins/trpc
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { csvToJson, jsonToCsv } from '../../index-core';
|
|
8
|
-
import type { CsvToJsonOptions, JsonToCsvOptions } from '../../src/types';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* tRPC context type (simplified)
|
|
12
|
-
*/
|
|
13
|
-
interface TRPCContext {
|
|
14
|
-
// tRPC context properties
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* tRPC procedure type (simplified)
|
|
19
|
-
*/
|
|
20
|
-
interface TRPCProcedure {
|
|
21
|
-
input(schema: any): any;
|
|
22
|
-
use(middleware: any): any;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* tRPC instance type (simplified)
|
|
27
|
-
*/
|
|
28
|
-
interface TRPCInstance {
|
|
29
|
-
procedure: TRPCProcedure;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Options for CSV parsing in tRPC procedures
|
|
34
|
-
*/
|
|
35
|
-
export interface CsvProcedureOptions extends CsvToJsonOptions {
|
|
36
|
-
/** Whether to return raw CSV text instead of parsed JSON */
|
|
37
|
-
raw?: boolean;
|
|
38
|
-
/** Whether to use async parsing */
|
|
39
|
-
async?: boolean;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Options for CSV generation in tRPC procedures
|
|
44
|
-
*/
|
|
45
|
-
export interface CsvResponseOptions extends JsonToCsvOptions {
|
|
46
|
-
/** Filename for download (default: 'export.csv') */
|
|
47
|
-
filename?: string;
|
|
48
|
-
/** Whether to return as Response object */
|
|
49
|
-
asResponse?: boolean;
|
|
50
|
-
/** Whether to use async generation */
|
|
51
|
-
async?: boolean;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Extract CSV text from various input formats
|
|
56
|
-
*/
|
|
57
|
-
function extractCsvText(input: any): string | null {
|
|
58
|
-
if (typeof input === 'string') {
|
|
59
|
-
return input;
|
|
60
|
-
}
|
|
61
|
-
if (input && typeof input === 'object' && typeof input.csv === 'string') {
|
|
62
|
-
return input.csv;
|
|
63
|
-
}
|
|
64
|
-
if (input && typeof input === 'object' && input.file && typeof input.file.text === 'function') {
|
|
65
|
-
return input.file.text();
|
|
66
|
-
}
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Create a tRPC procedure for CSV parsing
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
* // In your tRPC router:
|
|
75
|
-
* import { createCsvProcedure } from 'jtcsv/plugins/trpc';
|
|
76
|
-
*
|
|
77
|
-
* export const csvRouter = t.router({
|
|
78
|
-
* parse: createCsvProcedure(t, z.string(), { delimiter: ',' })
|
|
79
|
-
* });
|
|
80
|
-
*/
|
|
81
|
-
export function createCsvProcedure(
|
|
82
|
-
t: TRPCInstance,
|
|
83
|
-
schema: any,
|
|
84
|
-
options: CsvProcedureOptions = {}
|
|
85
|
-
): any {
|
|
86
|
-
if (!t || !t.procedure) {
|
|
87
|
-
throw new Error('createCsvProcedure expects initTRPC instance');
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return t.procedure
|
|
91
|
-
.input(schema)
|
|
92
|
-
.use(async ({ input, next }: { input: any; next: any }) => {
|
|
93
|
-
const csvText = extractCsvText(input);
|
|
94
|
-
if (!csvText) {
|
|
95
|
-
throw new Error('CSV input must be a string or { csv: string }');
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (options.async) {
|
|
99
|
-
const parsed = await csvToJson(csvText, options);
|
|
100
|
-
return next({ input: parsed });
|
|
101
|
-
} else {
|
|
102
|
-
const parsed = csvToJson(csvText, options);
|
|
103
|
-
return next({ input: parsed });
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Async version of createCsvProcedure
|
|
110
|
-
*/
|
|
111
|
-
export function createAsyncCsvProcedure(
|
|
112
|
-
t: TRPCInstance,
|
|
113
|
-
schema: any,
|
|
114
|
-
options: CsvProcedureOptions = {}
|
|
115
|
-
): any {
|
|
116
|
-
return createCsvProcedure(t, schema, { ...options, async: true });
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Create a tRPC procedure for CSV generation
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* // In your tRPC router:
|
|
124
|
-
* import { createCsvResponseProcedure } from 'jtcsv/plugins/trpc';
|
|
125
|
-
*
|
|
126
|
-
* export const csvRouter = t.router({
|
|
127
|
-
* export: createCsvResponseProcedure(t, z.array(z.any()), { filename: 'data.csv' })
|
|
128
|
-
* });
|
|
129
|
-
*/
|
|
130
|
-
export function createCsvResponseProcedure(
|
|
131
|
-
t: TRPCInstance,
|
|
132
|
-
schema: any,
|
|
133
|
-
options: CsvResponseOptions = {}
|
|
134
|
-
): any {
|
|
135
|
-
if (!t || !t.procedure) {
|
|
136
|
-
throw new Error('createCsvResponseProcedure expects initTRPC instance');
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return t.procedure
|
|
140
|
-
.input(schema)
|
|
141
|
-
.use(async ({ input, next }: { input: any; next: any }) => {
|
|
142
|
-
const { filename = 'export.csv', asResponse = false, ...csvOptions } = options;
|
|
143
|
-
const rows = Array.isArray(input) ? input : [input];
|
|
144
|
-
|
|
145
|
-
if (options.async) {
|
|
146
|
-
const csv = await jsonToCsv(rows, csvOptions);
|
|
147
|
-
|
|
148
|
-
if (asResponse) {
|
|
149
|
-
const response = new Response(csv, {
|
|
150
|
-
headers: {
|
|
151
|
-
'Content-Type': 'text/csv; charset=utf-8',
|
|
152
|
-
'Content-Disposition': `attachment; filename="${filename}"`
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
return next({ input: response });
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return next({ input: csv });
|
|
159
|
-
} else {
|
|
160
|
-
const csv = jsonToCsv(rows, csvOptions);
|
|
161
|
-
|
|
162
|
-
if (asResponse) {
|
|
163
|
-
const response = new Response(csv, {
|
|
164
|
-
headers: {
|
|
165
|
-
'Content-Type': 'text/csv; charset=utf-8',
|
|
166
|
-
'Content-Disposition': `attachment; filename="${filename}"`
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
return next({ input: response });
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return next({ input: csv });
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Async version of createCsvResponseProcedure
|
|
179
|
-
*/
|
|
180
|
-
export function createAsyncCsvResponseProcedure(
|
|
181
|
-
t: TRPCInstance,
|
|
182
|
-
schema: any,
|
|
183
|
-
options: CsvResponseOptions = {}
|
|
184
|
-
): any {
|
|
185
|
-
return createCsvResponseProcedure(t, schema, { ...options, async: true });
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Create a complete tRPC router for CSV operations
|
|
190
|
-
*
|
|
191
|
-
* @example
|
|
192
|
-
* // In your tRPC setup:
|
|
193
|
-
* import { createCsvRouter } from 'jtcsv/plugins/trpc';
|
|
194
|
-
*
|
|
195
|
-
* export const csvRouter = createCsvRouter(t);
|
|
196
|
-
*/
|
|
197
|
-
export function createCsvRouter(t: TRPCInstance, options: {
|
|
198
|
-
parseOptions?: CsvProcedureOptions;
|
|
199
|
-
responseOptions?: CsvResponseOptions;
|
|
200
|
-
} = {}) {
|
|
201
|
-
if (!t || !t.procedure) {
|
|
202
|
-
throw new Error('createCsvRouter expects initTRPC instance');
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
return {
|
|
206
|
-
parse: createCsvProcedure(t, z.string(), options.parseOptions),
|
|
207
|
-
parseAsync: createAsyncCsvProcedure(t, z.string(), options.parseOptions),
|
|
208
|
-
export: createCsvResponseProcedure(t, z.array(z.any()), options.responseOptions),
|
|
209
|
-
exportAsync: createAsyncCsvResponseProcedure(t, z.array(z.any()), options.responseOptions),
|
|
210
|
-
batch: t.procedure
|
|
211
|
-
.input(z.array(z.string()))
|
|
212
|
-
.use(async ({ input, next }: { input: string[]; next: any }) => {
|
|
213
|
-
const results = await Promise.all(
|
|
214
|
-
input.map((csv: string) => csvToJson(csv, options.parseOptions))
|
|
215
|
-
);
|
|
216
|
-
return next({ input: results });
|
|
217
|
-
}),
|
|
218
|
-
batchAsync: t.procedure
|
|
219
|
-
.input(z.array(z.string()))
|
|
220
|
-
.use(async ({ input, next }: { input: string[]; next: any }) => {
|
|
221
|
-
const results = await Promise.all(
|
|
222
|
-
input.map((csv: string) => csvToJson(csv, options.parseOptions))
|
|
223
|
-
);
|
|
224
|
-
return next({ input: results });
|
|
225
|
-
}),
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* tRPC middleware for CSV processing
|
|
231
|
-
* Adds CSV utilities to tRPC context
|
|
232
|
-
*/
|
|
233
|
-
export function createCsvMiddleware(options: CsvProcedureOptions = {}) {
|
|
234
|
-
return async ({ ctx, next }: { ctx: TRPCContext; next: any }) => {
|
|
235
|
-
const enhancedCtx = {
|
|
236
|
-
...ctx,
|
|
237
|
-
csv: {
|
|
238
|
-
parse: (csvText: string) => csvToJson(csvText, options),
|
|
239
|
-
parseAsync: async (csvText: string) => await csvToJson(csvText, options),
|
|
240
|
-
generate: (data: any, opts?: JsonToCsvOptions) => jsonToCsv(data, { ...options, ...opts }),
|
|
241
|
-
generateAsync: async (data: any, opts?: JsonToCsvOptions) =>
|
|
242
|
-
await jsonToCsv(data, { ...options, ...opts }),
|
|
243
|
-
}
|
|
244
|
-
};
|
|
245
|
-
|
|
246
|
-
return next({ ctx: enhancedCtx });
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Zod schema for CSV input validation
|
|
252
|
-
* Note: This is a placeholder - in real usage, import zod
|
|
253
|
-
*/
|
|
254
|
-
const z = {
|
|
255
|
-
string: () => ({ _type: 'string' }),
|
|
256
|
-
array: (schema: any) => ({ _type: 'array', schema }),
|
|
257
|
-
any: () => ({ _type: 'any' }),
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
export default {
|
|
261
|
-
createCsvProcedure,
|
|
262
|
-
createAsyncCsvProcedure,
|
|
263
|
-
createCsvResponseProcedure,
|
|
264
|
-
createAsyncCsvResponseProcedure,
|
|
265
|
-
createCsvRouter,
|
|
266
|
-
createCsvMiddleware,
|
|
267
|
-
};
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@jtcsv/trpc",
|
|
3
|
-
"version": "1.0.0",
|
|
4
|
-
"description": "tRPC helper for JTCSV CSV parsing",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"types": "index.d.ts",
|
|
7
|
-
"keywords": [
|
|
8
|
-
"trpc",
|
|
9
|
-
"csv",
|
|
10
|
-
"json",
|
|
11
|
-
"converter",
|
|
12
|
-
"jtcsv"
|
|
13
|
-
],
|
|
14
|
-
"author": "Ruslan Fomenko",
|
|
15
|
-
"license": "MIT",
|
|
16
|
-
"repository": {
|
|
17
|
-
"type": "git",
|
|
18
|
-
"url": "git+https://github.com/Linol-Hamelton/jtcsv.git",
|
|
19
|
-
"directory": "plugins/trpc"
|
|
20
|
-
},
|
|
21
|
-
"bugs": {
|
|
22
|
-
"url": "https://github.com/Linol-Hamelton/jtcsv/issues"
|
|
23
|
-
},
|
|
24
|
-
"homepage": "https://github.com/Linol-Hamelton/jtcsv/tree/main/plugins/trpc#readme",
|
|
25
|
-
"peerDependencies": {
|
|
26
|
-
"@trpc/server": "^10.0.0",
|
|
27
|
-
"jtcsv": "^2.1.3"
|
|
28
|
-
},
|
|
29
|
-
"files": [
|
|
30
|
-
"index.js",
|
|
31
|
-
"index.d.ts",
|
|
32
|
-
"README.md"
|
|
33
|
-
]
|
|
34
|
-
}
|