From 983c4d5919cbc747cbbef4fcfbd47be6448c3f36 Mon Sep 17 00:00:00 2001 From: Stuart Knightley Date: Mon, 4 Apr 2022 17:55:43 -0700 Subject: [PATCH] Amend generateInternalStream typings to remove any Fixes #423 #697 --- .../api_jszip/generate_internal_stream.md | 13 +++----- index.d.ts | 31 ++++++++++--------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/documentation/api_jszip/generate_internal_stream.md b/documentation/api_jszip/generate_internal_stream.md index 78fde7ad..11cb5dd4 100644 --- a/documentation/api_jszip/generate_internal_stream.md +++ b/documentation/api_jszip/generate_internal_stream.md @@ -21,13 +21,10 @@ __Metadata__ : see [the metadata of `generateAsync()`]({{site.baseurl}}/document ## Examples ```js -zip.generateInternalStream({type:"blob"}).accumulate(function callback(err, content) { - if (err) { - // handle error - } - // see FileSaver.js - saveAs(content, "hello.zip"); -}, function updateCallback(metadata) { - // print progression with metadata.percent and metadata.currentFile +zip +.generateInternalStream({type:"uint8array"}) +.accumulate() +.then(function (data) { + // data contains here the complete zip file as a uint8array (the type asked in generateInternalStream) }); ``` diff --git a/index.d.ts b/index.d.ts index 72cd983c..259e45bc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -169,22 +169,23 @@ declare namespace JSZip { createFolders?: boolean; decodeFileName?: (bytes: string[] | Uint8Array | Buffer) => string; } -} -declare namespace StreamHelper { - type DataEventCallback = (dataChunk: any, metadata: any) => any - type EndEventCallback = () => any - type ErrorEventCallback = (error: Error) => any + interface JSZipMetadata { + percent: number; + currentFile: string; + } + + type DataEventCallback = (dataChunk: T, metadata: JSZipMetadata) => void + type EndEventCallback = () => void + type ErrorEventCallback = (error: Error) => void - interface StreamHelper { + interface JSZipStreamHelper { /** * Register a listener on an event - * - * @param event The name of the event. Only 3 events are supported : data, end and error - * @param callback The function called when the event occurs - * @return The current StreamHelper object, for chaining */ - on(event: 'data' | 'end' | 'error', callback: DataEventCallback | EndEventCallback | ErrorEventCallback); + on(event: 'data', callback: DataEventCallback): this; + on(event: 'end', callback: EndEventCallback): this; + on(event: 'error', callback: ErrorEventCallback): this; /** * Read the whole stream and call a callback with the complete content @@ -192,21 +193,21 @@ declare namespace StreamHelper { * @param updateCallback The function called every time the stream updates * @return A Promise of the full content */ - accumulate(updateCallback?: (metadata: any) => any): Promise; + accumulate(updateCallback?: (metadata: JSZipMetadata) => void): Promise; /** * Resume the stream if the stream is paused. Once resumed, the stream starts sending data events again * * @return The current StreamHelper object, for chaining */ - resume(): StreamHelper; + resume(): this; /** * Pause the stream if the stream is running. Once paused, the stream stops sending data events * * @return The current StreamHelper object, for chaining */ - pause(): StreamHelper; + pause(): this; } } @@ -303,7 +304,7 @@ interface JSZip { * @param options Optional options for the generator * @return a StreamHelper */ - generateInternalStream(options?: JSZip.JSZipGeneratorOptions): StreamHelper.StreamHelper + generateInternalStream(options?: JSZip.JSZipGeneratorOptions): JSZip.JSZipStreamHelper; /** * Deserialize zip file asynchronously