formdata.d.ts 4.9 KB

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