Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: key by subpackageId + endpointId in API Playground #931

Merged
merged 4 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ exports[`getNavigationRoot > octoai > gets navigation root for /api-reference/as
"description": undefined,
"hidden": false,
"icon": undefined,
"id": "list",
"id": "subpackage_asset-library.list",
"method": "GET",
"slug": [
"api-reference",
Expand All @@ -204,7 +204,7 @@ exports[`getNavigationRoot > octoai > gets navigation root for /api-reference/oc
"description": "Return fields on an account",
"hidden": false,
"icon": undefined,
"id": "getAccount",
"id": "subpackage_account.getAccount",
"method": "GET",
"slug": [
"api-reference",
Expand Down
9 changes: 6 additions & 3 deletions packages/commons/fdr-utils/src/flattenApiDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { APIV1Read, DocsV1Read, FdrAPI } from "@fern-api/fdr-sdk";
import { isNonNullish, titleCase, visitDiscriminatedUnion } from "@fern-ui/core-utils";
import { stringifyEndpointPathParts } from "./stringifyEndpointPathParts";
import { isSubpackage } from "./subpackage";

/**
* Flattened API Definition lightly transforms the APIV1Read.ApiDefinition into a more usable format:
Expand Down Expand Up @@ -200,10 +201,12 @@ function flattenPackage(
const endpoints: FlattenedEndpointDefinition[] = [];
const methodAndPathToEndpoint = new Map<string, FlattenedEndpointDefinition>();

const packageId = isSubpackage(currentPackage) ? currentPackage.subpackageId : "root";

currentPackage.endpoints.forEach((endpoint) => {
const flattenedEndpoint: FlattenedEndpointDefinition = {
type: "endpoint",
id: endpoint.id,
id: endpoint.originalEndpointId ?? `${packageId}.${endpoint.id}`,
slug: [...parentSlugs, endpoint.urlSlug],
name: endpoint.name ?? stringifyEndpointPathParts(endpoint.path.parts),
description: endpoint.description,
Expand Down Expand Up @@ -251,7 +254,7 @@ function flattenPackage(
const websockets = currentPackage.websockets.map(
(websocket): FlattenedWebSocketChannel => ({
type: "websocket",
id: websocket.id,
id: `${packageId}.${websocket.id}`,
slug: [...parentSlugs, websocket.urlSlug],
name: websocket.name,
description: websocket.description,
Expand All @@ -272,7 +275,7 @@ function flattenPackage(
const webhooks = currentPackage.webhooks.map(
(webhook): FlattenedWebhookDefinition => ({
type: "webhook",
id: webhook.id,
id: `${packageId}.${webhook.id}`,
slug: [...parentSlugs, webhook.urlSlug],
name: webhook.name,
description: webhook.description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ const UnmemoizedEndpointContentCodeSnippets: React.FC<EndpointContentCodeSnippet
state={{
type: "endpoint",
api,
endpointId: endpoint.slug.join("/"),
endpointId: endpoint.id,
}}
// example={selectedClient.exampleCall}
/>
Expand Down
16 changes: 11 additions & 5 deletions packages/ui/app/src/api-playground/PlaygroundContext.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as Sentry from "@sentry/nextjs";
import { useAtom } from "jotai";
import { atomWithStorage } from "jotai/utils";
import { mapValues, noop } from "lodash-es";
Expand Down Expand Up @@ -61,7 +62,7 @@ export const PlaygroundContextProvider: FC<PropsWithChildren> = ({ children }) =
const { basePath } = useDocsContext();
const [selectionState, setSelectionState] = useState<PlaygroundSelectionState | undefined>();

const key = urljoin(basePath ?? "", "/api/fern-docs/resolve-api");
const key = urljoin(basePath ?? "/", "/api/fern-docs/resolve-api");

const { data } = useSWR<Record<string, ResolvedRootPackage> | null>(key, fetcher);
useEffect(() => {
Expand Down Expand Up @@ -90,14 +91,17 @@ export const PlaygroundContextProvider: FC<PropsWithChildren> = ({ children }) =
async (newSelectionState: PlaygroundSelectionState) => {
const matchedPackage = flattenedApis[newSelectionState.api];
if (matchedPackage == null) {
Sentry.captureMessage("Could not find package for API playground selection state", "fatal");
return;
}

if (newSelectionState.type === "endpoint") {
const matchedEndpoint = matchedPackage.apiDefinitions.find(
(definition) =>
isEndpoint(definition) && definition.slug.join("/") === newSelectionState.endpointId,
(definition) => isEndpoint(definition) && definition.id === newSelectionState.endpointId,
) as ResolvedApiDefinition.Endpoint | undefined;
if (matchedEndpoint == null) {
Sentry.captureMessage("Could not find endpoint for API playground selection state", "fatal");
}
setSelectionState(newSelectionState);
expandPlayground();
capturePosthogEvent("api_playground_opened", {
Expand All @@ -119,9 +123,11 @@ export const PlaygroundContextProvider: FC<PropsWithChildren> = ({ children }) =
}
} else if (newSelectionState.type === "websocket") {
const matchedWebSocket = matchedPackage.apiDefinitions.find(
(definition) =>
isWebSocket(definition) && definition.slug.join("/") === newSelectionState.webSocketId,
(definition) => isWebSocket(definition) && definition.id === newSelectionState.webSocketId,
) as ResolvedApiDefinition.Endpoint | undefined;
if (matchedWebSocket == null) {
Sentry.captureMessage("Could not find websocket for API playground selection state", "fatal");
}
setSelectionState(newSelectionState);
expandPlayground();
capturePosthogEvent("api_playground_opened", {
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/app/src/api-playground/PlaygroundDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,14 @@ export const PlaygroundDrawer: FC<PlaygroundDrawerProps> = ({ apis }) => {
const matchedEndpoint =
selectionState?.type === "endpoint"
? (matchedSection?.apiDefinitions.find(
(definition) => isEndpoint(definition) && definition.slug.join("/") === selectionState.endpointId,
(definition) => isEndpoint(definition) && definition.id === selectionState.endpointId,
) as ResolvedApiDefinition.Endpoint | undefined)
: undefined;

const matchedWebSocket =
selectionState?.type === "websocket"
? (matchedSection?.apiDefinitions.find(
(definition) => isWebSocket(definition) && definition.slug.join("/") === selectionState.webSocketId,
(definition) => isWebSocket(definition) && definition.id === selectionState.webSocketId,
) as ResolvedApiDefinition.WebSocket | undefined)
: undefined;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const PlaygroundEndpointSelectorContent = forwardRef<HTMLDivElement, Play
setSelectionStateAndOpen({
type: "endpoint",
api: group.api,
endpointId: endpoint.slug.join("/"),
endpointId: endpoint.id,
});
closeDropdown?.();
};
Expand All @@ -112,14 +112,16 @@ export const PlaygroundEndpointSelectorContent = forwardRef<HTMLDivElement, Play
setSelectionStateAndOpen({
type: "websocket",
api: group.api,
webSocketId: websocket.slug.join("/"),
webSocketId: websocket.id,
});
closeDropdown?.();
};

function renderApiDefinitionPackage(apiGroup: ApiGroup) {
const endpoints = apiGroup.items.filter((endpoint): endpoint is SidebarNode.ApiPage =>
matchesEndpoint(filterValue, apiGroup, endpoint),
const endpoints = apiGroup.items.filter(
(endpoint): endpoint is SidebarNode.ApiPage =>
matchesEndpoint(filterValue, apiGroup, endpoint) &&
(endpoint.apiType === "endpoint" || endpoint.apiType === "websocket"),
);
if (endpoints.length === 0) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exports[`resolveApiDefinition > should finish resolving 1`] = `
"errors": [],
"examples": [],
"headers": [],
"id": "getMerchants",
"id": "subpackage_merchants.getMerchants",
"method": "GET",
"path": [],
"pathParameters": [],
Expand Down
Loading