FormData.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  2. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  3. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  4. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  5. };
  6. var _FormData_instances, _FormData_entries, _FormData_setEntry;
  7. import { inspect } from "util";
  8. import { File } from "./File.js";
  9. import { isFile } from "./isFile.js";
  10. import { isBlob } from "./isBlob.js";
  11. import { isFunction } from "./isFunction.js";
  12. import { deprecateConstructorEntries } from "./deprecateConstructorEntries.js";
  13. export class FormData {
  14. constructor(entries) {
  15. _FormData_instances.add(this);
  16. _FormData_entries.set(this, new Map());
  17. if (entries) {
  18. deprecateConstructorEntries();
  19. entries.forEach(({ name, value, fileName }) => this.append(name, value, fileName));
  20. }
  21. }
  22. static [(_FormData_entries = new WeakMap(), _FormData_instances = new WeakSet(), Symbol.hasInstance)](value) {
  23. return Boolean(value
  24. && isFunction(value.constructor)
  25. && value[Symbol.toStringTag] === "FormData"
  26. && isFunction(value.append)
  27. && isFunction(value.set)
  28. && isFunction(value.get)
  29. && isFunction(value.getAll)
  30. && isFunction(value.has)
  31. && isFunction(value.delete)
  32. && isFunction(value.entries)
  33. && isFunction(value.values)
  34. && isFunction(value.keys)
  35. && isFunction(value[Symbol.iterator])
  36. && isFunction(value.forEach));
  37. }
  38. append(name, value, fileName) {
  39. __classPrivateFieldGet(this, _FormData_instances, "m", _FormData_setEntry).call(this, {
  40. name,
  41. fileName,
  42. append: true,
  43. rawValue: value,
  44. argsLength: arguments.length
  45. });
  46. }
  47. set(name, value, fileName) {
  48. __classPrivateFieldGet(this, _FormData_instances, "m", _FormData_setEntry).call(this, {
  49. name,
  50. fileName,
  51. append: false,
  52. rawValue: value,
  53. argsLength: arguments.length
  54. });
  55. }
  56. get(name) {
  57. const field = __classPrivateFieldGet(this, _FormData_entries, "f").get(String(name));
  58. if (!field) {
  59. return null;
  60. }
  61. return field[0];
  62. }
  63. getAll(name) {
  64. const field = __classPrivateFieldGet(this, _FormData_entries, "f").get(String(name));
  65. if (!field) {
  66. return [];
  67. }
  68. return field.slice();
  69. }
  70. has(name) {
  71. return __classPrivateFieldGet(this, _FormData_entries, "f").has(String(name));
  72. }
  73. delete(name) {
  74. __classPrivateFieldGet(this, _FormData_entries, "f").delete(String(name));
  75. }
  76. *keys() {
  77. for (const key of __classPrivateFieldGet(this, _FormData_entries, "f").keys()) {
  78. yield key;
  79. }
  80. }
  81. *entries() {
  82. for (const name of this.keys()) {
  83. const values = this.getAll(name);
  84. for (const value of values) {
  85. yield [name, value];
  86. }
  87. }
  88. }
  89. *values() {
  90. for (const [, value] of this) {
  91. yield value;
  92. }
  93. }
  94. [(_FormData_setEntry = function _FormData_setEntry({ name, rawValue, append, fileName, argsLength }) {
  95. const methodName = append ? "append" : "set";
  96. if (argsLength < 2) {
  97. throw new TypeError(`Failed to execute '${methodName}' on 'FormData': `
  98. + `2 arguments required, but only ${argsLength} present.`);
  99. }
  100. name = String(name);
  101. let value;
  102. if (isFile(rawValue)) {
  103. value = fileName === undefined
  104. ? rawValue
  105. : new File([rawValue], fileName, {
  106. type: rawValue.type,
  107. lastModified: rawValue.lastModified
  108. });
  109. }
  110. else if (isBlob(rawValue)) {
  111. value = new File([rawValue], fileName === undefined ? "blob" : fileName, {
  112. type: rawValue.type
  113. });
  114. }
  115. else if (fileName) {
  116. throw new TypeError(`Failed to execute '${methodName}' on 'FormData': `
  117. + "parameter 2 is not of type 'Blob'.");
  118. }
  119. else {
  120. value = String(rawValue);
  121. }
  122. const values = __classPrivateFieldGet(this, _FormData_entries, "f").get(name);
  123. if (!values) {
  124. return void __classPrivateFieldGet(this, _FormData_entries, "f").set(name, [value]);
  125. }
  126. if (!append) {
  127. return void __classPrivateFieldGet(this, _FormData_entries, "f").set(name, [value]);
  128. }
  129. values.push(value);
  130. }, Symbol.iterator)]() {
  131. return this.entries();
  132. }
  133. forEach(callback, thisArg) {
  134. for (const [name, value] of this) {
  135. callback.call(thisArg, value, name, this);
  136. }
  137. }
  138. get [Symbol.toStringTag]() {
  139. return "FormData";
  140. }
  141. [inspect.custom]() {
  142. return this[Symbol.toStringTag];
  143. }
  144. }