@wevu/web-apis 1.2.42 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -0
- package/dist/abort.d.mts +1 -1
- package/dist/fetch.d.mts +1 -1
- package/dist/fetch.mjs +187 -2
- package/dist/http-C-VyEBss.mjs +303 -0
- package/dist/http-CE8-CIHE.d.mts +66 -0
- package/dist/http.d.mts +2 -58
- package/dist/http.mjs +1 -219
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +3 -3
- package/dist/shared-Dc42USJC.d.mts +39 -0
- package/dist/shared.d.mts +2 -38
- package/dist/url-D0ANYIyk.d.mts +50 -0
- package/dist/url.d.mts +2 -49
- package/dist/web-DNBBg6y9.d.mts +51 -0
- package/dist/web-kSAO_PSD.mjs +178 -0
- package/dist/web.d.mts +2 -43
- package/dist/web.mjs +1 -115
- package/dist/websocket.d.mts +2 -2
- package/dist/websocket.mjs +1 -1
- package/dist/xhr.d.mts +1 -1
- package/dist/xhr.mjs +2 -2
- package/package.json +3 -3
- package/dist/fetch-BPqcYqI3.mjs +0 -243
package/dist/web.d.mts
CHANGED
|
@@ -1,43 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export type BlobLikePart = RequestGlobalsBlobLike;
|
|
4
|
-
export type BlobPart = ArrayBuffer | ArrayBufferView | BlobLikePart | string;
|
|
5
|
-
export type FormDataEntryValue = FilePolyfill | BlobPolyfill | string;
|
|
6
|
-
interface BlobPropertyBag {
|
|
7
|
-
type?: string;
|
|
8
|
-
}
|
|
9
|
-
interface FilePropertyBag extends BlobPropertyBag {
|
|
10
|
-
lastModified?: number;
|
|
11
|
-
}
|
|
12
|
-
export declare class BlobPolyfill {
|
|
13
|
-
readonly size: number;
|
|
14
|
-
readonly type: string;
|
|
15
|
-
private readonly parts;
|
|
16
|
-
constructor(parts?: BlobPart[], options?: BlobPropertyBag);
|
|
17
|
-
arrayBuffer(): Promise<ArrayBuffer>;
|
|
18
|
-
text(): Promise<string>;
|
|
19
|
-
}
|
|
20
|
-
export declare class FilePolyfill extends BlobPolyfill {
|
|
21
|
-
readonly lastModified: number;
|
|
22
|
-
readonly name: string;
|
|
23
|
-
constructor(parts: BlobPart[] | undefined, name: string, options?: FilePropertyBag);
|
|
24
|
-
get [Symbol.toStringTag](): string;
|
|
25
|
-
}
|
|
26
|
-
export declare class FormDataPolyfill {
|
|
27
|
-
private readonly entriesList;
|
|
28
|
-
append(name: string, value: string): void;
|
|
29
|
-
append(name: string, value: BlobLikePart, filename?: string): void;
|
|
30
|
-
delete(name: string): void;
|
|
31
|
-
get(name: string): string | BlobPolyfill | null;
|
|
32
|
-
getAll(name: string): FormDataEntryValue[];
|
|
33
|
-
has(name: string): boolean;
|
|
34
|
-
set(name: string, value: string): void;
|
|
35
|
-
set(name: string, value: BlobLikePart, filename?: string): void;
|
|
36
|
-
forEach(callback: (value: FormDataEntryValue, key: string, parent: FormDataPolyfill) => void): void;
|
|
37
|
-
entries(): Generator<[string, FormDataEntryValue], void, unknown>;
|
|
38
|
-
keys(): Generator<string, void, unknown>;
|
|
39
|
-
values(): Generator<FormDataEntryValue, void, unknown>;
|
|
40
|
-
[Symbol.iterator](): Generator<[string, FormDataEntryValue], void, unknown>;
|
|
41
|
-
get [Symbol.toStringTag](): string;
|
|
42
|
-
}
|
|
43
|
-
//#endregion
|
|
1
|
+
import { a as BlobPart, i as BlobLikePart, n as FormDataPolyfill, o as BlobPolyfill, r as FilePolyfill, t as FormDataEntryValue } from "./web-DNBBg6y9.mjs";
|
|
2
|
+
export { type BlobLikePart, type BlobPart, BlobPolyfill, FilePolyfill, type FormDataEntryValue, FormDataPolyfill };
|
package/dist/web.mjs
CHANGED
|
@@ -1,116 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//#region src/web.ts
|
|
3
|
-
function normalizeBlobPart(part) {
|
|
4
|
-
if (typeof part === "string") return Promise.resolve(encodeText(part));
|
|
5
|
-
if (isBlobLike(part)) return part.arrayBuffer();
|
|
6
|
-
if (isArrayBufferLike(part)) return Promise.resolve(cloneArrayBuffer(part));
|
|
7
|
-
if (ArrayBuffer.isView(part)) return Promise.resolve(cloneArrayBufferView(part));
|
|
8
|
-
return Promise.resolve(encodeText(String(part)));
|
|
9
|
-
}
|
|
10
|
-
var BlobPolyfill = class {
|
|
11
|
-
size;
|
|
12
|
-
type;
|
|
13
|
-
parts;
|
|
14
|
-
constructor(parts = [], options) {
|
|
15
|
-
this.parts = [...parts];
|
|
16
|
-
this.type = options?.type ?? "";
|
|
17
|
-
this.size = parts.reduce((total, part) => {
|
|
18
|
-
if (typeof part === "string") return total + String(part).length;
|
|
19
|
-
if (part && typeof part === "object" && typeof part.size === "number") return total + Number(part.size);
|
|
20
|
-
if (isArrayBufferLike(part)) return total + part.byteLength;
|
|
21
|
-
if (ArrayBuffer.isView(part)) return total + part.byteLength;
|
|
22
|
-
return total;
|
|
23
|
-
}, 0);
|
|
24
|
-
}
|
|
25
|
-
async arrayBuffer() {
|
|
26
|
-
const buffers = await Promise.all(this.parts.map((part) => normalizeBlobPart(part)));
|
|
27
|
-
const totalLength = buffers.reduce((sum, buffer) => sum + buffer.byteLength, 0);
|
|
28
|
-
const merged = new Uint8Array(totalLength);
|
|
29
|
-
let offset = 0;
|
|
30
|
-
for (const buffer of buffers) {
|
|
31
|
-
merged.set(new Uint8Array(buffer), offset);
|
|
32
|
-
offset += buffer.byteLength;
|
|
33
|
-
}
|
|
34
|
-
return merged.buffer;
|
|
35
|
-
}
|
|
36
|
-
async text() {
|
|
37
|
-
return decodeText(await this.arrayBuffer());
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
var FilePolyfill = class extends BlobPolyfill {
|
|
41
|
-
lastModified;
|
|
42
|
-
name;
|
|
43
|
-
constructor(parts = [], name, options) {
|
|
44
|
-
super(parts, options);
|
|
45
|
-
this.name = String(name);
|
|
46
|
-
this.lastModified = options?.lastModified ?? Date.now();
|
|
47
|
-
}
|
|
48
|
-
get [Symbol.toStringTag]() {
|
|
49
|
-
return "File";
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
function isFileLikePart(value) {
|
|
53
|
-
return typeof value.name === "string" && typeof value.lastModified === "number";
|
|
54
|
-
}
|
|
55
|
-
function normalizeFormDataValue(value, filename) {
|
|
56
|
-
if (value instanceof FilePolyfill) {
|
|
57
|
-
if (filename === void 0) return value;
|
|
58
|
-
return new FilePolyfill([value], filename, {
|
|
59
|
-
lastModified: value.lastModified,
|
|
60
|
-
type: value.type
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
if (typeof value !== "string") return new FilePolyfill([value], filename ?? (isFileLikePart(value) ? value.name : "blob"), {
|
|
64
|
-
lastModified: isFileLikePart(value) ? value.lastModified : void 0,
|
|
65
|
-
type: value.type
|
|
66
|
-
});
|
|
67
|
-
return String(value);
|
|
68
|
-
}
|
|
69
|
-
var FormDataPolyfill = class {
|
|
70
|
-
entriesList = [];
|
|
71
|
-
append(name, value, filename) {
|
|
72
|
-
this.entriesList.push([String(name), normalizeFormDataValue(value, filename)]);
|
|
73
|
-
}
|
|
74
|
-
delete(name) {
|
|
75
|
-
const normalizedName = String(name);
|
|
76
|
-
let index = this.entriesList.length;
|
|
77
|
-
while (index-- > 0) if (this.entriesList[index]?.[0] === normalizedName) this.entriesList.splice(index, 1);
|
|
78
|
-
}
|
|
79
|
-
get(name) {
|
|
80
|
-
return this.entriesList.find((entry) => entry[0] === String(name))?.[1] ?? null;
|
|
81
|
-
}
|
|
82
|
-
getAll(name) {
|
|
83
|
-
return this.entriesList.filter((entry) => entry[0] === String(name)).map((entry) => entry[1]);
|
|
84
|
-
}
|
|
85
|
-
has(name) {
|
|
86
|
-
return this.entriesList.some((entry) => entry[0] === String(name));
|
|
87
|
-
}
|
|
88
|
-
set(name, value, filename) {
|
|
89
|
-
this.delete(name);
|
|
90
|
-
if (typeof value === "string") {
|
|
91
|
-
this.append(name, value);
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
this.append(name, value, filename);
|
|
95
|
-
}
|
|
96
|
-
forEach(callback) {
|
|
97
|
-
for (const [key, value] of this.entriesList) callback(value, key, this);
|
|
98
|
-
}
|
|
99
|
-
*entries() {
|
|
100
|
-
yield* this.entriesList;
|
|
101
|
-
}
|
|
102
|
-
*keys() {
|
|
103
|
-
for (const [key] of this.entriesList) yield key;
|
|
104
|
-
}
|
|
105
|
-
*values() {
|
|
106
|
-
for (const [, value] of this.entriesList) yield value;
|
|
107
|
-
}
|
|
108
|
-
[Symbol.iterator]() {
|
|
109
|
-
return this.entries();
|
|
110
|
-
}
|
|
111
|
-
get [Symbol.toStringTag]() {
|
|
112
|
-
return "FormData";
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
//#endregion
|
|
1
|
+
import { n as FilePolyfill, r as BlobPolyfill, t as FormDataPolyfill } from "./web-kSAO_PSD.mjs";
|
|
116
2
|
export { BlobPolyfill, FilePolyfill, FormDataPolyfill };
|
package/dist/websocket.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as RequestGlobalsEventTarget, t as RequestGlobalsBlobLike } from "./shared-Dc42USJC.mjs";
|
|
2
2
|
import { WebSocketMiniProgramOptions } from "./networkDefaults.mjs";
|
|
3
|
-
import { BlobPolyfill } from "./web.mjs";
|
|
3
|
+
import { o as BlobPolyfill } from "./web-DNBBg6y9.mjs";
|
|
4
4
|
//#region src/websocket.d.ts
|
|
5
5
|
type WebSocketBinaryType = 'blob' | 'arraybuffer';
|
|
6
6
|
type WebSocketSendData = string | ArrayBuffer | ArrayBufferView | Blob | RequestGlobalsBlobLike;
|
package/dist/websocket.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { _ as resolveUrlConstructor$1, g as resolveTextEncoderConstructor, l as isArrayBufferLike, n as cloneArrayBuffer, r as cloneArrayBufferView, t as RequestGlobalsEventTarget, u as isBlobLike } from "./shared-BsJRaiBh.mjs";
|
|
2
2
|
import { URLPolyfill } from "./url.mjs";
|
|
3
|
-
import { BlobPolyfill } from "./web.mjs";
|
|
3
|
+
import { r as BlobPolyfill } from "./web-kSAO_PSD.mjs";
|
|
4
4
|
import { resolveWebSocketMiniProgramOptions } from "./networkDefaults.mjs";
|
|
5
5
|
import { wpi } from "@wevu/api";
|
|
6
6
|
//#region src/websocket.ts
|
package/dist/xhr.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RequestGlobalsEventTarget } from "./shared.mjs";
|
|
1
|
+
import { i as RequestGlobalsEventTarget } from "./shared-Dc42USJC.mjs";
|
|
2
2
|
import { RequestGlobalsMiniProgramOptions } from "./networkDefaults.mjs";
|
|
3
3
|
//#region src/xhr.d.ts
|
|
4
4
|
type XhrBody = Document | XMLHttpRequestBodyInit | null | undefined;
|
package/dist/xhr.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { f as resolveRequestGlobalsHost, t as RequestGlobalsEventTarget } from "./shared-BsJRaiBh.mjs";
|
|
2
2
|
import { AbortControllerPolyfill } from "./abort.mjs";
|
|
3
|
-
import { HeadersPolyfill, headersToObject } from "./http.mjs";
|
|
4
|
-
import {
|
|
3
|
+
import { o as HeadersPolyfill, s as headersToObject } from "./http-C-VyEBss.mjs";
|
|
4
|
+
import { fetch } from "./fetch.mjs";
|
|
5
5
|
//#region src/xhr.ts
|
|
6
6
|
function createProgressEvent(type) {
|
|
7
7
|
return {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wevu/web-apis",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.1",
|
|
5
5
|
"description": "Web API polyfills and global installers for mini-program runtimes",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -79,8 +79,8 @@
|
|
|
79
79
|
"node": "^20.19.0 || >=22.12.0"
|
|
80
80
|
},
|
|
81
81
|
"dependencies": {
|
|
82
|
-
"@weapp-core/constants": "0.2.
|
|
83
|
-
"@wevu/api": "0.3.
|
|
82
|
+
"@weapp-core/constants": "0.2.5",
|
|
83
|
+
"@wevu/api": "0.3.4"
|
|
84
84
|
},
|
|
85
85
|
"publishConfig": {
|
|
86
86
|
"access": "public",
|
package/dist/fetch-BPqcYqI3.mjs
DELETED
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
import { d as normalizeHeaderName, h as isUrlSearchParamsInstance, l as isArrayBufferLike, m as isUrlInstance, n as cloneArrayBuffer, o as encodeText, r as cloneArrayBufferView, u as isBlobLike } from "./shared-BsJRaiBh.mjs";
|
|
2
|
-
import { FormDataPolyfill } from "./web.mjs";
|
|
3
|
-
import { HeadersPolyfill, RequestPolyfill, ResponsePolyfill, getRequestBodyValue } from "./http.mjs";
|
|
4
|
-
import { resolveRequestMiniProgramOptions } from "./networkDefaults.mjs";
|
|
5
|
-
import { wpi } from "@wevu/api";
|
|
6
|
-
//#region src/multipart.ts
|
|
7
|
-
const CRLF = "\r\n";
|
|
8
|
-
function createMultipartBoundary() {
|
|
9
|
-
return `----weapp-vite-formdata-${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`;
|
|
10
|
-
}
|
|
11
|
-
function escapeMultipartName(value) {
|
|
12
|
-
return String(value).replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\r/g, "%0D").replace(/\n/g, "%0A");
|
|
13
|
-
}
|
|
14
|
-
function concatArrayBuffers(buffers) {
|
|
15
|
-
const totalLength = buffers.reduce((sum, buffer) => sum + buffer.byteLength, 0);
|
|
16
|
-
const merged = new Uint8Array(totalLength);
|
|
17
|
-
let offset = 0;
|
|
18
|
-
for (const buffer of buffers) {
|
|
19
|
-
merged.set(new Uint8Array(buffer), offset);
|
|
20
|
-
offset += buffer.byteLength;
|
|
21
|
-
}
|
|
22
|
-
return merged.buffer;
|
|
23
|
-
}
|
|
24
|
-
function isBlobFormDataValue(value) {
|
|
25
|
-
return typeof value !== "string" && typeof value?.arrayBuffer === "function";
|
|
26
|
-
}
|
|
27
|
-
function getMultipartFilename(value) {
|
|
28
|
-
const name = value.name;
|
|
29
|
-
return typeof name === "string" && name.length > 0 ? name : "blob";
|
|
30
|
-
}
|
|
31
|
-
async function createMultipartPart(name, value) {
|
|
32
|
-
if (!isBlobFormDataValue(value)) return [
|
|
33
|
-
encodeText(`Content-Disposition: form-data; name="${escapeMultipartName(name)}"${CRLF}${CRLF}`),
|
|
34
|
-
encodeText(String(value)),
|
|
35
|
-
encodeText(CRLF)
|
|
36
|
-
];
|
|
37
|
-
const filename = getMultipartFilename(value);
|
|
38
|
-
const headers = [
|
|
39
|
-
`Content-Disposition: form-data; name="${escapeMultipartName(name)}"; filename="${escapeMultipartName(filename)}"`,
|
|
40
|
-
`Content-Type: ${value.type || "application/octet-stream"}`,
|
|
41
|
-
"",
|
|
42
|
-
""
|
|
43
|
-
].join(CRLF);
|
|
44
|
-
return [
|
|
45
|
-
encodeText(headers),
|
|
46
|
-
await value.arrayBuffer(),
|
|
47
|
-
encodeText(CRLF)
|
|
48
|
-
];
|
|
49
|
-
}
|
|
50
|
-
async function encodeMultipartFormData(formData) {
|
|
51
|
-
const boundary = createMultipartBoundary();
|
|
52
|
-
const buffers = [];
|
|
53
|
-
for (const [name, value] of formData) {
|
|
54
|
-
buffers.push(encodeText(`--${boundary}${CRLF}`));
|
|
55
|
-
buffers.push(...await createMultipartPart(name, value));
|
|
56
|
-
}
|
|
57
|
-
buffers.push(encodeText(`--${boundary}--${CRLF}`));
|
|
58
|
-
return {
|
|
59
|
-
body: concatArrayBuffers(buffers),
|
|
60
|
-
contentType: `multipart/form-data; boundary=${boundary}`
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
//#endregion
|
|
64
|
-
//#region src/fetch.ts
|
|
65
|
-
const REQUEST_METHODS = [
|
|
66
|
-
"GET",
|
|
67
|
-
"HEAD",
|
|
68
|
-
"OPTIONS",
|
|
69
|
-
"POST",
|
|
70
|
-
"PUT",
|
|
71
|
-
"DELETE",
|
|
72
|
-
"TRACE",
|
|
73
|
-
"CONNECT"
|
|
74
|
-
];
|
|
75
|
-
const hasOwn = Object.prototype.hasOwnProperty;
|
|
76
|
-
function isObject(value) {
|
|
77
|
-
return typeof value === "object" && value !== null;
|
|
78
|
-
}
|
|
79
|
-
function createAbortError() {
|
|
80
|
-
if (typeof DOMException === "function") return new DOMException("The operation was aborted.", "AbortError");
|
|
81
|
-
const error = /* @__PURE__ */ new Error("The operation was aborted.");
|
|
82
|
-
error.name = "AbortError";
|
|
83
|
-
return error;
|
|
84
|
-
}
|
|
85
|
-
function normalizeMethod(method) {
|
|
86
|
-
const normalized = (method ?? "GET").toUpperCase();
|
|
87
|
-
if (REQUEST_METHODS.includes(normalized)) return normalized;
|
|
88
|
-
return "GET";
|
|
89
|
-
}
|
|
90
|
-
function setHeader(target, key, value) {
|
|
91
|
-
const normalizedKey = normalizeHeaderName(key);
|
|
92
|
-
if (!normalizedKey) return;
|
|
93
|
-
const nextValue = String(value);
|
|
94
|
-
for (const currentKey of Object.keys(target)) if (normalizeHeaderName(currentKey) === normalizedKey) delete target[currentKey];
|
|
95
|
-
target[key] = nextValue;
|
|
96
|
-
}
|
|
97
|
-
function hasHeader(target, key) {
|
|
98
|
-
const normalizedKey = normalizeHeaderName(key);
|
|
99
|
-
return Object.keys(target).some((currentKey) => normalizeHeaderName(currentKey) === normalizedKey);
|
|
100
|
-
}
|
|
101
|
-
function mergeHeaderSource(target, source) {
|
|
102
|
-
if (!source) return;
|
|
103
|
-
if (typeof source[Symbol.iterator] === "function") {
|
|
104
|
-
for (const entry of source) {
|
|
105
|
-
if (!entry || entry.length < 2) continue;
|
|
106
|
-
setHeader(target, entry[0], entry[1]);
|
|
107
|
-
}
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
if (typeof source.forEach === "function") {
|
|
111
|
-
source.forEach((value, key) => {
|
|
112
|
-
setHeader(target, key, value);
|
|
113
|
-
});
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
if (!isObject(source)) return;
|
|
117
|
-
for (const [key, value] of Object.entries(source)) {
|
|
118
|
-
if (Array.isArray(value)) {
|
|
119
|
-
setHeader(target, key, value.join(", "));
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
setHeader(target, key, value);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
function toHeaderMap(source) {
|
|
126
|
-
const headers = {};
|
|
127
|
-
mergeHeaderSource(headers, source);
|
|
128
|
-
return headers;
|
|
129
|
-
}
|
|
130
|
-
function isRequestLikeInput(input) {
|
|
131
|
-
return isObject(input) && typeof input.url === "string";
|
|
132
|
-
}
|
|
133
|
-
function isFormDataInstance(value) {
|
|
134
|
-
return Boolean(value && typeof value === "object" && (typeof FormData !== "undefined" && value instanceof FormData || value instanceof FormDataPolyfill));
|
|
135
|
-
}
|
|
136
|
-
async function extractRequestBodyFromInput(input) {
|
|
137
|
-
if (!input || typeof input.clone !== "function") return;
|
|
138
|
-
if (input.bodyUsed) throw new TypeError("Failed to execute fetch: request body is already used");
|
|
139
|
-
if (input instanceof RequestPolyfill) return getRequestBodyValue(input);
|
|
140
|
-
const cloned = input.clone();
|
|
141
|
-
if (cloned?.arrayBuffer) return cloned.arrayBuffer();
|
|
142
|
-
if (cloned?.text) return cloned.text();
|
|
143
|
-
}
|
|
144
|
-
async function normalizeRequestBody(body, headers) {
|
|
145
|
-
if (body == null) return;
|
|
146
|
-
if (typeof body === "string") {
|
|
147
|
-
if (!hasHeader(headers, "content-type")) headers["content-type"] = "text/plain;charset=UTF-8";
|
|
148
|
-
return body;
|
|
149
|
-
}
|
|
150
|
-
if (isUrlSearchParamsInstance(body)) {
|
|
151
|
-
if (!hasHeader(headers, "content-type")) headers["content-type"] = "application/x-www-form-urlencoded;charset=UTF-8";
|
|
152
|
-
return body.toString();
|
|
153
|
-
}
|
|
154
|
-
if (isArrayBufferLike(body)) return cloneArrayBuffer(body);
|
|
155
|
-
if (ArrayBuffer.isView(body)) return cloneArrayBufferView(body);
|
|
156
|
-
if (isBlobLike(body)) {
|
|
157
|
-
if (body.type && !hasHeader(headers, "content-type")) headers["content-type"] = body.type;
|
|
158
|
-
return body.arrayBuffer();
|
|
159
|
-
}
|
|
160
|
-
if (isFormDataInstance(body)) {
|
|
161
|
-
const payload = await encodeMultipartFormData(body);
|
|
162
|
-
if (!hasHeader(headers, "content-type")) headers["content-type"] = payload.contentType;
|
|
163
|
-
return payload.body;
|
|
164
|
-
}
|
|
165
|
-
return String(body);
|
|
166
|
-
}
|
|
167
|
-
async function resolveRequestMeta(input, init = {}) {
|
|
168
|
-
const requestInput = isRequestLikeInput(input) ? input : void 0;
|
|
169
|
-
const url = typeof input === "string" ? input : isUrlInstance(input) ? input.toString() : requestInput?.url;
|
|
170
|
-
if (!url) throw new TypeError("Failed to execute fetch: invalid request url");
|
|
171
|
-
const method = normalizeMethod(init.method ?? requestInput?.method);
|
|
172
|
-
const headers = toHeaderMap(requestInput?.headers);
|
|
173
|
-
mergeHeaderSource(headers, init.headers);
|
|
174
|
-
const hasBodyInInit = hasOwn.call(init, "body");
|
|
175
|
-
let rawBody = hasBodyInInit ? init.body : await extractRequestBodyFromInput(requestInput);
|
|
176
|
-
if (!hasBodyInInit && requestInput && (method === "GET" || method === "HEAD")) rawBody = void 0;
|
|
177
|
-
if ((method === "GET" || method === "HEAD") && rawBody != null) throw new TypeError("Failed to execute fetch: GET/HEAD request cannot have body");
|
|
178
|
-
return {
|
|
179
|
-
miniProgram: resolveRequestMiniProgramOptions(init.miniProgram, init.miniprogram),
|
|
180
|
-
url,
|
|
181
|
-
method,
|
|
182
|
-
headers,
|
|
183
|
-
body: await normalizeRequestBody(rawBody, headers),
|
|
184
|
-
signal: init.signal ?? requestInput?.signal ?? null
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
function createFetchResponse(data, status, headers, url) {
|
|
188
|
-
return new ResponsePolyfill(data instanceof Uint8Array ? cloneArrayBufferView(data) : data, {
|
|
189
|
-
status,
|
|
190
|
-
headers: new HeadersPolyfill(headers),
|
|
191
|
-
url
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
function isRequestTask(value) {
|
|
195
|
-
return isObject(value) && typeof value.abort === "function";
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* @description 使用 @wevu/api 的 request 能力实现 fetch 语义对齐。
|
|
199
|
-
*/
|
|
200
|
-
function fetch(input, init) {
|
|
201
|
-
return resolveRequestMeta(input, init).then((meta) => {
|
|
202
|
-
if (meta.signal?.aborted) return Promise.reject(createAbortError());
|
|
203
|
-
return new Promise((resolve, reject) => {
|
|
204
|
-
let settled = false;
|
|
205
|
-
let requestTask;
|
|
206
|
-
function onAbort() {
|
|
207
|
-
if (settled) return;
|
|
208
|
-
requestTask?.abort();
|
|
209
|
-
settled = true;
|
|
210
|
-
if (meta.signal) meta.signal.removeEventListener("abort", onAbort);
|
|
211
|
-
reject(createAbortError());
|
|
212
|
-
}
|
|
213
|
-
function cleanup() {
|
|
214
|
-
if (meta.signal) meta.signal.removeEventListener("abort", onAbort);
|
|
215
|
-
}
|
|
216
|
-
if (meta.signal) meta.signal.addEventListener("abort", onAbort, { once: true });
|
|
217
|
-
const requestResult = wpi.request({
|
|
218
|
-
...meta.miniProgram,
|
|
219
|
-
url: meta.url,
|
|
220
|
-
method: meta.method,
|
|
221
|
-
header: meta.headers,
|
|
222
|
-
data: meta.body,
|
|
223
|
-
responseType: "arraybuffer",
|
|
224
|
-
success: (res) => {
|
|
225
|
-
if (settled) return;
|
|
226
|
-
settled = true;
|
|
227
|
-
cleanup();
|
|
228
|
-
resolve(createFetchResponse(res.data, res.statusCode, toHeaderMap(res.header), meta.url));
|
|
229
|
-
},
|
|
230
|
-
fail: (error) => {
|
|
231
|
-
if (settled) return;
|
|
232
|
-
settled = true;
|
|
233
|
-
cleanup();
|
|
234
|
-
const message = isObject(error) && typeof error.errMsg === "string" ? error.errMsg : String(error);
|
|
235
|
-
reject(new TypeError(message));
|
|
236
|
-
}
|
|
237
|
-
});
|
|
238
|
-
requestTask = isRequestTask(requestResult) ? requestResult : void 0;
|
|
239
|
-
});
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
//#endregion
|
|
243
|
-
export { fetch as t };
|