@wevu/web-apis 1.3.5 → 1.4.0
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 +36 -8
- package/dist/abort.d.mts +2 -4
- package/dist/abort.mjs +2 -3
- package/dist/eventsource-DUCc1iHv.d.mts +82 -0
- package/dist/eventsource-ojngrwBI.mjs +340 -0
- package/dist/eventsource.d.mts +2 -0
- package/dist/eventsource.mjs +2 -0
- package/dist/fetch-CQoQx3mz.mjs +301 -0
- package/dist/fetch-CSESA-oq.d.mts +33 -0
- package/dist/fetch.d.mts +2 -35
- package/dist/fetch.mjs +1 -188
- package/dist/http-DCCqK1QL.mjs +505 -0
- package/dist/{http-CE8-CIHE.d.mts → http-Dd6YCSHK.d.mts} +6 -14
- package/dist/http.d.mts +1 -1
- package/dist/http.mjs +1 -1
- package/dist/index.d.mts +19 -54
- package/dist/index.mjs +35 -104
- package/dist/networkDefaults.d.mts +5 -11
- package/dist/networkDefaults.mjs +1 -2
- package/dist/{shared-85INNxXV.mjs → shared-CARmZrDA.mjs} +5 -9
- package/dist/{shared-Dc42USJC.d.mts → shared-DfU0oEwy.d.mts} +0 -2
- package/dist/shared.d.mts +1 -1
- package/dist/shared.mjs +1 -1
- package/dist/streams-6m8eQfZM.mjs +84 -0
- package/dist/streams.d.mts +31 -0
- package/dist/streams.mjs +1 -0
- package/dist/textCodec-muWByCAW.mjs +132 -0
- package/dist/{url-D0ANYIyk.d.mts → url-CEgyUgm2.d.mts} +0 -2
- package/dist/url-DjMmeEth.mjs +285 -0
- package/dist/url.d.mts +1 -1
- package/dist/url.mjs +1 -229
- package/dist/{web-uZ-hezcd.mjs → web-DDzJNeV3.mjs} +6 -8
- package/dist/{web-DNBBg6y9.d.mts → web-DjLufM4u.d.mts} +1 -7
- package/dist/web.d.mts +1 -1
- package/dist/web.mjs +1 -1
- package/dist/websocket.d.mts +3 -4
- package/dist/websocket.mjs +4 -5
- package/dist/xhr.d.mts +2 -3
- package/dist/xhr.mjs +4 -5
- package/package.json +20 -6
- package/dist/http-mcdF3N9S.mjs +0 -303
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { d as normalizeHeaderName, h as isUrlSearchParamsInstance, l as isArrayBufferLike, m as isUrlInstance, n as cloneArrayBuffer, r as cloneArrayBufferView, u as isBlobLike } from "./shared-CARmZrDA.mjs";
|
|
2
|
+
import { a as isReadableBodyStream, c as createMultipartBoundary, i as consumeBodyValue, l as encodeMultipartFormData, n as RequestPolyfill, o as isStreamingRequestBody, s as readBodyStream, t as ResponsePolyfill } from "./http-DCCqK1QL.mjs";
|
|
3
|
+
import { t as ReadableStream } from "./streams-6m8eQfZM.mjs";
|
|
4
|
+
import { t as FormDataPolyfill } from "./web-DDzJNeV3.mjs";
|
|
5
|
+
import { resolveRequestMiniProgramOptions } from "./networkDefaults.mjs";
|
|
6
|
+
import { wpi } from "@wevu/api";
|
|
7
|
+
// src/fetch/abort.ts
|
|
8
|
+
/** 准备请求体期间也遵循同一中止信号,避免无限输入阻塞请求取消。 */
|
|
9
|
+
function requestAbortReason(signal) {
|
|
10
|
+
if (signal.reason !== void 0) return signal.reason;
|
|
11
|
+
const error = /* @__PURE__ */ new Error("The operation was aborted.");
|
|
12
|
+
error.name = "AbortError";
|
|
13
|
+
return error;
|
|
14
|
+
}
|
|
15
|
+
function abortable(operation, signal) {
|
|
16
|
+
if (!signal) return operation;
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
const onAbort = () => {
|
|
19
|
+
signal.removeEventListener("abort", onAbort);
|
|
20
|
+
reject(requestAbortReason(signal));
|
|
21
|
+
};
|
|
22
|
+
operation.then((value) => {
|
|
23
|
+
signal.removeEventListener("abort", onAbort);
|
|
24
|
+
resolve(value);
|
|
25
|
+
}, (error) => {
|
|
26
|
+
signal.removeEventListener("abort", onAbort);
|
|
27
|
+
reject(error);
|
|
28
|
+
});
|
|
29
|
+
if (signal.aborted) onAbort();
|
|
30
|
+
else signal.addEventListener("abort", onAbort, { once: true });
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
// src/fetch/meta.ts
|
|
34
|
+
const REQUEST_METHODS = [
|
|
35
|
+
"GET",
|
|
36
|
+
"HEAD",
|
|
37
|
+
"OPTIONS",
|
|
38
|
+
"POST",
|
|
39
|
+
"PUT",
|
|
40
|
+
"DELETE",
|
|
41
|
+
"TRACE",
|
|
42
|
+
"CONNECT"
|
|
43
|
+
];
|
|
44
|
+
const hasOwn = Object.prototype.hasOwnProperty;
|
|
45
|
+
function isObject(value) {
|
|
46
|
+
return typeof value === "object" && value !== null;
|
|
47
|
+
}
|
|
48
|
+
function normalizeMethod(method) {
|
|
49
|
+
const normalized = (method ?? "GET").toUpperCase();
|
|
50
|
+
if (REQUEST_METHODS.includes(normalized)) return normalized;
|
|
51
|
+
return "GET";
|
|
52
|
+
}
|
|
53
|
+
function setHeader(target, key, value) {
|
|
54
|
+
const normalizedKey = normalizeHeaderName(key);
|
|
55
|
+
if (!normalizedKey) return;
|
|
56
|
+
const nextValue = String(value);
|
|
57
|
+
for (const currentKey of Object.keys(target)) if (normalizeHeaderName(currentKey) === normalizedKey) delete target[currentKey];
|
|
58
|
+
target[key] = nextValue;
|
|
59
|
+
}
|
|
60
|
+
function hasHeader(target, key) {
|
|
61
|
+
const normalizedKey = normalizeHeaderName(key);
|
|
62
|
+
return Object.keys(target).some((currentKey) => normalizeHeaderName(currentKey) === normalizedKey);
|
|
63
|
+
}
|
|
64
|
+
function mergeHeaderSource(target, source) {
|
|
65
|
+
if (!source) return;
|
|
66
|
+
if (typeof source[Symbol.iterator] === "function") {
|
|
67
|
+
for (const entry of source) {
|
|
68
|
+
if (!entry || entry.length < 2) continue;
|
|
69
|
+
setHeader(target, entry[0], entry[1]);
|
|
70
|
+
}
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (typeof source.forEach === "function") {
|
|
74
|
+
source.forEach((value, key) => {
|
|
75
|
+
setHeader(target, key, value);
|
|
76
|
+
});
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (!isObject(source)) return;
|
|
80
|
+
for (const [key, value] of Object.entries(source)) {
|
|
81
|
+
if (Array.isArray(value)) {
|
|
82
|
+
setHeader(target, key, value.join(", "));
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
setHeader(target, key, value);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function toHeaderMap(source) {
|
|
89
|
+
const headers = {};
|
|
90
|
+
mergeHeaderSource(headers, source);
|
|
91
|
+
return headers;
|
|
92
|
+
}
|
|
93
|
+
function isRequestLikeInput(input) {
|
|
94
|
+
return isObject(input) && typeof input.url === "string";
|
|
95
|
+
}
|
|
96
|
+
function isFormDataInstance(value) {
|
|
97
|
+
return Boolean(value && typeof value === "object" && (typeof FormData !== "undefined" && value instanceof FormData || value instanceof FormDataPolyfill));
|
|
98
|
+
}
|
|
99
|
+
async function extractRequestBodyFromInput(input, signal) {
|
|
100
|
+
if (!input || typeof input.clone !== "function") return;
|
|
101
|
+
if (input.bodyUsed) throw new TypeError("Failed to execute fetch: request body is already used");
|
|
102
|
+
if (input instanceof RequestPolyfill) {
|
|
103
|
+
if (isStreamingRequestBody(input)) throw new TypeError("Streaming request uploads are not supported");
|
|
104
|
+
const value = consumeBodyValue(input);
|
|
105
|
+
return isReadableBodyStream(value) ? readBodyStream(value, signal) : value;
|
|
106
|
+
}
|
|
107
|
+
const body = input;
|
|
108
|
+
if (isReadableBodyStream(body.body)) return readBodyStream(body.body, signal);
|
|
109
|
+
if (body.arrayBuffer) return body.arrayBuffer();
|
|
110
|
+
if (body.text) return body.text();
|
|
111
|
+
const cloned = input.clone();
|
|
112
|
+
if (cloned?.arrayBuffer) return cloned.arrayBuffer();
|
|
113
|
+
if (cloned?.text) return cloned.text();
|
|
114
|
+
}
|
|
115
|
+
async function normalizeRequestBody(body, headers) {
|
|
116
|
+
if (body == null) return;
|
|
117
|
+
if (typeof body.getReader === "function") throw new TypeError("Streaming request uploads are not supported");
|
|
118
|
+
if (typeof body === "string") {
|
|
119
|
+
if (!hasHeader(headers, "content-type")) headers["content-type"] = "text/plain;charset=UTF-8";
|
|
120
|
+
return body;
|
|
121
|
+
}
|
|
122
|
+
if (isUrlSearchParamsInstance(body)) {
|
|
123
|
+
if (!hasHeader(headers, "content-type")) headers["content-type"] = "application/x-www-form-urlencoded;charset=UTF-8";
|
|
124
|
+
return body.toString();
|
|
125
|
+
}
|
|
126
|
+
if (isArrayBufferLike(body)) return cloneArrayBuffer(body);
|
|
127
|
+
if (ArrayBuffer.isView(body)) return cloneArrayBufferView(body);
|
|
128
|
+
if (isBlobLike(body)) {
|
|
129
|
+
if (body.type && !hasHeader(headers, "content-type")) headers["content-type"] = body.type;
|
|
130
|
+
return body.arrayBuffer();
|
|
131
|
+
}
|
|
132
|
+
if (isFormDataInstance(body)) {
|
|
133
|
+
const contentTypeKey = Object.keys(headers).find((key) => normalizeHeaderName(key) === "content-type");
|
|
134
|
+
const payload = await encodeMultipartFormData(body, createMultipartBoundary(contentTypeKey ? headers[contentTypeKey] : void 0));
|
|
135
|
+
if (!hasHeader(headers, "content-type")) headers["content-type"] = payload.contentType;
|
|
136
|
+
return payload.body;
|
|
137
|
+
}
|
|
138
|
+
return String(body);
|
|
139
|
+
}
|
|
140
|
+
async function resolveRequestMeta(input, init = {}) {
|
|
141
|
+
const requestInput = isRequestLikeInput(input) ? input : void 0;
|
|
142
|
+
const signal = init.signal ?? requestInput?.signal ?? null;
|
|
143
|
+
if (signal?.aborted) throw requestAbortReason(signal);
|
|
144
|
+
const url = typeof input === "string" ? input : isUrlInstance(input) ? input.toString() : requestInput?.url;
|
|
145
|
+
if (!url) throw new TypeError("Failed to execute fetch: invalid request url");
|
|
146
|
+
const method = normalizeMethod(init.method ?? requestInput?.method);
|
|
147
|
+
const headers = toHeaderMap(requestInput?.headers);
|
|
148
|
+
mergeHeaderSource(headers, init.headers);
|
|
149
|
+
const hasBodyInInit = hasOwn.call(init, "body");
|
|
150
|
+
let rawBody = hasBodyInInit ? init.body : await abortable(extractRequestBodyFromInput(requestInput, signal), signal);
|
|
151
|
+
if (!hasBodyInInit && requestInput && (method === "GET" || method === "HEAD")) rawBody = void 0;
|
|
152
|
+
if ((method === "GET" || method === "HEAD") && rawBody != null) throw new TypeError("Failed to execute fetch: GET/HEAD request cannot have body");
|
|
153
|
+
return {
|
|
154
|
+
miniProgram: resolveRequestMiniProgramOptions(init.miniProgram, init.miniprogram),
|
|
155
|
+
url,
|
|
156
|
+
method,
|
|
157
|
+
headers,
|
|
158
|
+
body: await abortable(normalizeRequestBody(rawBody, headers), signal),
|
|
159
|
+
signal
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
// src/fetch/transport.ts
|
|
163
|
+
/** 缺少真实流式元数据时明确失败,避免把未知 HTTP 状态伪装为成功。 */
|
|
164
|
+
function unsupportedStream(message) {
|
|
165
|
+
const error = new Error(message);
|
|
166
|
+
error.name = "NotSupportedError";
|
|
167
|
+
return error;
|
|
168
|
+
}
|
|
169
|
+
function abortError(signal) {
|
|
170
|
+
if (signal?.reason !== void 0) return signal.reason;
|
|
171
|
+
const error = /* @__PURE__ */ new Error("The operation was aborted.");
|
|
172
|
+
error.name = "AbortError";
|
|
173
|
+
return error;
|
|
174
|
+
}
|
|
175
|
+
function networkError(error) {
|
|
176
|
+
const message = error && typeof error === "object" && "errMsg" in error ? String(error.errMsg) : String(error);
|
|
177
|
+
return new TypeError(message);
|
|
178
|
+
}
|
|
179
|
+
/** 一个请求持有一个终止状态;返回 Response 不代表底层传输已经结束。 */
|
|
180
|
+
function sendFetchRequest(meta) {
|
|
181
|
+
if (meta.signal?.aborted) return Promise.reject(abortError(meta.signal));
|
|
182
|
+
return new Promise((resolve, reject) => {
|
|
183
|
+
const streaming = meta.miniProgram.enableChunked === true;
|
|
184
|
+
let task;
|
|
185
|
+
let terminated = false;
|
|
186
|
+
let responseStarted = false;
|
|
187
|
+
let abortPending = false;
|
|
188
|
+
let nullBody = meta.method === "HEAD";
|
|
189
|
+
let controller;
|
|
190
|
+
const body = streaming ? new ReadableStream({
|
|
191
|
+
start(value) {
|
|
192
|
+
controller = value;
|
|
193
|
+
},
|
|
194
|
+
cancel() {
|
|
195
|
+
stop(void 0, true);
|
|
196
|
+
}
|
|
197
|
+
}) : null;
|
|
198
|
+
function cleanup() {
|
|
199
|
+
meta.signal?.removeEventListener("abort", onAbort);
|
|
200
|
+
task?.offHeadersReceived?.(onHeaders);
|
|
201
|
+
task?.offChunkReceived?.(onChunk);
|
|
202
|
+
}
|
|
203
|
+
function abortTask() {
|
|
204
|
+
if (task) task.abort();
|
|
205
|
+
else abortPending = true;
|
|
206
|
+
}
|
|
207
|
+
function stop(error, cancel = false) {
|
|
208
|
+
if (terminated) return;
|
|
209
|
+
terminated = true;
|
|
210
|
+
cleanup();
|
|
211
|
+
if (error !== void 0) {
|
|
212
|
+
controller?.error(error);
|
|
213
|
+
if (!responseStarted) reject(error);
|
|
214
|
+
} else if (!cancel) controller?.close();
|
|
215
|
+
if (error !== void 0 || cancel) abortTask();
|
|
216
|
+
}
|
|
217
|
+
function onAbort() {
|
|
218
|
+
stop(abortError(meta.signal));
|
|
219
|
+
}
|
|
220
|
+
function respond(data, status, headers) {
|
|
221
|
+
nullBody ||= [
|
|
222
|
+
204,
|
|
223
|
+
205,
|
|
224
|
+
304
|
|
225
|
+
].includes(status);
|
|
226
|
+
const response = new ResponsePolyfill(nullBody ? null : data, {
|
|
227
|
+
status,
|
|
228
|
+
headers: toHeaderMap(headers),
|
|
229
|
+
url: meta.url
|
|
230
|
+
});
|
|
231
|
+
responseStarted = true;
|
|
232
|
+
resolve(response);
|
|
233
|
+
}
|
|
234
|
+
function onHeaders(result) {
|
|
235
|
+
if (terminated || responseStarted) return;
|
|
236
|
+
if (!Number.isInteger(result.statusCode) || result.statusCode < 200 || result.statusCode > 599) {
|
|
237
|
+
stop(unsupportedStream("Streaming fetch requires a real HTTP statusCode in onHeadersReceived"));
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
respond(body, result.statusCode, result.header);
|
|
241
|
+
}
|
|
242
|
+
function onChunk(result) {
|
|
243
|
+
if (terminated || nullBody) return;
|
|
244
|
+
if (!responseStarted) {
|
|
245
|
+
stop(unsupportedStream("Streaming fetch requires response headers before chunks"));
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
controller.enqueue(new Uint8Array(result.data).slice());
|
|
249
|
+
}
|
|
250
|
+
function success(result) {
|
|
251
|
+
if (terminated) return;
|
|
252
|
+
if (streaming && !responseStarted) {
|
|
253
|
+
stop(unsupportedStream("Streaming fetch requires early response headers"));
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (!streaming) respond(result.data, result.statusCode, result.header);
|
|
257
|
+
stop();
|
|
258
|
+
}
|
|
259
|
+
meta.signal?.addEventListener("abort", onAbort, { once: true });
|
|
260
|
+
try {
|
|
261
|
+
const result = wpi.request({
|
|
262
|
+
...meta.miniProgram,
|
|
263
|
+
url: meta.url,
|
|
264
|
+
method: meta.method,
|
|
265
|
+
header: meta.headers,
|
|
266
|
+
data: meta.body,
|
|
267
|
+
responseType: "arraybuffer",
|
|
268
|
+
success,
|
|
269
|
+
fail: (error) => stop(networkError(error))
|
|
270
|
+
});
|
|
271
|
+
if (result && typeof result.abort === "function") task = result;
|
|
272
|
+
if (abortPending) task?.abort();
|
|
273
|
+
if (terminated) {
|
|
274
|
+
cleanup();
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
if (streaming) {
|
|
278
|
+
if (!task || typeof task.onHeadersReceived !== "function" || typeof task.onChunkReceived !== "function") {
|
|
279
|
+
stop(unsupportedStream("Streaming fetch requires RequestTask headers and chunk listeners"));
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
task.onHeadersReceived(onHeaders);
|
|
283
|
+
if (terminated) return;
|
|
284
|
+
task.onChunkReceived(onChunk);
|
|
285
|
+
if (terminated) {
|
|
286
|
+
cleanup();
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (meta.signal?.aborted) onAbort();
|
|
291
|
+
} catch (error) {
|
|
292
|
+
stop(error);
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
// src/fetch.ts
|
|
297
|
+
/** 使用小程序请求传输提供 Fetch 接口,流式能力由宿主真实响应元数据决定。 */
|
|
298
|
+
function fetch(input, init) {
|
|
299
|
+
return resolveRequestMeta(input, init).then(sendFetchRequest);
|
|
300
|
+
}
|
|
301
|
+
export { fetch as t };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { RequestGlobalsMiniProgramOptions } from "./networkDefaults.mjs";
|
|
2
|
+
import { t as URLPolyfill } from "./url-CEgyUgm2.mjs";
|
|
3
|
+
import "@wevu/api";
|
|
4
|
+
interface RequestLikeInput {
|
|
5
|
+
url: string;
|
|
6
|
+
method?: string;
|
|
7
|
+
headers?: unknown;
|
|
8
|
+
signal?: AbortSignal | null;
|
|
9
|
+
bodyUsed?: boolean;
|
|
10
|
+
clone?: () => {
|
|
11
|
+
arrayBuffer?: () => Promise<ArrayBuffer>;
|
|
12
|
+
text?: () => Promise<string>;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
interface RequestGlobalsFetchInit {
|
|
16
|
+
method?: string;
|
|
17
|
+
headers?: unknown;
|
|
18
|
+
body?: unknown;
|
|
19
|
+
signal?: AbortSignal | null;
|
|
20
|
+
miniProgram?: RequestGlobalsMiniProgramOptions;
|
|
21
|
+
miniprogram?: RequestGlobalsMiniProgramOptions;
|
|
22
|
+
[key: string]: unknown;
|
|
23
|
+
}
|
|
24
|
+
declare global {
|
|
25
|
+
interface RequestInit {
|
|
26
|
+
miniProgram?: RequestGlobalsMiniProgramOptions;
|
|
27
|
+
miniprogram?: RequestGlobalsMiniProgramOptions;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
type RequestGlobalsFetchInput = string | URL | URLPolyfill | RequestLikeInput;
|
|
31
|
+
/** 使用小程序请求传输提供 Fetch 接口,流式能力由宿主真实响应元数据决定。 */
|
|
32
|
+
declare function fetch(input: RequestGlobalsFetchInput, init?: RequestGlobalsFetchInit): Promise<Response>;
|
|
33
|
+
export { RequestGlobalsFetchInit as n, fetch as t };
|
package/dist/fetch.d.mts
CHANGED
|
@@ -1,36 +1,3 @@
|
|
|
1
1
|
import { RequestGlobalsMiniProgramOptions } from "./networkDefaults.mjs";
|
|
2
|
-
import { t as
|
|
3
|
-
|
|
4
|
-
interface RequestLikeInput {
|
|
5
|
-
url: string;
|
|
6
|
-
method?: string;
|
|
7
|
-
headers?: unknown;
|
|
8
|
-
signal?: AbortSignal | null;
|
|
9
|
-
bodyUsed?: boolean;
|
|
10
|
-
clone?: () => {
|
|
11
|
-
arrayBuffer?: () => Promise<ArrayBuffer>;
|
|
12
|
-
text?: () => Promise<string>;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
export interface RequestGlobalsFetchInit {
|
|
16
|
-
method?: string;
|
|
17
|
-
headers?: unknown;
|
|
18
|
-
body?: unknown;
|
|
19
|
-
signal?: AbortSignal | null;
|
|
20
|
-
miniProgram?: RequestGlobalsMiniProgramOptions;
|
|
21
|
-
miniprogram?: RequestGlobalsMiniProgramOptions;
|
|
22
|
-
[key: string]: unknown;
|
|
23
|
-
}
|
|
24
|
-
declare global {
|
|
25
|
-
interface RequestInit {
|
|
26
|
-
miniProgram?: RequestGlobalsMiniProgramOptions;
|
|
27
|
-
miniprogram?: RequestGlobalsMiniProgramOptions;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
type RequestGlobalsFetchInput = string | URL | URLPolyfill | RequestLikeInput;
|
|
31
|
-
/**
|
|
32
|
-
* @description 使用 @wevu/api 的 request 能力实现 fetch 语义对齐。
|
|
33
|
-
*/
|
|
34
|
-
export declare function fetch(input: RequestGlobalsFetchInput, init?: RequestGlobalsFetchInit): Promise<Response>;
|
|
35
|
-
//#endregion
|
|
36
|
-
export type { RequestGlobalsMiniProgramOptions };
|
|
2
|
+
import { n as RequestGlobalsFetchInit, t as fetch } from "./fetch-CSESA-oq.mjs";
|
|
3
|
+
export { type RequestGlobalsFetchInit, type RequestGlobalsMiniProgramOptions, fetch };
|
package/dist/fetch.mjs
CHANGED
|
@@ -1,188 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { a as encodeMultipartFormData, i as consumeBodyValue, n as RequestPolyfill, o as HeadersPolyfill, t as ResponsePolyfill } from "./http-mcdF3N9S.mjs";
|
|
3
|
-
import { t as FormDataPolyfill } from "./web-uZ-hezcd.mjs";
|
|
4
|
-
import { resolveRequestMiniProgramOptions } from "./networkDefaults.mjs";
|
|
5
|
-
import { wpi } from "@wevu/api";
|
|
6
|
-
//#region src/fetch.ts
|
|
7
|
-
const REQUEST_METHODS = [
|
|
8
|
-
"GET",
|
|
9
|
-
"HEAD",
|
|
10
|
-
"OPTIONS",
|
|
11
|
-
"POST",
|
|
12
|
-
"PUT",
|
|
13
|
-
"DELETE",
|
|
14
|
-
"TRACE",
|
|
15
|
-
"CONNECT"
|
|
16
|
-
];
|
|
17
|
-
const hasOwn = Object.prototype.hasOwnProperty;
|
|
18
|
-
function isObject(value) {
|
|
19
|
-
return typeof value === "object" && value !== null;
|
|
20
|
-
}
|
|
21
|
-
function createAbortError() {
|
|
22
|
-
if (typeof DOMException === "function") return new DOMException("The operation was aborted.", "AbortError");
|
|
23
|
-
const error = /* @__PURE__ */ new Error("The operation was aborted.");
|
|
24
|
-
error.name = "AbortError";
|
|
25
|
-
return error;
|
|
26
|
-
}
|
|
27
|
-
function normalizeMethod(method) {
|
|
28
|
-
const normalized = (method ?? "GET").toUpperCase();
|
|
29
|
-
if (REQUEST_METHODS.includes(normalized)) return normalized;
|
|
30
|
-
return "GET";
|
|
31
|
-
}
|
|
32
|
-
function setHeader(target, key, value) {
|
|
33
|
-
const normalizedKey = normalizeHeaderName(key);
|
|
34
|
-
if (!normalizedKey) return;
|
|
35
|
-
const nextValue = String(value);
|
|
36
|
-
for (const currentKey of Object.keys(target)) if (normalizeHeaderName(currentKey) === normalizedKey) delete target[currentKey];
|
|
37
|
-
target[key] = nextValue;
|
|
38
|
-
}
|
|
39
|
-
function hasHeader(target, key) {
|
|
40
|
-
const normalizedKey = normalizeHeaderName(key);
|
|
41
|
-
return Object.keys(target).some((currentKey) => normalizeHeaderName(currentKey) === normalizedKey);
|
|
42
|
-
}
|
|
43
|
-
function mergeHeaderSource(target, source) {
|
|
44
|
-
if (!source) return;
|
|
45
|
-
if (typeof source[Symbol.iterator] === "function") {
|
|
46
|
-
for (const entry of source) {
|
|
47
|
-
if (!entry || entry.length < 2) continue;
|
|
48
|
-
setHeader(target, entry[0], entry[1]);
|
|
49
|
-
}
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
if (typeof source.forEach === "function") {
|
|
53
|
-
source.forEach((value, key) => {
|
|
54
|
-
setHeader(target, key, value);
|
|
55
|
-
});
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
if (!isObject(source)) return;
|
|
59
|
-
for (const [key, value] of Object.entries(source)) {
|
|
60
|
-
if (Array.isArray(value)) {
|
|
61
|
-
setHeader(target, key, value.join(", "));
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
setHeader(target, key, value);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
function toHeaderMap(source) {
|
|
68
|
-
const headers = {};
|
|
69
|
-
mergeHeaderSource(headers, source);
|
|
70
|
-
return headers;
|
|
71
|
-
}
|
|
72
|
-
function isRequestLikeInput(input) {
|
|
73
|
-
return isObject(input) && typeof input.url === "string";
|
|
74
|
-
}
|
|
75
|
-
function isFormDataInstance(value) {
|
|
76
|
-
return Boolean(value && typeof value === "object" && (typeof FormData !== "undefined" && value instanceof FormData || value instanceof FormDataPolyfill));
|
|
77
|
-
}
|
|
78
|
-
async function extractRequestBodyFromInput(input) {
|
|
79
|
-
if (!input || typeof input.clone !== "function") return;
|
|
80
|
-
if (input.bodyUsed) throw new TypeError("Failed to execute fetch: request body is already used");
|
|
81
|
-
if (input instanceof RequestPolyfill) return consumeBodyValue(input);
|
|
82
|
-
const body = input;
|
|
83
|
-
if (body.arrayBuffer) return body.arrayBuffer();
|
|
84
|
-
if (body.text) return body.text();
|
|
85
|
-
const cloned = input.clone();
|
|
86
|
-
if (cloned?.arrayBuffer) return cloned.arrayBuffer();
|
|
87
|
-
if (cloned?.text) return cloned.text();
|
|
88
|
-
}
|
|
89
|
-
async function normalizeRequestBody(body, headers) {
|
|
90
|
-
if (body == null) return;
|
|
91
|
-
if (typeof body === "string") {
|
|
92
|
-
if (!hasHeader(headers, "content-type")) headers["content-type"] = "text/plain;charset=UTF-8";
|
|
93
|
-
return body;
|
|
94
|
-
}
|
|
95
|
-
if (isUrlSearchParamsInstance(body)) {
|
|
96
|
-
if (!hasHeader(headers, "content-type")) headers["content-type"] = "application/x-www-form-urlencoded;charset=UTF-8";
|
|
97
|
-
return body.toString();
|
|
98
|
-
}
|
|
99
|
-
if (isArrayBufferLike(body)) return cloneArrayBuffer(body);
|
|
100
|
-
if (ArrayBuffer.isView(body)) return cloneArrayBufferView(body);
|
|
101
|
-
if (isBlobLike(body)) {
|
|
102
|
-
if (body.type && !hasHeader(headers, "content-type")) headers["content-type"] = body.type;
|
|
103
|
-
return body.arrayBuffer();
|
|
104
|
-
}
|
|
105
|
-
if (isFormDataInstance(body)) {
|
|
106
|
-
const payload = await encodeMultipartFormData(body);
|
|
107
|
-
if (!hasHeader(headers, "content-type")) headers["content-type"] = payload.contentType;
|
|
108
|
-
return payload.body;
|
|
109
|
-
}
|
|
110
|
-
return String(body);
|
|
111
|
-
}
|
|
112
|
-
async function resolveRequestMeta(input, init = {}) {
|
|
113
|
-
const requestInput = isRequestLikeInput(input) ? input : void 0;
|
|
114
|
-
const url = typeof input === "string" ? input : isUrlInstance(input) ? input.toString() : requestInput?.url;
|
|
115
|
-
if (!url) throw new TypeError("Failed to execute fetch: invalid request url");
|
|
116
|
-
const method = normalizeMethod(init.method ?? requestInput?.method);
|
|
117
|
-
const headers = toHeaderMap(requestInput?.headers);
|
|
118
|
-
mergeHeaderSource(headers, init.headers);
|
|
119
|
-
const hasBodyInInit = hasOwn.call(init, "body");
|
|
120
|
-
let rawBody = hasBodyInInit ? init.body : await extractRequestBodyFromInput(requestInput);
|
|
121
|
-
if (!hasBodyInInit && requestInput && (method === "GET" || method === "HEAD")) rawBody = void 0;
|
|
122
|
-
if ((method === "GET" || method === "HEAD") && rawBody != null) throw new TypeError("Failed to execute fetch: GET/HEAD request cannot have body");
|
|
123
|
-
return {
|
|
124
|
-
miniProgram: resolveRequestMiniProgramOptions(init.miniProgram, init.miniprogram),
|
|
125
|
-
url,
|
|
126
|
-
method,
|
|
127
|
-
headers,
|
|
128
|
-
body: await normalizeRequestBody(rawBody, headers),
|
|
129
|
-
signal: init.signal ?? requestInput?.signal ?? null
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
function createFetchResponse(data, status, headers, url) {
|
|
133
|
-
return new ResponsePolyfill(data instanceof Uint8Array ? cloneArrayBufferView(data) : data, {
|
|
134
|
-
status,
|
|
135
|
-
headers: new HeadersPolyfill(headers),
|
|
136
|
-
url
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
function isRequestTask(value) {
|
|
140
|
-
return isObject(value) && typeof value.abort === "function";
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* @description 使用 @wevu/api 的 request 能力实现 fetch 语义对齐。
|
|
144
|
-
*/
|
|
145
|
-
function fetch(input, init) {
|
|
146
|
-
return resolveRequestMeta(input, init).then((meta) => {
|
|
147
|
-
if (meta.signal?.aborted) return Promise.reject(createAbortError());
|
|
148
|
-
return new Promise((resolve, reject) => {
|
|
149
|
-
let settled = false;
|
|
150
|
-
let requestTask;
|
|
151
|
-
function onAbort() {
|
|
152
|
-
if (settled) return;
|
|
153
|
-
requestTask?.abort();
|
|
154
|
-
settled = true;
|
|
155
|
-
if (meta.signal) meta.signal.removeEventListener("abort", onAbort);
|
|
156
|
-
reject(createAbortError());
|
|
157
|
-
}
|
|
158
|
-
function cleanup() {
|
|
159
|
-
if (meta.signal) meta.signal.removeEventListener("abort", onAbort);
|
|
160
|
-
}
|
|
161
|
-
if (meta.signal) meta.signal.addEventListener("abort", onAbort, { once: true });
|
|
162
|
-
const requestResult = wpi.request({
|
|
163
|
-
...meta.miniProgram,
|
|
164
|
-
url: meta.url,
|
|
165
|
-
method: meta.method,
|
|
166
|
-
header: meta.headers,
|
|
167
|
-
data: meta.body,
|
|
168
|
-
responseType: "arraybuffer",
|
|
169
|
-
success: (res) => {
|
|
170
|
-
if (settled) return;
|
|
171
|
-
settled = true;
|
|
172
|
-
cleanup();
|
|
173
|
-
resolve(createFetchResponse(res.data, res.statusCode, toHeaderMap(res.header), meta.url));
|
|
174
|
-
},
|
|
175
|
-
fail: (error) => {
|
|
176
|
-
if (settled) return;
|
|
177
|
-
settled = true;
|
|
178
|
-
cleanup();
|
|
179
|
-
const message = isObject(error) && typeof error.errMsg === "string" ? error.errMsg : String(error);
|
|
180
|
-
reject(new TypeError(message));
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
requestTask = isRequestTask(requestResult) ? requestResult : void 0;
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
//#endregion
|
|
188
|
-
export { fetch };
|
|
1
|
+
export { t as fetch } from "./fetch-CQoQx3mz.mjs";
|