Skip to content

Commit 8aafedf

Browse files
committed
feat: support generic D
1 parent 93cbefd commit 8aafedf

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

packages/axle/src/api.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import { type UseAxle, type UseAxleOptions } from './use'
44

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

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

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

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

18-
return useAxle<UV, R, P>({
18+
return useAxle<V, R, P, D>({
1919
url: () => patchUrl(url, pathParams),
2020
method,
2121
...rest,

packages/axle/src/instance.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
import qs from 'qs'
1313
import { objectToFormData } from './utils'
1414

15-
export interface AxleRequestConfig extends AxiosRequestConfig {}
15+
export interface AxleRequestConfig<D = any> extends AxiosRequestConfig<D> {}
1616

1717
export type {
1818
AxiosInstance,
@@ -27,16 +27,16 @@ export type {
2727

2828
export { AxiosError, isAxiosError } from 'axios'
2929

30-
export type FetchRunner = <R = AxiosResponse, P = Record<string, any>>(
30+
export type FetchRunner = <R = AxiosResponse, P = Record<string, any>, D = Record<string, any>>(
3131
url: string,
3232
params?: P,
33-
config?: AxleRequestConfig,
33+
config?: AxleRequestConfig<D>,
3434
) => Promise<R>
3535

36-
export type ModifyRunner = <R = AxiosResponse, P = Record<string, any>>(
36+
export type ModifyRunner = <R = AxiosResponse, P = Record<string, any>, D = Record<string, any>>(
3737
url: string,
3838
params?: P,
39-
config?: AxleRequestConfig,
39+
config?: AxleRequestConfig<D>,
4040
) => Promise<R>
4141

4242
export type FetchMethod = 'get' | 'delete' | 'options' | 'head'
@@ -110,10 +110,10 @@ export type AxleInstance = {
110110
}
111111

112112
export function createFetchRunner(service: AxiosInstance, method: FetchMethod, responseType: ResponseType) {
113-
return function <R = AxiosResponse, P = Record<string, any>>(
113+
return function <R = AxiosResponse, P = Record<string, any>, D = Record<string, any>>(
114114
url: string,
115115
params?: P,
116-
config?: AxleRequestConfig,
116+
config?: AxleRequestConfig<D>,
117117
): Promise<R> {
118118
return service[method](url, {
119119
params,
@@ -128,10 +128,10 @@ export function createModifyRunner(
128128
method: ModifyMethod,
129129
contentType: 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded',
130130
) {
131-
return function <R = AxiosResponse, P = Record<string, any>>(
131+
return function <R = AxiosResponse, P = Record<string, any>, D = Record<string, any>>(
132132
url: string,
133133
params?: P,
134-
config?: AxleRequestConfig,
134+
config?: AxleRequestConfig<D>,
135135
): Promise<R> {
136136
let normalizedParams: any = params ?? {}
137137

packages/axle/src/interceptors/responseRetryInterceptor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface ResponseRetryInterceptorOptions {
1313
export function responseRetryInterceptor(options: ResponseRetryInterceptorOptions): ResponseInterceptor {
1414
return {
1515
onFulfilled: (response) => response,
16-
async onRejected(error) {
16+
onRejected(error) {
1717
const matcher = createMatcher(options.include, options.exclude)
1818
if (!matcher(error.config.method ?? '', error.config.url ?? '', error?.response?.status) || isCancel(error)) {
1919
return Promise.reject(error)

packages/axle/src/use.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { ref, type Ref } from 'vue'
22
import { isFunction } from 'rattail'
33
import { type AxleInstance, type AxleRequestConfig, type RunnerMethod } from './instance'
44

5-
export interface RunOptions<V, P> {
5+
export interface RunOptions<V, P, D> {
66
url?: string
77
params?: P
8-
config?: AxleRequestConfig
8+
config?: AxleRequestConfig<D>
99
resetValue?: boolean
1010
cloneResetValue?: boolean | ((value: V) => V)
1111
}
@@ -26,19 +26,19 @@ export interface UseAxleRefs<V> {
2626
downloadProgress: Ref<number>
2727
}
2828

29-
export type Run<V, R, P> = {
30-
(options?: RunOptions<V, P>): Promise<R>
29+
export type Run<V, R, P, D> = {
30+
(options?: RunOptions<V, P, D>): Promise<R>
3131
} & UseAxleExtra<V>
3232

33-
export interface UseAxleOptions<V = any, R = any, P = Record<string, any>> {
33+
export interface UseAxleOptions<V = any, R = any, P = Record<string, any>, D = Record<string, any>> {
3434
url: string | (() => string)
3535
method: RunnerMethod
3636
value?: V
3737
params?: P | (() => P)
3838
resetValue?: boolean
3939
cloneResetValue?: boolean | ((value: V) => V)
4040
immediate?: boolean
41-
config?: AxleRequestConfig | (() => AxleRequestConfig)
41+
config?: AxleRequestConfig<D> | (() => AxleRequestConfig<D>)
4242
onBefore?(refs: UseAxleRefs<V>): void
4343
onAfter?(refs: UseAxleRefs<V>): void
4444
onTransform?(response: R, refs: UseAxleRefs<V>): V | Promise<V>
@@ -50,17 +50,17 @@ export interface ResetValueOptions<V> {
5050
cloneResetValue?: boolean | ((value: V) => V)
5151
}
5252

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

5555
export interface CreateUseAxleOptions {
5656
axle: AxleInstance
5757
immediate?: boolean
5858
onTransform?(response: any, refs: any): any
5959
}
6060

61-
export type UseAxle = <V = any, R = any, P = Record<string, any>>(
62-
options: UseAxleOptions<V, R, P>,
63-
) => UseAxleInstance<V, R, P>
61+
export type UseAxle = <V = any, R = any, P = Record<string, any>, D = Record<string, any>>(
62+
options: UseAxleOptions<V, R, P, D>,
63+
) => UseAxleInstance<V, R, P, D>
6464

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

72-
const useAxle: UseAxle = <V = any, R = any, P = Record<string, any>>(
73-
options: UseAxleOptions<V, R, P>,
74-
): UseAxleInstance<V, R, P> => {
72+
const useAxle: UseAxle = <V = any, R = any, P = Record<string, any>, D = Record<string, any>>(
73+
options: UseAxleOptions<V, R, P, D>,
74+
): UseAxleInstance<V, R, P, D> => {
7575
const {
7676
url: initialUrlOrGetter,
7777
method,
@@ -114,8 +114,8 @@ export function createUseAxle(options: CreateUseAxleOptions) {
114114

115115
let controller = new AbortController()
116116

117-
const run: Run<V, R, P> = Object.assign(
118-
async (options: RunOptions<V, P> = {}) => {
117+
const run: Run<V, R, P, D> = Object.assign(
118+
async (options: RunOptions<V, P, D> = {}) => {
119119
if (controller.signal.aborted) {
120120
controller = new AbortController()
121121
}

0 commit comments

Comments
 (0)