Skip to content

Commit

Permalink
docs: update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
haoziqaq committed Sep 24, 2024
1 parent 018e7c6 commit b5558ac
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 0 deletions.
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,90 @@ function sendAllRequest() {
<button @click="sendAllRequest">Send All Request</button>
</template>
```

### Enhancement

`createApi` is supported since `v0.9.0`, which is used to define APIs.

#### Define APIs

```ts
import { createAxle, createApi } from '@varlet/axle'
import { createUseAxle } from '@varlet/axle/use'

const axle = createAxle({
baseURL: '/api',
})

const useAxle = createUseAxle({
axle,
})

const api = createApi(axle, useAxle)

export const apiGetUsers = api<Response<User[]>>('/user', 'get')

export const apiGetUser = api<Response<User>>('/user/:id', 'get')

export const apiCreateUser = api<Response<User>, CreateUser>('/user', 'post')

export const apiUpdateUser = api<Response<User>, UpdateUser>('/user/:id', 'put')

export const apiDeleteUser = api<Response<User>>('/user/:id', 'delete')

export type Response<T> = {
data: T
code: number
message: string
success: boolean
}

export interface User {
id: string
name: string
}

export interface CreateUser {
name: string
}

export interface UpdateUser {
name: string
}
```

#### Invoke APIs

```ts
const route = useRoute()

const [users, getUsers] = apiGetUsers.use(/** same as useAxle and extends pathParams **/)

const [user, getUser] = apiGetUser.use<User>({
pathParams: () => ({ id: route.params.id }),
})

async function handleCreate(payload: CreateUser) {
const { success } = await apiCreateUser.load(payload)

if (success) {
getUsers()
}
}

async function handleUpdate(payload: UpdateUser, id: string) {
const { success } = await apiUpdateUser.load(payload, { id })

if (success) {
getUsers()
}
}

async function handleDelete(id: string) {
const { success } = await apiDeleteUser.load({}, { id })

if (success) {
getUsers()
}
}
```
87 changes: 87 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,90 @@ function sendAllRequest() {
<button @click="sendAllRequest">发送全部请求</button>
</template>
```

### 增强

`0.9.0` 开始支持 `createApi`,以增强 API 定义能力。

#### 定义 API

```ts
import { createAxle, createApi } from '@varlet/axle'
import { createUseAxle } from '@varlet/axle/use'

const axle = createAxle({
baseURL: '/api',
})

const useAxle = createUseAxle({
axle,
})

const api = createApi(axle, useAxle)

export const apiGetUsers = api<Response<User[]>>('/user', 'get')

export const apiGetUser = api<Response<User>>('/user/:id', 'get')

export const apiCreateUser = api<Response<User>, CreateUser>('/user', 'post')

export const apiUpdateUser = api<Response<User>, UpdateUser>('/user/:id', 'put')

export const apiDeleteUser = api<Response<User>>('/user/:id', 'delete')

export type Response<T> = {
data: T
code: number
message: string
success: boolean
}

export interface User {
id: string
name: string
}

export interface CreateUser {
name: string
}

export interface UpdateUser {
name: string
}
```

#### 调用 API

```ts
const route = useRoute()

const [users, getUsers] = apiGetUsers.use(/** 和 useAxle 一致并且扩展了 pathParams **/)

const [user, getUser] = apiGetUser.use<User>({
pathParams: () => ({ id: route.params.id }),
})

async function handleCreate(payload: CreateUser) {
const { success } = await apiCreateUser.load(payload)

if (success) {
getUsers()
}
}

async function handleUpdate(payload: UpdateUser, id: string) {
const { success } = await apiUpdateUser.load(payload, { id })

if (success) {
getUsers()
}
}

async function handleDelete(id: string) {
const { success } = await apiDeleteUser.load({}, { id })

if (success) {
getUsers()
}
}
```

0 comments on commit b5558ac

Please sign in to comment.