diff --git a/docs/parsers/whatwg-async-parser.md b/docs/parsers/whatwg-async-parser.md index ffd795b..8e69e74 100644 --- a/docs/parsers/whatwg-async-parser.md +++ b/docs/parsers/whatwg-async-parser.md @@ -62,11 +62,6 @@ const csv = await parser.parse(data).promise(); // The parse method return the stream transform readable side. // So data can be passed to a writable stream (a file, http request, etc.) parser.parse(data).pipeTo(writableStream); - -// You can also listen for events on the conversion and see how the header or the lines are coming out. -parser - .addEventListener('header', (header) => console.log(header)) - .addEventListener('line', (line) => console.log(line)); ``` ### Parameters diff --git a/packages/whatwg/README.md b/packages/whatwg/README.md index dc6e2af..50a91c2 100644 --- a/packages/whatwg/README.md +++ b/packages/whatwg/README.md @@ -172,13 +172,13 @@ To facilitate usage, `WHATWGAsyncParser` wraps `WHATWGTransformStream` exposing ### Usage ```js -import { TransformStream } from '@json2csv/whatwg'; +import { AsyncParser } from '@json2csv/whatwg'; const opts = {}; const asyncOpts = {}; const writableStrategy = {}; const readableStrategy = {}; -const parser = new TransformStream(opts, asyncOpts, writableStrategy, readableStrategy); +const parser = new AsyncParser(opts, asyncOpts, writableStrategy, readableStrategy); const response = await fetch('./my-file.json'); @@ -187,11 +187,6 @@ await response.body.pipeThrough(parser).pipeTo(writableStream); // The parse method return the stream transform readable side. // So data can be passed to a writable stream (a file, http request, etc.) parser.parse(data).pipeTo(writableStream); - -// You can also listen for events on the conversion and see how the header or the lines are coming out. -parser - .addEventListener('header', (header) => console.log(header)) - .addEventListener('line', (line) => console.log(line)); ``` #### Parameters diff --git a/packages/whatwg/src/AsyncParser.ts b/packages/whatwg/src/AsyncParser.ts index b05c147..61a28b4 100644 --- a/packages/whatwg/src/AsyncParser.ts +++ b/packages/whatwg/src/AsyncParser.ts @@ -1,5 +1,7 @@ import type { ParserOptions, StreamParserOptions } from '@json2csv/plainjs'; -import JSON2CSVWHATWGTransformStream from './TransformStream.js'; +import JSON2CSVWHATWGTransformStream, { + type AwaitableReadableStream, +} from './TransformStream.js'; export default class JSON2CSVNodeAsyncParser< TRaw extends object, @@ -36,7 +38,7 @@ export default class JSON2CSVNodeAsyncParser< | AsyncIterable | TRaw | ReadableStream, - ) { + ): AwaitableReadableStream { if (typeof data === 'string' || ArrayBuffer.isView(data)) { data = new ReadableStream({ start(controller) { @@ -76,6 +78,6 @@ export default class JSON2CSVNodeAsyncParser< this.writableStrategy, this.readableStrategy, ); - return data.pipeThrough(transform); + return data.pipeThrough(transform) as AwaitableReadableStream; } } diff --git a/packages/whatwg/src/TransformStream.ts b/packages/whatwg/src/TransformStream.ts index c9e15b0..811fe37 100644 --- a/packages/whatwg/src/TransformStream.ts +++ b/packages/whatwg/src/TransformStream.ts @@ -42,7 +42,7 @@ class JSON2CSVWHATWGTransformer } } -interface AwaitableReadableStream extends ReadableStream { +export interface AwaitableReadableStream extends ReadableStream { promise(): Promise; } diff --git a/packages/whatwg/src/index.ts b/packages/whatwg/src/index.ts index bd64449..b700aae 100644 --- a/packages/whatwg/src/index.ts +++ b/packages/whatwg/src/index.ts @@ -1,3 +1,6 @@ export type { ParserOptions, StreamParserOptions } from '@json2csv/plainjs'; export { default as AsyncParser } from './AsyncParser.js'; -export { default as TransformStream } from './TransformStream.js'; +export { + default as TransformStream, + type AwaitableReadableStream, +} from './TransformStream.js'; diff --git a/packages/whatwg/test/AsyncParser.ts b/packages/whatwg/test/AsyncParser.ts index 41ce180..12672cc 100644 --- a/packages/whatwg/test/AsyncParser.ts +++ b/packages/whatwg/test/AsyncParser.ts @@ -23,11 +23,11 @@ async function parseInput( | ReadableStream | Readable, ): Promise { - return ( - (await parser.parse( + return parser + .parse( nodeStream instanceof Readable ? Readable.toWeb(nodeStream) : nodeStream, - )) as any - ).promise(); + ) + .promise(); } export default function ( diff --git a/packages/whatwg/test/AsyncParserInMemory.ts b/packages/whatwg/test/AsyncParserInMemory.ts index 41ce180..12672cc 100644 --- a/packages/whatwg/test/AsyncParserInMemory.ts +++ b/packages/whatwg/test/AsyncParserInMemory.ts @@ -23,11 +23,11 @@ async function parseInput( | ReadableStream | Readable, ): Promise { - return ( - (await parser.parse( + return parser + .parse( nodeStream instanceof Readable ? Readable.toWeb(nodeStream) : nodeStream, - )) as any - ).promise(); + ) + .promise(); } export default function (