diff --git a/types/jszip/index.d.ts b/types/jszip/index.d.ts index b44d2bc538..7ea6180080 100644 --- a/types/jszip/index.d.ts +++ b/types/jszip/index.d.ts @@ -1,10 +1,140 @@ -// Type definitions for JSZip +// Type definitions for JSZip 3.1 // Project: http://stuk.github.com/jszip/ -// Definitions by: mzeiher +// Definitions by: mzeiher , forabi // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +/// + +interface JSZipSupport { + arraybuffer: boolean; + uint8array: boolean; + blob: boolean; + nodebuffer: boolean; +} + +type Compression = 'STORE' | 'DEFLATE'; + +interface Metadata { + percent: number; + currentFile: string; +} + +type OnUpdateCallback = (metadata: Metadata) => void; + +interface InputByType { + base64: string; + string: string; + text: string; + binarystring: string; + array: number[]; + unit8array: Uint8Array; + arraybuffer: ArrayBuffer; + blob: Blob; +} + +interface OutputByType { + base64: string; + text: string; + binarystring: string; + array: number[]; + unit8array: Uint8Array; + arraybuffer: ArrayBuffer; + blob: Blob; + nodebuffer: Buffer; +} + +type InputFileFormat = InputByType[keyof InputByType] | NodeJS.ReadableStream; + +declare namespace JSZip { + type InputType = keyof InputByType; + + type OutputType = keyof OutputByType; + + 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 {OnUpdateCallback} onUpdate a function to call on each internal update. + * @return Promise the promise of the result. + */ + async(type: T, onUpdate?: OnUpdateCallback): Promise; + nodeStream(type?: 'nodestream', onUpdate?: OnUpdateCallback): NodeJS.ReadableStream; + } + + interface JSZipFileOptions { + /** Set to `true` if the data is `base64` encoded. For example image data from a `` element. Plain text and HTML do not need this option. */ + base64?: boolean; + /** Set to `true` if the data should be treated as raw content, `false` if this is a text. If `base64` is used, + * this defaults to `true`, if the data is not a `string`, this will be set to `true`. + */ + binary?: boolean; + /** + * The last modification date, defaults to the current date. + */ + date?: Date; + compression?: string; + comment?: string; + /** Set to `true` if (and only if) the input is a "binary string" and has already been prepared with a `0xFF` mask. */ + optimizedBinaryString?: boolean; + /** Set to `true` if folders in the file path should be automatically created, otherwise there will only be virtual folders that represent the path to the file. */ + createFolders?: boolean; + /** Set to `true` if this is a directory and content should be ignored. */ + 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 { + compression: Compression; + } + + 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; + encodeFileName?(filename: string): string; + /** Stream the files and create file descriptors */ + streamFiles?: boolean; + /** DOS (default) or UNIX */ + platform?: 'DOS' | 'UNIX'; + } + + interface JSZipLoadOptions { + base64?: boolean; + checkCRC32?: boolean; + optimizedBinaryString?: boolean; + createFolders?: boolean; + } +} interface JSZip { - files: {[key: string]: JSZipObject}; + files: {[key: string]: JSZip.JSZipObject}; /** * Get a file from the archive @@ -12,7 +142,7 @@ interface JSZip { * @param Path relative path to file * @return File matching path, null if no file found */ - file(path: string): JSZipObject; + file(path: string): JSZip.JSZipObject; /** * Get files matching a RegExp from archive @@ -20,20 +150,21 @@ interface JSZip { * @param path RegExp to match * @return Return all matching files or an empty array */ - file(path: RegExp): JSZipObject[]; + file(path: RegExp): JSZip.JSZipObject[]; /** * 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?: JSZip.JSZipFileOptions): this; + file(path: string, data: null, options?: JSZip.JSZipFileOptions & { dir: true }): this; /** - * Return an new JSZip instance with the given folder as root + * Returns an new JSZip instance with the given folder as root * * @param name Name of the folder * @return New JSZip object with the given folder as root or null @@ -46,22 +177,22 @@ interface JSZip { * @param name RegExp to match * @return New array of JSZipFile objects which match the RegExp */ - folder(name: RegExp): JSZipObject[]; + folder(name: RegExp): JSZip.JSZipObject[]; /** * Call a callback function for each entry at this folder level. * * @param callback function */ - forEach(callback: (relativePath: string, file: JSZipObject) => void): void; + forEach(callback: (relativePath: string, file: JSZip.JSZipObject) => void): void; /** - * Get all files wchich match the given filter function + * Get all files which match the given filter function * * @param predicate Filter function * @return Array of matched elements */ - filter(predicate: (relativePath: string, file: JSZipObject) => boolean): JSZipObject[]; + filter(predicate: (relativePath: string, file: JSZip.JSZipObject) => boolean): JSZip.JSZipObject[]; /** * Removes the file or folder from the archive @@ -72,26 +203,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?: JSZip.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?: JSZip.JSZipGeneratorOptions<'nodebuffer'>, onUpdate?: OnUpdateCallback): NodeJS.ReadStream; /** * Deserialize zip file asynchronously @@ -100,133 +227,31 @@ interface JSZip { * @param options Options for deserializing * @return Returns promise */ - loadAsync(data: any, options?: JSZipLoadOptions): Promise; -} + loadAsync(data: InputFileFormat, options?: JSZip.JSZipLoadOptions): Promise; -type Serialization = ("string" | "text" | "base64" | "binarystring" | "uint8array" | - "arraybuffer" | "blob" | "nodebuffer"); - -interface JSZipObject { - name: string; - dir: boolean; - date: Date; - comment: string; - 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. - * @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; -} - -interface JSZipFileOptions { - base64?: boolean; - binary?: boolean; - date?: Date; - compression?: string; - comment?: string; - optimizedBinaryString?: boolean; - createFolders?: boolean; - dir?: boolean; -} - -interface JSZipObjectOptions { - /** deprecated */ - base64: boolean; - /** deprecated */ - binary: boolean; - /** deprecated */ - dir: boolean; - /** deprecated */ - date: Date; - compression: string; -} - -interface JSZipGeneratorOptions { - /** deprecated */ - base64?: boolean; - /** DEFLATE or STORE */ - compression?: string; - /** base64 (default), string, uint8array, arraybuffer, blob */ - type?: string; - comment?: string; - /** - * mime-type for the generated file. - * Useful when you need to generate a file with a different extension, ie: “.ods”. - */ - mimeType?: string; - /** streaming uses less memory */ - streamFiles?: boolean; - /** DOS (default) or UNIX */ - platform?: string; -} - -interface JSZipLoadOptions { - base64?: boolean; - checkCRC32?: boolean; - optimizedBinaryString?: boolean; - createFolders?: boolean; -} - -interface JSZipSupport { - arraybuffer: boolean; - uint8array: boolean; - blob: boolean; - nodebuffer: boolean; -} - -declare var JSZip: { /** * Create JSZip instance */ + + /** + * Create JSZip instance + * If no parameters given an empty zip archive will be created + * + * @param data Serialized zip archive + * @param options Description of the serialized zip archive + */ + new (data?: InputFileFormat, options?: JSZip.JSZipLoadOptions): this; + (): JSZip; - /** - * Create JSZip instance - * If no parameters given an empty zip archive will be created - * - * @param data Serialized zip archive - * @param options Description of the serialized zip archive - */ - (data: any, options?: JSZipLoadOptions): JSZip; - - /** - * Create JSZip instance - */ - new (): JSZip; - /** - * Create JSZip instance - * If no parameters given an empty zip archive will be created - * - * @param data Serialized zip archive - * @param options Description of the serialized zip archive - */ - new (data: any, options?: JSZipLoadOptions): JSZip; prototype: JSZip; support: JSZipSupport; + external: { + Promise: PromiseConstructorLike; + }; + version: string; } -declare module "jszip" { - export = JSZip; -} +declare var JSZip: JSZip; + +export = JSZip; diff --git a/types/jszip/jszip-tests.ts b/types/jszip/jszip-tests.ts index f7a4b9f10f..81ccda09db 100644 --- a/types/jszip/jszip-tests.ts +++ b/types/jszip/jszip-tests.ts @@ -1,32 +1,34 @@ -var SEVERITY = { +import * as JSZip from 'jszip'; + +const SEVERITY = { DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, FATAL: 4 -} +}; function createTestZip(): JSZip { - var zip = new JSZip(); + const zip = new JSZip(); zip.file("test.txt", "test string"); zip.file("test", null, { dir: true }); zip.file("test/test.txt", "test string"); - return zip + return zip; } -function filterWithFileAsync(zip: JSZip, as: Serialization, - cb: (relativePath: string, file: JSZipObject, value: any) => boolean) - : Promise { - var promises: Promise[] = []; - var promiseIndices: {[key: string]: number} = {}; - zip.forEach((relativePath: string, file: JSZipObject) => { - var promise = file.async(as); +function filterWithFileAsync(zip: JSZip, as: JSZip.OutputType, + cb: (relativePath: string, file: JSZip.JSZipObject, value: any) => boolean) + : Promise { + const promises: Array> = []; + const promiseIndices: {[key: string]: number} = {}; + zip.forEach((relativePath: string, file: JSZip.JSZipObject) => { + const promise = file.async(as); promiseIndices[file.name] = promises.length; promises.push(promise); }); - return Promise.all(promises).then(function(values: any[]) { - var filtered = zip.filter((relativePath: string, file: JSZipObject) => { - var index = promiseIndices[file.name]; + return Promise.all(promises).then((values: any[]) => { + const filtered = zip.filter((relativePath: string, file: JSZip.JSZipObject) => { + const index = promiseIndices[file.name]; return cb(relativePath, file, values[index]); }); return Promise.resolve(filtered); @@ -34,12 +36,12 @@ function filterWithFileAsync(zip: JSZip, as: Serialization, } function testJSZip() { - var zip = createTestZip(); - zip.generateAsync({compression: "DEFLATE", type: "base64"}).then(function(serializedZip: any) { - var newJszip = new JSZip(); + const zip = createTestZip(); + zip.generateAsync({compression: "DEFLATE", type: "base64"}).then((serializedZip) => { + const newJszip = new JSZip(); return newJszip.loadAsync(serializedZip, {base64: true/*, checkCRC32: true*/}); - }).then(function(newJszip: JSZip) { - newJszip.file("test.txt").async('text').then(function(text: string) { + }).then((newJszip: JSZip) => { + newJszip.file("test.txt").async('text').then((text: string) => { if (text === "test string") { log(SEVERITY.INFO, "all ok"); } else { @@ -47,7 +49,7 @@ function testJSZip() { } }).catch((e: any) => log(SEVERITY.ERROR, e)); - newJszip.file("test/test.txt").async('text').then(function(text: string) { + newJszip.file("test/test.txt").async('text').then((text: string) => { if (text === "test string") { log(SEVERITY.INFO, "all ok"); } else { @@ -55,20 +57,20 @@ function testJSZip() { } }).catch((e: any) => log(SEVERITY.ERROR, e)); - var folder = newJszip.folder("test"); - folder.file("test.txt").async('text').then(function(text: string) { - if (text == "test string") { + const folder = newJszip.folder("test"); + folder.file("test.txt").async('text').then((text: string) => { + if (text === "test string") { log(SEVERITY.INFO, "all ok"); } else { log(SEVERITY.ERROR, "wrong file"); } }).catch((e: any) => log(SEVERITY.ERROR, e)); - var folders = newJszip.folder(new RegExp("^test")); + const folders = newJszip.folder(new RegExp("^test")); - if (folders.length == 1) { + if (folders.length === 1) { log(SEVERITY.INFO, "all ok"); - if (folders[0].dir == true) { + if (folders[0].dir) { log(SEVERITY.INFO, "all ok"); } else { log(SEVERITY.ERROR, "wrong file"); @@ -77,11 +79,11 @@ function testJSZip() { log(SEVERITY.ERROR, "wrong number of folder"); } - var files = newJszip.file(new RegExp("^test")); - if (files.length == 2) { + const files = newJszip.file(new RegExp("^test")); + if (files.length === 2) { log(SEVERITY.INFO, "all ok"); - Promise.all([files[0].async('text'), files[1].async('text')]).then(function(texts: string[]) { - if (texts[0] == "test string" && texts[1] == 'test string') { + Promise.all([files[0].async('text'), files[1].async('text')]).then((texts: string[]) => { + if (texts[0] === "test string" && texts[1] === 'test string') { log(SEVERITY.INFO, "all ok"); } else { log(SEVERITY.ERROR, "wrong data in files"); @@ -91,32 +93,32 @@ function testJSZip() { log(SEVERITY.ERROR, "wrong number of files"); } - filterWithFileAsync(newJszip, 'text', (relativePath: string, file: JSZipObject, text: string) => { - if (text == "test string") { + filterWithFileAsync(newJszip, 'text', (relativePath: string, file: JSZip.JSZipObject, text: string) => { + if (text === "test string") { return true; } return false; - }).then(function(filterFiles: JSZipObject[]) { - if (filterFiles.length == 2) { + }).then((filterFiles: JSZip.JSZipObject[]) => { + if (filterFiles.length === 2) { log(SEVERITY.INFO, "all ok"); } else { log(SEVERITY.ERROR, "wrong number of files"); } }).catch((e: any) => log(SEVERITY.ERROR, e)); - }).catch((e: any)=> { console.error(e) }); + }).catch((e: any) => { console.error(e); }); } function testJSZipRemove() { - var newJszip = createTestZip(); + const newJszip = createTestZip(); newJszip.remove("test/test.txt"); - filterWithFileAsync(newJszip, 'text', (relativePath: string, file: JSZipObject, text: string) => { - if (text == "test string") { + filterWithFileAsync(newJszip, 'text', (relativePath: string, file: JSZip.JSZipObject, text: string) => { + if (text === "test string") { return true; } return false; - }).then(function(filterFiles: JSZipObject[]) { - if (filterFiles.length == 1) { + }).then((filterFiles: JSZip.JSZipObject[]) => { + if (filterFiles.length === 1) { log(SEVERITY.INFO, "all ok"); } else { log(SEVERITY.ERROR, "wrong number of files"); @@ -124,9 +126,9 @@ function testJSZipRemove() { }).catch((e: any) => log(SEVERITY.ERROR, e)); } -function log(severity:number, message: any) { - var log = ""; - switch(severity) { +function log(severity: number, message: any) { + let log = ""; + switch (severity) { case 0: log += "[DEBUG] "; break; @@ -143,7 +145,7 @@ function log(severity:number, message: any) { log += "[FATAL] "; break; default: - log += "[INFO]" + log += "[INFO]"; break; } console.log(log += message); diff --git a/types/jszip/tsconfig.json b/types/jszip/tsconfig.json index 02f0123b77..80c7836f06 100644 --- a/types/jszip/tsconfig.json +++ b/types/jszip/tsconfig.json @@ -1,13 +1,14 @@ { "compilerOptions": { "module": "commonjs", + "target": "es5", "lib": [ - "es6", + "es2015", "dom" ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" @@ -20,4 +21,4 @@ "index.d.ts", "jszip-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/jszip/tslint.json b/types/jszip/tslint.json new file mode 100644 index 0000000000..71ee04c4e1 --- /dev/null +++ b/types/jszip/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "no-unnecessary-generics": false + } +}