Skip to content

enroyCode/java2ts

Repository files navigation

java转ts

本项目旨在为提供一个自动生成前端http访问模型的能力,通过扫描后端服务接口进行前端代码生成。

当前仅支持springMVC注解扫描,进行ts格式数据模型生成。

源码地址

quickStart

提供dmeo工程 java2ts-sampler 启动后运行 src/resources 目录下的 download.http 文件即可生成前端模型代码。

依赖

<dependency>
    <groupId>io.gitee.enroy</groupId>
    <artifactId>java2ts-spring-boot-starter</artifactId>
    <version>${java2ts-spring-boot-starter.version}</version>
</dependency>

配置说明

java2ts:
  enabled: # 是否启用 true or false
  baseScanPackage: # 指定扫描的包路径
  outputPath: # 文件输出根目录,默认值 java2ts-ui
  apiRootPath: # 相对outputPath的api根目录,默认值 api
  modelRootPath: # 相对outputPath的model根目录,默认值 model
  fileExtension: # 生成的api和model的文件名拓展名,默认值 ts
  srcPath: # src目录替代符,默认值 @
  semicolonEnd: # import及变量定义是否以";"结尾,默认值true
  apiClientFactoryName: # ApiClient的client名,默认值ApiClientFactory
  apiClientInstanceMethod: # ApiClient的client名,默认值client()
  promiseResolveReturn: # promise.then ,默认值data => return res.data
  nullableDeclare: # 可为空对象声明,默认值 Nullable,通常在.d.ts文件中增加:declare type Nullable<T> = T | null | undefined;

RuntimeExProperties [扩展功能]

apiFilter [扫描api时,忽略指定api]

@Configuration
public class Java2tsConfiguration {
    @Bean
    public RuntimeExProperties runtimeExConfig() {
        return RuntimeExProperties.builder()//
                .apiFilter(TestController.class)// 扫描api时,忽略指定api
                .build();
    }
}

entityRenameSuffix [修改模型结尾]

@Configuration
public class Java2tsConfiguration {
    @Bean
    public RuntimeExProperties runtimeExConfig() {
        return RuntimeExProperties.builder()//
                .entityRenameSuffix("Controller", "Api")// 修改类以Controller结尾改为以Api结尾
                .build();
    }
}

entityRename [修改模型名]

@Configuration
public class Java2tsConfiguration {
    @Bean
    public RuntimeExProperties runtimeExConfig() {
        return RuntimeExProperties.builder()//
                .entityRename(Test.class, "MyTest")// 修改类名
                .build();
    }
}

modelPathRule [修改包路径]

@Configuration
public class Java2tsConfiguration {
    @Bean
    public RuntimeExProperties runtimeExConfig() {
        return RuntimeExProperties.builder()//
                .modelPathRule((cls) -> {//
                    Package pkg = cls.getPackage();
                    String pkgStr = pkg == null ? "" : pkg.getName();
                    if (pkgStr.startsWith("io.gitee.enroy.java2ts.sampler")) {
                        return true;
                    }
                    return false;
                }, (cls) -> {
                    String pkgName = cls.getPackage().getName();
                    int index = pkgName.indexOf("controller");
                    if (index > -1) {
                        return pkgName.substring(index + "controller".length() + 1);
                    }
                    return pkgName.substring("io.gitee.enroy.java2ts.sampler".length());
                }) //
                .build();
    }
}

ApiClientFactory样本文件

import axios, {AxiosInstance} from 'axios/index';
import {AxiosRequestConfig, AxiosResponse} from 'axios';
axios.defaults.timeout = 10000;

export default class ApiClientFactory {
  /**
   *
   */
  public static client() {
    // 可以在这里拦截
    return this.getNewInstance('');
  }

  public static getNewInstance(baseUrl: string): AxiosInstance {
    const instance = axios.create({
      baseURL: baseUrl,
    }) as AxiosInstance;
    this.interceptors(instance);
    return instance;
  }

  public static interceptors(instance: AxiosInstance) {
    // 请求拦截器
    instance.interceptors.request.use(function (config: AxiosRequestConfig) {
      config.headers = ApiClientFactory.getHeaders();
      return config;
    }, function (error: any) {
      return Promise.reject(error);
    });
    // 响应拦截器
    instance.interceptors.response.use(function (response: AxiosResponse) {
      if (response.data.errcode == '0') {
        return response.data;
      } else {
        let error = new Error();
        (error as any).response = response.data;
        throw error;
      }
    }, function (error) {
      if (error.response && error.response.status) {
        if (error.response.status === 502) {
          error.message = '请检查网络设置';
          return Promise.reject(error);
        }
        if (error.response.status === 503) {
          error.message = '服务升级中,请稍后再试';
          return Promise.reject(error);
        }
        if (error.response.status === 401) {
          // FIXME 401通常有重定向到登录页机制
          return Promise.reject(error);
        }
      }
      return Promise.reject(error.response.data);
    });
  }

  public static getHeaders() {
    return {
      'x-requested-with': 'XMLHttpRequest',
      'content-type': 'application/json',
    } as any;
  }
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages