Skip to content

缺失对如何自定义 requestLib 的说明或介绍,以及如何适配uniapp #46

Closed

Description

@normal-coder

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

Activity

  1. rookie-luochao commented on Aug 10, 2024

    @rookie-luochao
    Member

    我最近补充一下这个封装uniapp.request的测试用例吧,实现就是生成的client会调用request方法,request方法可以用requestLibPath去引用自定义的request函数,request接收的参数是用axios的参数形式,就是经典的request(url,{method: get,data:body},options)这种参数调用形式,对应到uniapp.request,就是需要判断method来映射到uniapp的request上面,不知道您能否听懂这个简要描述

  2. rookie-luochao commented on Aug 15, 2024

    @rookie-luochao
    Member
    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

  3. rookie-luochao commented on Aug 29, 2024

    @rookie-luochao
    Member

    补充:记录使用 @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[];
    
  4. changed the title [-]缺失对如何自定义 requestLib 的说明或介绍[/-] [+]缺失对如何自定义 requestLib 的说明或介绍,以及如何适配uniapp[/+] on Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions