Skip to content

Commit

Permalink
chore: clean
Browse files Browse the repository at this point in the history
  • Loading branch information
haoziqaq committed Jul 7, 2023
1 parent 585c49a commit 1f36fde
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### Intro

A tool library based on axios to simplify the development process
Progressive request tool based on axios.

### Quickstart

Expand Down Expand Up @@ -30,8 +30,16 @@ const axle = createAxle(/** @see https://axios-http.com **/)
// The built-in axios of the axle, the usage is exactly the same as that of axios, and shares the configuration with the axle.
const { axios } = axle

const response = await axle.get('/user', { current: 1, pageSize: 10 }, { headers: {} })
const response = await axle.post('/user', { name: 'Axle' }, { headers: {} })
axios.interceptors.response.use(
response => response,
error => Promise.reject(error)
)

const response = await axle.get('/url', { current: 1, pageSize: 10 }, { headers: {} })
const response = await axle.post('/url', { name: 'Axle' }, { headers: {} })
const response = await axle.getBlob('/url')
const response = await axle.postJSON('/url', { name: 'Axle' }, { headers: {} })
const response = await axle.postMultipart('/url', { name: 'Axle', file: new File() })
```

### Vue Composition Api
Expand All @@ -42,11 +50,9 @@ import { createAxle } from '@varlet/axle'
import { createUseAxle } from '@varlet/axle/use'
const axle = createAxle(/** @see https://axios-http.com **/)
const useAxle = createUseAxle(/** @see https://github.com/varletjs/axle/blob/main/src/use.ts?plain=1#L5 **/)
const useAxle = createUseAxle(/** Unstable **/)
// default immediate request
const [users, getUsers, loading, { error }] = useAxle({
/** @see https://github.com/varletjs/axle/blob/main/src/use.ts?plain=1#L26 **/
const [users, getUsers, { loading, error }] = useAxle({
runner: axle.get,
url: '/user',
params: { current: 1, pageSize: 10 },
Expand Down
44 changes: 22 additions & 22 deletions playground/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<script setup lang="ts">
import {
useApiGetUser,
useApiGetUsers,
useApiAddUser,
useApiDeleteUser,
useApiUpdateUser,
useApiPatchUser,
useApiDownloadFile,
useApiThrowError,
useGetUser,
useGetUsers,
useAddUser,
useDeleteUser,
useUpdateUser,
usePatchUser,
useDownloadFile,
useThrowError,
User,
} from './apis'
const id = ref('1')
const deleteId = ref('1')
const [users, apiGetUsers, { loading: isUsersLoading }] = useApiGetUsers<User[]>([], { immediate: true })
const [users, getUsers, { loading: isUsersLoading }] = useGetUsers<User[]>([], { immediate: true })
const [user, apiGetUser, { loading: isUserLoading, abort }] = useApiGetUser<User>(
const [user, getUser, { loading: isUserLoading, abort }] = useGetUser<User>(
{},
{
onSuccess(response) {
Expand All @@ -27,7 +27,7 @@ const [user, apiGetUser, { loading: isUserLoading, abort }] = useApiGetUser<User
}
)
const [addedUser, apiAddUser] = useApiAddUser<User>(
const [addedUser, addUser] = useAddUser<User>(
{},
{
onBefore() {
Expand All @@ -41,7 +41,7 @@ const [addedUser, apiAddUser] = useApiAddUser<User>(
}
)
const [updatedUser, apiUpdateUser] = useApiUpdateUser<User>(
const [updatedUser, updateUser] = useUpdateUser<User>(
{},
{
onBefore() {
Expand All @@ -55,7 +55,7 @@ const [updatedUser, apiUpdateUser] = useApiUpdateUser<User>(
}
)
const [patchedUser] = useApiPatchUser<User>(
const [patchedUser] = usePatchUser<User>(
{},
{
onBefore() {
Expand All @@ -69,7 +69,7 @@ const [patchedUser] = useApiPatchUser<User>(
}
)
const [deletedUser, apiDeleteUser] = useApiDeleteUser<User>(
const [deletedUser, deleteUser] = useDeleteUser<User>(
{},
{
onBefore() {
Expand All @@ -83,11 +83,11 @@ const [deletedUser, apiDeleteUser] = useApiDeleteUser<User>(
}
)
const [file, apiDownloadFile, { downloadProgress }] = useApiDownloadFile<Blob | null>(null, {
const [file, downloadFile, { downloadProgress }] = useDownloadFile<Blob | null>(null, {
onTransform: (response) => response,
})
const [errorUser, apiThrowError, { loading: isThrowErrorLoading }] = useApiThrowError(
const [errorUser, throwError, { loading: isThrowErrorLoading }] = useThrowError(
{},
{
onBefore(refs) {
Expand All @@ -104,16 +104,16 @@ const userRecord = reactive<User>({
async function handleSubmit() {
const options = { params: user }
userRecord.id ? await apiUpdateUser(options) : await apiAddUser(options)
userRecord.id ? await updateUser(options) : await addUser(options)
}
async function handleDelete() {
await apiDeleteUser({ params: { id: deleteId.value } })
await deleteUser({ params: { id: deleteId.value } })
}
watch(
() => [addedUser.value, updatedUser.value, deletedUser.value, patchedUser.value],
() => apiGetUsers()
() => getUsers()
)
</script>

Expand All @@ -129,7 +129,7 @@ watch(
<var-space direction="column">
<var-space align="center">
<var-input type="number" variant="outlined" v-model="id" />
<var-button type="primary" @click="apiGetUser({ params: { id } })">Search</var-button>
<var-button type="primary" @click="getUser({ params: { id } })">Search</var-button>
<var-button type="warning" @click="abort">Abort</var-button>
</var-space>

Expand Down Expand Up @@ -162,7 +162,7 @@ watch(
<var-space direction="column">
<h3>Download Progress: {{ downloadProgress * 100 }} %</h3>
<h3>File Size: {{ file?.size ?? 0 }}</h3>
<var-button type="primary" @click="() => apiDownloadFile()">Download (PS: Please use slow 3G)</var-button>
<var-button type="primary" @click="() => downloadFile()">Download (PS: Please use slow 3G)</var-button>
</var-space>

<var-divider margin="30px 0" />
Expand All @@ -171,6 +171,6 @@ watch(
<var-cell>name: throw error</var-cell>
<var-cell>loading: {{ isThrowErrorLoading }}</var-cell>
<var-cell>data: {{ errorUser ?? 'No Data' }}</var-cell>
<var-button type="primary" @click="() => apiThrowError()">Retry</var-button>
<var-button type="primary" @click="() => throwError()">Retry</var-button>
</var-space>
</template>
16 changes: 8 additions & 8 deletions playground/src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface User {

export type Options<D, R = any, P = Record<string, any>> = Partial<Omit<UseAxleOptions<D, R, P>, 'data'>>

export function useApiGetUsers<D>(data: D, options?: Options<D, Response<User[]>>) {
export function useGetUsers<D>(data: D, options?: Options<D, Response<User[]>>) {
return useAxle({
data,
url: '/user/list-user',
Expand All @@ -23,7 +23,7 @@ export function useApiGetUsers<D>(data: D, options?: Options<D, Response<User[]>
})
}

export function useApiGetUser<D>(data: D, options?: Options<D, Response<User>>) {
export function useGetUser<D>(data: D, options?: Options<D, Response<User>>) {
return useAxle({
data,
url: '/user/get-user',
Expand All @@ -32,7 +32,7 @@ export function useApiGetUser<D>(data: D, options?: Options<D, Response<User>>)
})
}

export function useApiAddUser<D>(data: D, options?: Options<D, Response<User>>) {
export function useAddUser<D>(data: D, options?: Options<D, Response<User>>) {
return useAxle({
data,
url: '/user/add-user',
Expand All @@ -41,7 +41,7 @@ export function useApiAddUser<D>(data: D, options?: Options<D, Response<User>>)
})
}

export function useApiDeleteUser<D>(data: D, options?: Options<D, Response<User>>) {
export function useDeleteUser<D>(data: D, options?: Options<D, Response<User>>) {
return useAxle({
data,
url: '/user/delete-user',
Expand All @@ -50,7 +50,7 @@ export function useApiDeleteUser<D>(data: D, options?: Options<D, Response<User>
})
}

export function useApiUpdateUser<D>(data: D, options?: Options<D, Response<User>>) {
export function useUpdateUser<D>(data: D, options?: Options<D, Response<User>>) {
return useAxle({
data,
url: '/user/update-user',
Expand All @@ -59,7 +59,7 @@ export function useApiUpdateUser<D>(data: D, options?: Options<D, Response<User>
})
}

export function useApiPatchUser<D>(data: D, options?: Options<D, Response<User>>) {
export function usePatchUser<D>(data: D, options?: Options<D, Response<User>>) {
return useAxle({
data,
url: '/user/patch-user',
Expand All @@ -68,7 +68,7 @@ export function useApiPatchUser<D>(data: D, options?: Options<D, Response<User>>
})
}

export function useApiDownloadFile<D>(data: D, options?: Options<D, Blob>) {
export function useDownloadFile<D>(data: D, options?: Options<D, Blob>) {
return useAxle({
data,
url: 'http://localhost:5173/logo.png',
Expand All @@ -77,7 +77,7 @@ export function useApiDownloadFile<D>(data: D, options?: Options<D, Blob>) {
})
}

export function useApiThrowError<D>(data: D, options?: Options<D, Response<User>>) {
export function useThrowError<D>(data: D, options?: Options<D, Response<User>>) {
return useAxle({
data,
url: '/user/throw-error',
Expand Down

0 comments on commit 1f36fde

Please sign in to comment.