FormData.js 5.6 KB

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