tRPC to OpenAPI generator
See original GitHub issueNote: I do not want to have this as part of the library itself, but an example would be good
Overview
With the addition of output parser (#1699), we should now have all the pieces to automatically create an OpenAPI-compliant endpoint
Some rough ideas:
- Create a new endpoint
/api/openapi.yml.ts
that can use the method earlier to generate a schema - Create a catch-all
/api/openapi/[...params].ts
that can dynamically read and call tRPC queries - https://github.com/StefanTerdell/zod-to-json-schema#readme can be useful
- Example how to create JSON schema from tRPC routes: https://gist.github.com/KATT/993fbcbaf123f08098a154631e5af68c
- This package can be of good inspo for the openapi output https://github.com/RobinTail/express-zod-api
A starting point for the api handler:
import { ProcedureType, TRPCError } from '@trpc/server';
import { NextApiRequest, NextApiResponse } from 'next';
import { createContext } from '~/server/context';
import { appRouter } from '~/server/routers/_app';
export default async function swag(req: NextApiRequest, res: NextApiResponse) {
const { params, ...queryParams } = req.query;
if (req.method !== 'POST' && req.method !== 'GET') {
res.status(405).json({
message: 'Invalid method',
});
return;
}
const path = Array.isArray(params) ? params.join('/') : params;
const ctx = await createContext({ req, res });
const type: ProcedureType = req.method === 'POST' ? 'mutation' : 'query';
try {
const caller = appRouter.createCaller({ ctx });
const fn = caller[type] as any;
const result = await fn(path, queryParams);
res.json(result);
return;
} catch (error) {
if (!(error instanceof TRPCError)) {
res.status(500).json({
message: 'Something went wrong',
});
return;
}
const formatted = appRouter.getErrorShape({
error,
path,
ctx,
input: params,
type,
});
// TODO: use https://github.com/trpc/trpc/blob/main/packages/server/src/http/internals/getHTTPStatusCode.ts#L55-L59
// Feel free to make them publicly exposed
const status = 500;
res.status(500).json(formatted);
}
}
Issue Analytics
- State:
- Created a year ago
- Reactions:12
- Comments:14 (11 by maintainers)
Top Results From Across the Web
jlalmes/trpc-openapi: OpenAPI support for tRPC - GitHub
1. Install trpc-openapi . · 2. Add OpenApiMeta to your tRPC instance. · 3. Enable openapi support for a procedure. · 4. Generate...
Read more >TRPC: End-to-end typesafe APIs made easy - Brian Lovin
58 comments · trpc.io. ... tRPC uses those type signatures to provide a typesafe client API, basically an SDK ... tRPC to OpenAPI...
Read more >Build end-to-end typesafe APIs with tRPC - DEV Community
tRPC is a very light library which lets you build fully typesafe APIs without the need of schemas or code generation. It allows...
Read more >openapi-types examples - CodeSandbox
express-oas-generatorModule to automatically generate OpenAPI (Swagger) specification for existing ExpressJS 4.x REST API applications · trpc-openapitRPC ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can try out the
alpha
release oftrpc-openapi
that is compatible with the current stable version of@trpc/server@9
. Bear in mind that the API may change before official release.https://www.npmjs.com/package/trpc-openapi
@Brian-McBride I plan to publish my
trpc-openapi
work promptly after tRPCv10
is released.