Skip to content

Commit 983c4d5

Browse files
committed
Amend generateInternalStream typings to remove any
Fixes #423 #697
1 parent 454030c commit 983c4d5

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

documentation/api_jszip/generate_internal_stream.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ __Metadata__ : see [the metadata of `generateAsync()`]({{site.baseurl}}/document
2121
## Examples
2222

2323
```js
24-
zip.generateInternalStream({type:"blob"}).accumulate(function callback(err, content) {
25-
if (err) {
26-
// handle error
27-
}
28-
// see FileSaver.js
29-
saveAs(content, "hello.zip");
30-
}, function updateCallback(metadata) {
31-
// print progression with metadata.percent and metadata.currentFile
24+
zip
25+
.generateInternalStream({type:"uint8array"})
26+
.accumulate()
27+
.then(function (data) {
28+
// data contains here the complete zip file as a uint8array (the type asked in generateInternalStream)
3229
});
3330
```

index.d.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -169,44 +169,45 @@ declare namespace JSZip {
169169
createFolders?: boolean;
170170
decodeFileName?: (bytes: string[] | Uint8Array | Buffer) => string;
171171
}
172-
}
173172

174-
declare namespace StreamHelper {
175-
type DataEventCallback = (dataChunk: any, metadata: any) => any
176-
type EndEventCallback = () => any
177-
type ErrorEventCallback = (error: Error) => any
173+
interface JSZipMetadata {
174+
percent: number;
175+
currentFile: string;
176+
}
177+
178+
type DataEventCallback<T> = (dataChunk: T, metadata: JSZipMetadata) => void
179+
type EndEventCallback = () => void
180+
type ErrorEventCallback = (error: Error) => void
178181

179-
interface StreamHelper {
182+
interface JSZipStreamHelper<T> {
180183
/**
181184
* Register a listener on an event
182-
*
183-
* @param event The name of the event. Only 3 events are supported : data, end and error
184-
* @param callback The function called when the event occurs
185-
* @return The current StreamHelper object, for chaining
186185
*/
187-
on(event: 'data' | 'end' | 'error', callback: DataEventCallback | EndEventCallback | ErrorEventCallback);
186+
on(event: 'data', callback: DataEventCallback<T>): this;
187+
on(event: 'end', callback: EndEventCallback): this;
188+
on(event: 'error', callback: ErrorEventCallback): this;
188189

189190
/**
190191
* Read the whole stream and call a callback with the complete content
191192
*
192193
* @param updateCallback The function called every time the stream updates
193194
* @return A Promise of the full content
194195
*/
195-
accumulate(updateCallback?: (metadata: any) => any): Promise<any>;
196+
accumulate(updateCallback?: (metadata: JSZipMetadata) => void): Promise<T>;
196197

197198
/**
198199
* Resume the stream if the stream is paused. Once resumed, the stream starts sending data events again
199200
*
200201
* @return The current StreamHelper object, for chaining
201202
*/
202-
resume(): StreamHelper;
203+
resume(): this;
203204

204205
/**
205206
* Pause the stream if the stream is running. Once paused, the stream stops sending data events
206207
*
207208
* @return The current StreamHelper object, for chaining
208209
*/
209-
pause(): StreamHelper;
210+
pause(): this;
210211
}
211212
}
212213

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

308309
/**
309310
* Deserialize zip file asynchronously

0 commit comments

Comments
 (0)