Skip to content

Commit

Permalink
feat: support generic D
Browse files Browse the repository at this point in the history
  • Loading branch information
haoziqaq committed Jan 16, 2025
1 parent 93cbefd commit 8aafedf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
10 changes: 5 additions & 5 deletions packages/axle/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { type UseAxle, type UseAxleOptions } from './use'

export type ApiPathParams = Record<string, any> | (() => Record<string, any>)

export type ApiUseOptions<V, R, P> = Partial<UseAxleOptions<V, R, P>> & { pathParams?: ApiPathParams }
export type ApiUseOptions<V, R, P, D> = Partial<UseAxleOptions<V, R, P, D>> & { pathParams?: ApiPathParams }

export function createApi(axle: AxleInstance, useAxle: UseAxle) {
return function api<R = any, P = Record<string, any>>(url: string, method: RunnerMethod) {
function load(params?: P, pathParams?: ApiPathParams, config?: AxleRequestConfig): Promise<R> {
return function api<R = any, P = Record<string, any>, D = Record<string, any>>(url: string, method: RunnerMethod) {
function load(params?: P, pathParams?: ApiPathParams, config?: AxleRequestConfig<D>): Promise<R> {
return axle[method](patchUrl(url, pathParams ?? {}), params, config)
}

function use<UV = any>(options: ApiUseOptions<UV, R, P> = {}) {
function use<V = any>(options: ApiUseOptions<V, R, P, D> = {}) {
const { pathParams = {}, ...rest } = options

return useAxle<UV, R, P>({
return useAxle<V, R, P, D>({
url: () => patchUrl(url, pathParams),
method,
...rest,
Expand Down
18 changes: 9 additions & 9 deletions packages/axle/src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
import qs from 'qs'
import { objectToFormData } from './utils'

export interface AxleRequestConfig extends AxiosRequestConfig {}
export interface AxleRequestConfig<D = any> extends AxiosRequestConfig<D> {}

export type {
AxiosInstance,
Expand All @@ -27,16 +27,16 @@ export type {

export { AxiosError, isAxiosError } from 'axios'

export type FetchRunner = <R = AxiosResponse, P = Record<string, any>>(
export type FetchRunner = <R = AxiosResponse, P = Record<string, any>, D = Record<string, any>>(
url: string,
params?: P,
config?: AxleRequestConfig,
config?: AxleRequestConfig<D>,
) => Promise<R>

export type ModifyRunner = <R = AxiosResponse, P = Record<string, any>>(
export type ModifyRunner = <R = AxiosResponse, P = Record<string, any>, D = Record<string, any>>(
url: string,
params?: P,
config?: AxleRequestConfig,
config?: AxleRequestConfig<D>,
) => Promise<R>

export type FetchMethod = 'get' | 'delete' | 'options' | 'head'
Expand Down Expand Up @@ -110,10 +110,10 @@ export type AxleInstance = {
}

export function createFetchRunner(service: AxiosInstance, method: FetchMethod, responseType: ResponseType) {
return function <R = AxiosResponse, P = Record<string, any>>(
return function <R = AxiosResponse, P = Record<string, any>, D = Record<string, any>>(
url: string,
params?: P,
config?: AxleRequestConfig,
config?: AxleRequestConfig<D>,
): Promise<R> {
return service[method](url, {
params,
Expand All @@ -128,10 +128,10 @@ export function createModifyRunner(
method: ModifyMethod,
contentType: 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded',
) {
return function <R = AxiosResponse, P = Record<string, any>>(
return function <R = AxiosResponse, P = Record<string, any>, D = Record<string, any>>(
url: string,
params?: P,
config?: AxleRequestConfig,
config?: AxleRequestConfig<D>,
): Promise<R> {
let normalizedParams: any = params ?? {}

Expand Down
2 changes: 1 addition & 1 deletion packages/axle/src/interceptors/responseRetryInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface ResponseRetryInterceptorOptions {
export function responseRetryInterceptor(options: ResponseRetryInterceptorOptions): ResponseInterceptor {
return {
onFulfilled: (response) => response,
async onRejected(error) {
onRejected(error) {
const matcher = createMatcher(options.include, options.exclude)
if (!matcher(error.config.method ?? '', error.config.url ?? '', error?.response?.status) || isCancel(error)) {
return Promise.reject(error)
Expand Down
30 changes: 15 additions & 15 deletions packages/axle/src/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { ref, type Ref } from 'vue'
import { isFunction } from 'rattail'
import { type AxleInstance, type AxleRequestConfig, type RunnerMethod } from './instance'

export interface RunOptions<V, P> {
export interface RunOptions<V, P, D> {
url?: string
params?: P
config?: AxleRequestConfig
config?: AxleRequestConfig<D>
resetValue?: boolean
cloneResetValue?: boolean | ((value: V) => V)
}
Expand All @@ -26,19 +26,19 @@ export interface UseAxleRefs<V> {
downloadProgress: Ref<number>
}

export type Run<V, R, P> = {
(options?: RunOptions<V, P>): Promise<R>
export type Run<V, R, P, D> = {
(options?: RunOptions<V, P, D>): Promise<R>
} & UseAxleExtra<V>

export interface UseAxleOptions<V = any, R = any, P = Record<string, any>> {
export interface UseAxleOptions<V = any, R = any, P = Record<string, any>, D = Record<string, any>> {
url: string | (() => string)
method: RunnerMethod
value?: V
params?: P | (() => P)
resetValue?: boolean
cloneResetValue?: boolean | ((value: V) => V)
immediate?: boolean
config?: AxleRequestConfig | (() => AxleRequestConfig)
config?: AxleRequestConfig<D> | (() => AxleRequestConfig<D>)
onBefore?(refs: UseAxleRefs<V>): void
onAfter?(refs: UseAxleRefs<V>): void
onTransform?(response: R, refs: UseAxleRefs<V>): V | Promise<V>
Expand All @@ -50,17 +50,17 @@ export interface ResetValueOptions<V> {
cloneResetValue?: boolean | ((value: V) => V)
}

export type UseAxleInstance<V, R, P> = [value: Ref<V>, run: Run<V, R, P>, extra: UseAxleExtra<V>]
export type UseAxleInstance<V, R, P, D> = [value: Ref<V>, run: Run<V, R, P, D>, extra: UseAxleExtra<V>]

export interface CreateUseAxleOptions {
axle: AxleInstance
immediate?: boolean
onTransform?(response: any, refs: any): any
}

export type UseAxle = <V = any, R = any, P = Record<string, any>>(
options: UseAxleOptions<V, R, P>,
) => UseAxleInstance<V, R, P>
export type UseAxle = <V = any, R = any, P = Record<string, any>, D = Record<string, any>>(
options: UseAxleOptions<V, R, P, D>,
) => UseAxleInstance<V, R, P, D>

export function normalizeValueGetter<T>(valueGetter: T | (() => T)) {
return isFunction(valueGetter) ? valueGetter() : valueGetter
Expand All @@ -69,9 +69,9 @@ export function normalizeValueGetter<T>(valueGetter: T | (() => T)) {
export function createUseAxle(options: CreateUseAxleOptions) {
const { axle, onTransform: defaultOnTransform, immediate: defaultImmediate = true } = options

const useAxle: UseAxle = <V = any, R = any, P = Record<string, any>>(
options: UseAxleOptions<V, R, P>,
): UseAxleInstance<V, R, P> => {
const useAxle: UseAxle = <V = any, R = any, P = Record<string, any>, D = Record<string, any>>(
options: UseAxleOptions<V, R, P, D>,
): UseAxleInstance<V, R, P, D> => {
const {
url: initialUrlOrGetter,
method,
Expand Down Expand Up @@ -114,8 +114,8 @@ export function createUseAxle(options: CreateUseAxleOptions) {

let controller = new AbortController()

const run: Run<V, R, P> = Object.assign(
async (options: RunOptions<V, P> = {}) => {
const run: Run<V, R, P, D> = Object.assign(
async (options: RunOptions<V, P, D> = {}) => {
if (controller.signal.aborted) {
controller = new AbortController()
}
Expand Down

0 comments on commit 8aafedf

Please sign in to comment.