Created
November 4, 2021 20:24
-
-
Save peterblazejewicz/9512e5a20d9ab197d955ce19544b1286 to your computer and use it in GitHub Desktop.
Formidable v1/v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/types/formidable/formidable-tests.ts b/types/formidable/formidable-tests.ts | |
index c70026e202..91b8449071 100644 | |
--- a/types/formidable/formidable-tests.ts | |
+++ b/types/formidable/formidable-tests.ts | |
@@ -1,6 +1,7 @@ | |
import formidable = require("formidable"); | |
import { | |
defaultOptions, | |
+ enabledPlugins, | |
plugins, | |
File, | |
formidable as formidableAlias, | |
@@ -19,7 +20,7 @@ const options: Options = { | |
enabledPlugins: [], | |
encoding: "utf-8", | |
fileWriteStreamHandler: undefined, | |
- hash: false, | |
+ hashAlgorithm: false, | |
keepExtensions: false, | |
maxFields: 1000, | |
maxFieldsSize: 20 * 1024 * 1024, | |
@@ -30,23 +31,24 @@ const options: Options = { | |
}; | |
const file: File = { | |
- hash: "sha1", | |
- lastModifiedDate: new Date(), | |
- name: "name", | |
- path: "path", | |
+ hashAlgorithm: false, | |
+ hash: "hash", | |
+ mtime: new Date(), | |
+ originalFilename: "name", | |
+ newFilename: "newfilename", | |
+ filepath: "path", | |
size: 20, | |
- type: "json", | |
+ mimetype: "json", | |
toJSON: () => ({ | |
- filename: file.name!, | |
+ newFilename: file.newFilename!, | |
length: 10, | |
- mime: "string", | |
- mtime: file.lastModifiedDate!, | |
- name: file.name, | |
- path: file.path, | |
- size: file.size, | |
- type: file.type, | |
+ mimetype: file.mimetype, | |
+ mtime: file.mtime!, | |
+ originalFilename: file.originalFilename, | |
+ filepath: file.filepath, | |
+ size: file.size | |
}), | |
- toString: () => `File: ${file.name}`, | |
+ toString: () => `File: ${file.originalFilename}`, | |
}; | |
// act/assert | |
@@ -65,6 +67,9 @@ Formidable.DEFAULT_OPTIONS; | |
defaultOptions; | |
defaultOptions.enabledPlugins; // $ExpectType EnabledPlugins | |
+// $ExpectType EnabledPlugins | |
+enabledPlugins; | |
+ | |
// $ExpectType EnabledPlugins | |
plugins; | |
diff --git a/types/formidable/index.d.ts b/types/formidable/index.d.ts | |
index 249da05fdf..fb49b66e0a 100644 | |
--- a/types/formidable/index.d.ts | |
+++ b/types/formidable/index.d.ts | |
@@ -1,4 +1,4 @@ | |
-// Type definitions for formidable 1.2 | |
+// Type definitions for formidable 2.0 | |
// Project: https://github.com/node-formidable/formidable | |
// Definitions by: Wim Looman <https://github.com/Nemo157> | |
// Martin Badin <https://github.com/martin-badin> | |
@@ -146,7 +146,7 @@ declare namespace formidable { | |
* | |
* @default false | |
*/ | |
- hash?: string | false | undefined; | |
+ hashAlgorithm?: string | false | undefined; | |
/** | |
* which by default writes to host machine file system every file parsed; The function should | |
@@ -170,6 +170,14 @@ declare namespace formidable { | |
*/ | |
multiples?: boolean | undefined; | |
+ /** | |
+ * Use it to control newFilename. Must return a string. Will be joined with | |
+ * options.uploadDir. | |
+ * | |
+ * @default undefined | |
+ */ | |
+ filename?: (name: string, ext: string, part: string, form: string) => string | undefined; | |
+ | |
enabledPlugins?: string[] | undefined; | |
} | |
@@ -190,10 +198,9 @@ declare namespace formidable { | |
/** | |
* @link https://github.com/node-formidable/formidable#file | |
*/ | |
- interface FileJSON extends Pick<File, "size" | "path" | "name" | "type" | "hash"> { | |
- filename: string; | |
+ interface FileJSON extends Pick<File, "size" | "filepath" | "originalFilename" | "mimetype" | "hash"> { | |
length: number; | |
- mime: string; | |
+ mimetype: string | null; | |
mtime: Date | null; | |
} | |
@@ -208,28 +215,36 @@ declare namespace formidable { | |
* The path this file is being written to. You can modify this in the `'fileBegin'` event in case | |
* you are unhappy with the way formidable generates a temporary path for your files. | |
*/ | |
- path: string; | |
+ filepath: string; | |
/** | |
* The name this file had according to the uploading client. | |
*/ | |
- name: string | null; | |
+ originalFilename: string | null; | |
+ | |
+ /** | |
+ * Calculated based on options provided | |
+ */ | |
+ newFilename: string | null; | |
/** | |
* The mime type of this file, according to the uploading client. | |
*/ | |
- type: string | null; | |
+ mimetype: string | null; | |
/** | |
* A Date object (or `null`) containing the time this file was last written to. Mostly here for | |
* compatibility with the [W3C File API Draft](http://dev.w3.org/2006/webapi/FileAPI/). | |
*/ | |
- lastModifiedDate?: Date | null | undefined; | |
+ mtime?: Date | null | undefined; | |
+ | |
+ hashAlgorithm: false | "sha1" | "md5" | "sha256"; | |
/** | |
- * If `options.hash` calculation was set, you can read the hex digest out of this var. | |
+ * If `options.hashAlgorithm` calculation was set, you can read the hex digest out of this var | |
+ * (at the end it will be a string). | |
*/ | |
- hash?: string | "sha1" | "md5" | "sha256" | null | undefined; | |
+ hash?: string | null; | |
/** | |
* This method returns a JSON-representation of the file, allowing you to JSON.stringify() the | |
@@ -281,6 +296,7 @@ declare namespace formidable { | |
declare const formidable: { | |
(options?: formidable.Options): Formidable; | |
defaultOptions: formidable.DefaultOptions; | |
+ enabledPlugins: formidable.EnabledPlugins; | |
plugins: formidable.EnabledPlugins; | |
errors: typeof errors; | |
File: typeof PersistentFile; | |
diff --git a/types/formidable/v1/Formidable.d.ts b/types/formidable/v1/Formidable.d.ts | |
new file mode 100644 | |
index 0000000000..1ba6c913d3 | |
--- /dev/null | |
+++ b/types/formidable/v1/Formidable.d.ts | |
@@ -0,0 +1,46 @@ | |
+import { IncomingMessage } from "http"; | |
+import { EmitData, EventData, Fields, File, Files, Options, Part, PluginFunction, DefaultOptions } from "./"; | |
+declare class IncomingForm { | |
+ static readonly DEFAULT_OPTIONS: DefaultOptions; | |
+ constructor(options?: Partial<Options>); | |
+ | |
+ /** | |
+ * Parses an incoming Node.js request containing form data. If callback is provided, all fields | |
+ * and files are collected and passed to the callback. | |
+ * | |
+ * @link https://github.com/node-formidable/formidable#parserequest-callback | |
+ */ | |
+ parse(request: IncomingMessage, callback: (err: any, fields: Fields, files: Files) => void): void; | |
+ | |
+ once(name: "end", callback: () => void): void; | |
+ once(name: "error", callback: (err: any) => void): void; | |
+ | |
+ on(name: "data", callback: (data: EventData) => void): void; | |
+ on(name: "error", callback: (err: any) => void): void; | |
+ on(name: "field", callback: (name: string, value: string) => void): void; | |
+ on(name: "fileBegin" | "file", callback: (formName: string, file: File) => void): void; | |
+ on(name: "progress", callback: (bytesReceived: number, bytesExpected: number) => void): void; | |
+ on(name: string, callback: () => void): void; | |
+ | |
+ emit(name: "data", data: EmitData): void; | |
+ | |
+ /** | |
+ * A method that allows you to extend the Formidable library. By default we include 4 plugins, | |
+ * which essentially are adapters to plug the different built-in parsers. | |
+ * | |
+ * @link https://github.com/node-formidable/formidable#useplugin-plugin | |
+ */ | |
+ use(plugin: PluginFunction): void; | |
+ | |
+ /** | |
+ * If you want to use Formidable to only handle certain parts for you, you can do something | |
+ * similar. Or see #387 for inspiration, you can for example validate the mime-type. | |
+ * | |
+ * @link https://github.com/node-formidable/formidable#formonpart | |
+ */ | |
+ onPart(part: Part): void; | |
+ | |
+ handlePart(part: Part): void; | |
+} | |
+ | |
+export = IncomingForm; | |
diff --git a/types/formidable/v1/FormidableError.d.ts b/types/formidable/v1/FormidableError.d.ts | |
new file mode 100644 | |
index 0000000000..8adc61ab83 | |
--- /dev/null | |
+++ b/types/formidable/v1/FormidableError.d.ts | |
@@ -0,0 +1,28 @@ | |
+declare class FormidableError extends Error { | |
+ internalCode: number; | |
+ httpCode?: number | undefined; | |
+ constructor(message: string, internalCode: number, httpCode?: number); | |
+} | |
+ | |
+declare const errors: { | |
+ FormidableError: typeof FormidableError; | |
+} & Record< | |
+ | "missingPlugin" | |
+ | "pluginFunction" | |
+ | "aborted" | |
+ | "noParser" | |
+ | "uninitializedParser" | |
+ | "filenameNotString" | |
+ | "maxFieldsSizeExceeded" | |
+ | "maxFieldsExceeded" | |
+ | "smallerThanMinFileSize" | |
+ | "biggerThanMaxFileSize" | |
+ | "noEmptyFiles" | |
+ | "missingContentType" | |
+ | "malformedMultipart" | |
+ | "missingMultipartBoundary" | |
+ | "unknownTransferEncoding", | |
+ number | |
+>; | |
+ | |
+export = errors; | |
diff --git a/types/formidable/v1/PersistentFile.d.ts b/types/formidable/v1/PersistentFile.d.ts | |
new file mode 100644 | |
index 0000000000..caae6c2788 | |
--- /dev/null | |
+++ b/types/formidable/v1/PersistentFile.d.ts | |
@@ -0,0 +1,14 @@ | |
+import { EventEmitter } from 'events'; | |
+import { File, FileJSON } from "./"; | |
+ | |
+declare class PersistentFile extends EventEmitter { | |
+ constructor(properties: File); | |
+ open(): void; | |
+ toJSON(): FileJSON; | |
+ toString(): string; | |
+ write(buffer: string, cb: () => void): void; | |
+ end(cb: () => void): void; | |
+ destroy(): void; | |
+} | |
+ | |
+export = PersistentFile; | |
diff --git a/types/formidable/v1/VolatileFile.d.ts b/types/formidable/v1/VolatileFile.d.ts | |
new file mode 100644 | |
index 0000000000..be3fc80c43 | |
--- /dev/null | |
+++ b/types/formidable/v1/VolatileFile.d.ts | |
@@ -0,0 +1,5 @@ | |
+import PersistentFile = require("./PersistentFile"); | |
+ | |
+declare const VolatileFile: typeof PersistentFile; | |
+ | |
+export = VolatileFile; | |
diff --git a/types/formidable/v1/formidable-tests.ts b/types/formidable/v1/formidable-tests.ts | |
new file mode 100644 | |
index 0000000000..c70026e202 | |
--- /dev/null | |
+++ b/types/formidable/v1/formidable-tests.ts | |
@@ -0,0 +1,188 @@ | |
+import formidable = require("formidable"); | |
+import { | |
+ defaultOptions, | |
+ plugins, | |
+ File, | |
+ formidable as formidableAlias, | |
+ Formidable, | |
+ IncomingForm, | |
+ MultipartParser, | |
+ Options, | |
+ PersistentFile, | |
+ VolatileFile, | |
+} from "formidable"; | |
+import * as http from "http"; | |
+ | |
+// arrange | |
+const options: Options = { | |
+ allowEmptyFiles: true, | |
+ enabledPlugins: [], | |
+ encoding: "utf-8", | |
+ fileWriteStreamHandler: undefined, | |
+ hash: false, | |
+ keepExtensions: false, | |
+ maxFields: 1000, | |
+ maxFieldsSize: 20 * 1024 * 1024, | |
+ maxFileSize: 200 * 1024 * 1024, | |
+ minFileSize: 1, | |
+ multiples: false, | |
+ uploadDir: "/dir", | |
+}; | |
+ | |
+const file: File = { | |
+ hash: "sha1", | |
+ lastModifiedDate: new Date(), | |
+ name: "name", | |
+ path: "path", | |
+ size: 20, | |
+ type: "json", | |
+ toJSON: () => ({ | |
+ filename: file.name!, | |
+ length: 10, | |
+ mime: "string", | |
+ mtime: file.lastModifiedDate!, | |
+ name: file.name, | |
+ path: file.path, | |
+ size: file.size, | |
+ type: file.type, | |
+ }), | |
+ toString: () => `File: ${file.name}`, | |
+}; | |
+ | |
+// act/assert | |
+formidable(options); // $ExpectType IncomingForm | |
+ | |
+formidableAlias(options); // $ExpectType IncomingForm | |
+ | |
+new IncomingForm(options); // $ExpectType IncomingForm | |
+ | |
+new Formidable(options); // $ExpectType IncomingForm | |
+ | |
+// $ExpectType DefaultOptions | |
+Formidable.DEFAULT_OPTIONS; | |
+ | |
+// $ExpectType DefaultOptions | |
+defaultOptions; | |
+defaultOptions.enabledPlugins; // $ExpectType EnabledPlugins | |
+ | |
+// $ExpectType EnabledPlugins | |
+plugins; | |
+ | |
+// $ExpectType PersistentFile | |
+new VolatileFile(file); | |
+ | |
+// $ExpectType PersistentFile | |
+new PersistentFile(file); | |
+ | |
+// $ExpectType PersistentFile | |
+new File(file); | |
+ | |
+MultipartParser.stateToString; | |
+ | |
+MultipartParser.STATES; | |
+ | |
+const form = new Formidable(options); | |
+form.on("data", data => { | |
+ // $ExpectType EventData | |
+ data; | |
+ | |
+ const { name, key, value, buffer, start, end, formname } = data; | |
+ | |
+ // $ExpectType EventNames | |
+ name; | |
+ // $ExpectType string | |
+ key; | |
+ // $ExpectType string | |
+ value; | |
+ // $ExpectType string | |
+ buffer; | |
+ // $ExpectType string | |
+ start; | |
+ // $ExpectType string | |
+ end; | |
+ // $ExpectType string | |
+ formname; | |
+}); | |
+ | |
+form.on("fileBegin", (formname, file) => { | |
+ // $ExpectType string | |
+ formname; | |
+ // $ExpectType File | |
+ file; | |
+ | |
+ form.emit("data", { name: "fileBegin", formname, value: file }); | |
+}); | |
+form.on("file", (formname, file) => { | |
+ // $ExpectType string | |
+ formname; | |
+ // $ExpectType File | |
+ file; | |
+ | |
+ form.emit("data", { name: "file", formname, value: file }); | |
+}); | |
+ | |
+form.on("progress", (bytesReceived, bytesExpected) => { | |
+ // $ExpectType number | |
+ bytesReceived; | |
+ // $ExpectType number | |
+ bytesExpected; | |
+}); | |
+ | |
+form.on("field", (name, value) => { | |
+ // $ExpectType string | |
+ name; | |
+ // $ExpectType string | |
+ value; | |
+}); | |
+ | |
+form.on("error", err => { | |
+ // $ExpectType any | |
+ err; | |
+}); | |
+ | |
+form.on("aborted", () => {}); | |
+ | |
+form.once("end", () => {}); | |
+form.once("error", err => { | |
+ // $ExpectType any | |
+ err; | |
+}); | |
+ | |
+form.use((self, options) => { | |
+ // $ExpectType IncomingForm | |
+ self; | |
+ // $ExpectType Partial<Options> | |
+ options; | |
+}); | |
+ | |
+form.onPart = part => { | |
+ // $ExpectType Part | |
+ part; | |
+ | |
+ part.on("data", buffer => { | |
+ // $ExpectType any | |
+ buffer; | |
+ }); | |
+ | |
+ form.handlePart(part); | |
+}; | |
+ | |
+http.createServer(req => { | |
+ // $ExpectType IncomingMessage | |
+ req; | |
+ | |
+ form.parse(req, (err, fields, files) => { | |
+ // $ExpectType any | |
+ err; | |
+ // $ExpectType Fields | |
+ fields; | |
+ // $ExpectType Files | |
+ files; | |
+ }); | |
+}); | |
+ | |
+// $ExpectType IncomingForm | |
+new IncomingForm(); | |
+ | |
+// $ExpectType IncomingForm | |
+formidable(); | |
diff --git a/types/formidable/v1/index.d.ts b/types/formidable/v1/index.d.ts | |
new file mode 100644 | |
index 0000000000..249da05fdf | |
--- /dev/null | |
+++ b/types/formidable/v1/index.d.ts | |
@@ -0,0 +1,297 @@ | |
+// Type definitions for formidable 1.2 | |
+// Project: https://github.com/node-formidable/formidable | |
+// Definitions by: Wim Looman <https://github.com/Nemo157> | |
+// Martin Badin <https://github.com/martin-badin> | |
+// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped | |
+ | |
+/// <reference types="node" /> | |
+ | |
+import { Stream } from "stream"; | |
+import Formidable = require("./Formidable"); | |
+import parsers = require("./parsers/index"); | |
+import PersistentFile = require("./PersistentFile"); | |
+import VolatileFile = require("./VolatileFile"); | |
+import errors = require("./FormidableError"); | |
+ | |
+declare namespace formidable { | |
+ type BufferEncoding = | |
+ | "ascii" | |
+ | "base64" | |
+ | "binary" | |
+ | "hex" | |
+ | "latin1" | |
+ | "ucs-2" | |
+ | "ucs2" | |
+ | "utf-8" | |
+ | "utf16le" | |
+ | "utf8"; | |
+ | |
+ type EventNames = | |
+ /** | |
+ * Emitted when the request was aborted by the user. Right now this can be due to a 'timeout' or | |
+ * 'close' event on the socket. After this event is emitted, an error event will follow. In the | |
+ * future there will be a separate 'timeout' event (needs a change in the node core). | |
+ * | |
+ * @link https://github.com/node-formidable/formidable#aborted | |
+ */ | |
+ | "aborted" | |
+ /** | |
+ * Emitted when the entire request has been received, and all contained files have finished | |
+ * flushing to disk. This is a great place for you to send your response. | |
+ * | |
+ * @link https://github.com/node-formidable/formidable#end | |
+ */ | |
+ | "end" | |
+ /** | |
+ * Emitted when there is an error processing the incoming form. A request that experiences an | |
+ * error is automatically paused, you will have to manually call request.resume() if you want the | |
+ * request to continue firing 'data' events. | |
+ * | |
+ * @link https://github.com/node-formidable/formidable#error | |
+ */ | |
+ | "error" | |
+ /** | |
+ * Emitted whenever a field / value pair has been received. | |
+ * | |
+ * @link https://github.com/node-formidable/formidable#field | |
+ */ | |
+ | "field" | |
+ /** | |
+ * Emitted whenever a field / file pair has been received. file is an instance of File. | |
+ * | |
+ * @link https://github.com/node-formidable/formidable#file-1 | |
+ */ | |
+ | "file" | |
+ /** | |
+ * Emitted whenever a new file is detected in the upload stream. Use this event if you want to | |
+ * stream the file to somewhere else while buffering the upload on the file system. | |
+ * | |
+ * @link https://github.com/node-formidable/formidable#filebegin | |
+ */ | |
+ | "fileBegin" | |
+ | "headerEnd" | |
+ | "headerField" | |
+ | "headersEnd" | |
+ | "headerValue" | |
+ | "partBegin" | |
+ | "partData" | |
+ /** | |
+ * Emitted after each incoming chunk of data that has been parsed. Can be used to roll your own | |
+ * progress bar. | |
+ * | |
+ * @link https://github.com/node-formidable/formidable#progress | |
+ */ | |
+ | "progress"; | |
+ | |
+ interface Options { | |
+ /** | |
+ * sets encoding for incoming form fields | |
+ * | |
+ * @default 'utf-8' | |
+ */ | |
+ encoding?: BufferEncoding | undefined; | |
+ | |
+ /** | |
+ * the directory for placing file uploads in. You can move them later by using fs.rename() | |
+ * | |
+ * @default os.tmpdir() | |
+ */ | |
+ uploadDir?: string | undefined; | |
+ | |
+ /** | |
+ * to include the extensions of the original files or not | |
+ * | |
+ * @default false | |
+ */ | |
+ keepExtensions?: boolean | undefined; | |
+ | |
+ /** | |
+ * allow upload empty files | |
+ * | |
+ * @default true | |
+ */ | |
+ allowEmptyFiles?: boolean | undefined; | |
+ | |
+ /** | |
+ * the minium size of uploaded file | |
+ * | |
+ * @default 1 | |
+ */ | |
+ minFileSize?: number | undefined; | |
+ | |
+ /** | |
+ * limit the size of uploaded file | |
+ * | |
+ * @default 200 * 1024 * 1024 | |
+ */ | |
+ maxFileSize?: number | undefined; | |
+ | |
+ /** | |
+ * limit the number of fields, set 0 for unlimited | |
+ * | |
+ * @default 1000 | |
+ */ | |
+ maxFields?: number | undefined; | |
+ | |
+ /** | |
+ * limit the amount of memory all fields together (except files) can allocate in bytes | |
+ * | |
+ * @default 20 * 1024 * 1024 | |
+ */ | |
+ maxFieldsSize?: number | undefined; | |
+ | |
+ /** | |
+ * include checksums calculated for incoming files, set this to some hash algorithm, see | |
+ * crypto.createHash for available algorithms | |
+ * | |
+ * @default false | |
+ */ | |
+ hash?: string | false | undefined; | |
+ | |
+ /** | |
+ * which by default writes to host machine file system every file parsed; The function should | |
+ * return an instance of a Writable stream that will receive the uploaded file data. With this | |
+ * option, you can have any custom behavior regarding where the uploaded file data will be | |
+ * streamed for. If you are looking to write the file uploaded in other types of cloud storages | |
+ * (AWS S3, Azure blob storage, Google cloud storage) or private file storage, this is the option | |
+ * you're looking for. When this option is defined the default behavior of writing the file in the | |
+ * host machine file system is lost. | |
+ * | |
+ * @default null | |
+ */ | |
+ fileWriteStreamHandler?: (() => void) | undefined; | |
+ | |
+ /** | |
+ * when you call the .parse method, the files argument (of the callback) will contain arrays of | |
+ * files for inputs which submit multiple files using the HTML5 multiple attribute. Also, the | |
+ * fields argument will contain arrays of values for fields that have names ending with '[]' | |
+ * | |
+ * @default false | |
+ */ | |
+ multiples?: boolean | undefined; | |
+ | |
+ enabledPlugins?: string[] | undefined; | |
+ } | |
+ | |
+ interface Fields { | |
+ [field: string]: string | string[]; | |
+ } | |
+ interface Files { | |
+ [file: string]: File | File[]; | |
+ } | |
+ | |
+ interface Part extends Stream { | |
+ filename?: string | undefined; | |
+ headers: Record<string, string>; | |
+ mime?: string | undefined; | |
+ name: string; | |
+ } | |
+ | |
+ /** | |
+ * @link https://github.com/node-formidable/formidable#file | |
+ */ | |
+ interface FileJSON extends Pick<File, "size" | "path" | "name" | "type" | "hash"> { | |
+ filename: string; | |
+ length: number; | |
+ mime: string; | |
+ mtime: Date | null; | |
+ } | |
+ | |
+ interface File { | |
+ /** | |
+ * The size of the uploaded file in bytes. If the file is still being uploaded (see `'fileBegin'` | |
+ * event), this property says how many bytes of the file have been written to disk yet. | |
+ */ | |
+ size: number; | |
+ | |
+ /** | |
+ * The path this file is being written to. You can modify this in the `'fileBegin'` event in case | |
+ * you are unhappy with the way formidable generates a temporary path for your files. | |
+ */ | |
+ path: string; | |
+ | |
+ /** | |
+ * The name this file had according to the uploading client. | |
+ */ | |
+ name: string | null; | |
+ | |
+ /** | |
+ * The mime type of this file, according to the uploading client. | |
+ */ | |
+ type: string | null; | |
+ | |
+ /** | |
+ * A Date object (or `null`) containing the time this file was last written to. Mostly here for | |
+ * compatibility with the [W3C File API Draft](http://dev.w3.org/2006/webapi/FileAPI/). | |
+ */ | |
+ lastModifiedDate?: Date | null | undefined; | |
+ | |
+ /** | |
+ * If `options.hash` calculation was set, you can read the hex digest out of this var. | |
+ */ | |
+ hash?: string | "sha1" | "md5" | "sha256" | null | undefined; | |
+ | |
+ /** | |
+ * This method returns a JSON-representation of the file, allowing you to JSON.stringify() the | |
+ * file which is useful for logging and responding to requests. | |
+ * | |
+ * @link https://github.com/node-formidable/formidable#filetojson | |
+ */ | |
+ toJSON(): FileJSON; | |
+ | |
+ toString(): string; | |
+ } | |
+ | |
+ interface EmitData { | |
+ formname: any; | |
+ key?: string | number | undefined; | |
+ name: "fileBegin" | "file"; | |
+ value: File | string; | |
+ } | |
+ | |
+ interface EventData { | |
+ name: EventNames; | |
+ buffer: string; | |
+ end: string; | |
+ formname: string; | |
+ key: string; | |
+ start: string; | |
+ value: string; | |
+ } | |
+ | |
+ type PluginFunction = (formidable: Formidable, options: Partial<Options>) => void; | |
+ | |
+ type MappedParsers = { | |
+ [P in keyof typeof parsers]: typeof parsers[P]; | |
+ }; | |
+ | |
+ type Plugins = ["octetstream", "querystring", "multipart", "json"]; | |
+ | |
+ type Plugin = keyof { [K in Plugins[number]]: K }; | |
+ | |
+ type DefaultOptions = Required<Omit<Options, "enabledPlugins">> & { | |
+ enabledPlugins: EnabledPlugins; | |
+ }; | |
+ | |
+ type EnabledPlugins = { | |
+ [P in Plugin]: PluginFunction; | |
+ }; | |
+} | |
+ | |
+declare const formidable: { | |
+ (options?: formidable.Options): Formidable; | |
+ defaultOptions: formidable.DefaultOptions; | |
+ plugins: formidable.EnabledPlugins; | |
+ errors: typeof errors; | |
+ File: typeof PersistentFile; | |
+ PersistentFile: typeof PersistentFile; | |
+ VolatileFile: typeof VolatileFile; | |
+ Formidable: typeof Formidable; | |
+ formidable: (options?: formidable.Options) => Formidable; | |
+ // alias | |
+ IncomingForm: typeof Formidable; | |
+ // parsers and mapped parsers | |
+ parsers: typeof parsers; | |
+} & formidable.MappedParsers; | |
+ | |
+export = formidable; | |
diff --git a/types/formidable/v1/parsers/index.d.ts b/types/formidable/v1/parsers/index.d.ts | |
new file mode 100644 | |
index 0000000000..9f78a6657a | |
--- /dev/null | |
+++ b/types/formidable/v1/parsers/index.d.ts | |
@@ -0,0 +1,56 @@ | |
+import { IncomingForm, Options } from "../"; | |
+import { PassThrough, Transform } from "stream"; | |
+ | |
+export class MultipartParser extends Transform { | |
+ constructor(options?: Partial<Options>); | |
+ _final(callback: () => void): void; | |
+ _handleCallback(name: string, buffer: Buffer, start?: number, end?: number): void; | |
+ _transform(buffer: Buffer, _: any, callback: () => void): number; | |
+ explain(): string; | |
+ initWithBoundary(str: string): void; | |
+ | |
+ static stateToString: (stateNumber: number) => string; | |
+ | |
+ static STATES: Record< | |
+ | "PARSER_UNINITIALIZED" | |
+ | "START" | |
+ | "START_BOUNDARY" | |
+ | "HEADER_FIELD_START" | |
+ | "HEADER_FIELD" | |
+ | "HEADER_VALUE_START" | |
+ | "HEADER_VALUE" | |
+ | "HEADER_VALUE_ALMOST_DONE" | |
+ | "HEADERS_ALMOST_DONE" | |
+ | "PART_DATA_START" | |
+ | "PART_DATA" | |
+ | "PART_END" | |
+ | "END", | |
+ number | |
+ >; | |
+} | |
+ | |
+export class OctetStreamParser extends PassThrough { | |
+ constructor(options?: Partial<Options>); | |
+} | |
+ | |
+export class QuerystringParser extends Transform { | |
+ constructor(options?: Partial<Options>); | |
+ _flush(callback: () => void): void; | |
+ _transform(buffer: Buffer, encoding: BufferEncoding, callback: () => void): void; | |
+} | |
+ | |
+export class StreamingQuerystring extends QuerystringParser { | |
+ emitField(key: string, val?: string): void; | |
+ getSection(buffer: Buffer, i: number): string; | |
+} | |
+ | |
+export class DummyParser extends Transform { | |
+ constructor(incomingForm: typeof IncomingForm, options?: Partial<Options>); | |
+ _flush(callback: () => void): void; | |
+} | |
+ | |
+export class JSONParser extends Transform { | |
+ constructor(options?: Partial<Options>); | |
+ _flush(callback: () => void): void; | |
+ _transform(chunk: any, encoding: BufferEncoding, callback: () => void): void; | |
+} | |
diff --git a/types/formidable/v1/tsconfig.json b/types/formidable/v1/tsconfig.json | |
new file mode 100644 | |
index 0000000000..a2b5c933b6 | |
--- /dev/null | |
+++ b/types/formidable/v1/tsconfig.json | |
@@ -0,0 +1,28 @@ | |
+{ | |
+ "compilerOptions": { | |
+ "module": "commonjs", | |
+ "lib": [ | |
+ "es6" | |
+ ], | |
+ "noImplicitAny": true, | |
+ "noImplicitThis": true, | |
+ "strictNullChecks": true, | |
+ "strictFunctionTypes": true, | |
+ "baseUrl": "../../", | |
+ "typeRoots": [ | |
+ "../../" | |
+ ], | |
+ "paths": { | |
+ "formidable": [ | |
+ "formidable/v1" | |
+ ] | |
+ }, | |
+ "types": [], | |
+ "noEmit": true, | |
+ "forceConsistentCasingInFileNames": true | |
+ }, | |
+ "files": [ | |
+ "index.d.ts", | |
+ "formidable-tests.ts" | |
+ ] | |
+} | |
diff --git a/types/formidable/v1/tslint.json b/types/formidable/v1/tslint.json | |
new file mode 100644 | |
index 0000000000..3db14f85ea | |
--- /dev/null | |
+++ b/types/formidable/v1/tslint.json | |
@@ -0,0 +1 @@ | |
+{ "extends": "dtslint/dt.json" } | |
diff --git a/types/restify/tsconfig.json b/types/restify/tsconfig.json | |
index 19a88d8570..761c3e5802 100644 | |
--- a/types/restify/tsconfig.json | |
+++ b/types/restify/tsconfig.json | |
@@ -12,6 +12,11 @@ | |
"typeRoots": [ | |
"../" | |
], | |
+ "paths": { | |
+ "formidable": [ | |
+ "formidable/v1" | |
+ ] | |
+ }, | |
"types": [], | |
"noEmit": true, | |
"forceConsistentCasingInFileNames": true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment