|
| 1 | +--- |
| 2 | +title: oRPC v1 Plugin |
| 3 | +description: Generate oRPC v1 contracts from OpenAPI with the oRPC plugin for openapi-ts. Fully compatible with validators, transformers, and all core features. |
| 4 | +--- |
| 5 | + |
| 6 | +<script setup lang="ts"> |
| 7 | +import AuthorsList from '@components/AuthorsList.vue'; |
| 8 | +import Heading from '@components/Heading.vue'; |
| 9 | +import { stephenZhou } from '@data/people.js'; |
| 10 | +import VersionLabel from '@components/VersionLabel.vue'; |
| 11 | +</script> |
| 12 | + |
| 13 | +<Heading> |
| 14 | + <h1>oRPC<span class="sr-only"> v1</span></h1> |
| 15 | + <VersionLabel value="v1" /> |
| 16 | +</Heading> |
| 17 | + |
| 18 | +::: warning |
| 19 | +oRPC plugin 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). |
| 20 | +::: |
| 21 | + |
| 22 | +### About |
| 23 | + |
| 24 | +[oRPC](https://orpc.dev) combines RPC with OpenAPI, allowing you to define and call remote or local procedures through a type-safe API while adhering to the OpenAPI specification. |
| 25 | + |
| 26 | +The oRPC plugin for Hey API generates contracts from your OpenAPI spec, fully compatible with all core features. |
| 27 | + |
| 28 | +### Collaborators |
| 29 | + |
| 30 | +<AuthorsList :people="[stephenZhou]" /> |
| 31 | + |
| 32 | +## Features |
| 33 | + |
| 34 | +- oRPC v1 support |
| 35 | +- seamless integration with `@hey-api/openapi-ts` ecosystem |
| 36 | +- generated contracts |
| 37 | +- minimal learning curve thanks to extending the underlying technology |
| 38 | + |
| 39 | +## Installation |
| 40 | + |
| 41 | +In your [configuration](/openapi-ts/get-started), add `orpc` to your plugins and you'll be ready to generate oRPC artifacts. :tada: |
| 42 | + |
| 43 | +```js |
| 44 | +export default { |
| 45 | + input: 'hey-api/backend', // sign up at app.heyapi.dev |
| 46 | + output: 'src/client', |
| 47 | + plugins: [ |
| 48 | + // ...other plugins |
| 49 | + 'orpc', // [!code ++] |
| 50 | + ], |
| 51 | +}; |
| 52 | +``` |
| 53 | + |
| 54 | +## Output |
| 55 | + |
| 56 | +The oRPC plugin will generate the following artifacts, depending on the input specification. |
| 57 | + |
| 58 | +## Contracts |
| 59 | + |
| 60 | +Contracts are generated from all endpoints. |
| 61 | + |
| 62 | +::: code-group |
| 63 | + |
| 64 | +```js [example] |
| 65 | +import { oc } from '@orpc/contract'; |
| 66 | + |
| 67 | +const addPet = oc.route({ |
| 68 | + description: 'Add a new pet to the store.', |
| 69 | + inputStructure: 'detailed', |
| 70 | + method: 'POST', |
| 71 | + operationId: 'addPet', |
| 72 | + path: '/pet', |
| 73 | + summary: 'Add a new pet to the store.', |
| 74 | + tags: ['pet'], |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +```js [config] |
| 79 | +export default { |
| 80 | + input: 'hey-api/backend', // sign up at app.heyapi.dev |
| 81 | + output: 'src/client', |
| 82 | + plugins: [ |
| 83 | + // ...other plugins |
| 84 | + 'orpc', |
| 85 | + ], |
| 86 | +}; |
| 87 | +``` |
| 88 | + |
| 89 | +::: |
| 90 | + |
| 91 | +### Validators |
| 92 | + |
| 93 | +To enable schema validation, set `validator` to `zod` or one of the available [validator plugins](/openapi-ts/validators). This will implicitly add the selected plugin with default values. |
| 94 | + |
| 95 | +For a more granular approach, manually add a validator plugin and set `validator` to the plugin name or `true` to automatically select a compatible plugin. Until you customize the validator plugin, both approaches will produce the same default output. |
| 96 | + |
| 97 | +::: code-group |
| 98 | + |
| 99 | +```js [example] |
| 100 | +import { oc } from '@orpc/contract'; |
| 101 | + |
| 102 | +import { vAddPetData, vAddPetResponse } from './valibot.gen'; |
| 103 | + |
| 104 | +const addPet = oc |
| 105 | + .route({ |
| 106 | + description: 'Add a new pet to the store.', |
| 107 | + inputStructure: 'detailed', |
| 108 | + method: 'POST', |
| 109 | + operationId: 'addPet', |
| 110 | + path: '/pet', |
| 111 | + summary: 'Add a new pet to the store.', |
| 112 | + tags: ['pet'], |
| 113 | + }) |
| 114 | + .input(vAddPetData) // [!code ++] |
| 115 | + .output(vAddPetResponse); // [!code ++] |
| 116 | +``` |
| 117 | + |
| 118 | +```js [config] |
| 119 | +export default { |
| 120 | + input: 'hey-api/backend', // sign up at app.heyapi.dev |
| 121 | + output: 'src/client', |
| 122 | + plugins: [ |
| 123 | + // ...other plugins |
| 124 | + { |
| 125 | + name: 'orpc', |
| 126 | + validator: true, // or 'valibot' // [!code ++] |
| 127 | + }, |
| 128 | + { |
| 129 | + name: 'valibot', // customize (optional) // [!code ++] |
| 130 | + // other options |
| 131 | + }, |
| 132 | + ], |
| 133 | +}; |
| 134 | +``` |
| 135 | + |
| 136 | +::: |
| 137 | + |
| 138 | +You can choose to validate only inputs or outputs. |
| 139 | + |
| 140 | +::: code-group |
| 141 | + |
| 142 | +```js [inputs] |
| 143 | +export default { |
| 144 | + input: 'hey-api/backend', // sign up at app.heyapi.dev |
| 145 | + output: 'src/client', |
| 146 | + plugins: [ |
| 147 | + // ...other plugins |
| 148 | + { |
| 149 | + name: 'orpc', |
| 150 | + validator: { |
| 151 | + input: 'zod', // [!code ++] |
| 152 | + }, |
| 153 | + }, |
| 154 | + ], |
| 155 | +}; |
| 156 | +``` |
| 157 | + |
| 158 | +```js [outputs] |
| 159 | +export default { |
| 160 | + input: 'hey-api/backend', // sign up at app.heyapi.dev |
| 161 | + output: 'src/client', |
| 162 | + plugins: [ |
| 163 | + // ...other plugins |
| 164 | + { |
| 165 | + name: 'orpc', |
| 166 | + validator: { |
| 167 | + output: 'zod', // [!code ++] |
| 168 | + }, |
| 169 | + }, |
| 170 | + ], |
| 171 | +}; |
| 172 | +``` |
| 173 | + |
| 174 | +::: |
| 175 | + |
| 176 | +Learn more about available validators on the [Validators](/openapi-ts/validators) page. |
| 177 | + |
| 178 | +## API |
| 179 | + |
| 180 | +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/orpc/types.ts) interface. |
| 181 | + |
| 182 | +<!--@include: ../../partials/examples.md--> |
| 183 | +<!--@include: ../../partials/sponsors.md--> |
0 commit comments