question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

tRPC to OpenAPI generator

See original GitHub issue

Note: 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:

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:closed
  • Created a year ago
  • Reactions:12
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

4reactions
jlalmescommented, May 23, 2022

You can try out the alpha release of trpc-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

3reactions
jlalmescommented, May 13, 2022

@Brian-McBride I plan to publish my trpc-openapi work promptly after tRPC v10 is released.

Read more comments on GitHub >

github_iconTop 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 >
Awesome tRPC Collection
A collection of resources on tRPC.
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found