Skip to content

Commit

Permalink
feat!: throw NuxtError for async data composables
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Jan 7, 2024
1 parent 2f2a72a commit 7576c07
Show file tree
Hide file tree
Showing 13 changed files with 1,051 additions and 1,602 deletions.
2 changes: 1 addition & 1 deletion .nuxtrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
experimental.typescriptBundlerResolution=true
future.typescriptBundlerResolution=true
2 changes: 1 addition & 1 deletion docs/api/use-my-api-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type UseApiDataOptions<T> = Pick<
function UseApiData<T = any>(
path: MaybeRefOrGetter<string>,
opts?: UseApiDataOptions<T>
): AsyncData<T | undefined, FetchError>
): AsyncData<T | undefined, NuxtError>
```

## Caching
Expand Down
33 changes: 19 additions & 14 deletions docs/guide/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ While the idea of this Nuxt module is to mask your real API (and credentials) by

Thus, if your API fails to deliver, you can still handle the error response in your Nuxt app just like you would with a direct API call.

Both [generated composables](/api/) per endpoint will throw an [ofetch](https://github.com/unjs/ofetch) `FetchError` if your API fails to deliver.
Both [generated composables](/api/) per endpoint will throw a `NuxtError` if your API fails to deliver.

Logging the available error properties will provide you insights on what went wrong:

Expand All @@ -21,19 +21,24 @@ console.log(error.data) // Whatever your API returned

See all available examples below.

## `FetchError` Type Declaration
## `NuxtError` Type Declaration

```ts
// See https://github.com/unjs/ofetch
interface FetchError<T = any> extends Error {
request?: FetchRequest
options?: FetchOptions
response?: FetchResponse<T>
data?: T
status?: number
statusText?: string
statusCode?: number
export interface NuxtError<DataT = unknown> extends H3Error<DataT> {}

// See https://github.com/unjs/h3
declare class H3Error<DataT = unknown> extends Error {
static __h3_error__: boolean
statusCode: number
fatal: boolean
unhandled: boolean
statusMessage?: string
data?: DataT
cause?: unknown
constructor(message: string, opts?: {
cause?: unknown
})
toJSON(): Pick<H3Error<DataT>, 'data' | 'statusCode' | 'statusMessage' | 'message'>
}
```

Expand Down Expand Up @@ -61,7 +66,7 @@ export default defineNuxtConfig({

### Usage with `useJsonPlaceholderData`

When using the `useMyApiData` composable, the `error` is already typed as a `FetchError`.
When using the `useMyApiData` composable, the `error` is already typed as a `NuxtError`.

```ts
const { data, error } = await useJsonPlaceholderData('not/available')
Expand All @@ -75,7 +80,7 @@ watchEffect(() => {
### Usage with `$jsonPlaceholder`

```ts
import type { FetchError } from 'ofetch'
import type { NuxtError } from '#app'

function onSubmit() {
try {
Expand All @@ -85,7 +90,7 @@ function onSubmit() {
})
}
catch (error) {
console.error((error as FetchError).data)
console.error((error as NuxtError).data)
}
}
```
4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"devDependencies": {
"@iconify-json/carbon": "^1.1.27",
"unocss": "^0.58.0",
"vitepress": "1.0.0-rc.32"
"unocss": "^0.58.3",
"vitepress": "1.0.0-rc.35"
}
}
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nuxt-api-party",
"type": "module",
"version": "0.22.3",
"packageManager": "pnpm@8.12.1",
"packageManager": "pnpm@8.14.0",
"description": "Nuxt 3 module to securely connect with any API",
"author": "Johann Schopplich <pkg@johannschopplich.com>",
"license": "MIT",
Expand Down Expand Up @@ -47,8 +47,8 @@
"release": "bumpp --commit --push --tag"
},
"dependencies": {
"@nuxt/kit": "^3.8.2",
"defu": "^6.1.3",
"@nuxt/kit": "^3.9.1",
"defu": "^6.1.4",
"ofetch": "^1.3.3",
"ohash": "^1.1.3",
"pathe": "^1.1.1",
Expand All @@ -59,16 +59,16 @@
"openapi-typescript": "^6.7.3"
},
"devDependencies": {
"@antfu/eslint-config": "^2.4.6",
"@nuxt/module-builder": "^0.5.4",
"@antfu/eslint-config": "^2.6.1",
"@nuxt/module-builder": "^0.5.5",
"@nuxt/test-utils": "^3.9.0",
"@types/node": "^20.10.5",
"@types/node": "^20.10.6",
"bumpp": "^9.2.1",
"eslint": "^8.56.0",
"nuxt": "^3.8.2",
"nuxt": "^3.9.1",
"nuxt-api-party": "workspace:*",
"typescript": "^5.3.3",
"vitest": "^1.1.0",
"vue-tsc": "^1.8.25"
"vitest": "^1.1.3",
"vue-tsc": "^1.8.27"
}
}
2 changes: 1 addition & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default defineNuxtConfig({
},
},

experimental: {
future: {
typescriptBundlerResolution: true,
},

Expand Down
6 changes: 3 additions & 3 deletions playground/pages/jsonPlaceholder.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import type { FetchError } from 'ofetch'
import type { JsonPlaceholderComment } from '../types'
import type { NuxtError } from '#app'
const route = useRoute()
Expand Down Expand Up @@ -51,9 +51,9 @@ async function onSubmit() {
console.log('formResponse:', formResponse.value)
}
catch (error) {
console.error(error as FetchError)
console.error(error as NuxtError)
// Log the API response body
console.error('Response body:', (error as FetchError).data)
console.error('Response body:', (error as NuxtError).data)
}
}
</script>
Expand Down
6 changes: 3 additions & 3 deletions playground/pages/petStore.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import type { FetchError } from 'ofetch'
import type { NuxtError } from '#app'
import type { components } from '#nuxt-api-party/petStore'
type Pet = components['schemas']['Pet']
Expand Down Expand Up @@ -81,9 +81,9 @@ async function abandonGarfield() {
})
}
catch (error) {
console.error(error as FetchError)
console.error(error as NuxtError)
// Log the API response body
console.error('Response body:', (error as FetchError).data)
console.error('Response body:', (error as NuxtError).data)
}
}
</script>
Expand Down

0 comments on commit 7576c07

Please sign in to comment.