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
description: Generate a type-safe Nuxt v3 client from OpenAPI with the Nuxt client for openapi-ts. Fully compatible with validators, transformers, and all core features.
2
+
title: Nuxt Client
3
+
description: Generate a type-safe Nuxt client from OpenAPI with the Nuxt client for openapi-ts. Fully compatible with validators, transformers, and all core features.
4
4
---
5
5
6
6
<scriptsetuplang="ts">
@@ -9,8 +9,7 @@ import VersionLabel from '@components/VersionLabel.vue';
9
9
</script>
10
10
11
11
<Heading>
12
-
<h1>Nuxt<spanclass="sr-only"> v3</span></h1>
13
-
<VersionLabelvalue="v3" />
12
+
<h1>Nuxt</h1>
14
13
</Heading>
15
14
16
15
::: warning
@@ -25,7 +24,6 @@ The Nuxt client for Hey API generates a type-safe client from your OpenAPI spec,
25
24
26
25
## Features
27
26
28
-
- Nuxt v3 support
29
27
- seamless integration with `@hey-api/openapi-ts` ecosystem
30
28
- type-safe response data and errors
31
29
- response data validation and transformation
@@ -36,7 +34,33 @@ The Nuxt client for Hey API generates a type-safe client from your OpenAPI spec,
36
34
37
35
## Installation
38
36
39
-
Start by adding `@hey-api/nuxt` to your dependencies.
37
+
### Automatic installation
38
+
39
+
Start by installing the `@hey-api/nuxt` Nuxt module.
40
+
41
+
::: code-group
42
+
43
+
```sh [npm]
44
+
npx nuxi module add @hey-api/nuxt
45
+
```
46
+
47
+
```sh [pnpm]
48
+
pnpx nuxi module add @hey-api/nuxt
49
+
```
50
+
51
+
```sh [yarn]
52
+
yarn dlx nuxi module @hey-api/nuxt
53
+
```
54
+
55
+
```sh [bun]
56
+
bunx nuxi module add @hey-api/nuxt
57
+
```
58
+
59
+
:::
60
+
61
+
### Manual installation
62
+
63
+
Add `@hey-api/nuxt` to your dependencies.
40
64
41
65
::: code-group
42
66
@@ -58,76 +82,135 @@ bun add @hey-api/nuxt
58
82
59
83
:::
60
84
61
-
In your [configuration](/openapi-ts/get-started), add `@hey-api/client-nuxt` to your plugins and you'll be ready to generate client artifacts. :tada:
62
-
63
-
::: code-group
85
+
Then, add it to the `modules` in your `nuxt.config.ts`:
64
86
65
-
```js [config]
66
-
exportdefault {
67
-
input:'hey-api/backend', // sign up at app.heyapi.dev
68
-
output:'src/client',
69
-
plugins: ['@hey-api/client-nuxt'], // [!code ++]
70
-
};
87
+
```ts
88
+
exportdefaultdefineNuxtConfig({
89
+
modules: [
90
+
'@hey-api/nuxt', // [!code ++]
91
+
],
92
+
});
71
93
```
72
94
73
-
```sh [cli]
74
-
npx @hey-api/openapi-ts \
75
-
-i hey-api/backend \
76
-
-o src/client \
77
-
-c @hey-api/client-nuxt # [!code ++]
95
+
## Getting started
96
+
97
+
Set an [input](/openapi-ts/configuration/input) within `nuxt.config.ts`, then start the Nuxt dev server.
98
+
99
+
```ts
100
+
exportdefaultdefineNuxtConfig({
101
+
heyApi: {
102
+
config: {
103
+
input: './path/to/openapi.json', // [!code ++]
104
+
},
105
+
},
106
+
});
78
107
```
79
108
80
-
:::
109
+
The generated client can be accessed from `#hey-api/`.
110
+
111
+
```ts
112
+
import { client } from'#hey-api/client.gen';
113
+
```
81
114
82
115
::: tip
83
116
84
-
If you add `@hey-api/nuxt`to your Nuxt modules, this step is not needed.
117
+
The `@hey-api/client-nuxt`plugin is automatically added.
85
118
86
119
:::
87
120
121
+
### Options
122
+
123
+
### `alias`
124
+
125
+
Configure an [alias](https://nuxt.com/docs/api/nuxt-config#alias) to access the Hey API client.
126
+
127
+
Defaults to `#hey-api`
128
+
129
+
### `autoImports`
130
+
131
+
Adds the generated SDK items to auto imports. Defaults to `true`.
132
+
133
+
#### `config`
134
+
135
+
Configuration to pass to `@hey-api/openapi-ts`.
136
+
137
+
-[input](/openapi-ts/configuration/input)
138
+
-[output](/openapi-ts/configuration/output)
139
+
- Defaults to `.nuxt/client`
140
+
-[parser](/openapi-ts/configuration/parser)
141
+
-[plugins](/openapi-ts/transformers)
142
+
88
143
## Configuration
89
144
90
-
The Nuxt client is built as a thin wrapper on top of Nuxt, extending its functionality to work with Hey API. If you're already familiar with Nuxt, configuring your client will feel like working directly with Nuxt.
145
+
When we configured the Nuxt module above, it created a [`client.gen.ts`](/openapi-ts/output#client) file. You will most likely want to configure the exported `client` instance. There are two ways to do that.
91
146
92
-
When we installed the client above, it created a [`client.gen.ts`](/openapi-ts/output#client) file. You will most likely want to configure the exported `client` instance. There are two ways to do that.
147
+
The Nuxt client is built as a thin wrapper on top of Nuxt, extending its functionality to work with Hey API. If you're already familiar with Nuxt, configuring your client will feel like working directly with Nuxt.
93
148
94
149
### `setConfig()`
95
150
96
151
This is the simpler approach. You can call the `setConfig()` method at the beginning of your application or anytime you need to update the client configuration. You can pass any Nuxt configuration option to `setConfig()`, and even your own [`$fetch`](#custom-fetch) implementation.
97
152
98
-
```js
99
-
import { client } from'client/client.gen';
153
+
::: code-group
100
154
101
-
client.setConfig({
102
-
baseURL:'https://example.com',
155
+
```vue [app.vue]
156
+
<script setup lang="ts">
157
+
import { client } from '#hey-api/client.gen';
158
+
159
+
await callOnce(async () => {
160
+
client.setConfig({
161
+
baseURL: 'https://example.com',
162
+
});
103
163
});
164
+
</script>
104
165
```
105
166
167
+
:::
168
+
106
169
The disadvantage of this approach is that your code may call the `client` instance before it's configured for the first time. Depending on your use case, you might need to use the second approach.
107
170
108
171
### Runtime API
109
172
110
173
Since `client.gen.ts` is a generated file, we can't directly modify it. Instead, we can tell our configuration to use a custom file implementing the Runtime API. We do that by specifying the `runtimeConfigPath` option.
111
174
112
-
```js
175
+
::: code-group
176
+
177
+
```ts [nuxt]
178
+
exportdefaultdefineNuxtConfig({
179
+
heyApi: {
180
+
config: {
181
+
input: 'hey-api/backend', // sign up at app.heyapi.dev
@@ -144,7 +227,7 @@ With this approach, `client.gen.ts` will call `createClientConfig()` before init
144
227
You can also create your own client instance. You can use it to manually send requests or point it to a different domain.
145
228
146
229
```js
147
-
import { createClient } from'./client/client';
230
+
import { createClient } from'#hey-api/client';
148
231
149
232
constmyClient=createClient({
150
233
baseURL:'https://example.com',
@@ -180,7 +263,7 @@ If you omit `composable`, `$fetch` is used by default.
180
263
:::
181
264
182
265
```js
183
-
import { client } from'client/client.gen';
266
+
import { client } from'#hey-api/client.gen';
184
267
185
268
constresult=awaitclient.get({
186
269
composable:'$fetch',
@@ -196,7 +279,7 @@ const result = await client.get({
196
279
The SDKs include auth mechanisms for every endpoint. You will want to configure the `auth` field to pass the right token for each request. The `auth` field can be a string or a function returning a string representing the token. The returned value will be attached only to requests that require auth.
197
280
198
281
```js
199
-
import { client } from'client/client.gen';
282
+
import { client } from'#hey-api/client.gen';
200
283
201
284
client.setConfig({
202
285
auth: () =>'<my_token>', // [!code ++]
@@ -207,7 +290,7 @@ client.setConfig({
207
290
If you're not using SDKs or generating auth, using interceptors is a common approach to configuring auth for each request.
208
291
209
292
```js
210
-
import { client } from'client/client.gen';
293
+
import { client } from'#hey-api/client.gen';
211
294
212
295
client.setConfig({
213
296
onRequest: ({ options }) => {
@@ -248,7 +331,7 @@ console.log(url); // prints '/foo/1?bar=baz'
248
331
You can implement your own `$fetch` method. This is useful if you need to extend the default `$fetch` method with extra functionality, or replace it altogether.
249
332
250
333
```js
251
-
import { client } from'client/client.gen';
334
+
import { client } from'#hey-api/client.gen';
252
335
253
336
client.setConfig({
254
337
$fetch: () => {
@@ -259,6 +342,31 @@ client.setConfig({
259
342
260
343
You can use any of the approaches mentioned in [Configuration](#configuration), depending on how granular you want your custom method to be.
261
344
345
+
## Standalone usage
346
+
347
+
You can generate the Hey API Nuxt client via the CLI instead of the Nuxt module.
348
+
349
+
In your [configuration](/openapi-ts/get-started), add `@hey-api/client-nuxt` to your plugins and you'll be ready to generate client artifacts. :tada:
350
+
351
+
::: code-group
352
+
353
+
```js [config]
354
+
exportdefault {
355
+
input:'hey-api/backend', // sign up at app.heyapi.dev
356
+
output:'src/client',
357
+
plugins: ['@hey-api/client-nuxt'], // [!code ++]
358
+
};
359
+
```
360
+
361
+
```sh [cli]
362
+
npx @hey-api/openapi-ts \
363
+
-i hey-api/backend \
364
+
-o src/client \
365
+
-c @hey-api/client-nuxt # [!code ++]
366
+
```
367
+
368
+
:::
369
+
262
370
## API
263
371
264
372
You can view the complete list of options in the [UserConfig](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/plugins/@hey-api/client-nuxt/types.d.ts) interface.
0 commit comments