Skip to content

Commit

Permalink
feat: add openapi example
Browse files Browse the repository at this point in the history
  • Loading branch information
karrui committed Nov 15, 2024
1 parent 50e1f16 commit 523b87a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/server/modules/me/me.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,31 @@
*
* This is an example router, you can delete this file and then update `../pages/api/trpc/[trpc].tsx`
*/
import { z } from 'zod'

import { protectedProcedure, router } from '~/server/trpc'

export const meRouter = router({
get: protectedProcedure.query(async ({ ctx }) => {
// Remember to write a new query if you want to return something different than what
// the authMiddleware in protectedProcedure returns.
return ctx.user
}),
get: protectedProcedure
.meta({
openapi: {
protect: true,
method: 'GET',
path: '/users/me',
},
})
.input(z.object({}).optional())
.output(
z.object({
id: z.string(),
email: z.string(),
name: z.string().nullable(),
image: z.string().nullable(),
}),
)
.query(async ({ ctx }) => {
// Remember to write a new query if you want to return something different than what
// the authMiddleware in protectedProcedure returns.
return ctx.user
}),
})
8 changes: 8 additions & 0 deletions src/server/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ import { generateOpenApiDocument } from 'trpc-swagger'

import { getBaseUrl } from '~/utils/getBaseUrl'
import { appRouter } from './modules/_app'
import { sessionOptions } from './modules/auth/session'

export const openApiDocument = generateOpenApiDocument(appRouter, {
title: 'tRPC Swagger',
version: '1.0.0', // consider making this pull version from package.json
baseUrl: `${getBaseUrl()}/api`,
securitySchemes: {
ironSession: {
type: 'apiKey',
in: 'cookie',
name: sessionOptions.cookieName,
},
},
})

0 comments on commit 523b87a

Please sign in to comment.