Skip to content

Commit

Permalink
clean up, remove loc
Browse files Browse the repository at this point in the history
  • Loading branch information
mosch committed Jan 8, 2025
1 parent d4a90d7 commit ed41a29
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 108 deletions.
4 changes: 3 additions & 1 deletion docs/pages/configuration/api-catalog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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));
},
},
// ...
};
Expand Down
56 changes: 46 additions & 10 deletions packages/zudoku/src/lib/plugins/openapi/Route.tsx
Original file line number Diff line number Diff line change
@@ -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;
}) => (
<OasConfigProvider value={{ config }}>
<GraphQLProvider client={client}>
<Outlet />
</GraphQLProvider>
</OasConfigProvider>
);
}) => {
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 (
<OasConfigProvider
value={{
config: {
...config,
version: currentVersion,
versions: Object.fromEntries(
versions.map((version) => [version, joinPath(basePath, version)]),
),
...input,
},
}}
>
<GraphQLProvider client={client}>
<Outlet />
</GraphQLProvider>
</OasConfigProvider>
);
};
124 changes: 27 additions & 97 deletions packages/zudoku/src/lib/plugins/openapi/index.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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,
});

Expand Down Expand Up @@ -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: (
<OpenApiRoute
basePath={basePath}
versions={versions}
client={client}
config={config}
/>
),
};
},
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: (
<OpenApiRoute
client={client}
config={{
...config,
version,
versions: Object.fromEntries(
versions.map((version) => [
version,
basePath + "/v" + version,
]),
),
...input,
}}
/>
),
};
const { OperationList } = await import("./OperationList.js");
return { element: <OperationList /> };
},
children: [
{
index: true,
async lazy() {
const { OperationList } = await import("./OperationList.js");
return { element: <OperationList /> };
},
},
],
};
}),
{
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: (
<OpenApiRoute
client={client}
config={{
...config,
version,
versions: Object.fromEntries(
versions.map((version) => [
version,
basePath + "/v" + version,
]),
),
...input,
}}
/>
),
};
},
children: [
{
index: true,
async lazy() {
const { OperationList } = await import("./OperationList.js");
return { element: <OperationList /> };
},
},
],
},
] satisfies RouteObject[],
],
},
],
};
};

0 comments on commit ed41a29

Please sign in to comment.