https://github.com/ehsan-shv/nuxt-api-layer-aspida
Type-safe API layer with Axios, Aspida, and composition api.
https://github.com/ehsan-shv/nuxt-api-layer-aspida
Last synced: about 1 year ago
JSON representation
Type-safe API layer with Axios, Aspida, and composition api.
- Host: GitHub
- URL: https://github.com/ehsan-shv/nuxt-api-layer-aspida
- Owner: ehsan-shv
- Created: 2023-07-15T20:41:58.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-07-16T06:27:01.000Z (almost 3 years ago)
- Last Synced: 2025-03-22T19:01:46.125Z (about 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 139 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Type-safe API layer for Nuxt.js 3
> Type-safe API layer with Axios, [Aspida](https://github.com/aspida/aspida), and composition api.
1. Create a directory for each endpoint in /api directory
2. Create an endpoint type definition file
3. run `yarn run build:api`
4. Access to the whole auto generated endpoints by `useApi()` composition
### Example:
```typescript
// api/types.ts
export type Post = {
UserId: number
title: string
body: string
}
export type PostsListItem = {
UserId: number
id: number
title: string
body: string
}
```
```typescript
// /api/posts/index.ts
import { DefineMethods } from 'aspida'
import { Post, PostsListItem } from '~/api/types'
export type Methods = DefineMethods<{
get: {
resBody: PostsListItem[]
}
post: {
reqBody: Post
resBody: Post
}
}>
```
```typescript
// app.vue
Hello World!
Get Posts
const getPosts = async () => {
const response = await useApi().posts.get()
}
```