Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Nuxt client #1519

Merged
merged 34 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d3fccb0
feat: add Nuxt client
mrlubos Dec 31, 2024
258a6ea
Merge branch 'main' of https://github.com/hey-api/openapi-ts into fea…
mrlubos Jan 9, 2025
6e11752
refactor: import identifiers only when they are used
mrlubos Jan 9, 2025
56e34c6
test(ci): bump action-setup pnpm version
mrlubos Jan 9, 2025
2c83ed0
test(ci): remove pnpm version from action-setup
mrlubos Jan 9, 2025
338ff04
test(lint): ignore .output folder
mrlubos Jan 9, 2025
8059d61
test: replace typecheck script
mrlubos Jan 9, 2025
255ace1
chore: update packages
mrlubos Jan 9, 2025
d40cdb8
chore: update packages
mrlubos Jan 9, 2025
104fb2d
chore: fix nuxt client test script
mrlubos Jan 9, 2025
97e39bc
Merge branch 'main' of https://github.com/hey-api/openapi-ts into fea…
mrlubos Jan 10, 2025
8d8fbd2
chore: update engines
mrlubos Jan 10, 2025
90dd3ca
chore: fix typecheck
mrlubos Jan 10, 2025
deefb7f
Merge branch 'main' of https://github.com/hey-api/openapi-ts into fea…
mrlubos Jan 10, 2025
4216670
chore: do not use compact URL in pkg.pr.new
mrlubos Jan 11, 2025
ca013b3
chore: add auth
mrlubos Jan 12, 2025
848f34c
chore: add transformer and validator
mrlubos Jan 12, 2025
14d3c4c
chore: update link to nuxt example
mrlubos Jan 12, 2025
be2a6e1
chore: update tests
mrlubos Jan 12, 2025
7e4b6d8
chore: update types to support refs
mrlubos Jan 12, 2025
b734e8f
chore: fix watch param
mrlubos Jan 12, 2025
63ff632
Merge branch 'main' of https://github.com/hey-api/openapi-ts into fea…
mrlubos Jan 12, 2025
2efa623
chore: add computed query
mrlubos Jan 13, 2025
005ac1e
Merge branch 'main' of https://github.com/hey-api/openapi-ts into fea…
mrlubos Jan 14, 2025
afb3985
chore: improve unwrapRefs function
mrlubos Jan 15, 2025
bbbaf3e
Merge branch 'main' of https://github.com/hey-api/openapi-ts into fea…
mrlubos Jan 15, 2025
3e48b45
Merge branch 'main' of https://github.com/hey-api/openapi-ts into fea…
mrlubos Jan 15, 2025
18a7cc1
fix: unwrap into new object
mrlubos Jan 18, 2025
6c13639
chore: improve interceptors
mrlubos Jan 18, 2025
1244759
chore: add separate client snapshots file
mrlubos Jan 18, 2025
f89856c
Merge branch 'main' into feat/client-nuxt
mrlubos Jan 18, 2025
bc88e0e
chore: fix build
mrlubos Jan 18, 2025
e456677
chore: fix snapshots
mrlubos Jan 18, 2025
0fce530
chore: fix typecheck
mrlubos Jan 18, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fifty-lions-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/client-nuxt': minor
---

