swagger api generator

This commit is contained in:
Lea 2024-01-23 21:35:46 +01:00
parent c90bfd4b47
commit 8fe74710e0
Signed by: Lea
GPG key ID: 1BAFFE8347019C42
16 changed files with 108 additions and 1 deletions

View file

@ -39,4 +39,6 @@ Check out our [Next.js deployment documentation](https://nextjs.org/docs/deploym
```sql
CREATE TABLE aliases (id INTEGER PRIMARY KEY AUTOINCREMENT, address TEXT NOT NULL, alias TEXT NOT NULL, pending INTEGER DEFAULT 0, temporary INTEGER DEFAULT 0);
CREATE TABLE temp_alias_requests (key TEXT PRIMARY KEY, address TEXT NOT NULL, alias TEXT NOT NULL, expires INTEGER NOT NULL);
CREATE TABLE api_keys (id INTEGER PRIMARY KEY AUTOINCREMENT, address TEXT NOT NULL, token TEXT NOT NULL);
```

View file

@ -0,0 +1,19 @@
import { Button, Flex, Heading, Text } from "@radix-ui/themes";
import { ExternalLinkIcon } from "lucide-react";
import Link from "next/link";
export default function ApiKeys() {
return (
<>
<Flex justify="between" align="center" direction="row" gap="5">
<Flex direction="column">
<Heading>API keys</Heading>
<Text size="3" weight="light">Generate long lasting API keys that can be used to perform certain actions programatically.</Text>
</Flex>
<Link href="/api-doc" target="_blank">
<Button variant="outline">API docs <ExternalLinkIcon size="16" /></Button>
</Link>
</Flex>
</>
);
}

View file

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 25 KiB

View file

@ -0,0 +1,11 @@
import { getApiDocs } from '@/lib/swagger';
import ReactSwagger from './swagger';
export default async function IndexPage() {
const spec = await getApiDocs();
return (
<section className="container">
<ReactSwagger spec={spec} />
</section>
);
}

View file

@ -0,0 +1,14 @@
'use client';
import SwaggerUI from 'swagger-ui-react';
import 'swagger-ui-react/swagger-ui.css';
type Props = {
spec: Record<string, any>,
};
function ReactSwagger({ spec }: Props) {
return <SwaggerUI spec={spec} />;
}
export default ReactSwagger;

View file

@ -0,0 +1,13 @@
export default async function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>
{children}
</body>
</html>
);
}

15
src/app/api/test/route.ts Normal file
View file

@ -0,0 +1,15 @@
/**
* @swagger
* /api/test:
* get:
* description: Returns the hello world
* responses:
* 200:
* description: Hello World!
*/
export async function GET(_request: Request) {
// Do whatever you want
return new Response('Hello World!', {
status: 200,
});
}

View file

@ -2,7 +2,7 @@
import { isAdmin } from "@/lib/util";
import { Avatar, Button, Card, Flex, IconButton, Popover, ScrollArea, Text, Tooltip } from "@radix-ui/themes";
import { BookUserIcon, HomeIcon, ScrollTextIcon, Users2Icon } from "lucide-react";
import { BookUserIcon, HomeIcon, KeyRoundIcon, ScrollTextIcon, Users2Icon } from "lucide-react";
import { signOut, useSession } from "next-auth/react";
import Link from "next/link";
import dayjs from "dayjs";
@ -67,6 +67,14 @@ export default function NavigationPanel({ mobileUi }: { mobileUi?: boolean }) {
</Link>
</Tooltip>
<Tooltip content="API Keys" side={tooltipSide}>
<Link href="/api-keys">
<IconButton variant="outline" size='4'>
<KeyRoundIcon />
</IconButton>
</Link>
</Tooltip>
{isAdmin(session.data) && (
<>
<Tooltip content="Users" side={tooltipSide}>

25
src/lib/swagger.ts Normal file
View file

@ -0,0 +1,25 @@
import { createSwaggerSpec } from 'next-swagger-doc';
export const getApiDocs = async () => {
const spec = createSwaggerSpec({
apiFolder: 'src/app/api', // define api folder under app folder
definition: {
openapi: '3.0.0',
info: {
title: 'Maddy Panel API',
version: '1.0',
},
components: {
securitySchemes: {
BearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
},
security: [],
},
});
return spec;
};