API call

Front end call

In the font end, any .vue file, simplify API calls:

useFetch

*.vue
<script setup lang="ts">const { data: count } = await useFetch('/api/count')</script>

$fetch

All vue pages include the ofetch library, and is auto-imported as the $fetch alias globally across your application. It's what useFetch uses behind the scenes.

*.vue
<script setup>async function addTodo() {  const todo = await $fetch('/api/todos', {    method: 'POST',    body: {      // My todo data    }  })}</script>

Backend call

In the backend, you can easily manage the server-only part of your app, from API endpoints to middleware. Both endpoints and middleware can be defined like this:

server/api/test.ts
export default defineEventHandler(async (event) => {  // ... Do whatever you want here})

© Copyright 2024 fast2build