-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Go-like syntax to handle errors #370
Comments
I like the idea. Only thinking it might be more suitable for |
I fully agree with @pi0. @enkot, Generally APIs Currently the work around will be to let your api return |
By the way, you could support both syntaxes in the same return object: const { data, error } = $fetch('/api/v1', { throw: false })
// And:
const [data, error] = $fetch('/api/v1', { throw: false }) Implementation: export function createIsomorphicDestructurable<
T extends Record<string, unknown>,
A extends readonly any[],
>(obj: T, arr: A): T & A {
const clone = { ...obj }
Object.defineProperty(clone, Symbol.iterator, {
enumerable: false,
value() {
let index = 0
return {
next: () => ({
value: arr[index++],
done: index > arr.length,
}),
}
},
})
return clone as T & A
} |
Good point 👍 |
@enkot Absolutely! It's the same implementation under the hood. The VueUse one also has IE11 support, probably because VueUse still supports Vue 2. :) |
Agree, generally the same for all routes, but not 100%. |
Describe the feature
I suggest to add
throw: boolean
option so we can return the HTTP error instead of throwing it. This way users can get full type safety after implementation of Typed API definition.Example:
or not throw by default:
data
- 2xx response if OK, otherwiseundefined
error
- 5xx, 4xx response if not OK, otherwiseundefined
Alternatives
Return array:
Additional information
The text was updated successfully, but these errors were encountered: