-
-
Notifications
You must be signed in to change notification settings - Fork 139
docs: OpenAPI to Contract #1520
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
Merged
+101
−74
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c68fa0f
chore: sync sponsors
github-actions[bot] 6068d87
chore: sync sponsors
github-actions[bot] 6fc9ee4
Merge branch 'middleapi:main' into main
dinwwwh 40b9562
Merge branch 'middleapi:main' into main
dinwwwh 00b00c4
Merge branch 'middleapi:main' into main
dinwwwh 4459702
Merge branch 'middleapi:main' into main
dinwwwh bdbde55
docs: OpenAPI to Contract
dinwwwh a2bd77c
Apply suggestion from @gemini-code-assist[bot]
dinwwwh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| --- | ||
| title: Hey API Integration | ||
| description: Generate oRPC contracts from OpenAPI with Hey API or adapt a Hey API generated client into an oRPC client. | ||
| --- | ||
|
|
||
| # Hey API Integration | ||
|
|
||
| [Hey API](https://heyapi.dev/) can be integrated with oRPC in two ways, depending on what you start with: | ||
|
|
||
| - Generate an oRPC contract from an existing OpenAPI specification. | ||
| - Convert an existing Hey API generated client directly into an oRPC client. | ||
|
|
||
| ::: warning | ||
| 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. | ||
| ::: | ||
|
|
||
| ## Convert OpenAPI to an oRPC Contract | ||
|
|
||
| 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. | ||
|
|
||
| Once generated, you can use the contract to: | ||
|
|
||
| - Implement the contract on your own server with [Implement Contract](/docs/contract-first/implement-contract). | ||
| - Create a type-safe client with [OpenAPILink](/docs/openapi/client/openapi-link). | ||
| - Use the generated contract as a reference alongside [Define Contract](/docs/contract-first/define-contract) to better understand its structure. | ||
|
|
||
| ## Convert a Hey API Client Directly to an oRPC Client | ||
|
|
||
| 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`. | ||
dinwwwh marked this conversation as resolved.
Show resolved
Hide resolved
dinwwwh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```ts | ||
| import { experimental_toORPCClient } from '@orpc/hey-api' | ||
| import * as sdk from 'src/client/sdk.gen' | ||
|
|
||
| export const client = experimental_toORPCClient(sdk) | ||
|
|
||
| const { body } = await client.listPlanets() | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| ### Error Handling | ||
|
|
||
| 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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| --- | ||
| title: OpenAPI to Contract | ||
| description: Generate an oRPC contract from an existing OpenAPI specification with the Hey API oRPC plugin. | ||
| --- | ||
|
|
||
| # OpenAPI to Contract | ||
|
|
||
| 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. | ||
|
|
||
| ::: warning | ||
| The Hey API `orpc` plugin is currently beta and may introduce breaking changes while the integration stabilizes. | ||
| ::: | ||
|
|
||
| ## Example | ||
|
|
||
| ```sh | ||
| npm install -D @hey-api/openapi-ts | ||
| ``` | ||
|
|
||
| ```ts [openapi-ts.config.ts] | ||
| import { defineConfig } from '@hey-api/openapi-ts' | ||
|
|
||
| export default defineConfig({ | ||
| input: 'https://get.heyapi.dev/hey-api/backend', | ||
| output: 'src/client', | ||
| plugins: [ | ||
| { | ||
| name: 'orpc', | ||
| validator: { | ||
| input: 'zod', | ||
| }, | ||
| }, | ||
| ], | ||
| }) | ||
| ``` | ||
|
|
||
| Then run: | ||
|
|
||
| ```sh | ||
| npx @hey-api/openapi-ts | ||
| ``` | ||
|
|
||
| This generates an oRPC-compatible contract from your OpenAPI specification. In this example, `zod` is used for generated input validation. | ||
|
|
||
| For more details about configuration options and plugin behavior, see the [Hey API oRPC plugin documentation](https://heyapi.dev/openapi-ts/plugins/orpc). | ||
|
|
||
| ## What To Do Next | ||
|
|
||
| Once the contract is generated, what you do next depends on how you want to use it: | ||
|
|
||
| - Implement the contract on your own server with [Implement Contract](/docs/contract-first/implement-contract). | ||
| - Create a type-safe client with [OpenAPILink](/docs/openapi/client/openapi-link). | ||
| - Use the generated contract as a reference alongside [Define Contract](/docs/contract-first/define-contract) to better understand its structure. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.