# Request API 配置说明
# Ajax
后端请求:
Content-Type: 'application/json'
类型
# type: api, source: backEdn
读此文档,默认认为对 typescript 有一点了解
interface HttpRequestConfigExt {
testUrl?: string;
/**
* 描述来源
*/
source: HttpDataSource;
/**
* 描述请求方式,
*/
type: HttpRequestType;
/**
* 转换请求参数为下划线
*/
underlizeQuery?: boolean;
// 针对该请求的转换器
convert?: Constructor<any>;
}
/**
* http 请求配置, 继承自基础配置
*/
interface HttpRequestConfig extends HttpRequestConfigExt {
path: string;
method: HTTPMethod;
baseUrl?: string;
// 'production' | 'development' | 'test' | 'qa'
env?: ENV;
headers?: HttpHeader;
// GET 请求参数
query?: APIQueryArgCollection;
// POST 请求参数, 支持 FromData
data?: APIBodyArgCollection;
isAuth?: boolean;
timeout?: Timestamp;
// xhr mode
mode?: 'no-cors' | 'cors';
// 携带 cookie
withCredentials?: boolean;
underlizeDataKey?: boolean;
camelizeResponseData?: boolean;
referer?: string;
useFetch?: boolean;
useCache?: boolean;
// encodeURIComponent url query 参数,
useEncode?: boolean;
}
基础配置请参考基础配置文档
path
: 请求路径required
method
: http请求方法required
baseUrl
: 该配置会和path
拼接成完整的请求 url,进行发送请求。
注意:如果省略 baseUrl 不传,则需要将
path
写成完整的请求路径
,否则会出现前端配置错误
, 同时请求必须是https
开头, 同时 baseUrl 会被认为是正式地址
env
: 控制当前接口环境 默认为:production
注意: 如果不设置 env, 默认会是
production
环境
query
: 请求 url query 参数, 默认为 {}data
: 请求 body; 默认为 {}
注意: 如果是data 为 FormData 类型的数据类型, 请设置 header 中的
Content-Type: 'application/x-www-form-urlencoded'
isAuth
: 该请求是否需要登录态。默认为 true, 如果不需要登录态,明确设置为false
注意: 默认会从 localStorage 或者是 sessionStorage 中获取
key
为tokenParams
的值作为登录态
timeout
: 请求超时时间, 默认使用 20 * 1000mode
: 请参考 xhr 对象中对 mode 的描述withCredentials
: 请参考 xhr 对象中对 withCredentials 的描述underlizeDataKey
: 是否需要将data
对象转换为下划线, 如果不需要,请明确设置为false
camelizeResponseData
: 是否需要将response
对象转换为驼峰, 如果不需要,请明确设置为false
referer
: 设置请求的 referer 头; 仅在 fetch api 中才支持referrerPolicy
: 配合设置请求的 referer 头; 仅在 fetch api 中才支持, 类型值为: "" | "no-referrer" | "no-referrer-when-downgrade" | "origin" | "origin-when-cross-origin" | "same-origin" | "strict-origin" | "strict-origin-when-cross-origin" | "unsafe-url";useFetch
: 控制内部请求是否使用 fetch api; 如果检测到当前环境中不支持 fetch api ,同样会切换到 xhruseCache
: 对于 get 请求接口返回的数据进行缓存useEncode
: 对于请求 query 进行 encodeURIComponent; 默认处理
注意: 当测试地址为空时,非正式环境: testUrl 未设置,切换为正式接口;
最佳使用方式:
import { slugRequest } from "@tencent/slug-request";
const config2: ApiTypes.HttpRequestConfig = {
baseUrl: 'https://test.game.qq.com',
path: '/wmp/v3.1/public/searchNews.php',
query: {
p1: 1,
},
source: 'v4',
type: 'api',
method: 'POST',
underlizeQuery: false
}
interface ResponseData {
username: string;
avatar: string;
}
export async function requestV4Detail() {
return slugRequest<ResponseData>(config2);
}