diff --git a/server/api/openapi.json.ts b/server/api/openapi.json.ts new file mode 100644 index 0000000..8494457 --- /dev/null +++ b/server/api/openapi.json.ts @@ -0,0 +1,5 @@ +import { openApiDocument } from '../openapi' + +export default defineEventHandler(() => { + return openApiDocument +}) diff --git a/server/openapi.ts b/server/openapi.ts new file mode 100644 index 0000000..b81e555 --- /dev/null +++ b/server/openapi.ts @@ -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'], +}) diff --git a/server/trpc/routers/survey/index.ts b/server/trpc/routers/survey/index.ts index 1dc7982..7af1584 100644 --- a/server/trpc/routers/survey/index.ts +++ b/server/trpc/routers/survey/index.ts @@ -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({ diff --git a/server/trpc/trpc.ts b/server/trpc/trpc.ts index edb3a9a..f3b1f1b 100644 --- a/server/trpc/trpc.ts +++ b/server/trpc/trpc.ts @@ -9,6 +9,7 @@ */ import { TRPCError, initTRPC } from '@trpc/server' import superjson from 'superjson' +import type { OpenApiMeta } from 'trpc-nuxt' import type { Context } from '~/server/trpc/context' interface Meta { @@ -19,6 +20,7 @@ interface Meta { const t = initTRPC .context() .meta() + .meta() .create({ transformer: superjson, defaultMeta: {