@@ -10,16 +10,18 @@ type AsyncDataResult<T> = AsyncData<T, Error>;
10
10
/**
11
11
* Transforms an object's Promise-returning functions into functions compatible with useAsyncData.
12
12
*
13
+ * Only includes functions that return Promises; other properties are excluded.
14
+ *
13
15
* For each function in the object:
14
16
* - If the function returns a Promise and takes no arguments, it becomes a function that accepts optional AsyncDataOptions.
15
17
* - If the function returns a Promise and takes arguments, it becomes a function that accepts an argsSupplier and optional AsyncDataOptions.
16
18
*
17
- * This allows you to use the functions within a Nuxt application, leveraging the reactivity and data fetching capabilities of useAsyncData.
18
- *
19
19
* @template T - The type of the object.
20
20
*/
21
21
export type AsyncDataWrapper < T > = {
22
- [ K in keyof T ] : T [ K ] extends ( ...args : infer Args ) => Promise < infer R >
22
+ [ K in keyof T as T [ K ] extends ( ...args : any [ ] ) => Promise < any >
23
+ ? K
24
+ : never ] : T [ K ] extends ( ...args : infer Args ) => Promise < infer R >
23
25
? Args extends [ ]
24
26
? /**
25
27
* Functions without arguments.
@@ -33,7 +35,10 @@ export type AsyncDataWrapper<T> = {
33
35
* @param options - Optional AsyncDataOptions to configure useAsyncData.
34
36
* @returns AsyncDataResult containing the data, pending state, and error.
35
37
*/
36
- ( argsSupplier : ( ) => Args , options ?: AsyncDataOptions < R > ) => AsyncDataResult < R >
38
+ (
39
+ argsSupplier : ( ) => Args ,
40
+ options ?: AsyncDataOptions < R > ,
41
+ ) => AsyncDataResult < R >
37
42
: never ;
38
43
} ;
39
44
@@ -56,13 +61,15 @@ export type AsyncDataWrapper<T> = {
56
61
* const wrappedObject = useAsyncDataWrapper(originalObject);
57
62
* ```
58
63
*/
59
- export function useAsyncDataWrapper < T extends Record < string , any > > ( obj : T ) : AsyncDataWrapper < T > {
64
+ export function useAsyncDataWrapper < T extends Record < string , any > > (
65
+ obj : T ,
66
+ ) : AsyncDataWrapper < T > {
60
67
const composable = { } as AsyncDataWrapper < T > ;
61
68
const proto = Object . getPrototypeOf ( obj ) ;
62
69
63
70
// Get function names from the object's prototype, excluding the constructor
64
71
const functionNames = Object . getOwnPropertyNames ( proto ) . filter (
65
- key => key !== 'constructor' && typeof obj [ key ] === 'function'
72
+ key => key !== 'constructor' && typeof obj [ key ] === 'function' ,
66
73
) ;
67
74
68
75
for ( const key of functionNames ) {
@@ -89,27 +96,41 @@ export function useAsyncDataWrapper<T extends Record<string, any>>(obj: T): Asyn
89
96
// Reactive reference to arguments
90
97
const argsRef = computed ( ( ) => argsSupplier ! ( ) ) ;
91
98
// Unique key for useAsyncData
92
- const dataKeyRef = computed ( ( ) => `${ key } -${ JSON . stringify ( argsRef . value ) } ` ) ;
99
+ const dataKeyRef = computed (
100
+ ( ) => `${ key } -${ JSON . stringify ( argsRef . value ) } ` ,
101
+ ) ;
93
102
94
103
// Call useAsyncData with the generated key and function
95
104
const asyncDataResult = useAsyncData (
96
105
dataKeyRef . value ,
97
- ( ) => originalFunction ( ...unref ( argsRef ) ) ,
106
+ ( ) => {
107
+ const result = originalFunction ( ...unref ( argsRef ) ) ;
108
+ // Ensure we return a Promise
109
+ return result instanceof Promise ? result : Promise . resolve ( result ) ;
110
+ } ,
98
111
{
99
112
// Re-execute when arguments change
100
113
watch : [ argsRef ] ,
101
114
// Spread additional options
102
115
...options ,
103
- }
116
+ } ,
104
117
) ;
105
118
106
119
return asyncDataResult ;
107
120
} else {
108
121
// For functions without arguments
109
- const asyncDataResult = useAsyncData ( key , ( ) => originalFunction ( ) , {
110
- // Spread additional options
111
- ...options ,
112
- } ) ;
122
+ const asyncDataResult = useAsyncData (
123
+ key ,
124
+ ( ) => {
125
+ const result = originalFunction ( ) ;
126
+ // Ensure we return a Promise
127
+ return result instanceof Promise ? result : Promise . resolve ( result ) ;
128
+ } ,
129
+ {
130
+ // Spread additional options
131
+ ...options ,
132
+ } ,
133
+ ) ;
113
134
114
135
return asyncDataResult ;
115
136
}
0 commit comments