Skip to content

Commit

Permalink
Add server url cache to update request code sample
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettJephson committed Sep 24, 2024
1 parent 6aff461 commit 2b7ea27
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Metadata, Viewport } from 'next';
import { notFound, redirect } from 'next/navigation';
import React from 'react';

import { serverUrlCache } from '@/components/DocumentView/OpenAPI/ServerUrlCache';
import { PageAside } from '@/components/PageAside';
import { PageBody, PageCover } from '@/components/PageBody';
import { PageHrefContext, absoluteHref, pageHref } from '@/lib/links';
Expand All @@ -26,6 +27,8 @@ export default async function Page(props: {
}) {
const { params, searchParams } = props;

serverUrlCache.parse(searchParams);

const {
content: contentPointer,
contentTarget,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { fetchOpenAPIBlock } from '@/lib/openapi';
import { tcls } from '@/lib/tailwind';

import OpenAPIContext from './OpenAPIContext';
import { serverUrlCache } from './ServerUrlCache';
import { BlockProps } from '../Block';
import { PlainCodeBlock } from '../CodeBlock';

Expand Down Expand Up @@ -46,6 +47,8 @@ async function OpenAPIBody(props: BlockProps<DocumentBlockSwagger>) {
return null;
}

const serverUrl = serverUrlCache.get('serverUrl');

return (
<OpenAPIContext block={block} data={data}>
<OpenAPIOperation
Expand All @@ -61,6 +64,7 @@ async function OpenAPIBody(props: BlockProps<DocumentBlockSwagger>) {
defaultInteractiveOpened: context.mode === 'print',
id: block.meta?.id,
blockKey: block.key,
serverUrl
}}
className="openapi-block"
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createSearchParamsCache, parseAsString } from 'nuqs/server';

export const serverUrlCache = createSearchParamsCache({
serverUrl: parseAsString
})
3 changes: 2 additions & 1 deletion packages/react-openapi/src/OpenAPICodeSample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ export function OpenAPICodeSample(props: {
}
});

const serverUrl = context.serverUrl ?? getServersURL(data.servers);
const requestBody = noReference(data.operation.requestBody);
const requestBodyContent = requestBody ? Object.entries(requestBody.content)[0] : undefined;
const input: CodeSampleInput = {
url:
getServersURL(data.servers, context.enumSelectors) +
serverUrl +
data.path +
(searchParams.size ? `?${searchParams.toString()}` : ''),
method: data.method,
Expand Down
1 change: 0 additions & 1 deletion packages/react-openapi/src/OpenAPIServerURL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export function OpenAPIServerURL(props: {
const serverIndex = !isNaN(Number(ctx?.state?.server)) ? Number(ctx?.state?.server) : 0;
const server = servers[serverIndex];
const parts = parseServerURL(server?.url ?? '');
console.log({ ctxState: ctx?.state });

return (
<ServerURLForm context={context} servers={servers} serverIndex={serverIndex}>
Expand Down
3 changes: 2 additions & 1 deletion packages/react-openapi/src/OpenAPIServerURLForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { OpenAPIClientContext } from './types';
import { OpenAPIV3 } from 'openapi-types';
import { ServerSelector } from './ServerSelector';
import { useOpenAPIContext } from './OpenAPIContextProvider';
import { getServersURL } from './utils';

export function ServerURLForm(props: {
children: React.ReactNode;
Expand Down Expand Up @@ -80,7 +81,7 @@ export function ServerURLForm(props: {
update({
server: `${serverIndex}`,
...state,
...(ctx?.state?.edit ? undefined : { edit: 'true' }),
...(ctx?.state?.edit ? { serverUrl: getServersURL(servers, state) } : { edit: 'true' }),
});
}}
title={ctx?.state?.edit ? undefined : 'Try different server options'}
Expand Down
5 changes: 2 additions & 3 deletions packages/react-openapi/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ export interface OpenAPIClientContext {
* Optional id attached to the OpenAPI Operation heading and used as an anchor
*/
id?: string;

/**
* Selectors to update openapi enums, e.g. for server url variables
* Optional serverUrl to use with OpenAPI operations
*/
enumSelectors?: Record<string, string>;
serverUrl?: string | null;
}

export interface OpenAPIFetcher {
Expand Down

0 comments on commit 2b7ea27

Please sign in to comment.