Skip to content

Commit

Permalink
docs: add interceptors guide (closes #69)
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Apr 17, 2024
1 parent b6331c8 commit 4f8c21e
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 36 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![nuxt-api-party](./docs/public/og.jpg)](https://nuxt-api-party.byjohann.dev)

# nuxt-api-party
# Nuxt API Party

[![npm version](https://img.shields.io/npm/v/nuxt-api-party?color=a1b858&label=)](https://www.npmjs.com/package/nuxt-api-party)

Expand Down Expand Up @@ -33,7 +33,7 @@ npx nuxi@latest module add api-party
> [!TIP]
> [📖 Read the documentation](https://nuxt-api-party.byjohann.dev)
Add this module `nuxt-api-party` to your Nuxt config and prepare your first API connection by setting an endpoint object with the following properties for the `apiParty` module option:
Add the Nuxt API Party to your Nuxt config and prepare your first API connection by setting an endpoint object with the following properties for the `apiParty` module option:

```ts
// `nuxt.config.ts`
Expand Down
19 changes: 11 additions & 8 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { defineConfig } from 'vitepress'
import type { DefaultTheme } from 'vitepress'
import { description, name, version } from '../../package.json'
import { description, version } from '../../package.json'
import {
github,
name,
ogImage,
ogUrl,
releases,
Expand All @@ -13,7 +14,7 @@ const url = new URL(ogUrl)
export default defineConfig({
lang: 'en-US',
title: name,
description: 'Connect to any API securely',
description: 'Server proxy and generated API composables',
head: [
['link', { rel: 'icon', href: '/logo.svg', type: 'image/svg+xml' }],
['meta', { name: 'author', content: 'Johann Schopplich' }],
Expand Down Expand Up @@ -82,10 +83,11 @@ function nav(): DefaultTheme.NavItem[] {
{
text: 'Advanced',
items: [
{ text: 'Hydration', link: '/guide/hydration' },
{ text: 'Caching', link: '/guide/caching' },
{ text: 'Interceptors', link: '/guide/interceptors' },
{ text: 'Cookies', link: '/guide/cookies' },
{ text: 'Retries', link: '/guide/retries' },
{ text: 'Hydration', link: '/guide/hydration' },
{ text: 'Dynamic Backend URL', link: '/guide/dynamic-backend-url' },
],
},
Expand All @@ -106,8 +108,8 @@ function nav(): DefaultTheme.NavItem[] {
{
text: 'Composables',
items: [
{ text: 'useMyApiData', link: '/api/use-my-api-data' },
{ text: '$myApi', link: '/api/my-api' },
{ text: 'useFetch Substitute', link: '/api/use-my-api-data' },
{ text: '$fetch Substitute', link: '/api/my-api' },
],
},
],
Expand Down Expand Up @@ -144,10 +146,11 @@ function sidebarGuide(): DefaultTheme.SidebarItem[] {
{
text: 'Advanced',
items: [
{ text: 'Hydration', link: '/guide/hydration' },
{ text: 'Caching', link: '/guide/caching' },
{ text: 'Interceptors', link: '/guide/interceptors' },
{ text: 'Cookies', link: '/guide/cookies' },
{ text: 'Retries', link: '/guide/retries' },
{ text: 'Hydration', link: '/guide/hydration' },
{ text: 'Dynamic Backend URL', link: '/guide/dynamic-backend-url' },
],
},
Expand All @@ -166,8 +169,8 @@ function sidebarApi(): DefaultTheme.SidebarItem[] {
{
text: 'Composables',
items: [
{ text: 'useMyApiData', link: '/api/use-my-api-data' },
{ text: '$myApi', link: '/api/my-api' },
{ text: 'useFetch Substitute', link: '/api/use-my-api-data' },
{ text: '$fetch Substitute', link: '/api/my-api' },
],
},
]
Expand Down
1 change: 1 addition & 0 deletions docs/.vitepress/meta.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* VitePress head */
export const name = 'Nuxt API Party'
export const ogUrl = 'https://nuxt-api-party.byjohann.dev/'
export const ogImage = `${ogUrl}og.jpg`

Expand Down
8 changes: 4 additions & 4 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

## Composables

`nuxt-api-party` offers two distinct composable types to return data from your APIs. All composables are [auto-imported](https://nuxt.com/docs/guide/concepts/auto-imports) and globally available inside your components:
Nuxt API Party offers two distinct composable types to return data from your APIs. All composables are [auto-imported](https://nuxt.com/docs/guide/concepts/auto-imports) and globally available inside your components:

- [`useMyApiData`](/api/use-my-api-data) – Returns multiple values similar to [`useFetch`](https://nuxt.com/docs/api/composables/use-fetch)
- [`$myApi`](/api/my-api) – Returns the response data, similar to [`$fetch`](https://nuxt.com/docs/api/utils/dollarfetch#fetch)
- [Substitute for Nuxt `useFetch`](/api/use-my-api-data) – Returns multiple values similar to [`useFetch`](https://nuxt.com/docs/api/composables/use-fetch)
- [Substitute for Nuxt `$fetch`](/api/my-api) – Returns the response data, similar to [`$fetch`](https://nuxt.com/docs/api/utils/dollarfetch#fetch)

::: info
`$myApi` and `useMyApiData` are placeholders. They are used as examples in the documentation. The actual composables are generated based on your API endpoint ID.
:::

## Dynamic Composables
## Generated Composables

The composables are generated based on your API endpoint ID. For example, if you were to call your API `jsonPlaceholder`, the generated composables are:

Expand Down
2 changes: 1 addition & 1 deletion docs/api/my-api.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `$myApi`
# Nuxt `$fetch` Substitute

::: info
`$myApi` is a placeholder used as an example in the documentation. The composable is generated based on your API endpoint ID. For example, if you were to call an endpoint `jsonPlaceholder`, the composable will be called `$jsonPlaceholder`.
Expand Down
2 changes: 1 addition & 1 deletion docs/api/use-my-api-data.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `useMyApiData`
# Nuxt `useFetch` Substitute

::: info
`useMyApiData` is a placeholder used as an example in the documentation. The composable is generated based on your API endpoint ID. For example, if you were to call an endpoint `jsonPlaceholder`, the composable will be called `useJsonPlaceholderData`.
Expand Down
2 changes: 1 addition & 1 deletion docs/config/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Module Configuration

Adapt `nuxt-api-party` to your needs with the following options in your `nuxt.config.ts` file:
Adapt Nuxt API Party to your needs with the following options in your `nuxt.config.ts` file:

```ts
// `nuxt.config.ts`
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/error-handling.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Error Handling

While the idea of this Nuxt module is to mask your real API (and credentials) within the server proxy, `nuxt-api-party` will minimize the hassle of handling errors by passing through the following properties to the response on the client:
While the idea of this Nuxt module is to mask your real API (and credentials) within the server proxy, Nuxt API Party will minimize the hassle of handling errors by passing through the following properties to the response on the client:

- Response body
- HTTP status code
Expand Down
6 changes: 3 additions & 3 deletions docs/guide/getting-started.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Getting Started

This guide will walk you through the steps to get started with `nuxt-api-party`.
This guide will walk you through the steps to get started with Nuxt API Party.

## Step 1: Install `nuxt-api-party`
## Step 1: Install Nuxt API Party

```bash
npx nuxi@latest module add api-party
```

## Step 2: Use `nuxt-api-party`
## Step 2: Use the `nuxt-api-party` module

Add `nuxt-api-party` to your Nuxt configuration:

Expand Down
84 changes: 84 additions & 0 deletions docs/guide/interceptors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Interceptors

A common question when working with APIs is how to handle the HTTP status code. This is where interceptors come in. Interceptors are functions that are called before and after the fetch request is made. They can be used to modify the request, response, or options. Nuxt uses [`ofetch`](https://github.com/unjs/ofetch) for its fetch implementation, which provides a way to [intercept requests and responses](https://github.com/unjs/ofetch?tab=readme-ov-file#%EF%B8%8F-interceptors).

**tl;dr**: To use interceptors, pass an object with the following properties to the composables:

- `onRequest({ request, options })`
- `onRequestError({ request, options, error })`
- `onResponse({ request, options, response })`
- `onResponseError({ request, options, response })`

Getting the HTTP status code is as simple as logging the response status from the `response` object in the `onResponse` interceptor.

```ts
const { data } = await useApiData('path', {
onResponse(ctx) {
const status = ctx.response.status
console.log('The status code is:', status)
},
})
```

::: tip
The examples below work with both the `$fetch` and `useFetch` composable substitutes, `$myApi` and `useMyApiData` respectively.
:::

## `onRequest({ request, options })`

`onRequest` is called as soon as the given composable is called, allowing you to modify options or log the request.

```ts
const { data } = await useApiData('path', {
async onRequest({ request, options }) {
// Log the request
console.log(request, options)

// Add a timestamp to the search parameters
options.query ||= {}
options.query.t = Date.now()
},
})
```

## `onRequestError({ request, options, error })`

`onRequestError` will be called when the fetch request fails before it is made, allowing you to log the error.

```ts
const { data } = await useApiData('path', {
async onRequestError({ request, options, error }) {
// Log the error
console.error(error)
},
})
```

## `onResponse({ request, options, response })`

`onResponse` will be called after the fetch request is made, allowing you to log the response or modify the data.

```ts
const { data } = await useApiData('path', {
async onResponse({ request, response, options }) {
// Log the response status code and body
console.log(response.status, response.body)
},
})
```

## `onResponseError({ request, options, response })`

`onResponseError` is the same as `onResponse` but will be called when the fetch request fails and `response.ok` is `false`.

```ts
const { data } = await useApiData('path', {
async onResponseError({ request, response, options }) {
// Log the response status code and body
console.error(
response.status,
response.body
)
},
})
```
8 changes: 4 additions & 4 deletions docs/guide/openapi-types.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# OpenAPI Types

If your API has an [OpenAPI](https://swagger.io/resources/open-api/) schema, `nuxt-api-party` can use it to generate types for you. These include path names, supported HTTP methods, request body, response body, query parameters, and headers.
If your API has an [OpenAPI](https://swagger.io/resources/open-api/) schema, Nuxt API Party can use it to generate types for you. These include path names, supported HTTP methods, request body, response body, query parameters, and headers.

::: info
Usage of this feature requires [`openapi-typescript`](https://www.npmjs.com/package/openapi-typescript) to generate TypeScript definitions from your OpenAPI schema file. It is installed alongside `nuxt-api-party`.
Usage of this feature requires [`openapi-typescript`](https://www.npmjs.com/package/openapi-typescript) to generate TypeScript definitions from your OpenAPI schema file. It is installed alongside Nuxt API Party.
:::

## Schema Generation
Expand Down Expand Up @@ -98,7 +98,7 @@ export default defineNuxtConfig({

## Using the Types

For most usages, no further intervention is needed. `nuxt-api-party` will use the types generated from this config to infer the correct types automatically when `$myApi` and `useMyApiData` is used.
For most usages, no further intervention is needed. Nuxt API Party will use the types generated from this config to infer the correct types automatically when `$myApi` and `useMyApiData` is used.

However, there may be a few things you may want to do now that you have type information.

Expand Down Expand Up @@ -142,7 +142,7 @@ const data = await $myApi('/foo/{id}', {
```

::: warning
Issues will **NOT** be reported at runtime by `nuxt-api-party` if the wrong parameters are used. The **incomplete** path will be sent to the backend **as-is**.
Issues will **NOT** be reported at runtime by Nuxt API Party if the wrong parameters are used. The **incomplete** path will be sent to the backend **as-is**.
:::

### Route Method Overloading
Expand Down
22 changes: 11 additions & 11 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
layout: home
titleTemplate: Connect to any API securely
titleTemplate: Server Proxy and Generated API Composables
hero:
name: nuxt-api-party
text: Connect to any API securely
tagline: Server proxy and dynamic composables
name: Nuxt API Party
text: Server Proxy and Generated API Composables
tagline: Connect to all your APIs securely
image:
src: /logo-shadow.svg
alt: nuxt-api-party
alt: Nuxt API Party Logo
actions:
- theme: brand
text: Get Started
Expand All @@ -20,24 +20,24 @@ hero:
link: https://github.com/johannschopplich/nuxt-api-party

features:
- title: Dynamic composable names
- title: Dynamic composables
icon: <span class="i-carbon:list-dropdown"></span>
details: Auto-generated composables for each API endpoint.
link: /api/
linkText: API Reference
- title: Protected API credentials
- title: Protected API Credentials
icon: <span class="i-carbon:ibm-cloud-hyper-protect-dbaas"></span>
details: A Nuxt server route proxies your requests. No CORS issues!
link: /guide/how-it-works
linkText: How It Works
- title: Familiar Data Handling
icon: <span class="i-carbon:noodle-bowl"></span>
details: Handle requests just like useFetch or $fetch.
details: Handle requests just like Nuxt `useFetch` and `$fetch`.
link: /api/use-my-api-data
linkText: useMyApiData
- title: Connect multiple APIs
linkText: Async Data Composable
- title: Connect Multiple APIs
icon: <span class="i-carbon:ibm-watson-query"></span>
details: Use multiple APIs in your app with one module.
details: Configure all your APIs once and use them throughout your app.
link: /guide/getting-started.html#step-3-set-up-api-endpoints
linkText: Set Up API Endpoints
---

0 comments on commit 4f8c21e

Please sign in to comment.