Swagger to JS & Vue & Axios Codegen
npm install swagger-vue-axios axios --save-dev
const swaggerGen = require('swagger-vue-axios')
const jsonData = require('../api-docs.json')
const fs = require('fs')
const path = require('path')
let opt = {
swagger: jsonData,
moduleName: 'api',
className: 'api'
}
const codeResult = swaggerGen(opt)
fs.writeFileSync(path.join(__dirname, '../dist/api.js'), codeResult)
In Vue.js main file set API domain
import { setDomain } from './lib/api-client.js'
import { setAxiosInstance } from './lib/api-client.js'
setDomain('http://localhost:3000/api')
setAxiosInstance(axios)
Import API function into Vue.js component, for example to log in
import { user_login as userLogin } from '../lib/api-client.js'
userLogin({
credentials: {
username: 'admin',
password: 'admin'
}
}).then(function (response) {
console.log(response.data) // {id: "<token>", ttl: 1209600, created: "2017-01-01T00:00:00.000Z", userId: 1}
})
All requests use axios module with promise, for more information about that follow axios documentation