背景
前文设计过程中对其进行了描述,在此处进一步以vite
插件的形式设计实现。本文将对其使用方法进行简要介绍。
使用方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import * as path from "path";
const camelCase = (str) => { str = str?.toLowerCase(); return str.charAt(0).toUpperCase() + str.slice(1, str.length) } export default defineConfig({ plugins: [ OpenApiServer({ api: /^\/api\/v3/, url: path.resolve(__dirname, "openapi.json"), mockServer:[ /^\/mock\/api\/v3/, ()=>/^\/api\/v3/ ] , rewrite: { operationId(api) { return camelCase(api.method) + api.path.replace('/api/v3/', '') .replace(/-/g, "/") .replace(":", "/By/") .replace(" ", "/") .replace(/\s/g, "") .split("/") .map(camelCase).join(""); }, },
}) ]
|
1 2 3 4 5 6 7 8 9 10
|
{ ... "include": [ + "./vite-plugins/client.d.ts" ], ... }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import {GetItems, setup} from '~openapi-server'
import axios from "axios";
const instance = axios.create({
}); setup((ref) => { ref.requestor = (...params: any[]) => fetch(...params); ref.requestor = (...params) => instance(...params); })
GetItems() .then((res) => { console.log(res) });
|
Bug~