Skip to content

Commit 1aec7e7

Browse files
committed
feat: added slot typing
1 parent 4641149 commit 1aec7e7

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

packages/villus/src/Mutation.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import { defineComponent } from 'vue';
1+
import { defineComponent, UnwrapRef, VNode } from 'vue';
22
import { useMutation } from './useMutation';
3+
import type { BaseQueryApi } from './useQuery';
34
import { normalizeChildren } from './utils';
45

5-
export const Mutation = defineComponent({
6+
type QuerySlotProps = UnwrapRef<Pick<BaseQueryApi, 'data' | 'error' | 'execute' | 'isDone' | 'isFetching'>>;
7+
8+
const MutationImpl = defineComponent({
69
name: 'Mutation',
710
props: {
811
query: {
@@ -24,3 +27,11 @@ export const Mutation = defineComponent({
2427
};
2528
},
2629
});
30+
31+
export const Mutation = MutationImpl as typeof MutationImpl & {
32+
new (): {
33+
$slots: {
34+
default: (arg: QuerySlotProps) => VNode[];
35+
};
36+
};
37+
};

packages/villus/src/Query.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { defineComponent, Ref, toRef, watch } from 'vue';
1+
import { defineComponent, Ref, toRef, UnwrapRef, VNode, watch } from 'vue';
22
import { CachePolicy } from './types';
33
import { useQuery, BaseQueryApi } from './useQuery';
44
import { normalizeChildren } from './utils';
55

6-
export const Query = defineComponent({
6+
type QuerySlotProps = UnwrapRef<Pick<BaseQueryApi, 'data' | 'error' | 'execute' | 'isDone' | 'isFetching'>>;
7+
8+
const QueryImpl = defineComponent({
79
name: 'Query',
810
props: {
911
query: {
@@ -55,13 +57,15 @@ export const Query = defineComponent({
5557
);
5658

5759
return () => {
58-
return normalizeChildren(ctx, {
60+
const slotProps: QuerySlotProps = {
5961
data: data.value,
6062
error: error.value,
6163
isFetching: isFetching.value,
6264
isDone: isDone.value,
6365
execute,
64-
});
66+
};
67+
68+
return normalizeChildren(ctx, slotProps);
6569
};
6670
}
6771

@@ -79,3 +83,11 @@ export const Query = defineComponent({
7983
return createRenderFn(useQuery(opts));
8084
},
8185
});
86+
87+
export const Query = QueryImpl as typeof QueryImpl & {
88+
new (): {
89+
$slots: {
90+
default: (arg: QuerySlotProps) => VNode[];
91+
};
92+
};
93+
};

packages/villus/src/Subscription.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
import { defineComponent, toRef, watch } from 'vue';
1+
import { defineComponent, toRef, VNode, watch } from 'vue';
22
import { normalizeChildren } from './utils';
33
import { useSubscription, defaultReducer, Reducer } from './useSubscription';
4+
import { CombinedError } from '../dist/villus';
45

5-
export const Subscription = defineComponent({
6+
interface SubscriptionSlotProps {
7+
data: unknown;
8+
error: CombinedError | null;
9+
isPaused: boolean;
10+
pause(): void;
11+
resume(): void;
12+
}
13+
14+
const SubscriptionImpl = defineComponent({
615
name: 'Subscription',
716
props: {
817
query: {
@@ -45,13 +54,23 @@ export const Subscription = defineComponent({
4554
});
4655

4756
return () => {
48-
return normalizeChildren(ctx, {
57+
const slotProps: SubscriptionSlotProps = {
4958
data: data.value,
5059
error: error.value,
5160
pause,
5261
isPaused: isPaused.value,
5362
resume,
54-
});
63+
};
64+
65+
return normalizeChildren(ctx, slotProps);
5566
};
5667
},
5768
});
69+
70+
export const Subscription = SubscriptionImpl as typeof SubscriptionImpl & {
71+
new (): {
72+
$slots: {
73+
default: (arg: SubscriptionSlotProps) => VNode[];
74+
};
75+
};
76+
};

0 commit comments

Comments
 (0)