Skip to content

Commit dd914a8

Browse files
dinwwwhgithub-actions[bot]gemini-code-assist[bot]
authored
docs: OpenAPI to Contract (#1520)
Fixes: #1061 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Documentation** * Added an "OpenAPI to Contract" guide for generating oRPC contracts from OpenAPI specs. * Expanded Hey API integration docs with two integration workflows and TypeScript examples for client generation and conversion. * Reorganized OpenAPI documentation and navigation for improved discoverability, moving Hey API content into the OpenAPI integrations section. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 30668ea commit dd914a8

File tree

4 files changed

+101
-74
lines changed

4 files changed

+101
-74
lines changed

apps/content/.vitepress/config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export default withMermaid(defineConfig({
107107
{ text: 'Define Contract', link: '/docs/contract-first/define-contract' },
108108
{ text: 'Implement Contract', link: '/docs/contract-first/implement-contract' },
109109
{ text: 'Router to Contract', link: '/docs/contract-first/router-to-contract' },
110+
{ text: 'OpenAPI to Contract', link: '/docs/openapi/openapi-to-contract' },
110111
],
111112
},
112113
{
@@ -190,7 +191,7 @@ export default withMermaid(defineConfig({
190191
{ text: 'AI SDK', link: '/docs/integrations/ai-sdk' },
191192
{ text: 'Better Auth', link: '/docs/integrations/better-auth' },
192193
{ text: 'Durable Iterator', link: '/docs/integrations/durable-iterator' },
193-
{ text: 'Hey API', link: '/docs/integrations/hey-api' },
194+
{ text: 'Hey API', link: '/docs/openapi/integrations/hey-api' },
194195
{ text: 'OpenTelemetry', link: '/docs/integrations/opentelemetry' },
195196
{ text: 'Pinia Colada', link: '/docs/integrations/pinia-colada' },
196197
{ text: 'Pino', link: '/docs/integrations/pino' },
@@ -261,6 +262,7 @@ export default withMermaid(defineConfig({
261262
{ text: 'OpenAPI Handler', link: '/docs/openapi/openapi-handler' },
262263
{ text: 'OpenAPI Specification', link: '/docs/openapi/openapi-specification' },
263264
{ text: 'Scalar (Swagger)', link: '/docs/openapi/scalar' },
265+
{ text: 'OpenAPI to Contract', link: '/docs/openapi/openapi-to-contract' },
264266
{
265267
text: 'Plugins',
266268
collapsed: true,
@@ -281,6 +283,7 @@ export default withMermaid(defineConfig({
281283
text: 'Integrations',
282284
collapsed: true,
283285
items: [
286+
{ text: 'Hey API', link: '/docs/openapi/integrations/hey-api' },
284287
{ text: 'Implement Contract in NestJS', link: '/docs/openapi/integrations/implement-contract-in-nest' },
285288
{ text: 'tRPC', link: '/docs/openapi/integrations/trpc' },
286289
],

apps/content/docs/integrations/hey-api.md

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: Hey API Integration
3+
description: Generate oRPC contracts from OpenAPI with Hey API or adapt a Hey API generated client into an oRPC client.
4+
---
5+
6+
# Hey API Integration
7+
8+
[Hey API](https://heyapi.dev/) can be integrated with oRPC in two ways, depending on what you start with:
9+
10+
- Generate an oRPC contract from an existing OpenAPI specification.
11+
- Convert an existing Hey API generated client directly into an oRPC client.
12+
13+
::: warning
14+
The [Hey API](https://heyapi.dev/) integration is still unstable. As Hey API continues to evolve, this integration may introduce breaking changes in the future.
15+
:::
16+
17+
## Convert OpenAPI to an oRPC Contract
18+
19+
If you already have an OpenAPI specification, you can use Hey API to generate an oRPC contract. See [OpenAPI to Contract](/docs/openapi/openapi-to-contract) for the complete setup and next steps.
20+
21+
Once generated, you can use the contract to:
22+
23+
- Implement the contract on your own server with [Implement Contract](/docs/contract-first/implement-contract).
24+
- Create a type-safe client with [OpenAPILink](/docs/openapi/client/openapi-link).
25+
- Use the generated contract as a reference alongside [Define Contract](/docs/contract-first/define-contract) to better understand its structure.
26+
27+
## Convert a Hey API Client Directly to an oRPC Client
28+
29+
If you already have a generated [Hey API client](https://heyapi.dev/openapi-ts/output) and want to use it as an oRPC client without generating a contract first, use `toORPCClient`.
30+
31+
```ts
32+
import { experimental_toORPCClient } from '@orpc/hey-api'
33+
import * as sdk from 'src/client/sdk.gen'
34+
35+
export const client = experimental_toORPCClient(sdk)
36+
37+
const { body } = await client.listPlanets()
38+
```
39+
40+
This `client` behaves like any standard oRPC [server-side client](/docs/client/server-side) or [client-side client](/docs/client/client-side), so you can use it with any oRPC-compatible library.
41+
42+
### Error Handling
43+
44+
Internally, oRPC passes the `throwOnError` option to the Hey API client. If the original Hey API client throws an error, oRPC forwards it as is, ensuring consistent error handling.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: OpenAPI to Contract
3+
description: Generate an oRPC contract from an existing OpenAPI specification with the Hey API oRPC plugin.
4+
---
5+
6+
# OpenAPI to Contract
7+
8+
If you already have an [OpenAPI Specification](https://swagger.io/specification/), you can generate an oRPC contract with [Hey API](https://heyapi.dev/)'s `orpc` plugin instead of defining the contract manually.
9+
10+
::: warning
11+
The Hey API `orpc` plugin is currently beta and may introduce breaking changes while the integration stabilizes.
12+
:::
13+
14+
## Example
15+
16+
```sh
17+
npm install -D @hey-api/openapi-ts
18+
```
19+
20+
```ts [openapi-ts.config.ts]
21+
import { defineConfig } from '@hey-api/openapi-ts'
22+
23+
export default defineConfig({
24+
input: 'https://get.heyapi.dev/hey-api/backend',
25+
output: 'src/client',
26+
plugins: [
27+
{
28+
name: 'orpc',
29+
validator: {
30+
input: 'zod',
31+
},
32+
},
33+
],
34+
})
35+
```
36+
37+
Then run:
38+
39+
```sh
40+
npx @hey-api/openapi-ts
41+
```
42+
43+
This generates an oRPC-compatible contract from your OpenAPI specification. In this example, `zod` is used for generated input validation.
44+
45+
For more details about configuration options and plugin behavior, see the [Hey API oRPC plugin documentation](https://heyapi.dev/openapi-ts/plugins/orpc).
46+
47+
## What To Do Next
48+
49+
Once the contract is generated, what you do next depends on how you want to use it:
50+
51+
- Implement the contract on your own server with [Implement Contract](/docs/contract-first/implement-contract).
52+
- Create a type-safe client with [OpenAPILink](/docs/openapi/client/openapi-link).
53+
- Use the generated contract as a reference alongside [Define Contract](/docs/contract-first/define-contract) to better understand its structure.

0 commit comments

Comments
 (0)