SwaggerToTypeScript 1.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet tool install --global SwaggerToTypeScript --version 1.3.0
                    
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
                    
if you are setting up this repo
dotnet tool install --local SwaggerToTypeScript --version 1.3.0
                    
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=SwaggerToTypeScript&version=1.3.0
                    
nuke :add-package SwaggerToTypeScript --version 1.3.0
                    

Swagger To TypeScript

这是一个从 Swagger 生成 TypeScript 模型和请求的简单工具。

  • 支持生成 TypeScript 模型
  • 支持生成 TypeScript 请求
  • 支持生成 TypeScript 枚举

待优化

  • 自定义 UploadRequest

安装

dotnet tool install -g SwaggerToTypeScript

升级

dotnet tool update -g SwaggerToTypeScript

使用

SwaggerToTypeScript -url http://localhost:5007/swagger/v1/swagger.json -output ./src/api/ -filename model.ts -namespace '@/utils/request'

参数

-url                      Swagger Url
-filename                 文件名,默认:model.ts
-output                   保存路径,默认:当前执行目录
-namespace                request 命名空间,默认:@/utils/request

发布

发布到 newget.org

发布后,将 Release 目录下的 newgut.1.1.0.nupkg 上传到 newget.org

dotnet pack -c Release
dotnet pack --configuration Release

dotnet nuget push ./bin/Release/SwaggerToTypeScript.1.1.0.nupkg -k xxx -s https://api.nuget.org/v3/index.json

dotnet nuget push bin/Release/newgut.1.1.0.nupkg --api-key YOUR_API_KEY --source https://api.nuget.org/v3/index.json

注意

  1. requst.ts 需要自行实现。

示例

import type { AxiosError, InternalAxiosRequestConfig } from 'axios'
import axios from 'axios'
import { showFailToast, showNotify } from 'vant'
import { localStorage } from '@/utils/local-storage'
import { STORAGE_TOKEN_KEY } from '@/stores/mutation-type'

// 这里是用于设定请求后端时,所用的 Token KEY
// 可以根据自己的需要修改,常见的如 Access-Token,Authorization
// 需要注意的是,请尽量保证使用中横线`-` 来作为分隔符,
// 避免被 nginx 等负载均衡器丢弃了自定义的请求头
export const REQUEST_TOKEN_KEY = 'Authorization'

const getApi = () => {
  const host =
    window.location.host.toLowerCase() == 'cms.91goodschool.com'
      ? 'https://cmsapi.91goodschool.com'
      : window.location.host.toLowerCase() == 'cscms.91goodschool.com'
      ? 'https://cscmsapi.91goodschool.com'
      : process.env.VUE_APP_API_BASE_URL;
  return host;
}

// 创建 axios 实例
const request = axios.create({
  // API 请求的默认前缀
  baseURL: getApi(),
  timeout: 6000 * 10, // 请求超时时间
})

export type RequestError = AxiosError<{
  message?: string
  result?: any
  errorMessage?: string
  code?: number;
  success?: boolean;
}>

// 异常拦截处理器
const errorHandler = (error: RequestError): Promise<any> => {
  if (error.response) {
    const { data = {}, status, statusText } = error.response

    // 异常错误提示
    const { code, success, message } = data;
    if (success === false && code !== 0) {
      showFailToast(message);
    }

    // 403 无权限
    if (status === 403) {
      showNotify({
        type: 'danger',
        message: (data && data.message) || statusText,
      })
    }
    // 401 未登录/未授权
    if (status === 401 && data.result && data.result.isLogin) {
      showNotify({
        type: 'danger',
        message: 'Authorization verification failed',
      })
      // 如果你需要直接跳转登录页面
      // location.replace(loginRoutePath)
    }
  }
  return Promise.reject(error)
}

// 请求拦截器
const requestHandler = (config: InternalAxiosRequestConfig): InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig> => {
  const savedToken = localStorage.get(STORAGE_TOKEN_KEY)
  // 如果 token 存在
  // 让每个请求携带自定义 token, 请根据实际情况修改
  if (savedToken)
    config.headers[REQUEST_TOKEN_KEY] = 'Bearer ' + savedToken

  return config
}

// Add a request interceptor
request.interceptors.request.use(requestHandler, errorHandler)

// 响应拦截器
const responseHandler = (response: { data: any }) => {

  // 错误的时候返回
  const { data } = response;
  if (!data) {
    // return '[HTTP] Request has no return value';
    throw new Error('请求失败');
  }
  //  这里 code,result,message为 后台统一的字段,需要在 types.ts内修改为项目自己的接口返回格式
  const { code, result, data: resultData, message } = data;

  // TODO 判断 data 是不是对象,如果不是则返回
  if (typeof data !== 'object' || data === null || Array.isArray(data)) {
    return data;
  }

  // 这里进行特殊拦截,用于判断是否存在 code 和 data 如果不存在,则直接返回所有值,目前用于接口上传
  if (data && !Reflect.has(data, 'code') && !Reflect.has(data, 'data')) {
    return data;
  }

  // 这里逻辑可以根据项目进行修改
  const hasSuccess = data && Reflect.has(data, 'code') && code === 0;
  if (hasSuccess) {
    return result ?? resultData;
  }
  else if (message) {
    showFailToast(message);
    throw new Error('请求失败');
  }

  // 自定义错误编码处理
  if (code == 401) {
    // const userStore = useUserStoreWithOut();
    // userStore.logout(true);
  }

  return response.data
}

// Add a response interceptor
request.interceptors.response.use(responseHandler, errorHandler)

export default request

输出结果示例

