Skip to content

Commit

Permalink
feat: basic OpenAPI support
Browse files Browse the repository at this point in the history
  • Loading branch information
kripod committed Apr 18, 2024
1 parent 138e1b9 commit 1a5e9e2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import starlight from "@astrojs/starlight";
import { defineConfig } from "astro/config";

export default defineConfig({
site: "https://fxhu.kripod.dev/",
integrations: [
starlight({
title: "FXHU",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"devDependencies": {
"@astrojs/check": "0.5.10",
"@types/node": "20.12.7",
"openapi-types": "12.1.3",
"prettier": "3.2.5",
"prettier-plugin-astro": "0.13.0",
"tsx": "4.7.2",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/pages/.well-known/api-catalog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { APIRoute } from "astro";

import { mediaType } from "../api/v1/openapi.json";

export const GET: APIRoute = (context) =>
Response.json(
{
linkset: [
{
anchor: new URL("/api/v1/", context.site),
"service-desc": [
{
href: new URL("/api/v1/openapi.json", context.site),
type: mediaType,
},
],
},
],
},
{
headers: {
"content-type": "application/linkset+json",
},
},
);
40 changes: 40 additions & 0 deletions src/pages/api/v1/openapi.json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { APIRoute } from "astro";
import type { OpenAPIV3_1 } from "openapi-types";

const OAS_VERSION = "3.1.0";

export const mediaType = "application/openapi+json;version=" + OAS_VERSION;

export const GET: APIRoute = (context) =>
Response.json(
{
openapi: OAS_VERSION,
info: {
title: "FXHU",
license: {
name: "MIT",
identifier: "MIT",
},
version: "1.0",
},
servers: [{ url: new URL("/api/v1", context.site).toString() }],
paths: {
"/symbols.json": {},
"/symbols/{symbol}.json": {},
"/symbols/{symbol}/{year}.json": {},
"/years.json": {},
"/years/{year}.json": {},
},
components: {
parameters: {},
},
externalDocs: {
url: context.site!.toString(),
},
} satisfies OpenAPIV3_1.Document,
{
headers: {
"content-type": mediaType,
},
},
);

0 comments on commit 1a5e9e2

Please sign in to comment.