diff --git a/types/jszip/index.d.ts b/types/jszip/index.d.ts index b44d2bc538..160c236ece 100644 --- a/types/jszip/index.d.ts +++ b/types/jszip/index.d.ts @@ -1,7 +1,11 @@ // Type definitions for JSZip // Project: http://stuk.github.com/jszip/ -// Definitions by: mzeiher +// Definitions by: mzeiher , forabi // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + + +/// interface JSZip { files: {[key: string]: JSZipObject}; @@ -26,11 +30,11 @@ interface JSZip { * Add a file to the archive * * @param path Relative path to file - * @param content Content of the file + * @param data Content of the file * @param options Optional information about the file * @return JSZip object */ - file(path: string, data: any, options?: JSZipFileOptions): JSZip; + file(path: string, data: InputByType[T] | Promise, options?: JSZipFileOptions): JSZip; /** * Return an new JSZip instance with the given folder as root @@ -72,26 +76,22 @@ interface JSZip { remove(path: string): JSZip; /** - * @deprecated since version 3.0 - * @see {@link generateAsync} - * http://stuk.github.io/jszip/documentation/upgrade_guide.html + * Generates a new archive asynchronously + * + * @param options Optional options for the generator + * @param onUpdate The optional function called on each internal update with the metadata. + * @return The serialized archive */ - generate(options?: JSZipGeneratorOptions): any; + generateAsync(options?: JSZipGeneratorOptions, onUpdate?: OnUpdateCallback): Promise; /** * Generates a new archive asynchronously * * @param options Optional options for the generator - * @return The serialized archive + * @param onUpdate The optional function called on each internal update with the metadata. + * @return A Node.js `ReadableStream` */ - generateAsync(options?: JSZipGeneratorOptions, onUpdate?: Function): Promise; - - /** - * @deprecated since version 3.0 - * @see {@link loadAsync} - * http://stuk.github.io/jszip/documentation/upgrade_guide.html - */ - load(): void; + generateNodeStream(options?: JSZipGeneratorOptions<'nodebuffer'>, onUpdate?: OnUpdateCallback): NodeJS.ReadStream; /** * Deserialize zip file asynchronously @@ -100,44 +100,65 @@ interface JSZip { * @param options Options for deserializing * @return Returns promise */ - loadAsync(data: any, options?: JSZipLoadOptions): Promise; + loadAsync(data: InputFileFormat, options?: JSZipLoadOptions): Promise; } -type Serialization = ("string" | "text" | "base64" | "binarystring" | "uint8array" | - "arraybuffer" | "blob" | "nodebuffer"); +type Compression = 'STORE' | 'DEFLATE'; + +type ReadFileFormat = 'base64' | 'string' | 'text' | 'binarystring' | 'array' | 'unit8array' | 'arraybuffer' | 'blob' | 'nodebuffer'; + +type InputByType = { + base64: string; + string: string; + text: string; + binarystring: string; + array: Array; + unit8array: Uint8Array; + arraybuffer: ArrayBuffer; + blob: Blob; +} + +type InputType = keyof InputByType; + +type InputFileFormat = string | ArrayBuffer | Uint8Array | Buffer | Blob | NodeJS.ReadableStream; + +type OutputByType = { + base64: string; + text: string; + binarystring: string; + array: Array; + unit8array: Uint8Array; + arraybuffer: ArrayBuffer; + blob: Blob; + nodebuffer: Buffer; +} +type OutputType = keyof OutputByType; + +type Metadata = { + percent: number; + currentFile: string; +}; + +type OnUpdateCallback = (metadata: Metadata) => void; interface JSZipObject { name: string; dir: boolean; date: Date; comment: string; + /** The UNIX permissions of the file, if any. */ + unixPermissions: number | string | null; + /** The UNIX permissions of the file, if any. */ + dosPermissions: number | null; options: JSZipObjectOptions; /** * Prepare the content in the asked type. * @param {String} type the type of the result. - * @param {Function} onUpdate a function to call on each internal update. + * @param {OnUpdateCallback} onUpdate a function to call on each internal update. * @return Promise the promise of the result. */ - async(type: Serialization, onUpdate?: Function): Promise; - - /** - * @deprecated since version 3.0 - */ - asText(): void; - /** - * @deprecated since version 3.0 - */ - asBinary(): void; - /** - * @deprecated since version 3.0 - */ - asArrayBuffer(): void; - /** - * @deprecated since version 3.0 - */ - asUint8Array(): void; - //asNodeBuffer(): void; + async(type: T, onUpdate?: OnUpdateCallback): Promise; } interface JSZipFileOptions { @@ -149,37 +170,38 @@ interface JSZipFileOptions { optimizedBinaryString?: boolean; createFolders?: boolean; dir?: boolean; + + /** 6 bits number. The DOS permissions of the file, if any. */ + dosPermissions?: number | null; + /** + * 16 bits number. The UNIX permissions of the file, if any. + * Also accepts a `string` representing the octal value: `"644"`, `"755"`, etc. + */ + unixPermissions?: number | string | null; } interface JSZipObjectOptions { - /** deprecated */ - base64: boolean; - /** deprecated */ - binary: boolean; - /** deprecated */ - dir: boolean; - /** deprecated */ - date: Date; - compression: string; + compression: Compression; } -interface JSZipGeneratorOptions { - /** deprecated */ - base64?: boolean; - /** DEFLATE or STORE */ - compression?: string; - /** base64 (default), string, uint8array, arraybuffer, blob */ - type?: string; +interface JSZipGeneratorOptions { + compression?: Compression; + compressionOptions?: null | { + level: number; + } + type?: T; comment?: string; /** * mime-type for the generated file. * Useful when you need to generate a file with a different extension, ie: “.ods”. + * @default 'application/zip' */ mimeType?: string; - /** streaming uses less memory */ + encodeFileName?: (filename: string) => string; + /** Stream the files and create file descriptors */ streamFiles?: boolean; /** DOS (default) or UNIX */ - platform?: string; + platform?: 'DOS' | 'UNIX'; } interface JSZipLoadOptions { @@ -208,7 +230,7 @@ declare var JSZip: { * @param data Serialized zip archive * @param options Description of the serialized zip archive */ - (data: any, options?: JSZipLoadOptions): JSZip; + (data: InputFileFormat, options?: JSZipLoadOptions): JSZip; /** * Create JSZip instance @@ -221,10 +243,14 @@ declare var JSZip: { * @param data Serialized zip archive * @param options Description of the serialized zip archive */ - new (data: any, options?: JSZipLoadOptions): JSZip; + new (data: InputFileFormat, options?: JSZipLoadOptions): JSZip; prototype: JSZip; support: JSZipSupport; + external: { + Promise: PromiseConstructorLike; + }; + version: string; } declare module "jszip" { diff --git a/types/jszip/jszip-tests.ts b/types/jszip/jszip-tests.ts index f7a4b9f10f..afbe3cb585 100644 --- a/types/jszip/jszip-tests.ts +++ b/types/jszip/jszip-tests.ts @@ -14,7 +14,7 @@ function createTestZip(): JSZip { return zip } -function filterWithFileAsync(zip: JSZip, as: Serialization, +function filterWithFileAsync(zip: JSZip, as: OutputType, cb: (relativePath: string, file: JSZipObject, value: any) => boolean) : Promise { var promises: Promise[] = []; @@ -124,6 +124,7 @@ function testJSZipRemove() { }).catch((e: any) => log(SEVERITY.ERROR, e)); } + function log(severity:number, message: any) { var log = ""; switch(severity) {