feat: initial release
5 changes: 5 additions & 0 deletions .changeset/tough-pans-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: add support for Nuxt client
2 changes: 1 addition & 1 deletion docs/.vitepress/config/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default defineConfig({
},
{
link: '/openapi-ts/clients/nuxt',
text: 'Nuxt <span data-soon>soon</span>',
text: 'Nuxt',
},
{
link: '/openapi-ts/clients/legacy',
Expand Down
2 changes: 1 addition & 1 deletion docs/.vitepress/theme/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const { Layout } = DefaultTheme;
background-color: var(--vp-c-bg);
border-bottom: 1px solid var(--vp-c-divider);
color: var(--vp-c-text-1);
column-gap: 1.5rem;
column-gap: 0.5rem;
display: flex;
font-size: var(--announcement-font-size);
height: var(--announcement-height);
Expand Down
2 changes: 1 addition & 1 deletion docs/openapi-ts/clients.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Hey API natively supports the following clients.
- [Fetch API](/openapi-ts/clients/fetch)
- [Axios](/openapi-ts/clients/axios)
- [Next.js](/openapi-ts/clients/next-js) <span data-soon>Soon</span>
- [Nuxt](/openapi-ts/clients/nuxt) <span data-soon>Soon</span>
- [Nuxt](/openapi-ts/clients/nuxt)
- [Legacy](/openapi-ts/clients/legacy)

Don't see your client? Let us know your interest by [opening an issue](https://github.com/hey-api/openapi-ts/issues).
Expand Down
2 changes: 1 addition & 1 deletion docs/openapi-ts/clients/next-js.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Next.js
title: Next.js client
description: Next.js client for Hey API. Compatible with all our features.
---

Expand Down
207 changes: 204 additions & 3 deletions docs/openapi-ts/clients/nuxt.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,215 @@
---
title: Nuxt
title: Nuxt client
description: Nuxt client for Hey API. Compatible with all our features.
---

# Nuxt <span data-soon>soon</span>
# Nuxt

::: warning
This feature isn't in development yet. Help us prioritize it by voting on [GitHub](https://github.com/hey-api/openapi-ts/issues/998).
Nuxt client is currently in beta. The interface might change before it becomes stable. We encourage you to leave feedback on [GitHub](https://github.com/hey-api/openapi-ts/issues).
:::

[Nuxt](https://nuxt.com/) is an open source framework that makes web development intuitive and powerful.

## Installation

Start by adding `@hey-api/client-nuxt` to your dependencies.

::: code-group

```sh [npm]
npm install @hey-api/client-nuxt
```

```sh [pnpm]
pnpm add @hey-api/client-nuxt
```

```sh [yarn]
yarn add @hey-api/client-nuxt
```

```sh [bun]
bun add @hey-api/client-nuxt
```

:::

In your [configuration](/openapi-ts/get-started), set `client` to `@hey-api/client-nuxt` and you'll be ready to use the Nuxt client. :tada:

::: code-group

```js [config]
export default {
client: '@hey-api/client-nuxt', // [!code ++]
input: 'path/to/openapi.json',
output: 'src/client',
};
```

```sh [cli]
npx @hey-api/openapi-ts \
-c @hey-api/client-nuxt \ # [!code ++]
-i path/to/openapi.json \
-o src/client
```

:::

## Configuration

If you're using SDKs, you will want to configure the internal client instance. You can do that with the `setConfig()` method. Call it at the beginning of your application.

```js
import { client } from 'client/sdk.gen';

client.setConfig({
baseURL: 'https://example.com',
});
```

If you aren't using SDKs, you can create your own client instance.

```js
import { createClient } from '@hey-api/client-nuxt';

const client = createClient({
baseURL: 'https://example.com',
});
```

## Interceptors

Interceptors (middleware) can be used to modify requests before they're sent or responses before they're returned to your application. Nuxt provides interceptors through ofetch, please refer to their documentation on [$fetch](https://nuxt.com/docs/api/utils/dollarfetch).

You can pass any Nuxt/ofetch arguments to the client instance.

```js
import { client } from 'client/sdk.gen';

const result = await client.get({
composable: '$fetch',
onRequest: (context) => {
// do something
},
url: '/foo',
});
```

## Customization

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, customizing your client will feel like working directly with Nuxt. You can customize requests in three ways – through SDKs, per client, or per request.

### SDKs

This is the most common requirement. The generated SDKs consume an internal client instance, so you will want to configure that.

```js
import { client } from 'client/sdk.gen';

client.setConfig({
baseURL: 'https://example.com',
});
```

You can pass any Nuxt configuration option to `setConfig()`, and even your own `$fetch` implementation.

### Client

If you need to create a client pointing to a different domain, you can create your own client instance.

```js
import { createClient } from '@hey-api/client-nuxt';

const myClient = createClient({
baseURL: 'https://example.com',
});
```

You can then pass this instance to any SDK function through the `client` option. This will override the internal instance.

```js
const response = await getFoo({
client: myClient,
});
```

### Request

Alternatively, you can pass the Nuxt configuration options to each SDK function. This is useful if you don't want to create a client instance for one-off use cases.

```js
const response = await getFoo({
baseURL: 'https://example.com', // <-- override internal configuration
});
```

## Auth

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.

```js
import { client } from 'client/sdk.gen';

client.setConfig({
auth: () => '<my_token>', // [!code ++]
baseURL: 'https://example.com',
});
```

If you're not using SDKs or generating auth, using interceptors is a common approach to configuring auth for each request.

```js
import { client } from 'client/sdk.gen';

client.setConfig({
onRequest: ({ options }) => {
options.headers.set('Authorization', 'Bearer <my_token>'); // [!code ++]
},
});
```

## Build URL

If you need to access the compiled URL, you can use the `buildUrl()` method. It's loosely typed by default to accept almost any value; in practice, you will want to pass a type hint.

```ts
type FooData = {
path: {
fooId: number;
};
query?: {
bar?: string;
};
url: '/foo/{fooId}';
};

const url = client.buildUrl<FooData>({
path: {
fooId: 1,
},
query: {
bar: 'baz',
},
url: '/foo/{fooId}',
});
console.log(url); // prints '/foo/1?bar=baz'
```

## Bundling

Sometimes, you may not want to declare client packages as a dependency. This scenario is common if you're using Hey API to generate output that is repackaged and published for other consumers under your own brand. For such cases, our clients support bundling through the `client.bundle` configuration option.

```js
export default {
client: {
bundle: true, // [!code ++]
name: '@hey-api/client-nuxt',
},
input: 'path/to/openapi.json',
output: 'src/client',
};
```

<!--@include: ../../examples.md-->
<!--@include: ../../sponsors.md-->
3 changes: 2 additions & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"devDependencies": {
"sharp": "0.33.5",
"vitepress": "1.3.0"
"vitepress": "1.3.0",
"vue": "3.5.13"
}
}
24 changes: 24 additions & 0 deletions examples/openapi-ts-nuxt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Nuxt dev/build outputs
.output
.data
.nuxt
.nitro
.cache
dist

# Node dependencies
node_modules

# Logs
logs
*.log

# Misc
.DS_Store
.fleet
.idea

# Local env files
.env
.env.*
!.env.example
2 changes: 2 additions & 0 deletions examples/openapi-ts-nuxt/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
engine-strict=true
save-exact=true
75 changes: 75 additions & 0 deletions examples/openapi-ts-nuxt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Nuxt Minimal Starter

Look at the [Nuxt documentation](https://nuxt.com/docs/getting-started/introduction) to learn more.

## Setup

Make sure to install dependencies:

```bash
# npm
npm install

# pnpm
pnpm install

# yarn
yarn install

# bun
bun install
```

## Development Server

Start the development server on `http://localhost:3000`:

```bash
# npm
npm run dev

# pnpm
pnpm dev

# yarn
yarn dev

# bun
bun run dev
```

## Production

Build the application for production:

```bash
# npm
npm run build

# pnpm
pnpm build

# yarn
yarn build

# bun
bun run build
```

Locally preview production build:

```bash
# npm
npm run preview

# pnpm
pnpm preview

# yarn
yarn preview

# bun
bun run preview
```

Check out the [deployment documentation](https://nuxt.com/docs/getting-started/deployment) for more information.
Loading
Loading