-
Notifications
You must be signed in to change notification settings - Fork 0
Add OpenAPI documentation with Swagger UI for tRPC API #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,72 @@ | ||||||
| 'use client'; | ||||||
|
|
||||||
| import { useEffect, useRef, useState } from 'react'; | ||||||
|
|
||||||
| export default function ApiDocsPage() { | ||||||
| const containerRef = useRef<HTMLDivElement>(null); | ||||||
| const [isLoading, setIsLoading] = useState(true); | ||||||
|
|
||||||
| useEffect(() => { | ||||||
| let mounted = true; | ||||||
|
|
||||||
| // Dynamically load Swagger UI | ||||||
| const loadSwaggerUI = async () => { | ||||||
| if (!containerRef.current) return; | ||||||
|
|
||||||
| // Check if CSS is already loaded | ||||||
| const existingLink = document.querySelector( | ||||||
| 'link[href*="swagger-ui.css"]' | ||||||
| ); | ||||||
| if (!existingLink) { | ||||||
| // Load CSS from the installed package | ||||||
| const link = document.createElement('link'); | ||||||
| link.rel = 'stylesheet'; | ||||||
| link.href = | ||||||
| '/_next/static/css/swagger-ui.css'; | ||||||
| document.head.appendChild(link); | ||||||
| } | ||||||
|
|
||||||
| try { | ||||||
| // Load and initialize Swagger UI | ||||||
| const SwaggerUIBundle = ( | ||||||
| await import('swagger-ui-dist/swagger-ui-es-bundle.js') | ||||||
| ).default; | ||||||
|
|
||||||
| if (mounted && containerRef.current) { | ||||||
| SwaggerUIBundle({ | ||||||
| domNode: containerRef.current, | ||||||
| url: '/api/docs/openapi.json', | ||||||
| deepLinking: true, | ||||||
| }); | ||||||
| setIsLoading(false); | ||||||
| } | ||||||
| } catch (error) { | ||||||
| console.error('Failed to load Swagger UI:', error); | ||||||
| setIsLoading(false); | ||||||
|
Comment on lines
+43
to
+45
|
||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| loadSwaggerUI(); | ||||||
|
|
||||||
| return () => { | ||||||
| mounted = false; | ||||||
| }; | ||||||
| }, []); | ||||||
|
|
||||||
| return ( | ||||||
| <div className="min-h-screen bg-white"> | ||||||
| {isLoading && ( | ||||||
| <div className="flex items-center justify-center p-8"> | ||||||
| <p>Loading API documentation...</p> | ||||||
| </div> | ||||||
| )} | ||||||
| <div ref={containerRef} /> | ||||||
| <style jsx global>{` | ||||||
| /* Swagger UI CSS is loaded via the JS bundle */ | ||||||
|
||||||
| /* Swagger UI CSS is loaded via the JS bundle */ | |
| /* Additional styling adjustments for Swagger UI */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { NextResponse } from 'next/server'; | ||
| import { openApiDocument } from '@repo/api'; | ||
|
|
||
| export async function GET() { | ||
| return NextResponse.json(openApiDocument); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export { appRouter } from './root'; | ||
| export type { AppRouter } from './root'; | ||
| export { createContext } from './context'; | ||
| export { openApiDocument } from './openapi'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CSS file path
/_next/static/css/swagger-ui.cssappears to be incorrect. Theswagger-ui-distpackage provides CSS files that should be accessed through the node_modules path or copied to the public directory. Next.js won't automatically serve CSS fromswagger-ui-distat this path.Consider using the CSS directly from the package using an import statement or copying the CSS file to the public directory and referencing it properly. For example:
Or serve it from a public asset path if you copy
swagger-ui.cssfrom the package to your public directory.