elysiajs / elysia-swagger

A plugin for Elysia to auto-generate Swagger page
MIT License
74 stars 36 forks source link

bugfix(accept external swagger paths) #113

Closed Autumnlight02 closed 2 months ago

Autumnlight02 commented 3 months ago

This PR resolves an issue related to path options. The reason as to why is the following scenario: I use trpc with Elysia.js, my goal is now to include Swagger documentation of that process. Below is my personal usecase / way as in how I generate the trpc documentation and how I use it.

import { trpc } from "@elysiajs/trpc";
import { Elysia } from "elysia";
import { appRouter } from "./router/router";
import { swagger } from "@elysiajs/swagger";
import {
    generateOpenApiDocument,
} from "trpc-openapi";

export const openApiDocument = generateOpenApiDocument(appRouter, {
  title: "tRPC OpenAPI",
  version: "1.0.0",
  baseUrl: "http://localhost:3000",
});

const swaggerExec = swagger({
  documentation: {
    paths: openApiDocument.paths,
  },
});

const app = new Elysia()
  .use(
    trpc(appRouter, {
      endpoint: "/trpc",
      createContext: (args) => {
        console.log(args);
        return {};
      },
    })
  )
  .use(swaggerExec)
  .listen(3000);

console.log(
  `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);