Skip to content

Add Vue example for openapi-fetch #1654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/openapi-fetch/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ _Note: if you’re using Svelte without SvelteKit, the root example in `src/rout

[View a code example in GitHub](https://github.com/drwpow/openapi-typescript/tree/main/packages/openapi-fetch/examples/sveltekit)

## Vue
## Vue 3

There isn’t an example app in Vue yet. Are you using it in Vue? Please [open a PR to add it!](https://github.com/drwpow/openapi-typescript/pulls)
[Vue 3](https://vuejs.org/) is a popular framework with a large ecosystem. Vue 3’s Composition API is a perfect match for openapi-fetch, as it allows for easy separation of concerns and reactivity.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉


---
## [View a code example in GitHub](https://github.com/drwpow/openapi-typescript/tree/main/packages/openapi-fetch/examples/vue-3)

Additional examples are always welcome! Please [open a PR](https://github.com/drwpow/openapi-typescript/pulls) with your examples.
30 changes: 30 additions & 0 deletions packages/openapi-fetch/examples/vue-3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
.DS_Store
dist
dist-ssr
coverage
*.local

/cypress/videos/
/cypress/screenshots/

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

*.tsbuildinfo
12 changes: 12 additions & 0 deletions packages/openapi-fetch/examples/vue-3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# openapi-fetch + Vue

Example of using openapi-fetch with [Vue](https://vuejs.org/).

## Setup

```sh
pnpm i
pnpm run dev
```

You’ll see the server running at `localhost:5173`
1 change: 1 addition & 0 deletions packages/openapi-fetch/examples/vue-3/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
13 changes: 13 additions & 0 deletions packages/openapi-fetch/examples/vue-3/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="icon" href="/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vite App</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
29 changes: 29 additions & 0 deletions packages/openapi-fetch/examples/vue-3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "vue",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "run-p type-check \"build-only {@}\" --",
"preview": "vite preview",
"build-only": "vite build",
"type-check": "vue-tsc --build --force",
"generate": "openapi-typescript src/schema/catfact.json -o src/generated/catfact.d.ts"
},
"dependencies": {
"npm-run-all": "^4.1.5",
"openapi-fetch": "workspace:^",
"vue": "^3.4.21"
},
"devDependencies": {
"@tsconfig/node20": "^20.1.4",
"@types/node": "^20.12.5",
"@vitejs/plugin-vue": "^5.0.4",
"@vue/tsconfig": "^0.5.1",
"openapi-typescript": "workspace:^",
"typescript": "~5.4.0",
"vite": "^5.2.8",
"vue-tsc": "^2.0.11"
}
}
Binary file not shown.
46 changes: 46 additions & 0 deletions packages/openapi-fetch/examples/vue-3/src/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<script setup lang="ts">
import { useCatFactQuery } from "./composables/catfact-query";

const { error, isFetching, isReady, state, execute } = useCatFactQuery({
params: {
query: {
max_length: 140,
},
},
});
</script>

<template>
<div class="demo">
<button @click="execute">Execute</button>
<pre class="code-block">{{
JSON.stringify(
{
isReady,
isFetching,
error,
state,
},
undefined,
2
)
}}</pre>
</div>
</template>

<style scoped>
.demo {
padding: 16px;
margin: 0 auto;
width: 800px;
}
.demo .code-block {
background-color: #202127;
padding: 4px 8px;
margin: 12px 0;
border-radius: 4px;
word-break: break-all;
white-space: pre-wrap;
word-wrap: break-word;
}
</style>
86 changes: 86 additions & 0 deletions packages/openapi-fetch/examples/vue-3/src/assets/base.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* color palette from <https://github.com/vuejs/theme> */
:root {
--vt-c-white: #ffffff;
--vt-c-white-soft: #f8f8f8;
--vt-c-white-mute: #f2f2f2;

--vt-c-black: #181818;
--vt-c-black-soft: #222222;
--vt-c-black-mute: #282828;

--vt-c-indigo: #2c3e50;

--vt-c-divider-light-1: rgba(60, 60, 60, 0.29);
--vt-c-divider-light-2: rgba(60, 60, 60, 0.12);
--vt-c-divider-dark-1: rgba(84, 84, 84, 0.65);
--vt-c-divider-dark-2: rgba(84, 84, 84, 0.48);

--vt-c-text-light-1: var(--vt-c-indigo);
--vt-c-text-light-2: rgba(60, 60, 60, 0.66);
--vt-c-text-dark-1: var(--vt-c-white);
--vt-c-text-dark-2: rgba(235, 235, 235, 0.64);
}

/* semantic color variables for this project */
:root {
--color-background: var(--vt-c-white);
--color-background-soft: var(--vt-c-white-soft);
--color-background-mute: var(--vt-c-white-mute);

--color-border: var(--vt-c-divider-light-2);
--color-border-hover: var(--vt-c-divider-light-1);

--color-heading: var(--vt-c-text-light-1);
--color-text: var(--vt-c-text-light-1);

--section-gap: 160px;
}

@media (prefers-color-scheme: dark) {
:root {
--color-background: var(--vt-c-black);
--color-background-soft: var(--vt-c-black-soft);
--color-background-mute: var(--vt-c-black-mute);

--color-border: var(--vt-c-divider-dark-2);
--color-border-hover: var(--vt-c-divider-dark-1);

--color-heading: var(--vt-c-text-dark-1);
--color-text: var(--vt-c-text-dark-2);
}
}

*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
font-weight: normal;
}

body {
min-height: 100vh;
color: var(--color-text);
background: var(--color-background);
transition:
color 0.5s,
background-color 0.5s;
line-height: 1.6;
font-family:
Inter,
-apple-system,
BlinkMacSystemFont,
'Segoe UI',
Roboto,
Oxygen,
Ubuntu,
Cantarell,
'Fira Sans',
'Droid Sans',
'Helvetica Neue',
sans-serif;
font-size: 15px;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions packages/openapi-fetch/examples/vue-3/src/assets/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@import './base.css';

#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
font-weight: normal;
}

@media (min-width: 1024px) {
body {
display: flex;
place-items: center;
}

#app {
display: grid;
grid-template-columns: 1fr;
padding: 0 2rem;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import client from "#/lib";
import { ref } from "vue";
import type { ParamsOption, RequestBodyOption } from "openapi-fetch";
import type { paths } from "#/generated/catfact";

interface AppError {
code: number;
message: string;
}

type FactCatQueryOptions<T> = ParamsOption<T> & RequestBodyOption<T>;

type CatFactResponse =
paths["/fact"]["get"]["responses"]["200"]["content"]["application/json"];

export const useCatFactQuery = (
fetchOptions: FactCatQueryOptions<paths["/fact"]["get"]>
) => {
const state = ref<CatFactResponse>();
const isReady = ref(false);
const isFetching = ref(false);
const error = ref<AppError | undefined>(undefined);

async function execute() {
error.value = undefined;
isReady.value = false;
isFetching.value = true;

const { data, error: fetchError } = await client.GET("/fact", fetchOptions);

if (fetchError) {
error.value = fetchError;
} else {
state.value = data;
isReady.value = true;
}

isFetching.value = false;
}

execute();

return {
state,
isReady,
isFetching,
error,
execute,
};
};
Loading
Loading