nuxt-openapi-hyperfetch 0.1.7-alpha.1 → 0.1.8-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +291 -292
- package/INSTRUCTIONS.md +327 -327
- package/LICENSE +202 -202
- package/README.md +231 -227
- package/dist/cli/logger.d.ts +26 -0
- package/dist/cli/logger.js +36 -0
- package/dist/cli/logo.js +5 -5
- package/dist/generators/components/connector-generator/generator.d.ts +12 -0
- package/dist/generators/components/connector-generator/generator.js +116 -0
- package/dist/generators/components/connector-generator/templates.d.ts +18 -0
- package/dist/generators/components/connector-generator/templates.js +222 -0
- package/dist/generators/components/connector-generator/types.d.ts +32 -0
- package/dist/generators/components/connector-generator/types.js +7 -0
- package/dist/generators/components/schema-analyzer/index.d.ts +17 -0
- package/dist/generators/components/schema-analyzer/index.js +20 -0
- package/dist/generators/components/schema-analyzer/intent-detector.d.ts +17 -0
- package/dist/generators/components/schema-analyzer/intent-detector.js +143 -0
- package/dist/generators/components/schema-analyzer/openapi-reader.d.ts +11 -0
- package/dist/generators/components/schema-analyzer/openapi-reader.js +76 -0
- package/dist/generators/components/schema-analyzer/resource-grouper.d.ts +6 -0
- package/dist/generators/components/schema-analyzer/resource-grouper.js +132 -0
- package/dist/generators/components/schema-analyzer/schema-field-mapper.d.ts +35 -0
- package/dist/generators/components/schema-analyzer/schema-field-mapper.js +220 -0
- package/dist/generators/components/schema-analyzer/types.d.ts +156 -0
- package/dist/generators/components/schema-analyzer/types.js +7 -0
- package/dist/generators/nuxt-server/generator.d.ts +2 -1
- package/dist/generators/nuxt-server/generator.js +21 -21
- package/dist/generators/shared/runtime/apiHelpers.d.ts +81 -41
- package/dist/generators/shared/runtime/apiHelpers.js +97 -104
- package/dist/generators/shared/runtime/pagination.d.ts +168 -0
- package/dist/generators/shared/runtime/pagination.js +179 -0
- package/dist/generators/shared/runtime/useDeleteConnector.d.ts +16 -0
- package/dist/generators/shared/runtime/useDeleteConnector.js +93 -0
- package/dist/generators/shared/runtime/useDetailConnector.d.ts +14 -0
- package/dist/generators/shared/runtime/useDetailConnector.js +50 -0
- package/dist/generators/shared/runtime/useFormConnector.d.ts +19 -0
- package/dist/generators/shared/runtime/useFormConnector.js +113 -0
- package/dist/generators/shared/runtime/useListConnector.d.ts +25 -0
- package/dist/generators/shared/runtime/useListConnector.js +125 -0
- package/dist/generators/shared/runtime/zod-error-merger.d.ts +23 -0
- package/dist/generators/shared/runtime/zod-error-merger.js +106 -0
- package/dist/generators/shared/templates/api-callbacks-plugin.js +54 -11
- package/dist/generators/shared/templates/api-pagination-plugin.d.ts +51 -0
- package/dist/generators/shared/templates/api-pagination-plugin.js +152 -0
- package/dist/generators/use-async-data/generator.d.ts +2 -1
- package/dist/generators/use-async-data/generator.js +14 -14
- package/dist/generators/use-async-data/runtime/useApiAsyncData.js +114 -13
- package/dist/generators/use-async-data/runtime/useApiAsyncDataRaw.js +88 -10
- package/dist/generators/use-async-data/templates.js +17 -17
- package/dist/generators/use-fetch/generator.d.ts +2 -1
- package/dist/generators/use-fetch/generator.js +12 -12
- package/dist/generators/use-fetch/runtime/useApiRequest.js +149 -40
- package/dist/generators/use-fetch/templates.js +14 -14
- package/dist/index.js +25 -0
- package/dist/module/index.d.ts +4 -0
- package/dist/module/index.js +93 -0
- package/dist/module/types.d.ts +27 -0
- package/dist/module/types.js +1 -0
- package/docs/API-REFERENCE.md +886 -887
- package/eslint.config.js +13 -0
- package/package.json +23 -2
- package/src/cli/config.ts +140 -140
- package/src/cli/logger.ts +124 -66
- package/src/cli/logo.ts +25 -25
- package/src/cli/types.ts +50 -50
- package/src/generators/nuxt-server/generator.ts +272 -270
- package/src/generators/shared/runtime/apiHelpers.ts +507 -507
- package/src/generators/shared/templates/api-callbacks-plugin.ts +352 -352
- package/src/generators/use-async-data/generator.ts +205 -204
- package/src/generators/use-async-data/runtime/useApiAsyncData.ts +229 -229
- package/src/generators/use-async-data/runtime/useApiAsyncDataRaw.ts +245 -245
- package/src/generators/use-async-data/templates.ts +257 -257
- package/src/generators/use-fetch/generator.ts +170 -169
- package/src/generators/use-fetch/runtime/useApiRequest.ts +234 -234
- package/src/generators/use-fetch/templates.ts +214 -214
- package/src/index.ts +265 -265
- package/src/module/index.ts +133 -0
- package/src/module/types.ts +31 -0
- package/src/generators/tanstack-query/generator.ts +0 -11
|
@@ -1,234 +1,234 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
/**
|
|
3
|
-
* Nuxt Runtime Helper - This file is copied to the generated output
|
|
4
|
-
* It requires Nuxt 3 to be installed in the target project
|
|
5
|
-
*/
|
|
6
|
-
import { watch } from 'vue';
|
|
7
|
-
import type { UseFetchOptions } from '#app';
|
|
8
|
-
import {
|
|
9
|
-
getGlobalHeaders,
|
|
10
|
-
getGlobalBaseUrl,
|
|
11
|
-
applyPick,
|
|
12
|
-
applyRequestModifications,
|
|
13
|
-
mergeCallbacks,
|
|
14
|
-
type RequestContext,
|
|
15
|
-
type ModifiedRequestContext,
|
|
16
|
-
type FinishContext,
|
|
17
|
-
type ApiRequestOptions as BaseApiRequestOptions,
|
|
18
|
-
} from '../../shared/runtime/apiHelpers.js';
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Helper type to infer transformed data type
|
|
22
|
-
* If transform is provided, infer its return type
|
|
23
|
-
* If pick is provided, return partial object (type inference for nested paths is complex)
|
|
24
|
-
* Otherwise, return original type
|
|
25
|
-
*/
|
|
26
|
-
type MaybeTransformed<T, Options> = Options extends { transform: (...args: any) => infer R }
|
|
27
|
-
? R
|
|
28
|
-
: Options extends { pick: ReadonlyArray<any> }
|
|
29
|
-
? any // With nested paths, type inference is complex, so we use any
|
|
30
|
-
: T;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Options for useFetch API requests with lifecycle callbacks.
|
|
34
|
-
* Extends all native Nuxt useFetch options plus our custom callbacks, transform, and pick.
|
|
35
|
-
* Native options like baseURL, method, body, headers, query, lazy, server, immediate, etc. are all available.
|
|
36
|
-
*/
|
|
37
|
-
export type ApiRequestOptions<T = any> = BaseApiRequestOptions<T> & Omit<UseFetchOptions<T>, 'transform' | 'pick'>;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Enhanced useFetch wrapper with lifecycle callbacks and request interception
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```typescript
|
|
44
|
-
* // Basic usage
|
|
45
|
-
* const { data, error } = useApiRequest<Pet>('/api/pets', {
|
|
46
|
-
* method: 'POST',
|
|
47
|
-
* body: { name: 'Max' },
|
|
48
|
-
* });
|
|
49
|
-
*
|
|
50
|
-
* // With transform (type is inferred!)
|
|
51
|
-
* const { data } = useApiRequest<Pet>('/api/pets/1', {
|
|
52
|
-
* transform: (pet) => ({ displayName: pet.name, available: pet.status === 'available' })
|
|
53
|
-
* });
|
|
54
|
-
* // data is Ref<{ displayName: string, available: boolean }>
|
|
55
|
-
*
|
|
56
|
-
* // With pick (simple fields)
|
|
57
|
-
* const { data } = useApiRequest<Pet>('/api/pets/1', {
|
|
58
|
-
* pick: ['id', 'name'] as const
|
|
59
|
-
* });
|
|
60
|
-
*
|
|
61
|
-
* // With pick (nested dot notation)
|
|
62
|
-
* const { data } = useApiRequest('/api/user', {
|
|
63
|
-
* pick: ['person.name', 'person.email', 'status']
|
|
64
|
-
* });
|
|
65
|
-
* // Result: { person: { name: '...', email: '...' }, status: '...' }
|
|
66
|
-
*
|
|
67
|
-
* // With callbacks
|
|
68
|
-
* const { data } = useApiRequest<Pet>('/api/pets', {
|
|
69
|
-
* onRequest: (ctx) => ({ headers: { 'Authorization': `Bearer ${token}` } }),
|
|
70
|
-
* onSuccess: (pet) => console.log('Got pet:', pet),
|
|
71
|
-
* onError: (err) => console.error('Failed:', err),
|
|
72
|
-
* });
|
|
73
|
-
* ```
|
|
74
|
-
*/
|
|
75
|
-
export function useApiRequest<T = any, Options extends ApiRequestOptions<T> = ApiRequestOptions<T>>(
|
|
76
|
-
url: string | (() => string),
|
|
77
|
-
options?: Options
|
|
78
|
-
) {
|
|
79
|
-
const {
|
|
80
|
-
onRequest,
|
|
81
|
-
onSuccess,
|
|
82
|
-
onError,
|
|
83
|
-
onFinish,
|
|
84
|
-
skipGlobalCallbacks,
|
|
85
|
-
transform,
|
|
86
|
-
pick,
|
|
87
|
-
...fetchOptions
|
|
88
|
-
} = options || {};
|
|
89
|
-
|
|
90
|
-
// Prepare request context for onRequest interceptor
|
|
91
|
-
const urlValue = typeof url === 'function' ? url() : url;
|
|
92
|
-
const requestContext: RequestContext = {
|
|
93
|
-
url: urlValue,
|
|
94
|
-
method: fetchOptions.method || 'GET',
|
|
95
|
-
body: fetchOptions.body,
|
|
96
|
-
headers: fetchOptions.headers,
|
|
97
|
-
query: fetchOptions.query,
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
// Merge local and global callbacks
|
|
101
|
-
const mergedCallbacks = mergeCallbacks(
|
|
102
|
-
urlValue,
|
|
103
|
-
{ onRequest, onSuccess, onError, onFinish },
|
|
104
|
-
skipGlobalCallbacks
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
// Apply global headers configuration (from composable or plugin)
|
|
108
|
-
const modifiedOptions = { ...fetchOptions };
|
|
109
|
-
const globalHeaders = getGlobalHeaders();
|
|
110
|
-
if (Object.keys(globalHeaders).length > 0) {
|
|
111
|
-
modifiedOptions.headers = {
|
|
112
|
-
...globalHeaders,
|
|
113
|
-
...modifiedOptions.headers, // User headers override global headers
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// Apply global base URL from runtimeConfig.public.apiBaseUrl (if not already set per-composable)
|
|
118
|
-
if (!modifiedOptions.baseURL) {
|
|
119
|
-
modifiedOptions.baseURL = getGlobalBaseUrl();
|
|
120
|
-
}
|
|
121
|
-
if (!modifiedOptions.baseURL) {
|
|
122
|
-
console.warn(
|
|
123
|
-
'[nuxt-openapi-hyperfetch] No baseURL configured. Set runtimeConfig.public.apiBaseUrl in nuxt.config.ts or pass baseURL in options.'
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// Execute merged onRequest interceptor and apply modifications
|
|
128
|
-
if (mergedCallbacks.onRequest) {
|
|
129
|
-
try {
|
|
130
|
-
const result = mergedCallbacks.onRequest(requestContext);
|
|
131
|
-
|
|
132
|
-
// Handle async onRequest
|
|
133
|
-
if (result && typeof result === 'object' && 'then' in result) {
|
|
134
|
-
result
|
|
135
|
-
.then((modifications) => {
|
|
136
|
-
if (modifications) {
|
|
137
|
-
applyRequestModifications(modifiedOptions, modifications);
|
|
138
|
-
}
|
|
139
|
-
})
|
|
140
|
-
.catch((error) => {
|
|
141
|
-
console.error('Error in merged onRequest callback:', error);
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
// Handle sync onRequest with return value
|
|
145
|
-
else if (result && typeof result === 'object') {
|
|
146
|
-
applyRequestModifications(modifiedOptions, result);
|
|
147
|
-
}
|
|
148
|
-
} catch (error) {
|
|
149
|
-
console.error('Error in merged onRequest callback:', error);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// Make the actual request using Nuxt's useFetch
|
|
154
|
-
const result = useFetch<T>(url, modifiedOptions);
|
|
155
|
-
|
|
156
|
-
// Create a ref for transformed data
|
|
157
|
-
type TransformedType = MaybeTransformed<T, Options>;
|
|
158
|
-
const transformedData = ref<TransformedType | null>(null);
|
|
159
|
-
|
|
160
|
-
// Track if callbacks have been executed to avoid duplicates
|
|
161
|
-
let successExecuted = false;
|
|
162
|
-
let errorExecuted = false;
|
|
163
|
-
|
|
164
|
-
// Watch for changes in data, error, and pending states
|
|
165
|
-
watch(
|
|
166
|
-
() => [result.data.value, result.error.value, result.pending.value] as const,
|
|
167
|
-
async ([data, error, pending], prev) => {
|
|
168
|
-
const [prevData, prevError, prevPending] = prev ?? [undefined, undefined, undefined];
|
|
169
|
-
// Apply transformations when data arrives
|
|
170
|
-
if (data && data !== prevData) {
|
|
171
|
-
let processedData: any = data;
|
|
172
|
-
|
|
173
|
-
// Step 1: Apply pick if specified
|
|
174
|
-
if (pick) {
|
|
175
|
-
processedData = applyPick(processedData, pick);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// Step 2: Apply transform if specified
|
|
179
|
-
if (transform) {
|
|
180
|
-
try {
|
|
181
|
-
processedData = transform(processedData);
|
|
182
|
-
} catch (err) {
|
|
183
|
-
console.error('Error in transform function:', err);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// Update transformed data ref
|
|
188
|
-
transformedData.value = processedData as TransformedType;
|
|
189
|
-
|
|
190
|
-
// onSuccess - when data arrives and no error (using merged callback)
|
|
191
|
-
if (!error && !successExecuted && mergedCallbacks.onSuccess) {
|
|
192
|
-
successExecuted = true;
|
|
193
|
-
try {
|
|
194
|
-
await mergedCallbacks.onSuccess(processedData);
|
|
195
|
-
} catch (err) {
|
|
196
|
-
console.error('Error in merged onSuccess callback:', err);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// onError - when an error occurs (using merged callback)
|
|
202
|
-
if (error && error !== prevError && !errorExecuted && mergedCallbacks.onError) {
|
|
203
|
-
errorExecuted = true;
|
|
204
|
-
try {
|
|
205
|
-
await mergedCallbacks.onError(error);
|
|
206
|
-
} catch (err) {
|
|
207
|
-
console.error('Error in merged onError callback:', err);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// onFinish - when request completes (was pending, now not) (using merged callback)
|
|
212
|
-
if (prevPending && !pending && mergedCallbacks.onFinish) {
|
|
213
|
-
const finishContext: FinishContext<TransformedType> = {
|
|
214
|
-
data: transformedData.value || undefined,
|
|
215
|
-
error: error || undefined,
|
|
216
|
-
success: !!transformedData.value && !error,
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
try {
|
|
220
|
-
await mergedCallbacks.onFinish(finishContext);
|
|
221
|
-
} catch (err) {
|
|
222
|
-
console.error('Error in merged onFinish callback:', err);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
},
|
|
226
|
-
{ immediate: true }
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
// Return result with transformed data
|
|
230
|
-
return {
|
|
231
|
-
...result,
|
|
232
|
-
data: transformedData as Ref<TransformedType | null>,
|
|
233
|
-
};
|
|
234
|
-
}
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* Nuxt Runtime Helper - This file is copied to the generated output
|
|
4
|
+
* It requires Nuxt 3 to be installed in the target project
|
|
5
|
+
*/
|
|
6
|
+
import { watch } from 'vue';
|
|
7
|
+
import type { UseFetchOptions } from '#app';
|
|
8
|
+
import {
|
|
9
|
+
getGlobalHeaders,
|
|
10
|
+
getGlobalBaseUrl,
|
|
11
|
+
applyPick,
|
|
12
|
+
applyRequestModifications,
|
|
13
|
+
mergeCallbacks,
|
|
14
|
+
type RequestContext,
|
|
15
|
+
type ModifiedRequestContext,
|
|
16
|
+
type FinishContext,
|
|
17
|
+
type ApiRequestOptions as BaseApiRequestOptions,
|
|
18
|
+
} from '../../shared/runtime/apiHelpers.js';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Helper type to infer transformed data type
|
|
22
|
+
* If transform is provided, infer its return type
|
|
23
|
+
* If pick is provided, return partial object (type inference for nested paths is complex)
|
|
24
|
+
* Otherwise, return original type
|
|
25
|
+
*/
|
|
26
|
+
type MaybeTransformed<T, Options> = Options extends { transform: (...args: any) => infer R }
|
|
27
|
+
? R
|
|
28
|
+
: Options extends { pick: ReadonlyArray<any> }
|
|
29
|
+
? any // With nested paths, type inference is complex, so we use any
|
|
30
|
+
: T;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Options for useFetch API requests with lifecycle callbacks.
|
|
34
|
+
* Extends all native Nuxt useFetch options plus our custom callbacks, transform, and pick.
|
|
35
|
+
* Native options like baseURL, method, body, headers, query, lazy, server, immediate, etc. are all available.
|
|
36
|
+
*/
|
|
37
|
+
export type ApiRequestOptions<T = any> = BaseApiRequestOptions<T> & Omit<UseFetchOptions<T>, 'transform' | 'pick'>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Enhanced useFetch wrapper with lifecycle callbacks and request interception
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* // Basic usage
|
|
45
|
+
* const { data, error } = useApiRequest<Pet>('/api/pets', {
|
|
46
|
+
* method: 'POST',
|
|
47
|
+
* body: { name: 'Max' },
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* // With transform (type is inferred!)
|
|
51
|
+
* const { data } = useApiRequest<Pet>('/api/pets/1', {
|
|
52
|
+
* transform: (pet) => ({ displayName: pet.name, available: pet.status === 'available' })
|
|
53
|
+
* });
|
|
54
|
+
* // data is Ref<{ displayName: string, available: boolean }>
|
|
55
|
+
*
|
|
56
|
+
* // With pick (simple fields)
|
|
57
|
+
* const { data } = useApiRequest<Pet>('/api/pets/1', {
|
|
58
|
+
* pick: ['id', 'name'] as const
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* // With pick (nested dot notation)
|
|
62
|
+
* const { data } = useApiRequest('/api/user', {
|
|
63
|
+
* pick: ['person.name', 'person.email', 'status']
|
|
64
|
+
* });
|
|
65
|
+
* // Result: { person: { name: '...', email: '...' }, status: '...' }
|
|
66
|
+
*
|
|
67
|
+
* // With callbacks
|
|
68
|
+
* const { data } = useApiRequest<Pet>('/api/pets', {
|
|
69
|
+
* onRequest: (ctx) => ({ headers: { 'Authorization': `Bearer ${token}` } }),
|
|
70
|
+
* onSuccess: (pet) => console.log('Got pet:', pet),
|
|
71
|
+
* onError: (err) => console.error('Failed:', err),
|
|
72
|
+
* });
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export function useApiRequest<T = any, Options extends ApiRequestOptions<T> = ApiRequestOptions<T>>(
|
|
76
|
+
url: string | (() => string),
|
|
77
|
+
options?: Options
|
|
78
|
+
) {
|
|
79
|
+
const {
|
|
80
|
+
onRequest,
|
|
81
|
+
onSuccess,
|
|
82
|
+
onError,
|
|
83
|
+
onFinish,
|
|
84
|
+
skipGlobalCallbacks,
|
|
85
|
+
transform,
|
|
86
|
+
pick,
|
|
87
|
+
...fetchOptions
|
|
88
|
+
} = options || {};
|
|
89
|
+
|
|
90
|
+
// Prepare request context for onRequest interceptor
|
|
91
|
+
const urlValue = typeof url === 'function' ? url() : url;
|
|
92
|
+
const requestContext: RequestContext = {
|
|
93
|
+
url: urlValue,
|
|
94
|
+
method: fetchOptions.method || 'GET',
|
|
95
|
+
body: fetchOptions.body,
|
|
96
|
+
headers: fetchOptions.headers,
|
|
97
|
+
query: fetchOptions.query,
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// Merge local and global callbacks
|
|
101
|
+
const mergedCallbacks = mergeCallbacks(
|
|
102
|
+
urlValue,
|
|
103
|
+
{ onRequest, onSuccess, onError, onFinish },
|
|
104
|
+
skipGlobalCallbacks
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
// Apply global headers configuration (from composable or plugin)
|
|
108
|
+
const modifiedOptions = { ...fetchOptions };
|
|
109
|
+
const globalHeaders = getGlobalHeaders();
|
|
110
|
+
if (Object.keys(globalHeaders).length > 0) {
|
|
111
|
+
modifiedOptions.headers = {
|
|
112
|
+
...globalHeaders,
|
|
113
|
+
...modifiedOptions.headers, // User headers override global headers
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Apply global base URL from runtimeConfig.public.apiBaseUrl (if not already set per-composable)
|
|
118
|
+
if (!modifiedOptions.baseURL) {
|
|
119
|
+
modifiedOptions.baseURL = getGlobalBaseUrl();
|
|
120
|
+
}
|
|
121
|
+
if (!modifiedOptions.baseURL) {
|
|
122
|
+
console.warn(
|
|
123
|
+
'[nuxt-openapi-hyperfetch] No baseURL configured. Set runtimeConfig.public.apiBaseUrl in nuxt.config.ts or pass baseURL in options.'
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Execute merged onRequest interceptor and apply modifications
|
|
128
|
+
if (mergedCallbacks.onRequest) {
|
|
129
|
+
try {
|
|
130
|
+
const result = mergedCallbacks.onRequest(requestContext);
|
|
131
|
+
|
|
132
|
+
// Handle async onRequest
|
|
133
|
+
if (result && typeof result === 'object' && 'then' in result) {
|
|
134
|
+
result
|
|
135
|
+
.then((modifications) => {
|
|
136
|
+
if (modifications) {
|
|
137
|
+
applyRequestModifications(modifiedOptions, modifications);
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
.catch((error) => {
|
|
141
|
+
console.error('Error in merged onRequest callback:', error);
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
// Handle sync onRequest with return value
|
|
145
|
+
else if (result && typeof result === 'object') {
|
|
146
|
+
applyRequestModifications(modifiedOptions, result);
|
|
147
|
+
}
|
|
148
|
+
} catch (error) {
|
|
149
|
+
console.error('Error in merged onRequest callback:', error);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Make the actual request using Nuxt's useFetch
|
|
154
|
+
const result = useFetch<T>(url, modifiedOptions);
|
|
155
|
+
|
|
156
|
+
// Create a ref for transformed data
|
|
157
|
+
type TransformedType = MaybeTransformed<T, Options>;
|
|
158
|
+
const transformedData = ref<TransformedType | null>(null);
|
|
159
|
+
|
|
160
|
+
// Track if callbacks have been executed to avoid duplicates
|
|
161
|
+
let successExecuted = false;
|
|
162
|
+
let errorExecuted = false;
|
|
163
|
+
|
|
164
|
+
// Watch for changes in data, error, and pending states
|
|
165
|
+
watch(
|
|
166
|
+
() => [result.data.value, result.error.value, result.pending.value] as const,
|
|
167
|
+
async ([data, error, pending], prev) => {
|
|
168
|
+
const [prevData, prevError, prevPending] = prev ?? [undefined, undefined, undefined];
|
|
169
|
+
// Apply transformations when data arrives
|
|
170
|
+
if (data && data !== prevData) {
|
|
171
|
+
let processedData: any = data;
|
|
172
|
+
|
|
173
|
+
// Step 1: Apply pick if specified
|
|
174
|
+
if (pick) {
|
|
175
|
+
processedData = applyPick(processedData, pick);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Step 2: Apply transform if specified
|
|
179
|
+
if (transform) {
|
|
180
|
+
try {
|
|
181
|
+
processedData = transform(processedData);
|
|
182
|
+
} catch (err) {
|
|
183
|
+
console.error('Error in transform function:', err);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Update transformed data ref
|
|
188
|
+
transformedData.value = processedData as TransformedType;
|
|
189
|
+
|
|
190
|
+
// onSuccess - when data arrives and no error (using merged callback)
|
|
191
|
+
if (!error && !successExecuted && mergedCallbacks.onSuccess) {
|
|
192
|
+
successExecuted = true;
|
|
193
|
+
try {
|
|
194
|
+
await mergedCallbacks.onSuccess(processedData);
|
|
195
|
+
} catch (err) {
|
|
196
|
+
console.error('Error in merged onSuccess callback:', err);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// onError - when an error occurs (using merged callback)
|
|
202
|
+
if (error && error !== prevError && !errorExecuted && mergedCallbacks.onError) {
|
|
203
|
+
errorExecuted = true;
|
|
204
|
+
try {
|
|
205
|
+
await mergedCallbacks.onError(error);
|
|
206
|
+
} catch (err) {
|
|
207
|
+
console.error('Error in merged onError callback:', err);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// onFinish - when request completes (was pending, now not) (using merged callback)
|
|
212
|
+
if (prevPending && !pending && mergedCallbacks.onFinish) {
|
|
213
|
+
const finishContext: FinishContext<TransformedType> = {
|
|
214
|
+
data: transformedData.value || undefined,
|
|
215
|
+
error: error || undefined,
|
|
216
|
+
success: !!transformedData.value && !error,
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
try {
|
|
220
|
+
await mergedCallbacks.onFinish(finishContext);
|
|
221
|
+
} catch (err) {
|
|
222
|
+
console.error('Error in merged onFinish callback:', err);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
{ immediate: true }
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
// Return result with transformed data
|
|
230
|
+
return {
|
|
231
|
+
...result,
|
|
232
|
+
data: transformedData as Ref<TransformedType | null>,
|
|
233
|
+
};
|
|
234
|
+
}
|