-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: pass-through response status and headers to client (#44)
* feat: pass-through response headers to client Breaking Change Resolves #25 and #41 * chore: fix tests * fix: Add missing imports for h3 utilities * test: add blob test * docs: update error handling guide * chore: fix linting issue --------- Co-authored-by: Johann Schopplich <[email protected]>
- Loading branch information
1 parent
650fc44
commit 5a76bda
Showing
11 changed files
with
172 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Error Handling | ||
|
||
While the idea of this Nuxt module is to mask your real API (and credentials) by creating a server proxy, `nuxt-api-party` will minimize the hassle of handling errors by passing the following properties to the client: | ||
|
||
- Response body | ||
- HTTP status code | ||
- HTTP status message | ||
- Headers | ||
|
||
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. | ||
|
||
Logging the `error.data` property will provide you with the response body, like: | ||
|
||
```json | ||
{ | ||
"message": "Not Found", | ||
"statusCode": 404, | ||
"statusMessage": "Not Found", | ||
"url": "/api/foo/bar" | ||
} | ||
``` | ||
|
||
See all available examples below. | ||
|
||
## `FetchError` 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 | ||
statusMessage?: string | ||
} | ||
``` | ||
|
||
## Examples | ||
|
||
::: info | ||
The examples below assume that you have set up an API endpoint called `jsonPlaceholder`: | ||
|
||
```ts | ||
// `nuxt.config.ts` | ||
export default defineNuxtConfig({ | ||
modules: ['nuxt-api-party'], | ||
|
||
apiParty: { | ||
endpoints: { | ||
jsonPlaceholder: { | ||
url: 'https://jsonplaceholder.typicode.com' | ||
} | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
::: | ||
|
||
### Usage with `useJsonPlaceholderData` | ||
|
||
When using the `useMyApiData` composable, the `error` is already typed as a `FetchError`. | ||
|
||
```ts | ||
const { data, error } = await useJsonPlaceholderData('not/available') | ||
|
||
watchEffect(() => { | ||
if (error.value) | ||
console.error(error.data) | ||
}) | ||
``` | ||
|
||
### Usage with `$jsonPlaceholder` | ||
|
||
```ts | ||
import type { FetchError } from 'ofetch' | ||
|
||
function onSubmit() { | ||
try { | ||
const response = await $jsonPlaceholder('not/available', { | ||
method: 'POST', | ||
body: form.value | ||
}) | ||
} | ||
catch (error) { | ||
console.error((error as FetchError).data) | ||
} | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
<script setup lang="ts"> | ||
import type { TestApiTodo } from '~/types' | ||
const data = await $testApi<TestApiTodo>('todos') | ||
const json = await $testApi<TestApiTodo>('todos') | ||
const blob = await $testApi('blob') | ||
useTestResult(data) | ||
useTestResult({ | ||
json, | ||
blob, | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default defineEventHandler(() => { | ||
return new Blob(['Foo'], { type: 'text/plain' }) | ||
}) |