• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Based on https://github.com/octet-stream/form-data/blob/2d0f0dc371517444ce1f22cdde13f51995d0953a/lib/FormData.ts (MIT)
2/// <reference types="node" />
3
4import { File } from './file'
5import { SpecIterator, SpecIterableIterator } from './fetch'
6
7/**
8 * A `string` or `File` that represents a single value from a set of `FormData` key-value pairs.
9 */
10declare type FormDataEntryValue = string | File
11
12/**
13 * Provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using fetch().
14 */
15export declare class FormData {
16  /**
17   * Appends a new value onto an existing key inside a FormData object,
18   * or adds the key if it does not already exist.
19   *
20   * The difference between `set()` and `append()` is that if the specified key already exists, `set()` will overwrite all existing values with the new one, whereas `append()` will append the new value onto the end of the existing set of values.
21   *
22   * @param name The name of the field whose data is contained in `value`.
23   * @param value The field's value. This can be [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
24    or [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File). If none of these are specified the value is converted to a string.
25   * @param fileName The filename reported to the server, when a Blob or File is passed as the second parameter. The default filename for Blob objects is "blob". The default filename for File objects is the file's filename.
26   */
27  append(name: string, value: unknown, fileName?: string): void
28
29  /**
30   * Set a new value for an existing key inside FormData,
31   * or add the new field if it does not already exist.
32   *
33   * @param name The name of the field whose data is contained in `value`.
34   * @param value The field's value. This can be [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
35    or [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File). If none of these are specified the value is converted to a string.
36   * @param fileName The filename reported to the server, when a Blob or File is passed as the second parameter. The default filename for Blob objects is "blob". The default filename for File objects is the file's filename.
37   *
38   */
39  set(name: string, value: unknown, fileName?: string): void
40
41  /**
42   * Returns the first value associated with a given key from within a `FormData` object.
43   * If you expect multiple values and want all of them, use the `getAll()` method instead.
44   *
45   * @param {string} name A name of the value you want to retrieve.
46   *
47   * @returns A `FormDataEntryValue` containing the value. If the key doesn't exist, the method returns null.
48   */
49  get(name: string): FormDataEntryValue | null
50
51  /**
52   * Returns all the values associated with a given key from within a `FormData` object.
53   *
54   * @param {string} name A name of the value you want to retrieve.
55   *
56   * @returns An array of `FormDataEntryValue` whose key matches the value passed in the `name` parameter. If the key doesn't exist, the method returns an empty list.
57   */
58  getAll(name: string): FormDataEntryValue[]
59
60  /**
61   * Returns a boolean stating whether a `FormData` object contains a certain key.
62   *
63   * @param name A string representing the name of the key you want to test for.
64   *
65   * @return A boolean value.
66   */
67  has(name: string): boolean
68
69  /**
70   * Deletes a key and its value(s) from a `FormData` object.
71   *
72   * @param name The name of the key you want to delete.
73   */
74  delete(name: string): void
75
76  /**
77   * Executes given callback function for each field of the FormData instance
78   */
79  forEach: (
80    callbackfn: (value: FormDataEntryValue, key: string, iterable: FormData) => void,
81    thisArg?: unknown
82  ) => void
83
84  /**
85   * Returns an [`iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) allowing to go through all keys contained in this `FormData` object.
86   * Each key is a `string`.
87   */
88  keys: () => SpecIterableIterator<string>
89
90  /**
91   * Returns an [`iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) allowing to go through all values contained in this object `FormData` object.
92   * Each value is a [`FormDataValue`](https://developer.mozilla.org/en-US/docs/Web/API/FormDataEntryValue).
93   */
94  values: () => SpecIterableIterator<FormDataEntryValue>
95
96  /**
97   * Returns an [`iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) allowing to go through the `FormData` key/value pairs.
98   * The key of each pair is a string; the value is a [`FormDataValue`](https://developer.mozilla.org/en-US/docs/Web/API/FormDataEntryValue).
99   */
100  entries: () => SpecIterableIterator<[string, FormDataEntryValue]>
101
102  /**
103   * An alias for FormData#entries()
104   */
105  [Symbol.iterator]: () => SpecIterableIterator<[string, FormDataEntryValue]>
106
107  readonly [Symbol.toStringTag]: string
108}
109