/* tslint:disable */
/* eslint-disable */

//----------------------
// <auto-generated>
//     自动生成 Swagger To TypeScript v1.0.0
// </auto-generated>
//----------------------

import request from '@/utils/request';

/**
 * 网页设计器 2.0(platform:91xuanxiao、91goodschool,默认:91xuanxiao)
 */
namespace DesignerApi {

    /**
     * 组件 - 列表
     */
    export const components = (platform: string): Promise<WebDesignerComponentInfo[]> => {
        return request.get(`/api/designer/${platform}/components`);
    };

    /**
     * 组件 - 属性
     */
    export const componentProperties = (platform: string): Promise<ComponentPropertiesResponse> => {
        return request.get(`/api/designer/${platform}/component-properties`);
    };

    /**
     * 机构 - 详情
     */
    export const agencyGet = (platform: string): Promise<WebDesignerAgencyInfo> => {
        return request.get(`/api/designer/${platform}/agency`);
    };

    /**
     * 机构 - 更新
     * @returns OK
     */
    export const agencyPost = (platform: string) => {
        return request.post(`/api/designer/${platform}/agency`);
    };

    /**
     * 上传 - 快速上传
     * @returns OK
     */
    export const upload = (platform: string, file: String) => {
        return request.post(`/api/designer/${platform}/upload`, { file });
    };

    /**
     * 获取机构跳转数据
     */
    export const agencyData = (platform: string, agencyId: string): Promise<XuanXiaoHomeIndex> => {
        return request.get(`/api/designer/${platform}/agency-data/${agencyId}`);
    };

}

export { DesignerApi, EntryApi, HomeApi, OpenApi, SchoolApi, TestApi, TokenApi, UploadApi };

/** 简介组件 01 - 配图方式 */
export enum EIntroduction01MediaType {
    图片 = "图片",
    视频 = "视频",
    轮播 = "轮播",
}

/** 导航图标类型 */
export enum ENavigation01IconType {
    矢量图标 = "矢量图标",
    图片图标 = "图片图标",
}

/** 导航样式 */
export enum ENavigation01Style {
    三个 = "三个",
    四个 = "四个",
    五个 = "五个",
}

/** 通用排序类型 */
export enum ESortType {
    Forward = "Forward",
    Backward = "Backward",
    Top = "Top",
    Bottom = "Bottom",
    Exchange = "Exchange",
    Before = "Before",
    After = "After",
    Custom = "Custom",
}

export enum ESourceService {
    好学校 = "好学校",
    选校 = "选校",
    艺考 = "艺考",
    学历 = "学历",
    高考 = "高考",
    烘焙 = "烘焙",
    复读 = "复读",
    留学 = "留学",
}

export enum ESourceType {
    学校课程 = "学校课程",
    学校资讯 = "学校资讯",
    学校介绍 = "学校介绍",
}

/** 标题模板 */
export enum ETitle01Template {
    默认 = "默认",
    模板1 = "模板1",
    模板2 = "模板2",
}

/** 机构主题样式 */
export enum EWebDesignerAgencyThemeStyle {
    默认 = "默认",
    样式1 = "样式1",
    样式2 = "样式2",
}

/** 网页设计器组件背景 */
export enum EWebDesignerComponentBackground {
    无背景 = "无背景",
    主题色 = "主题色",
    纯色 = "纯色",
    图片 = "图片",
}

/** 组件类别/分类 */
export enum EWebDesignerComponentCategory {
    基础组件 = "基础组件",
    内容组件 = "内容组件",
    营销组件 = "营销组件",
}

/** 网页设计器组件主题 */
export enum EWebDesignerComponentTheme {
    浅色 = "浅色",
    深色 = "深色",
}

/** 组件类型 */
export enum EWebDesignerComponentType {
    标题 = "标题",
    轮播 = "轮播",
    导航 = "导航",
    简介 = "简介",
    统计 = "统计",
    课程 = "课程",
    咨询 = "咨询",
    师资 = "师资",
    优势 = "优势",
    流程 = "流程",
    按钮 = "按钮",
    评价 = "评价",
    相册 = "相册",
    问答 = "问答",
    广告 = "广告",
    荣誉 = "荣誉",
    资讯 = "资讯",
    校区 = "校区",
    留言 = "留言",
    关注 = "关注",
    视频 = "视频",
    引导 = "引导",
    富文本 = "富文本",
}

/** 跳转类型 */
export enum EWebDesignerJumpType {
    无 = "无",
    栏目 = "栏目",
    课程 = "课程",
    资讯 = "资讯",
    师资 = "师资",
    问答 = "问答",
    相册 = "相册",
    咨询 = "咨询",
    页面 = "页面",
}

export enum EXuanXiaoConsultMethod {
    美洽 = "美洽",
    电话 = "电话",
    链接 = "链接",
    企微客服 = "企微客服",
}

export interface EntryArticleParam {
    id?: number | undefined;
    content: string;
    readonly contentHtml?: string | undefined;
    cover?: string | undefined;
    summary?: string | undefined;
    slug?: string | undefined;
    sort?: number;
    readonly thumbnail?: string | undefined;
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last Updated
1.9.1 110 7/17/2025
1.9.0 180 4/10/2025
1.8.1 174 4/8/2025
1.8.0 166 4/8/2025
1.7.1 172 4/8/2025
1.7.0 119 2/27/2025
1.6.0 117 2/9/2025
1.5.0 118 2/9/2025
1.3.0 119 2/9/2025
1.2.0 116 2/5/2025
1.1.5 123 2/5/2025
1.1.4 114 2/5/2025
1.1.3 115 2/5/2025