Skip to content

Commit

Permalink
Amend generateInternalStream typings to remove any
Browse files Browse the repository at this point in the history
Fixes #423 #697
  • Loading branch information
Stuk committed Apr 5, 2022
1 parent 454030c commit 983c4d5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
13 changes: 5 additions & 8 deletions documentation/api_jszip/generate_internal_stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
```
31 changes: 16 additions & 15 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,44 +169,45 @@ 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<T> = (dataChunk: T, metadata: JSZipMetadata) => void
type EndEventCallback = () => void
type ErrorEventCallback = (error: Error) => void

interface StreamHelper {
interface JSZipStreamHelper<T> {
/**
* 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<T>): 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
*
* @param updateCallback The function called every time the stream updates
* @return A Promise of the full content
*/
accumulate(updateCallback?: (metadata: any) => any): Promise<any>;
accumulate(updateCallback?: (metadata: JSZipMetadata) => void): Promise<T>;

/**
* 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;
}
}

Expand Down Expand Up @@ -303,7 +304,7 @@ interface JSZip {
* @param options Optional options for the generator
* @return a StreamHelper
*/
generateInternalStream(options?: JSZip.JSZipGeneratorOptions): StreamHelper.StreamHelper
generateInternalStream<T extends JSZip.OutputType>(options?: JSZip.JSZipGeneratorOptions<T>): JSZip.JSZipStreamHelper<OutputByType[T]>;

/**
* Deserialize zip file asynchronously
Expand Down

0 comments on commit 983c4d5

Please sign in to comment.