Open normal-coder opened 1 month ago
我最近补充一下这个封装uniapp.request的测试用例吧,实现就是生成的client会调用request方法,request方法可以用requestLibPath去引用自定义的request函数,request接收的参数是用axios的参数形式,就是经典的request(url,{method: get,data:body},options)这种参数调用形式,对应到uniapp.request,就是需要判断method来映射到uniapp的request上面,不知道您能否听懂这个简要描述
export default async function request(url, options = {}) {
return new Promise((resolve, reject) => {
const {
method = 'GET',
headers = {},
data = {},
timeout,
responseType = 'json',
withCredentials,
...otherOptions
} = options;
uni.request({
url,
method,
header: headers,
data,
timeout,
dataType: responseType === 'json' ? 'json' : 'text',
withCredentials, // 用于跨域请求时是否携带凭证
...otherOptions,
success: (res) => {
// 构造符合 Axios 的响应对象
const response = {
data: res.data,
status: res.statusCode,
statusText: res.errMsg,
headers: res.header,
config: options,
request: res
};
// 根据 HTTP 状态码判断请求是否成功
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(response);
} else {
reject(response);
}
},
fail: (error) => {
// 构造符合 Axios 错误格式的对象
const err = {
message: error.errMsg || 'Request failed',
config: options,
request: error
};
reject(err);
}
});
});
}
你只需要用 uni.app 实现一个request函数,然后配置 openapi-ts-request 配置就可以了
import type { GenerateServiceProps } from 'openapi-ts-request';
const schemaPath =
'http://xxxx.com/v2/api-docs?group=HealthCenter_API';
export default [
{
schemaPath: schemaPath,
requestLibPath: 'import request from "@/core/request.ts"',
},
] as GenerateServiceProps[];
如果还有疑问,可以加我vx: 1055120207
补充:记录使用 @uni-helper/axios-adapter
适配 uni.request
请求
import type { GenerateServiceProps } from 'openapi-ts-request';
const schemaPath =
'http://xxxx.com/v2/api-docs?group=HealthCenter_API';
export default [
{
schemaPath: schemaPath,
requestImportStatement: `import request from 'axios';\n
import { createUniAppAxiosAdapter } from '@uni-helper/axios-adapter';\n
request.defaults.adapter = createUniAppAxiosAdapter();`,
},
] as GenerateServiceProps[];
Is your feature request related to a problem? Please describe.
想试试这个库接 uniapp,发现对 uni.request 封装的细节就提了个标题。。。
指定请求库 requestLibPath 也是,就一个参数。然后细节就不管了。。没有任何标准或输入输出,header 或回调函数的实现要求说明。
Describe the solution you'd like
希望有完整的说明文档或适当提供及程序之
Describe alternatives you've considered
Additional context
-