|
@@ -0,0 +1,92 @@
|
|
|
+export class FormDataImpl {
|
|
|
+ private _data: Map<string, string | string[]>;
|
|
|
+
|
|
|
+ constructor() {
|
|
|
+ this._data = new Map<string, string | string[]>();
|
|
|
+ }
|
|
|
+
|
|
|
+ isImplType = true;
|
|
|
+
|
|
|
+ append(key: string, value: string) {
|
|
|
+ if (this._data.has(key)) {
|
|
|
+ const currentValue = this._data.get(key);
|
|
|
+ if (Array.isArray(currentValue)) {
|
|
|
+ this._data.set(key, [...currentValue, value]);
|
|
|
+ } else {
|
|
|
+ this._data.set(key, [currentValue ?? '', value]);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ this._data.set(key, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ get(key: string) {
|
|
|
+ return this._data.get(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ getAll(key: string) {
|
|
|
+ return Array.isArray(this._data.get(key)) ? this._data.get(key) || [] : [this._data.get(key) || ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ has(key: string) {
|
|
|
+ return this._data.has(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ delete(key: string) {
|
|
|
+ this._data.delete(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ keys() {
|
|
|
+ return Array.from(this._data.keys());
|
|
|
+ }
|
|
|
+
|
|
|
+ values() {
|
|
|
+ return Array.from(this._data.values());
|
|
|
+ }
|
|
|
+
|
|
|
+ entries() {
|
|
|
+ return Array.from(this._data.entries());
|
|
|
+ }
|
|
|
+
|
|
|
+ forEach(callback: (value: string | string[], key: string, map: Map<string, string | string[]>) => void, thisArg?: any) {
|
|
|
+ this._data.forEach(callback, thisArg);
|
|
|
+ }
|
|
|
+
|
|
|
+ toString() {
|
|
|
+ const pairs: string[] = [];
|
|
|
+ this._data.forEach((value, key) => {
|
|
|
+ if (Array.isArray(value)) {
|
|
|
+ value.forEach((v) => pairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(v)}`));
|
|
|
+ } else {
|
|
|
+ pairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return pairs.join('&');
|
|
|
+ }
|
|
|
+
|
|
|
+ toBrowserFormData() {
|
|
|
+ if (typeof window !== 'undefined' && window.FormData) {
|
|
|
+ const browserFormData = new window.FormData();
|
|
|
+ this._data.forEach((value, key) => {
|
|
|
+ if (Array.isArray(value)) {
|
|
|
+ value.forEach((v) => browserFormData.append(key, v));
|
|
|
+ } else {
|
|
|
+ browserFormData.append(key, value);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return browserFormData;
|
|
|
+ } else {
|
|
|
+ throw new Error('Browser environment is required to use toBrowserFormData');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ getData() {
|
|
|
+ const json = {} as Record<string, string|string[]>;
|
|
|
+ this._data.forEach((value, key) => json[key] = value);
|
|
|
+ return JSON.stringify(json);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//兼容SSR情况下的 NodeJs
|
|
|
+if (typeof FormData === 'undefined')
|
|
|
+ global.FormData = FormDataImpl as any;
|