Skip to content

Commit

Permalink
swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler committed Mar 30, 2024
1 parent 034ed5e commit c078726
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 19 deletions.
34 changes: 31 additions & 3 deletions actions/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export class Swagger implements Action {
},
host: `${config.server.web.host}:${config.server.web.port}`,
basePath: `${config.server.web.apiRoute}/`,
schemes: ["https", "http"],
schemes:
["0.0.0.0", "localhost"].indexOf(config.server.web.host) >= 0
? ["http"]
: ["https", "http"],
paths: swaggerPaths,

securityDefinitions: {
Expand Down Expand Up @@ -76,12 +79,37 @@ function buildSwaggerPaths() {
if (!swaggerPaths[formattedPath]) swaggerPaths[formattedPath] = {};

swaggerPaths[formattedPath][method] = {
summary: action.description || "no description",
summary: action.description || action.name,
consumes: ["application/json"],
produces: ["application/json"],
responses: swaggerResponses,
security: [],
parameters: [], //TODO
parameters: Object.keys(action.inputs)
.sort()
.map((inputName) => {
return {
// in: action?.web?.route.toString().includes(`:${inputName}`)
// ? "path"
// : "query",
in: "query",
name: inputName,
type: "string", // not really true, but helps the swagger validator
required: action.inputs[inputName].required ?? false,
// ||
// route.path.includes(`:${inputName}`)
// ? true
// : false
default:
action.inputs[inputName].default !== null &&
action.inputs[inputName].default !== undefined
? typeof action.inputs[inputName].default === "object"
? JSON.stringify(action.inputs[inputName].default)
: typeof action.inputs[inputName].default === "function"
? action.inputs[inputName].default()
: `${action.inputs[inputName].default}`
: undefined,
};
}),
};
}

Expand Down
Binary file modified bun.lockb
Binary file not shown.
25 changes: 18 additions & 7 deletions components/SwaggerCard.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { Card } from "react-bootstrap";
import { useEffect } from "react";

declare var SwaggerUIBundle: any; // imported via the layout

export const SwaggerCard = () => {
return (
<Card bg="primary">
<Card.Header>API Endpoints</Card.Header>
<Card.Body></Card.Body>
</Card>
);
useEffect(() => {
SwaggerUIBundle({
dom_id: "#swaggerContainer",
url: `/api/swagger`,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset,
],
deepLinking: true,
docExpansion: "none",
filter: true,
});
}, []);

return <div id="swaggerContainer" />;
};
7 changes: 6 additions & 1 deletion initializers/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,13 @@ const transpileAllPages = async (pages: string[]) => {
await unlink(path.join(transpiledPagesDir, f));
}

await Bun.build({
const result = await Bun.build({
...{ entrypoints: pages },
...transpilerOptions,
});

if (!result.success) {
logger.fatal("Build failed");
for (const message of result.logs) console.error(message);
}
};
34 changes: 34 additions & 0 deletions layouts/swagger.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from "react";

export type LayoutProps = {
title: string;
};

const swaggerVersion = "5.13.0";

export const SwaggerLayout = (props: React.PropsWithChildren<LayoutProps>) => {
return (
<React.StrictMode>
<html lang="en">
<head>
<meta charSet="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>

<link
rel="stylesheet"
href={`https://unpkg.com/swagger-ui-dist@${swaggerVersion}/swagger-ui.css`}
/>
<script
src={`https://unpkg.com/swagger-ui-dist@${swaggerVersion}/swagger-ui-bundle.js`}
></script>

<title>{props.title}</title>
</head>
<body>{props.children}</body>
</html>
</React.StrictMode>
);
};
11 changes: 3 additions & 8 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { mount } from "../util/browser";
import { StatusCard } from "../components/StatusCard";
import { MainLayout } from "../layouts/main";
import { HelloCard } from "../components/HelloCard";
import { SwaggerCard } from "../components/SwaggerCard";

export default function Page() {
return (
<MainLayout title="Hello World">
<Container>
<h1>Hello World</h1>
<p>sups.</p>
<p>
<a href="/swagger">View API Endpoints</a>
</p>
<hr />
<Row>
<Col md={6}>
Expand All @@ -21,12 +22,6 @@ export default function Page() {
<HelloCard />
</Col>
</Row>
<br />
<Row>
<Col md={12}>
<SwaggerCard />
</Col>
</Row>
</Container>
</MainLayout>
);
Expand Down
20 changes: 20 additions & 0 deletions pages/swagger.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Container, Row, Col } from "react-bootstrap";
import { mount } from "../util/browser";
import { SwaggerLayout } from "../layouts/swagger";
import { SwaggerCard } from "../components/SwaggerCard";

export default function Page() {
return (
<SwaggerLayout title="API Endpoints">
<Container>
<Row>
<Col md={12}>
<SwaggerCard />
</Col>
</Row>
</Container>
</SwaggerLayout>
);
}

mount(Page);
4 changes: 4 additions & 0 deletions servers/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export class WebServer extends Server<ReturnType<typeof Bun.serve>> {
} catch {}
}

for (const [key, value] of url.searchParams) {
params.set(key, value);
}

const { response, error } = await connection.act(
actionName,
params,
Expand Down

0 comments on commit c078726

Please sign in to comment.