Skip to content

Commit

Permalink
feat: openapi survey create
Browse files Browse the repository at this point in the history
  • Loading branch information
qin-guan committed Jul 16, 2023
1 parent 04a425e commit 99a2313
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
5 changes: 5 additions & 0 deletions server/api/openapi.json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { openApiDocument } from '../openapi'

export default defineEventHandler(() => {
return openApiDocument
})
13 changes: 13 additions & 0 deletions server/openapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { generateOpenApiDocument } from 'trpc-openapi'

import { appRouter } from './trpc/routers'

// Generate OpenAPI schema document
export const openApiDocument = generateOpenApiDocument(appRouter, {
title: 'Example CRUD API',
description: 'OpenAPI compliant REST API built using tRPC with Next.js',
version: '1.0.0',
baseUrl: 'https://wsa.qinguan.me/api',
docsUrl: 'https://github.com/jlalmes/trpc-openapi',
tags: ['auth', 'users', 'posts'],
})
26 changes: 14 additions & 12 deletions server/trpc/routers/survey/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,21 @@ export const surveyRouter = router({
return await ctx.prisma.survey.findMany()
}),

create: protectedProcedure.input(
z.object({
title: z.string(),
description: z.string(),
workshop: z.boolean(),
questions: questionsSchema,
permissions: surveyPermissionSchema,
create: protectedProcedure
.meta({ openapi: { method: 'POST', path: '/surveys' } })
.input(
z.object({
title: z.string(),
description: z.string(),
workshop: z.boolean(),
questions: questionsSchema,
permissions: surveyPermissionSchema,
}),
).mutation(async ({ ctx, input }) => {
return await ctx.prisma.survey.create({
data: input,
})
}),
).mutation(async ({ ctx, input }) => {
return await ctx.prisma.survey.create({
data: input,
})
}),

update: protectedProcedure.input(
z.object({
Expand Down
2 changes: 2 additions & 0 deletions server/trpc/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
import { TRPCError, initTRPC } from '@trpc/server'
import superjson from 'superjson'
import type { OpenApiMeta } from 'trpc-nuxt'

Check failure on line 12 in server/trpc/trpc.ts

View workflow job for this annotation

GitHub Actions / ci

Module '"trpc-nuxt"' has no exported member 'OpenApiMeta'.
import type { Context } from '~/server/trpc/context'

interface Meta {
Expand All @@ -19,6 +20,7 @@ interface Meta {
const t = initTRPC
.context<Context>()
.meta<Meta>()
.meta<OpenApiMeta>()
.create({
transformer: superjson,
defaultMeta: {
Expand Down

0 comments on commit 99a2313

Please sign in to comment.