Skip to content

Commit

Permalink
feat: add useConvert composable to convert data structure from api
Browse files Browse the repository at this point in the history
  • Loading branch information
flingyp committed Sep 12, 2023
1 parent c6392aa commit cf33e1f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ declare global {
const useCloned: typeof import('@vueuse/core')['useCloned']
const useColorMode: typeof import('@vueuse/core')['useColorMode']
const useConfirmDialog: typeof import('@vueuse/core')['useConfirmDialog']
const useConvert: typeof import('./src/composables/common/useConvert/index')['useConvert']
const useCounter: typeof import('@vueuse/core')['useCounter']
const useCssModule: typeof import('vue')['useCssModule']
const useCssVar: typeof import('@vueuse/core')['useCssVar']
Expand Down Expand Up @@ -471,6 +472,7 @@ declare module 'vue' {
readonly useCloned: UnwrapRef<typeof import('@vueuse/core')['useCloned']>
readonly useColorMode: UnwrapRef<typeof import('@vueuse/core')['useColorMode']>
readonly useConfirmDialog: UnwrapRef<typeof import('@vueuse/core')['useConfirmDialog']>
readonly useConvert: UnwrapRef<typeof import('./src/composables/common/useConvert/index')['useConvert']>
readonly useCounter: UnwrapRef<typeof import('@vueuse/core')['useCounter']>
readonly useCssModule: UnwrapRef<typeof import('vue')['useCssModule']>
readonly useCssVar: UnwrapRef<typeof import('@vueuse/core')['useCssVar']>
Expand Down
9 changes: 9 additions & 0 deletions src/composables/common/useConvert/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Use with useRequest to decouple the service and transform the data into a front-end custom type data structure
* @param callback
* @example
* const { data } = useRequest({ url: '/demo', method: 'GET' })
* const resResult = useConvert<number>(() => 1)
* @returns
*/
export const useConvert = <T> (callback: () => T): T => callback()
3 changes: 2 additions & 1 deletion src/composables/common/useRequest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ interface ResponseOptions<K = unknown> {
data: K
}

export const useRequest = async <T = unknown> (options: RequestOptions): Promise<ResponseOptions<T>> => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const useRequest = async <T = any> (options: RequestOptions): Promise<ResponseOptions<T>> => {
const {
url, method, params, data, responseType, headers,
} = options
Expand Down

0 comments on commit cf33e1f

Please sign in to comment.