You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add runtime hooks for API request and response handling (#89)
* feat: add runtime hooks for API request and response handling
* fix: tests
* fix: update server imports to use nitropack/runtime
* feat: remove hooks from server $api function
* feat: enhance hooks with dynamic endpoints
* docs: update hooks documentation
* chore: prefer `@ts-expect-error` over `any`
* refactor: use ofetch's `FetchResponse` over plain Response
* docs: overhaul hooks docs
---------
Co-authored-by: Johann Schopplich <[email protected]>
Nuxt API Party provides a number of hooks that can be used to customize the module's behavior. Hooks are functions that are called at specific points in the module's lifecycle. You can use hooks to modify the module's configuration.
3
+
Nuxt API Party provides a set of powerful hooks that allow you to customize the behavior of the module at various stages. The hook system supports both Nuxt and Nitro environments with fully typed, merged hooks that ensure both generic and endpoint-specific handlers are executed in the correct order.
4
4
5
5
For more information on how to work with hooks, see the [Nuxt documentation](https://nuxt.com/docs/guide/going-further/hooks).
6
6
7
7
## Available Hooks
8
8
9
-
| Hook Name | Arguments | Description |
10
-
| ---------- | --------- | ----------- |
11
-
|`api-party:extend`|`options`| Called during module initialization after the options have been resolved. Can be used to modify the endpoint configuration. |
|`api-party:extend`|`options`| Called during module initialization after options are resolved. Allows modifying endpoint configuration. |
12
+
|`api-party:request`|`ctx, [event]`| Called before each API request. This generic hook runs on both Nuxt and Nitro platforms. |
13
+
|`api-party:request:${endpointId}`|`ctx, [event]`| Called specifically for the designated endpoint. Merged with the generic request hook. |
14
+
|`api-party:response`|`ctx, [event]`| Called after each API response. This generic hook is used to handle response modifications on both platforms. |
15
+
|`api-party:response:${endpointId}`|`ctx, [event]`| Called for the specific endpoint response. Merged with the generic response hook. |
12
16
13
-
## Usage
17
+
::: info Merging Hooks
18
+
Both generic and endpoint-specific hooks are merged so that:
14
19
15
-
To use hooks, define them in the `hooks` property of your `nuxt.config.ts` file. The following example demonstrates how to use the `api-party:extend` hook:
20
+
- For requests: The generic `api-party:request` hook executes first, followed by `api-party:request:${endpointId}`.
21
+
- For responses: The endpoint-specific `api-party:response:${endpointId}` hook executes first, followed by `api-party:response`.
22
+
:::
16
23
17
-
```ts
18
-
// `nuxt.config.ts`
24
+
## Nuxt Runtime Hooks
25
+
26
+
Register Nuxt runtime hooks either in your `nuxt.config.ts` file, in a client plugin or at runtime. These hooks are useful for extending [API endpoints](/config/#apiparty-endpoints) with additional configuration or for intercepting API calls for tasks like logging, metrics, or dynamically adding headers.
27
+
28
+
The only hook called at module initialization is `api-party:extend`. This hook is useful for modifying endpoint configuration before the module is fully initialized. For example, you can log the resolved server endpoints:
All other hooks are called at runtime, either on the client side (or the server side on SSR requests). For example, you can add headers to all requests using the `api-party:request` hook:
0 commit comments