Skip to content

Commit a64afba

Browse files
committed
feat: support _custom for internal custom watch
1 parent 4229f10 commit a64afba

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

packages/axle/src/api.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { watch } from 'vue'
21
import { isFunction } from 'rattail'
32
import { type AxleInstance, type AxleRequestConfig, type RunnerMethod } from './instance'
43
import {
5-
normalizeValueGetter,
64
UseAxleInstance,
75
UseAxleOptionsWithRunnable,
86
type UseAxle,
@@ -11,6 +9,7 @@ import {
119
} from './use'
1210

1311
export type ApiPathParams = Record<string, any> | (() => Record<string, any>)
12+
1413
export type ApiWatchOptions = WatchOptions & {
1514
pathParams?: boolean
1615
}
@@ -35,25 +34,21 @@ export function createApi(axle: AxleInstance, useAxle: UseAxle) {
3534
function use<V = R>(options?: ApiUseOptionsWithRunnable<V, R, P, D>): UseAxleInstance<V, R | undefined, P, D>
3635
function use<V = R>(options?: ApiUseOptions<V, R, P, D>): UseAxleInstance<V, R, P, D>
3736
function use<V = R>(options: any = {}): any {
38-
const { pathParams = {}, watch: apiWatch, ...rest } = options
39-
40-
const enableWatchPathParams = isFunction(pathParams) && (apiWatch?.pathParams || apiWatch === true)
37+
const { pathParams = {}, watch: watchOptions, ...rest } = options
38+
const enableWatchPathParams = isFunction(pathParams) && (watchOptions?.pathParams || watchOptions === true)
39+
const normalizedWatchOptions =
40+
watchOptions === true
41+
? { params: true, config: true, _custom: enableWatchPathParams ? pathParams : undefined }
42+
: watchOptions === false
43+
? { params: false, config: false }
44+
: { params: false, config: false, ...watchOptions, _custom: enableWatchPathParams ? pathParams : undefined }
4145

42-
const [data, run, extra] = useAxle<V, R, P, D>({
46+
return useAxle<V, R, P, D>({
4347
url: () => patchUrl(url, pathParams),
4448
method,
45-
watch: apiWatch,
49+
watch: normalizedWatchOptions,
4650
...rest,
4751
})
48-
49-
watch(
50-
() => [enableWatchPathParams ? normalizeValueGetter(pathParams) : undefined],
51-
() => {
52-
run()
53-
},
54-
)
55-
56-
return [data, run, extra]
5752
}
5853

5954
function patchUrl(url: string, pathParams: ApiPathParams) {

packages/axle/src/use.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ export type Run<V, R, P, D> = {
3434
(options?: RunOptions<V, P, D>): Promise<R>
3535
} & UseAxleExtra<V>
3636

37-
export interface WatchDeps {
38-
params?: boolean
39-
config?: boolean
40-
}
41-
42-
export type WatchOptions = boolean | WatchDeps
37+
export type WatchOptions =
38+
| boolean
39+
| {
40+
params?: boolean
41+
config?: boolean
42+
_custom?: () => any
43+
}
4344

4445
export interface UseAxleOptions<V = any, R = any, P = Record<string, any>, D = Record<string, any>> {
4546
url: string | (() => string)
@@ -327,14 +328,14 @@ export function createUseAxle(options: CreateUseAxleOptions) {
327328
if (watchOptions) {
328329
const normalizedWatchOptions =
329330
watchOptions === true ? { params: true, config: true } : { params: false, config: false, ...watchOptions }
330-
331331
const enableWatchParams = isFunction(initialParamsOrGetter) && normalizedWatchOptions.params
332332
const enableWatchConfig = isFunction(initialConfigOrGetter) && normalizedWatchOptions.config
333333

334334
watch(
335335
() => [
336336
enableWatchParams ? normalizeValueGetter(initialParamsOrGetter) : undefined,
337337
enableWatchConfig ? normalizeValueGetter(initialConfigOrGetter) : undefined,
338+
normalizedWatchOptions._custom ? normalizedWatchOptions._custom() : undefined,
338339
],
339340
() => {
340341
run({

0 commit comments

Comments
 (0)