index.d.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Type definitions for JSZip 3.1
  2. // Project: http://stuk.github.com/jszip/, https://github.com/stuk/jszip
  3. // Definitions by: mzeiher <https://github.com/mzeiher>, forabi <https://github.com/forabi>
  4. // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
  5. // TypeScript Version: 2.3
  6. /// <reference types="node" />
  7. interface JSZipSupport {
  8. arraybuffer: boolean;
  9. uint8array: boolean;
  10. blob: boolean;
  11. nodebuffer: boolean;
  12. }
  13. type Compression = 'STORE' | 'DEFLATE';
  14. interface Metadata {
  15. percent: number;
  16. currentFile: string;
  17. }
  18. type OnUpdateCallback = (metadata: Metadata) => void;
  19. interface InputByType {
  20. base64: string;
  21. string: string;
  22. text: string;
  23. binarystring: string;
  24. array: number[];
  25. uint8array: Uint8Array;
  26. arraybuffer: ArrayBuffer;
  27. blob: Blob;
  28. stream: NodeJS.ReadableStream;
  29. }
  30. interface OutputByType {
  31. base64: string;
  32. string: string;
  33. text: string;
  34. binarystring: string;
  35. array: number[];
  36. uint8array: Uint8Array;
  37. arraybuffer: ArrayBuffer;
  38. blob: Blob;
  39. nodebuffer: Buffer;
  40. }
  41. // This private `_data` property on a JSZipObject uses this interface.
  42. // If/when it is made public this should be uncommented.
  43. // interface CompressedObject {
  44. // compressedSize: number;
  45. // uncompressedSize: number;
  46. // crc32: number;
  47. // compression: object;
  48. // compressedContent: string|ArrayBuffer|Uint8Array|Buffer;
  49. // }
  50. type InputFileFormat = InputByType[keyof InputByType];
  51. declare namespace JSZip {
  52. type InputType = keyof InputByType;
  53. type OutputType = keyof OutputByType;
  54. interface JSZipObject {
  55. name: string;
  56. dir: boolean;
  57. date: Date;
  58. comment: string;
  59. /** The UNIX permissions of the file, if any. */
  60. unixPermissions: number | string | null;
  61. /** The UNIX permissions of the file, if any. */
  62. dosPermissions: number | null;
  63. options: JSZipObjectOptions;
  64. /**
  65. * Prepare the content in the asked type.
  66. * @param type the type of the result.
  67. * @param onUpdate a function to call on each internal update.
  68. * @return Promise the promise of the result.
  69. */
  70. async<T extends OutputType>(type: T, onUpdate?: OnUpdateCallback): Promise<OutputByType[T]>;
  71. nodeStream(type?: 'nodebuffer', onUpdate?: OnUpdateCallback): NodeJS.ReadableStream;
  72. }
  73. interface JSZipFileOptions {
  74. /** Set to `true` if the data is `base64` encoded. For example image data from a `<canvas>` element. Plain text and HTML do not need this option. */
  75. base64?: boolean;
  76. /**
  77. * Set to `true` if the data should be treated as raw content, `false` if this is a text. If `base64` is used,
  78. * this defaults to `true`, if the data is not a `string`, this will be set to `true`.
  79. */
  80. binary?: boolean;
  81. /**
  82. * The last modification date, defaults to the current date.
  83. */
  84. date?: Date;
  85. compression?: string;
  86. comment?: string;
  87. /** Set to `true` if (and only if) the input is a "binary string" and has already been prepared with a `0xFF` mask. */
  88. optimizedBinaryString?: boolean;
  89. /** 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. */
  90. createFolders?: boolean;
  91. /** Set to `true` if this is a directory and content should be ignored. */
  92. dir?: boolean;
  93. /** 6 bits number. The DOS permissions of the file, if any. */
  94. dosPermissions?: number | null;
  95. /**
  96. * 16 bits number. The UNIX permissions of the file, if any.
  97. * Also accepts a `string` representing the octal value: `"644"`, `"755"`, etc.
  98. */
  99. unixPermissions?: number | string | null;
  100. }
  101. interface JSZipObjectOptions {
  102. compression: Compression;
  103. }
  104. interface JSZipGeneratorOptions<T extends OutputType = OutputType> {
  105. compression?: Compression;
  106. compressionOptions?: null | {
  107. level: number;
  108. };
  109. type?: T;
  110. comment?: string;
  111. /**
  112. * mime-type for the generated file.
  113. * Useful when you need to generate a file with a different extension, ie: “.ods”.
  114. * @default 'application/zip'
  115. */
  116. mimeType?: string;
  117. encodeFileName?(filename: string): string;
  118. /** Stream the files and create file descriptors */
  119. streamFiles?: boolean;
  120. /** DOS (default) or UNIX */
  121. platform?: 'DOS' | 'UNIX';
  122. }
  123. interface JSZipLoadOptions {
  124. base64?: boolean;
  125. checkCRC32?: boolean;
  126. optimizedBinaryString?: boolean;
  127. createFolders?: boolean;
  128. }
  129. }
  130. interface JSZip {
  131. files: {[key: string]: JSZip.JSZipObject};
  132. /**
  133. * Get a file from the archive
  134. *
  135. * @param Path relative path to file
  136. * @return File matching path, null if no file found
  137. */
  138. file(path: string): JSZip.JSZipObject | null;
  139. /**
  140. * Get files matching a RegExp from archive
  141. *
  142. * @param path RegExp to match
  143. * @return Return all matching files or an empty array
  144. */
  145. file(path: RegExp): JSZip.JSZipObject[];
  146. /**
  147. * Add a file to the archive
  148. *
  149. * @param path Relative path to file
  150. * @param data Content of the file
  151. * @param options Optional information about the file
  152. * @return JSZip object
  153. */
  154. file<T extends JSZip.InputType>(path: string, data: InputByType[T] | Promise<InputByType[T]>, options?: JSZip.JSZipFileOptions): this;
  155. file<T extends JSZip.InputType>(path: string, data: null, options?: JSZip.JSZipFileOptions & { dir: true }): this;
  156. /**
  157. * Returns an new JSZip instance with the given folder as root
  158. *
  159. * @param name Name of the folder
  160. * @return New JSZip object with the given folder as root or null
  161. */
  162. folder(name: string): JSZip | null;
  163. /**
  164. * Returns new JSZip instances with the matching folders as root
  165. *
  166. * @param name RegExp to match
  167. * @return New array of JSZipFile objects which match the RegExp
  168. */
  169. folder(name: RegExp): JSZip.JSZipObject[];
  170. /**
  171. * Call a callback function for each entry at this folder level.
  172. *
  173. * @param callback function
  174. */
  175. forEach(callback: (relativePath: string, file: JSZip.JSZipObject) => void): void;
  176. /**
  177. * Get all files which match the given filter function
  178. *
  179. * @param predicate Filter function
  180. * @return Array of matched elements
  181. */
  182. filter(predicate: (relativePath: string, file: JSZip.JSZipObject) => boolean): JSZip.JSZipObject[];
  183. /**
  184. * Removes the file or folder from the archive
  185. *
  186. * @param path Relative path of file or folder
  187. * @return Returns the JSZip instance
  188. */
  189. remove(path: string): JSZip;
  190. /**
  191. * Generates a new archive asynchronously
  192. *
  193. * @param options Optional options for the generator
  194. * @param onUpdate The optional function called on each internal update with the metadata.
  195. * @return The serialized archive
  196. */
  197. generateAsync<T extends JSZip.OutputType>(options?: JSZip.JSZipGeneratorOptions<T>, onUpdate?: OnUpdateCallback): Promise<OutputByType[T]>;
  198. /**
  199. * Generates a new archive asynchronously
  200. *
  201. * @param options Optional options for the generator
  202. * @param onUpdate The optional function called on each internal update with the metadata.
  203. * @return A Node.js `ReadableStream`
  204. */
  205. generateNodeStream(options?: JSZip.JSZipGeneratorOptions<'nodebuffer'>, onUpdate?: OnUpdateCallback): NodeJS.ReadableStream;
  206. /**
  207. * Deserialize zip file asynchronously
  208. *
  209. * @param data Serialized zip file
  210. * @param options Options for deserializing
  211. * @return Returns promise
  212. */
  213. loadAsync(data: InputFileFormat, options?: JSZip.JSZipLoadOptions): Promise<JSZip>;
  214. /**
  215. * Create JSZip instance
  216. */
  217. /**
  218. * Create JSZip instance
  219. * If no parameters given an empty zip archive will be created
  220. *
  221. * @param data Serialized zip archive
  222. * @param options Description of the serialized zip archive
  223. */
  224. new (data?: InputFileFormat, options?: JSZip.JSZipLoadOptions): this;
  225. (): JSZip;
  226. prototype: JSZip;
  227. support: JSZipSupport;
  228. external: {
  229. Promise: PromiseConstructorLike;
  230. };
  231. version: string;
  232. }
  233. declare var JSZip: JSZip;
  234. export = JSZip;