From ed41a29412bdf3b4c952dcba419e935096f78229 Mon Sep 17 00:00:00 2001 From: Moritz Date: Wed, 8 Jan 2025 15:53:18 +0100 Subject: [PATCH] clean up, remove loc --- docs/pages/configuration/api-catalog.md | 4 +- .../zudoku/src/lib/plugins/openapi/Route.tsx | 56 ++++++-- .../zudoku/src/lib/plugins/openapi/index.tsx | 124 ++++-------------- 3 files changed, 76 insertions(+), 108 deletions(-) diff --git a/docs/pages/configuration/api-catalog.md b/docs/pages/configuration/api-catalog.md index b70d6469..c6b666b6 100644 --- a/docs/pages/configuration/api-catalog.md +++ b/docs/pages/configuration/api-catalog.md @@ -95,7 +95,9 @@ const config = { catalog: { navigationId: "catalog", label: "API Catalog", - filterItems: (item, { auth: AuthState }) => , + filterItems: (items, { auth: AuthState }) => { + return items.filter((items) => items.tags.includes(auth)); + }, }, // ... }; diff --git a/packages/zudoku/src/lib/plugins/openapi/Route.tsx b/packages/zudoku/src/lib/plugins/openapi/Route.tsx index ef37495e..3e87065f 100644 --- a/packages/zudoku/src/lib/plugins/openapi/Route.tsx +++ b/packages/zudoku/src/lib/plugins/openapi/Route.tsx @@ -1,19 +1,55 @@ -import { Outlet } from "react-router"; +import { Outlet, useParams } from "react-router"; +import { joinPath } from "../../util/joinPath.js"; import type { GraphQLClient } from "./client/GraphQLClient.js"; import { GraphQLProvider } from "./client/GraphQLContext.js"; import { OasConfigProvider } from "./context.js"; -import { OasPluginContext } from "./interfaces.js"; +import { type OasPluginConfig } from "./interfaces.js"; export const OpenApiRoute = ({ + basePath, + versions, config, client, }: { - config: OasPluginContext; + basePath: string; + versions: string[]; + config: OasPluginConfig; client: GraphQLClient; -}) => ( - - - - - -); +}) => { + const { version } = useParams<"version">(); + + const input = + config.type === "file" + ? { + type: config.type, + input: version + ? config.input[version]! + : Object.values(config.input).at(0)!, + } + : { type: config.type, input: config.input }; + + const currentVersion = version ?? versions.at(0); + + if (!currentVersion) { + throw new Error("No version found"); + } + + return ( + [version, joinPath(basePath, version)]), + ), + ...input, + }, + }} + > + + + + + ); +}; diff --git a/packages/zudoku/src/lib/plugins/openapi/index.tsx b/packages/zudoku/src/lib/plugins/openapi/index.tsx index e14afbfa..3334c290 100644 --- a/packages/zudoku/src/lib/plugins/openapi/index.tsx +++ b/packages/zudoku/src/lib/plugins/openapi/index.tsx @@ -1,4 +1,4 @@ -import { matchPath, type RouteObject } from "react-router"; +import { matchPath } from "react-router"; import { type ZudokuPlugin } from "../../core/plugins.js"; import { graphql } from "./graphql/index.js"; @@ -128,16 +128,12 @@ export const openApiPlugin = (config: OpenApiPluginOptions): ZudokuPlugin => { try { const version = - versions.find((v) => path === `${basePath}/v${v}`) ?? - Object.keys(config.input)[0]; - - if (!version) { - throw new Error("No version found"); - } + versions.find((v) => path === joinPath(basePath, v)) ?? + Object.keys(config.input).at(0); const data = await client.fetch(GetCategoriesQuery, { type: config.type, - input: config.type === "file" ? config.input[version] : config.input, + input: config.type === "file" ? config.input[version!] : config.input, version, }); @@ -170,98 +166,32 @@ export const openApiPlugin = (config: OpenApiPluginOptions): ZudokuPlugin => { return []; } }, - getRoutes: () => - [ - ...versions.map((version) => { + getRoutes: () => [ + { + path: basePath + "/:version?", + async lazy() { + const { OpenApiRoute } = await import("./Route.js"); return { - path: basePath + "/v" + version, + element: ( + + ), + }; + }, + children: [ + { + index: true, async lazy() { - const { OpenApiRoute } = await import("./Route.js"); - const input = - config.type === "file" - ? { type: config.type, input: config.input[version]! } - : { type: config.type, input: config.input }; - - if (!input.input) { - throw new Error("No input found"); - } - return { - element: ( - [ - version, - basePath + "/v" + version, - ]), - ), - ...input, - }} - /> - ), - }; + const { OperationList } = await import("./OperationList.js"); + return { element: }; }, - children: [ - { - index: true, - async lazy() { - const { OperationList } = await import("./OperationList.js"); - return { element: }; - }, - }, - ], - }; - }), - { - path: basePath, - async lazy() { - const { OpenApiRoute } = await import("./Route.js"); - - const input = - config.type === "file" - ? { - type: config.type, - input: Object.values(config.input).at(0)!, - } - : { type: config.type, input: config.input }; - - const version = versions.at(0); - if (!version) { - throw new Error("No version found"); - } - - return { - element: ( - [ - version, - basePath + "/v" + version, - ]), - ), - ...input, - }} - /> - ), - }; }, - children: [ - { - index: true, - async lazy() { - const { OperationList } = await import("./OperationList.js"); - return { element: }; - }, - }, - ], - }, - ] satisfies RouteObject[], + ], + }, + ], }; };