diff --git a/packages/core/src/adapters/download.ts b/packages/core/src/adapters/download.ts index a3fcbfe..91aec08 100644 --- a/packages/core/src/adapters/download.ts +++ b/packages/core/src/adapters/download.ts @@ -1,6 +1,7 @@ import statuses from "statuses-es"; import type { UnCancelTokenListener } from "../core/UnCancelToken"; import { UnCanceledError } from "../core/UnCanceledError"; +import { UnError } from "../core/UnError"; import { settle } from "../core/settle"; import type { UnConfig, UnData, UnResponse } from "../types"; import { buildDownloadConfig } from "../utils"; @@ -26,7 +27,6 @@ export const downloadAdapter = ( signal?.removeEventListener("abort", onCanceled); }; - let response: UnResponse; let task: UniApp.DownloadTask | undefined; task = uni.downloadFile({ @@ -40,8 +40,7 @@ export const downloadAdapter = ( // 当 statusCode 不合法、statuses 抛出错误时,设置 statusText 为 undefined statusText = undefined; } - response = { - ...response, + const response: UnResponse = { // @ts-expect-error no types errMsg: res?.errMsg ?? res?.errmsg ?? res?.msg ?? res?.message, // @ts-expect-error no types @@ -61,21 +60,6 @@ export const downloadAdapter = ( }, task, }; - }, - fail: (err) => { - response = { - ...response, - errMsg: err?.errMsg ?? err?.errmsg ?? err?.msg ?? err?.message, - errno: err?.errno, - }; - }, - complete: () => { - if (onHeadersReceived) { - task?.offHeadersReceived(onHeadersReceived); - } - if (onProgressUpdate) { - task?.offProgressUpdate(onProgressUpdate); - } settle>( (val) => { resolve(val); @@ -88,6 +72,17 @@ export const downloadAdapter = ( response, ); }, + fail: (err) => { + reject(new UnError(err.errMsg, UnError.ERR_NETWORK, config, task)); + }, + complete: () => { + if (onHeadersReceived) { + task?.offHeadersReceived(onHeadersReceived); + } + if (onProgressUpdate) { + task?.offProgressUpdate(onProgressUpdate); + } + }, }); if (onHeadersReceived) { diff --git a/packages/core/src/adapters/request.ts b/packages/core/src/adapters/request.ts index 75e81d7..b1033b5 100644 --- a/packages/core/src/adapters/request.ts +++ b/packages/core/src/adapters/request.ts @@ -1,6 +1,7 @@ import statuses from "statuses-es"; import type { UnCancelTokenListener } from "../core/UnCancelToken"; import { UnCanceledError } from "../core/UnCanceledError"; +import { UnError } from "../core/UnError"; import { settle } from "../core/settle"; import type { UnConfig, UnData, UnResponse } from "../types"; import { buildRequestConfig } from "../utils"; @@ -19,7 +20,6 @@ export const requestAdapter = ( signal?.removeEventListener?.("abort", onCanceled); }; - let response: UnResponse; let task: UniApp.RequestTask | undefined; task = uni.request({ @@ -33,8 +33,7 @@ export const requestAdapter = ( // 当 statusCode 不合法、statuses 抛出错误时,设置 statusText 为 undefined statusText = undefined; } - response = { - ...response, + const response: UnResponse = { // @ts-expect-error no types errMsg: res?.errMsg ?? res?.errmsg ?? res?.msg ?? res?.message, // @ts-expect-error no types @@ -50,24 +49,6 @@ export const requestAdapter = ( data: res?.data, task, }; - }, - fail: (err) => { - response = { - ...response, - // @ts-expect-error no types - errMsg: err?.errMsg ?? err?.errmsg ?? err?.msg ?? err?.message, - // @ts-expect-error no types - errno: err?.errno, - }; - }, - complete: () => { - if (onHeadersReceived) { - task?.offHeadersReceived(onHeadersReceived); - } - if (onChunkReceived) { - // @ts-expect-error uni-app types lost - task?.offChunkReceived(onChunkReceived); - } settle>( (val) => { resolve(val); @@ -80,6 +61,18 @@ export const requestAdapter = ( response, ); }, + fail: (err) => { + reject(new UnError(err.errMsg, UnError.ERR_NETWORK, config, task)); + }, + complete: () => { + if (onHeadersReceived) { + task?.offHeadersReceived(onHeadersReceived); + } + if (onChunkReceived) { + // @ts-expect-error uni-app types lost + task?.offChunkReceived(onChunkReceived); + } + }, }); if (onHeadersReceived) { diff --git a/packages/core/src/adapters/upload.ts b/packages/core/src/adapters/upload.ts index 3c61a38..b0d4534 100644 --- a/packages/core/src/adapters/upload.ts +++ b/packages/core/src/adapters/upload.ts @@ -1,6 +1,7 @@ import statuses from "statuses-es"; import type { UnCancelTokenListener } from "../core/UnCancelToken"; import { UnCanceledError } from "../core/UnCanceledError"; +import { UnError } from "../core/UnError"; import { settle } from "../core/settle"; import type { UnConfig, UnData, UnResponse } from "../types"; import { buildUploadConfig } from "../utils"; @@ -24,7 +25,6 @@ export const uploadAdapter = (config: UnConfig) => signal?.removeEventListener("abort", onCanceled); }; - let response: UnResponse; let task: UniApp.UploadTask | undefined; task = uni.uploadFile({ @@ -38,8 +38,7 @@ export const uploadAdapter = (config: UnConfig) => // 当 statusCode 不合法、statuses 抛出错误时,设置 statusText 为 undefined statusText = undefined; } - response = { - ...response, + const response: UnResponse = { // @ts-expect-error no types errMsg: res?.errMsg ?? res?.errmsg ?? res?.msg ?? res?.message, // @ts-expect-error no types @@ -53,23 +52,6 @@ export const uploadAdapter = (config: UnConfig) => data: res?.data, task, }; - }, - fail: (err) => { - response = { - ...response, - // @ts-expect-error no types - errMsg: err?.errMsg ?? err?.errmsg ?? err?.msg ?? err?.message, - // @ts-expect-error no types - errno: err?.errno, - }; - }, - complete: () => { - if (onHeadersReceived) { - task?.offHeadersReceived(onHeadersReceived); - } - if (onProgressUpdate) { - task?.offProgressUpdate(onProgressUpdate); - } settle>( (val) => { resolve(val); @@ -82,6 +64,17 @@ export const uploadAdapter = (config: UnConfig) => response, ); }, + fail: (err) => { + reject(new UnError(err.errMsg, UnError.ERR_NETWORK, config, task)); + }, + complete: () => { + if (onHeadersReceived) { + task?.offHeadersReceived(onHeadersReceived); + } + if (onProgressUpdate) { + task?.offProgressUpdate(onProgressUpdate); + } + }, }); if (onHeadersReceived) {