mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 15:45:13 +00:00
Add server url cache to update request code sample
This commit is contained in:
@@ -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';
|
||||
@@ -26,6 +27,8 @@ export default async function Page(props: {
|
||||
}) {
|
||||
const { params, searchParams } = props;
|
||||
|
||||
serverUrlCache.parse(searchParams);
|
||||
|
||||
const {
|
||||
content: contentPointer,
|
||||
contentTarget,
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -46,6 +47,8 @@ async function OpenAPIBody(props: BlockProps<DocumentBlockSwagger>) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const serverUrl = serverUrlCache.get('serverUrl');
|
||||
|
||||
return (
|
||||
<OpenAPIContext block={block} data={data}>
|
||||
<OpenAPIOperation
|
||||
@@ -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"
|
||||
/>
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import { createSearchParamsCache, parseAsString } from 'nuqs/server';
|
||||
|
||||
export const serverUrlCache = createSearchParamsCache({
|
||||
serverUrl: parseAsString
|
||||
})
|
||||
@@ -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,
|
||||
|
||||
@@ -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}>
|
||||
|
||||
@@ -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;
|
||||
@@ -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'}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user