-
I started exploring this lib yesterday. Then I wanted to expose the server API with The docs (https://orpc.unnoq.com/docs/openapi/getting-started#defining-routes) explain to add // in backend server
export const listPlanet = os
.route({ method: 'GET', path: '/planets' }) // ✅ this is new code
.input(z.object({
limit: z.number().int().min(1).max(100).optional(),
cursor: z.number().int().min(0).default(0),
}))
.output(z.array(PlanetSchema))
.handler(async ({ input }) => {
// your list code here
return [{ id: 1, name: 'name' }]
})
// ... With this change, I think that i can still use OpenAPI with an existing // in a react spa app
import type { JsonifiedClient } from '@orpc/openapi-client'
import type { ContractRouterClient } from '@orpc/contract'
import { createORPCClient } from '@orpc/client'
import { OpenAPILink } from '@orpc/openapi-client/fetch'
import { contract } from '#backend/api/root-contract' // ✅ this is new code
const link = new OpenAPILink(
contract, // ✅ this is new code
{
url: 'http://localhost:3000/api',
headers: () => ({
'x-api-key': 'my-api-key',
}),
// fetch: <-- polyfill fetch if needed
}
)
const client: JsonifiedClient<ContractRouterClient<typeof contract>> = createORPCClient(link) Obviously i don't want to expose server code into frontend so I don't want to do something like this // in a react spa app
import type { JsonifiedClient } from '@orpc/openapi-client'
import type { ContractRouterClient } from '@orpc/contract'
import { createORPCClient } from '@orpc/client'
import { OpenAPILink } from '@orpc/openapi-client/fetch'
import { router } from '#backend/api/root-router' // ✅ this is new code
const link = new OpenAPILink(
router, // ✅ this is new code
{
url: 'http://localhost:3000/api',
headers: () => ({
'x-api-key': 'my-api-key',
}),
// fetch: <-- polyfill fetch if needed
}
) QuestionsNow I'm thinking that in order to expose the server router via OpenAPI it's mandatory to use a "contract first" approach.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You should migrate to "contract-first" or maybe (I'm not try yet) convert router into json and use it as a "contract" |
Beta Was this translation helpful? Give feedback.
You should migrate to "contract-first" or maybe (I'm not try yet) convert router into json and use it as a "contract"