chenshuai2144 / openapi2typescript

一个被大范围使用的小工具
312 stars 136 forks source link

生成service代码时,能否去除后台统一包装的那一层结构? #168

Open HongKing opened 1 month ago

HongKing commented 1 month ago

需求描述: 后台返回的数据都会统一包装一层(如下),实际使用时大多都直接用data里的内容(只有出错时才用code、message),能否增加配置标识是否提取返回对象中的某个属性作为真正的返回参数?

{
  status: number,
  code?: string,
  message?: string,
  data?: any
}

后台openapi.json: api-docs.json

目前生成的代码

// service.ts
export async function test22(
  options ?: {[key: string]: any}
) {
  return request<Platform.ReturnDataUserInfo>('/api/app/test2', {
  method: 'GET',
    ...(options || {}),
  });
}
// typings.d.ts
declare namespace Platform {
  type ReturnDataUserInfo = {
    code?: string;
    data?: UserInfo;
    level?: string;
    message?: string;
    status?: string;
  };
  type UserInfo = {
    clientIp?: string;
    currentRoleId?: string;
    expires?: number;
    instituteId?: string;
    language?: string;
    nickName?: string;
    nonce?: string;
    officeId?: string;
    officeList?: string[];
    realName?: string;
    requestId?: string;
    roleList?: string[];
    userId?: string;
    userName?: string;
  };
}

期望的效果

// service.ts
export async function test22(
  options ?: {[key: string]: any}
) {
  return request<Platform.UserInfo>('/api/app/test2', {
  method: 'GET',
    ...(options || {}),
  });
}

另:

  1. 若namespace为空,生成的typings.d.ts文件是否可以去除namespace标识,并在对应的service.ts中import这个typings.d.ts?
  2. typingsd.ts中的对象,是否能根据 要生成的paths包含的对象来过滤一下,这样某些情况下,可以手动删除paths,只留需要的内容,再生成service文件

相似问题 #157