Vue 使用typescript如何優(yōu)雅的調(diào)用swagger API
Swagger 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù),后端集成下Swagger,然后就可以提供一個(gè)在線文檔地址給前端同學(xué)。
前端如何優(yōu)雅的調(diào)用呢?
入門(mén)版
根據(jù)文檔,用axios自動(dòng)來(lái)調(diào)用
// 應(yīng)用管理相關(guān)接口 import axios from '../interceptors.js' // 獲取應(yīng)用列表 export const getList = (data) => { return axios({ url: '/app/list?sort=createdDate,desc', method: 'get', params: data }) }
這里的問(wèn)題是,有多少個(gè)接口,你就要編寫(xiě)多少個(gè)函數(shù),且數(shù)據(jù)結(jié)構(gòu)需要查看文檔獲取。
進(jìn)階版本
使用typescript,編寫(xiě)API,通過(guò)Type定義數(shù)據(jù)結(jié)構(gòu),進(jìn)行約束。
問(wèn)題: 還是需要手寫(xiě)
優(yōu)雅版本
swagger 其實(shí)是一個(gè)json-schema描述文檔,我們可以基于此,自動(dòng)生成。
很早之前,寫(xiě)過(guò)一個(gè)插件 generator-swagger-2-t, 簡(jiǎn)單的實(shí)現(xiàn)了將swagger生成typescript api。
今天,筆者對(duì)這個(gè)做了升級(jí),方便支持后端返回的泛型數(shù)據(jù)結(jié)構(gòu)。
安裝
需要同時(shí)安裝 Yeoman 和 -swagger-2-ts
npm install -g generator-swagger-2-ts
然后cd到你的工作目錄,執(zhí)行:
yo swagger-2-ts
按提示
- 輸入swagger-ui 地址,例如http://192.168.86.8:8051/swagger-ui.html
- 可選生成js 或者 typescript
- 可以自定義生成的api class名稱(chēng)、api文件名
- API 支持泛型
也可以通過(guò)命令行直接傳遞參數(shù)
yo swagger-2-ts --swaggerUrl=http://localhost:8080/swagger-ui.html --className=API --type=typescript --outputFile=api.ts
- swaggerUrl: swagger ui url swaggerui地址
- className: API class name 類(lèi)名
- type: typescript or javascipt
- outputFile: api 文件保存路徑
生成代碼demo:
export type AccountUserInfo = { disableTime?: string isDisable?: number lastLoginIp?: string lastLoginPlace?: string lastLoginTime?: string openId?: string } export type BasePayloadResponse = { data?: object desc?: string retcode?: string } /** * User Account Controller * @class UserAccountAPI */ export class UserAccountAPI { /** * changeUserState * @method * @name UserAccountAPI#changeUserState * @param accountUserInfo - accountUserInfo * @param $domain API域名,沒(méi)有指定則使用構(gòu)造函數(shù)指定的 */ changeUserState(parameters: { 'accountUserInfo': AccountUserInfo, $queryParameters?: any, $domain?: string }): Promise<AxiosResponse<BasePayloadResponse>> { let config: AxiosRequestConfig = { baseURL: parameters.$domain || this.$defaultDomain, url: '/userAccount/changeUserState', method: 'PUT' } config.headers = {} config.params = {} config.headers[ 'Accept' ] = '*/*' config.headers[ 'Content-Type' ] = 'application/json' config.data = parameters.accountUserInfo return axios.request(config) } _UserAccountAPI: UserAccountAPI = null; /** * 獲取 User Account Controller API * return @class UserAccountAPI */ getUserAccountAPI(): UserAccountAPI { if (!this._UserAccountAPI) { this._UserAccountAPI = new UserAccountAPI(this.$defaultDomain) } return this._UserAccountAPI } } /** * 管理系統(tǒng)接口描述 * @class API */ export class API { /** * API構(gòu)造函數(shù) * @param domain API域名 */ constructor(domain?: string) { this.$defaultDomain = domain || 'http://localhost:8080' } }
使用
import { API } from './http/api/manageApi' // in main.ts let api = new API("/api/") api.getUserAccountAPI().changeUserState({ isDisable: 1 openId: 'open id' })
Vue中最佳實(shí)踐
main.ts 全局定義
import { API } from './http/api/manageApi' Vue.prototype.$manageApi = new API('/api/')
增加.d.ts
增加types文件,方便使用智能提示
import { API } from '@/http/api/manageApi' import { MarkAPI } from '@/http/api/mark-center-api' declare module "vue/types/vue" { interface Vue { $manageApi: API $markApi: MarkAPI } }
實(shí)際使用
現(xiàn)在可以在vue里直接調(diào)用了。
this.$manageApi .getUserAccountAPI().changeUserState({isDisable: 1, openId: 'open id'})
開(kāi)源地址
https://github.com/jadepeng/generator-swagger-2-ts
總結(jié)
到此這篇關(guān)于Vue 使用typescript如何優(yōu)雅的調(diào)用swagger API的文章就介紹到這了,更多相關(guān)Vue 使用typescript內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vite/Vuecli配置proxy代理解決跨域問(wèn)題
這篇文章主要介紹了vite/Vuecli配置proxy代理解決跨域問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12vue 倒計(jì)時(shí)結(jié)束跳轉(zhuǎn)頁(yè)面實(shí)現(xiàn)代碼
在商場(chǎng)大家經(jīng)??吹酵\?chē)收費(fèi)倒計(jì)時(shí)支付頁(yè)面,今天小編通過(guò)本文給大家分享vue 倒計(jì)時(shí)結(jié)束跳轉(zhuǎn)頁(yè)面功能,感興趣的朋友一起看看吧2023-10-10vue通過(guò)cookie獲取用戶(hù)登錄信息的思路詳解
這篇文章主要介紹了vue通過(guò)cookie獲取用戶(hù)登錄信息的思路詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10利用Vue模擬實(shí)現(xiàn)element-ui的分頁(yè)器效果
這篇文章主要為大家詳細(xì)介紹了如何利用Vue模擬實(shí)現(xiàn)element-ui的分頁(yè)器效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-11-11