|
@@ -3,15 +3,28 @@ if (typeof globalThis.Headers === 'undefined') {
|
|
|
// 这里会由headers-polyfill.js注册
|
|
// 这里会由headers-polyfill.js注册
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Response 类型枚举
|
|
|
|
|
+type ResponseType = 'basic' | 'cors' | 'default' | 'error' | 'opaque' | 'opaqueredirect'
|
|
|
|
|
+
|
|
|
export class ResponsePolyfill {
|
|
export class ResponsePolyfill {
|
|
|
|
|
+ // 标准 Response 接口的额外属性(小程序环境简化实现)
|
|
|
|
|
+ readonly redirected: boolean = false
|
|
|
|
|
+ readonly type: ResponseType = 'basic'
|
|
|
|
|
+ readonly url: string = ''
|
|
|
|
|
+
|
|
|
constructor(
|
|
constructor(
|
|
|
public body: string | ArrayBuffer | null,
|
|
public body: string | ArrayBuffer | null,
|
|
|
public init: {
|
|
public init: {
|
|
|
status?: number
|
|
status?: number
|
|
|
statusText?: string
|
|
statusText?: string
|
|
|
headers?: Record<string, string>
|
|
headers?: Record<string, string>
|
|
|
|
|
+ url?: string
|
|
|
} = {}
|
|
} = {}
|
|
|
- ) {}
|
|
|
|
|
|
|
+ ) {
|
|
|
|
|
+ if (init.url) {
|
|
|
|
|
+ this.url = init.url
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
get ok(): boolean {
|
|
get ok(): boolean {
|
|
|
return this.status >= 200 && this.status < 300
|
|
return this.status >= 200 && this.status < 300
|
|
@@ -33,6 +46,19 @@ export class ResponsePolyfill {
|
|
|
return false // 小程序环境简单实现
|
|
return false // 小程序环境简单实现
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // bytes() 是一个返回 Promise<Uint8Array> 的方法(较新的 Web API)
|
|
|
|
|
+ async bytes(): Promise<Uint8Array> {
|
|
|
|
|
+ if (this.body instanceof ArrayBuffer) {
|
|
|
|
|
+ return new Uint8Array(this.body)
|
|
|
|
|
+ }
|
|
|
|
|
+ if (typeof this.body === 'string') {
|
|
|
|
|
+ // 将字符串转为 Uint8Array
|
|
|
|
|
+ const encoder = new TextEncoder()
|
|
|
|
|
+ return encoder.encode(this.body)
|
|
|
|
|
+ }
|
|
|
|
|
+ throw new Error('No body available')
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
async arrayBuffer(): Promise<ArrayBuffer> {
|
|
async arrayBuffer(): Promise<ArrayBuffer> {
|
|
|
if (this.body instanceof ArrayBuffer) {
|
|
if (this.body instanceof ArrayBuffer) {
|
|
|
return this.body
|
|
return this.body
|
|
@@ -40,6 +66,16 @@ export class ResponsePolyfill {
|
|
|
throw new Error('Not implemented')
|
|
throw new Error('Not implemented')
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ async blob(): Promise<Blob> {
|
|
|
|
|
+ // 小程序环境不支持 Blob,抛出错误
|
|
|
|
|
+ throw new Error('Blob is not supported in mini program environment')
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async formData(): Promise<FormData> {
|
|
|
|
|
+ // 小程序环境不支持 FormData,抛出错误
|
|
|
|
|
+ throw new Error('FormData is not supported in mini program environment')
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
async text(): Promise<string> {
|
|
async text(): Promise<string> {
|
|
|
if (typeof this.body === 'string') {
|
|
if (typeof this.body === 'string') {
|
|
|
return this.body
|
|
return this.body
|
|
@@ -47,29 +83,33 @@ export class ResponsePolyfill {
|
|
|
throw new Error('Not implemented')
|
|
throw new Error('Not implemented')
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async json<T = any>(): Promise<T> {
|
|
|
|
|
|
|
+ async json<T = unknown>(): Promise<T> {
|
|
|
if (typeof this.body === 'string') {
|
|
if (typeof this.body === 'string') {
|
|
|
try {
|
|
try {
|
|
|
return JSON.parse(this.body)
|
|
return JSON.parse(this.body)
|
|
|
- } catch (e) {
|
|
|
|
|
|
|
+ } catch {
|
|
|
throw new Error('Invalid JSON')
|
|
throw new Error('Invalid JSON')
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
throw new Error('Not implemented')
|
|
throw new Error('Not implemented')
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- clone(): ResponsePolyfill {
|
|
|
|
|
- return new ResponsePolyfill(this.body, { ...this.init })
|
|
|
|
|
|
|
+ clone(): this {
|
|
|
|
|
+ return new ResponsePolyfill(this.body, { ...this.init }) as this
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- static json(data: any, init?: ResponseInit): ResponsePolyfill {
|
|
|
|
|
|
|
+ static json(data: unknown, init?: ResponseInit): ResponsePolyfill {
|
|
|
const headers = new Headers(init && 'headers' in init ? init.headers : undefined)
|
|
const headers = new Headers(init && 'headers' in init ? init.headers : undefined)
|
|
|
if (!headers.has('Content-Type')) {
|
|
if (!headers.has('Content-Type')) {
|
|
|
headers.set('Content-Type', 'application/json')
|
|
headers.set('Content-Type', 'application/json')
|
|
|
}
|
|
}
|
|
|
|
|
+ // @ts-expect-error - Headers.raw() 是非标准 API,但在小程序环境中需要使用
|
|
|
|
|
+ const headersRecord = typeof (headers as { raw?: () => Record<string, string> }).raw === 'function'
|
|
|
|
|
+ ? (headers as { raw: () => Record<string, string> }).raw()
|
|
|
|
|
+ : {}
|
|
|
return new ResponsePolyfill(JSON.stringify(data), {
|
|
return new ResponsePolyfill(JSON.stringify(data), {
|
|
|
...init,
|
|
...init,
|
|
|
- headers: (headers as any).raw ? (headers as any).raw() : {}
|
|
|
|
|
|
|
+ headers: headersRecord
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -88,6 +128,7 @@ export class ResponsePolyfill {
|
|
|
// 可选的全局注册函数
|
|
// 可选的全局注册函数
|
|
|
export function registerGlobalResponsePolyfill(): void {
|
|
export function registerGlobalResponsePolyfill(): void {
|
|
|
if (typeof globalThis.Response === 'undefined') {
|
|
if (typeof globalThis.Response === 'undefined') {
|
|
|
- globalThis.Response = ResponsePolyfill as any
|
|
|
|
|
|
|
+ // @ts-expect-error - 全局类型赋值
|
|
|
|
|
+ globalThis.Response = ResponsePolyfill
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|