feat: 添加符合 HTTP 规范的 statusText (#49)

pull/54/head
zjxxxxxxxxx 2023-11-01 20:45:19 +08:00 committed by GitHub
parent 7e7330405e
commit 3848fdd81b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 86 additions and 49 deletions

View File

@ -55,7 +55,7 @@ axios-miniprogram 是一款为小程序平台量身定制的轻量级请求库
## 关于在跨端框架中使用时的支持度 ## 关于在跨端框架中使用时的支持度
问:在 uni-app 或者 Taro 等等这类跨端框架中使用时,该请求库支持 h5APPvue3taro 等等吗? 问:在 uni-app 或者 Taro 等等这类跨端框架中使用时,该请求库支持 h5APPvue3 等等吗?
答:该请求库只是对框架提供的请求 API 进行了封装,并没有使用什么黑魔法。理论上来讲,框架支持的平台,该库也必然支持。 答:该请求库只是对框架提供的请求 API 进行了封装,并没有使用什么黑魔法。理论上来讲,框架支持的平台,该库也必然支持。

View File

@ -125,10 +125,6 @@ axios.defaults.adapter = (config) => {
// 默认值200 // 默认值200
status: response.statusCode, status: response.statusCode,
// 状态文本
// 默认值:'OK'
statusText: 'OK',
// 响应头 // 响应头
// 默认值:{} // 默认值:{}
headers: response.header, headers: response.header,
@ -146,10 +142,6 @@ axios.defaults.adapter = (config) => {
// 默认值400 // 默认值400
status: 400, status: 400,
// 状态文本
// 默认值:'Fail'
statusText: 'Fail',
// 响应头 // 响应头
// 默认值:{} // 默认值:{}
headers: {}, headers: {},
@ -180,10 +172,6 @@ axios.defaults.adapter = (config) => {
// 默认值200 // 默认值200
status: response.statusCode, status: response.statusCode,
// 状态文本
// 默认值:'OK'
statusText: 'OK',
// 响应头 // 响应头
// 默认值:{} // 默认值:{}
headers: response.header, headers: response.header,
@ -198,10 +186,6 @@ axios.defaults.adapter = (config) => {
// 默认值400 // 默认值400
status: 400, status: 400,
// 状态文本
// 默认值:'Fail'
statusText: 'Fail',
// 响应头 // 响应头
// 默认值:{} // 默认值:{}
headers: {}, headers: {},
@ -225,10 +209,6 @@ axios.defaults.adapter = (config) => {
// 默认值200 // 默认值200
status: response.statusCode, status: response.statusCode,
// 状态文本
// 默认值:'OK'
statusText: 'OK',
// 响应头 // 响应头
// 默认值:{} // 默认值:{}
headers: response.header, headers: response.header,
@ -246,10 +226,6 @@ axios.defaults.adapter = (config) => {
// 默认值400 // 默认值400
status: 400, status: 400,
// 状态文本
// 默认值:'Fail'
statusText: 'Fail',
// 响应头 // 响应头
// 默认值:{} // 默认值:{}
headers: {}, headers: {},

View File

@ -9,7 +9,7 @@ async function main() {
const { framework } = await enquirer.prompt<{ framework: string }>({ const { framework } = await enquirer.prompt<{ framework: string }>({
type: 'select', type: 'select',
name: 'framework', name: 'framework',
message: '请选择端框架', message: '请选择端框架',
choices: readdirSync(resolve('examples')).filter((i) => i !== '.DS_Store'), choices: readdirSync(resolve('examples')).filter((i) => i !== '.DS_Store'),
}); });

View File

@ -29,13 +29,11 @@ export function noop() {
export function mockResponse( export function mockResponse(
status = 200, status = 200,
statusText = 'OK',
headers: AnyObject = {}, headers: AnyObject = {},
data: AnyObject = {}, data: AnyObject = {},
) { ) {
return { return {
status, status,
statusText,
headers, headers,
data, data,
}; };
@ -72,13 +70,13 @@ function mockAdapterBase(
if (!canceled) { if (!canceled) {
switch (type) { switch (type) {
case 'success': case 'success':
config.success(mockResponse(200, 'OK', headers, data)); config.success(mockResponse(200, headers, data));
break; break;
case 'error': case 'error':
config.success(mockResponse(500, 'ERROR', headers, data)); config.success(mockResponse(500, headers, data));
break; break;
case 'fail': case 'fail':
config.fail(mockResponse(400, 'FAIL', headers, data)); config.fail(mockResponse(400, headers, data));
break; break;
} }

View File

@ -44,10 +44,6 @@ export interface AxiosAdapterResponse extends AnyObject {
* *
*/ */
status?: number; status?: number;
/**
*
*/
statusText?: string;
/** /**
* *
*/ */
@ -66,10 +62,6 @@ export interface AxiosAdapterResponseError extends AnyObject {
* *
*/ */
status?: number; status?: number;
/**
*
*/
statusText?: string;
/** /**
* *
*/ */
@ -344,6 +336,7 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
): AxiosAdapterPlatformTask { ): AxiosAdapterPlatformTask {
const options = baseOptions as AxiosAdapterDownloadOptions; const options = baseOptions as AxiosAdapterDownloadOptions;
const { params, success } = options; const { params, success } = options;
options.filePath = params?.filePath; options.filePath = params?.filePath;
options.success = (_response) => { options.success = (_response) => {
const response = Object.assign( const response = Object.assign(
@ -375,6 +368,7 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
): AxiosAdapterPlatformTask { ): AxiosAdapterPlatformTask {
const options = baseOptions as AxiosAdapterUploadOptions; const options = baseOptions as AxiosAdapterUploadOptions;
const { name, filePath, fileType, ...formData } = options.data as AnyObject; const { name, filePath, fileType, ...formData } = options.data as AnyObject;
options.name = name; options.name = name;
options.filePath = filePath; options.filePath = filePath;
options.formData = formData; options.formData = formData;

View File

@ -0,0 +1,68 @@
const httpStatusCodes = {
100: 'Continue',
101: 'Switching Protocols',
102: 'Processing',
103: 'Early Hints',
200: 'OK',
201: 'Created',
202: 'Accepted',
203: 'Non-Authoritative Information',
204: 'No Content',
205: 'Reset Content',
206: 'Partial Content',
207: 'Multi-Status',
208: 'Already Reported',
226: 'IM Used',
300: 'Multiple Choices',
301: 'Moved Permanently',
302: 'Found',
303: 'See Other',
304: 'Not Modified',
307: 'Temporary Redirect',
308: 'Permanent Redirect',
400: 'Bad Request',
401: 'Unauthorized',
402: 'Payment Required',
403: 'Forbidden',
404: 'Not Found',
405: 'Method Not Allowed',
406: 'Not Acceptable',
407: 'Proxy Authentication Required',
408: 'Request Timeout',
409: 'Conflict',
410: 'Gone',
411: 'Length Required',
412: 'Precondition Failed',
413: 'Content Too Large',
414: 'URI Too Long',
415: 'Unsupported Media Type',
416: 'Range Not Satisfiable',
417: 'Expectation Failed',
418: "I'm a teapot",
421: 'Misdirected Request',
422: 'Unprocessable Content',
423: 'Locked',
424: 'Failed Dependency',
425: 'Too Early',
426: 'Upgrade Required',
428: 'Precondition Required',
429: 'Too Many Requests',
431: 'Request Header Fields Too Large',
451: 'Unavailable For Legal Reasons',
500: 'Internal Server Error',
501: 'Not Implemented',
502: 'Bad Gateway',
503: 'Service Unavailable',
504: 'Gateway Timeout',
505: 'HTTP Version Not Supported',
506: 'Variant Also Negotiates',
507: 'Insufficient Storage',
508: 'Loop Detected',
510: 'Not Extended',
511: 'Network Authentication Required',
} as const;
export function getHttpStatusText(status: number) {
// @ts-ignore
return httpStatusCodes[status] || 'Unknown';
}

View File

@ -1,5 +1,6 @@
import { isFunction, isPlainObject } from '../helpers/types'; import { isFunction, isPlainObject } from '../helpers/types';
import { transformURL } from '../helpers/transformURL'; import { transformURL } from '../helpers/transformURL';
import { getHttpStatusText } from '../helpers/getHttpStatusText';
import { import {
AxiosRequestConfig, AxiosRequestConfig,
AxiosResponse, AxiosResponse,
@ -46,11 +47,11 @@ export function request(config: AxiosRequestConfig) {
console.error(err); console.error(err);
} }
function success(baseResponse: AxiosAdapterResponse): void { function success(rawResponse: AxiosAdapterResponse): void {
const response = baseResponse as AxiosResponse; const response = rawResponse as AxiosResponse;
response.status = response.status ?? 200; response.status ||= 200;
response.statusText = response.statusText ?? 'OK'; response.statusText ||= getHttpStatusText(response.status);
response.headers = response.headers ?? {}; response.headers ||= {};
response.config = config; response.config = config;
response.request = adapterTask; response.request = adapterTask;
@ -62,12 +63,12 @@ export function request(config: AxiosRequestConfig) {
} }
} }
function fail(baseResponseError: AxiosAdapterResponseError): void { function fail(rawResponseError: AxiosAdapterResponseError): void {
const responseError = baseResponseError as AxiosResponseError; const responseError = rawResponseError as AxiosResponseError;
responseError.isFail = true; responseError.isFail = true;
responseError.status = responseError.status ?? 400; responseError.status ||= 400;
responseError.statusText = responseError.statusText ?? 'Fail'; responseError.statusText ||= getHttpStatusText(responseError.status);
responseError.headers = responseError.headers ?? {}; responseError.headers ||= {};
responseError.config = config; responseError.config = config;
responseError.request = adapterTask; responseError.request = adapterTask;