$ yarn create nuxt-app open-api-sample
yarn create v1.22.18
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
[4/4] 🔨 Building fresh packages...
warning Your current version of Yarn is out of date. The latest version is "1.22.19", while you're on "1.22.18".
info To upgrade, run the following command:
$ curl --compressed -o- -L https://yarnpkg.com/install.sh | bash
success Installed "create-nuxt-app@4.0.0" with binaries:
- create-nuxt-app
create-nuxt-app v4.0.0
✨ Generating Nuxt.js project in open-api-sample
? Project name: open-api-sample
? Programming language: TypeScript
? Package manager: Yarn
? UI framework: None
? Nuxt.js modules: Axios - Promise based HTTP client
? Linting tools: (Press <space> to select, <a> to toggle all, <i> to invert selection)
? Testing framework: None
? Rendering mode: Universal (SSR / SSG)
? Deployment target: Server (Node.js hosting)
? Development tools: (Press <space> to select, <a> to toggle all, <i> to invert selection)
? What is your GitHub username? i-zacky
? Version control system: None
🎉 Successfully created project open-api-sample
To get started:
cd open-api-sample
yarn dev
To build & start for production:
cd open-api-sample
yarn build
yarn start
For TypeScript users.
See : https://typescript.nuxtjs.org/cookbook/components/
✨ Done in 144.63s.
docker run --rm \
-v "${PWD}/openapi:/out" \
openapitools/openapi-generator-cli:v6.0.1 generate \
--generator-name typescript-axios \
--input-spec https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml \
--output /out \
--api-package api \
--model-package model \
--generate-alias-as-model \
--additional-properties supportsES6=true \
--additional-properties withInterfaces=true \
--additional-properties withSeparateModelsAndApi=true
apiFactory
でOpenAPI Generatorによって自動生成されたAPIクライアントを束ねていく
return
ブロックを更新していくdeclare module
で型定義をするimport { Context, Inject } from '@nuxt/types/app'
import * as openapi from '@/openapi/index'
const apiFactory = (context: Context) => {
const config = () => {
const basePath = process.env.API_BASE_URL
return new openapi.Configuration({
basePath,
})
}
return {
PetApi: new openapi.PetApi(config(), '', context.$axios),
StoreApi: new openapi.StoreApi(config(), '', context.$axios),
UserApi: new openapi.UserApi(config(), '', context.$axios),
}
}
export default (context: Context, inject: Inject) => {
inject('api', apiFactory(context))
}
declare module 'vue/types/vue' {
interface Vue {
$api: ReturnType<typeof apiFactory>
}
}
export default {
// plugins以外は省略
plugins: ['~/plugins/api']
}
<template>
<Tutorial/>
</template>
<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
name: 'IndexPage',
methods: {
async getPetById(id: number) {
// this.$api.XXX.YYYでOpenAPI Generatorで自動生成したAPIクライアントを呼び出せる
await this.$api.PetApi.getPetById(id)
}
}
})
</script>