-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(projects): @sa/axios: createRequest, createFlatRequest, createHo…
…okRequest
- Loading branch information
1 parent
9112193
commit c5c456b
Showing
14 changed files
with
403 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
/** Request id key */ | ||
/** request id key */ | ||
export const REQUEST_ID_KEY = 'X-Request-Id'; | ||
|
||
/** the backend error code key */ | ||
export const BACKEND_ERROR_CODE = 'BACKEND_ERROR'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,8 @@ | |
"*": { | ||
"*": ["./src/*"] | ||
} | ||
}, | ||
"dependencies": { | ||
"@sa/axios": "workspace:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { ref } from 'vue'; | ||
import type { Ref } from 'vue'; | ||
import { createFlatRequest } from '@sa/axios'; | ||
import type { | ||
AxiosError, | ||
CreateAxiosDefaults, | ||
CustomAxiosRequestConfig, | ||
MappedType, | ||
RequestOption, | ||
ResponseType | ||
} from '@sa/axios'; | ||
import useLoading from './use-loading'; | ||
|
||
export type HookRequestInstanceResponseSuccessData<T = any> = { | ||
data: Ref<T>; | ||
error: Ref<null>; | ||
}; | ||
|
||
export type HookRequestInstanceResponseFailData<T = any> = { | ||
data: Ref<null>; | ||
error: Ref<AxiosError<T>>; | ||
}; | ||
|
||
export type HookRequestInstanceResponseData<T = any> = { | ||
loading: Ref<boolean>; | ||
} & (HookRequestInstanceResponseSuccessData<T> | HookRequestInstanceResponseFailData<T>); | ||
|
||
export interface HookRequestInstance { | ||
<T = any, R extends ResponseType = 'json'>( | ||
config: CustomAxiosRequestConfig | ||
): HookRequestInstanceResponseData<MappedType<R, T>>; | ||
cancelRequest: (requestId: string) => void; | ||
cancelAllRequest: () => void; | ||
} | ||
|
||
/** | ||
* create a hook request instance | ||
* | ||
* @param axiosConfig | ||
* @param options | ||
*/ | ||
export default function createHookRequest<ResponseData = any>( | ||
axiosConfig?: CreateAxiosDefaults, | ||
options?: Partial<RequestOption<ResponseData>> | ||
) { | ||
const request = createFlatRequest<ResponseData>(axiosConfig, options); | ||
|
||
const hookRequest: HookRequestInstance = function hookRequest<T = any, R extends ResponseType = 'json'>( | ||
config: CustomAxiosRequestConfig | ||
) { | ||
const { loading, startLoading, endLoading } = useLoading(); | ||
|
||
const data = ref<MappedType<R, T> | null>(null); | ||
const error = ref<AxiosError<MappedType<R, T>> | null>(null); | ||
|
||
startLoading(); | ||
|
||
request(config).then(res => { | ||
if (res.data) { | ||
data.value = res.data; | ||
} else { | ||
error.value = res.error; | ||
} | ||
|
||
endLoading(); | ||
}); | ||
|
||
return { | ||
loading, | ||
data, | ||
error | ||
}; | ||
} as HookRequestInstance; | ||
|
||
hookRequest.cancelRequest = request.cancelRequest; | ||
hookRequest.cancelAllRequest = request.cancelAllRequest; | ||
|
||
return hookRequest; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.