mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-11-27 10:43:32 +00:00
Keep each OpenAPI spec's server choice separate
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import { renderToReadableStream } from 'react-dom/server';
|
||||
|
||||
import type { OpenAPIClientContext } from './context';
|
||||
import { OpenAPIPathMultipleServers } from './OpenAPIPathMultipleServers';
|
||||
import { translations } from './translations';
|
||||
import type { OpenAPIOperationData } from './types';
|
||||
|
||||
const context: OpenAPIClientContext = {
|
||||
translation: translations.en,
|
||||
icons: {
|
||||
chevronDown: null,
|
||||
chevronRight: null,
|
||||
plus: null,
|
||||
copy: null,
|
||||
check: null,
|
||||
lock: null,
|
||||
mcp: null,
|
||||
hashtag: null,
|
||||
},
|
||||
scalarRuntimeURL: '',
|
||||
$$isClientContext$$: true,
|
||||
};
|
||||
|
||||
async function renderPath(servers: string[]) {
|
||||
const data: OpenAPIOperationData = {
|
||||
path: '/v1/devices',
|
||||
method: 'get',
|
||||
servers: servers.map((url) => ({ url })),
|
||||
operation: { responses: {} },
|
||||
securities: [],
|
||||
};
|
||||
const stream = await renderToReadableStream(
|
||||
<OpenAPIPathMultipleServers data={data} context={context} />
|
||||
);
|
||||
return new Response(stream).text();
|
||||
}
|
||||
|
||||
describe('OpenAPIPathMultipleServers', () => {
|
||||
it('starts on its own default server after another spec that lists the same server', async () => {
|
||||
await renderPath(['https://trader.example.com', 'https://sandbox.example.com']);
|
||||
const html = await renderPath(['https://assets.example.com', 'https://trader.example.com']);
|
||||
|
||||
expect(html).toContain('https://assets.example.com');
|
||||
expect(html).not.toContain('https://trader.example.com');
|
||||
});
|
||||
});
|
||||
@@ -8,8 +8,6 @@ import type { OpenAPIServerWithCustomProperties } from './types';
|
||||
import { getDefaultServerURL, interpolateServerURL } from './util/server';
|
||||
import { createStateKey } from './utils';
|
||||
|
||||
export const serversStateKey = createStateKey('servers');
|
||||
|
||||
/**
|
||||
* Display the path of an operation.
|
||||
*/
|
||||
@@ -20,7 +18,6 @@ export function OpenAPIPathMultipleServers(
|
||||
const { path, servers } = data;
|
||||
|
||||
const defaultServer = getDefaultServerURL(servers);
|
||||
const { key, setKey } = useSelectState(serversStateKey, defaultServer);
|
||||
const formattedPath = formatPath(path);
|
||||
|
||||
const items = servers
|
||||
@@ -36,14 +33,15 @@ export function OpenAPIPathMultipleServers(
|
||||
};
|
||||
});
|
||||
|
||||
// The store outlives client-side navigation, so it can hold a server from another spec.
|
||||
const selectedServer = items.find((item) => item.key === key)?.key ?? defaultServer;
|
||||
// The store outlives client-side navigation: one key per server list keeps another spec's choice out.
|
||||
const stateKey = createStateKey('servers', items.map((item) => item.key).join(' '));
|
||||
const { key, setKey } = useSelectState(stateKey, defaultServer);
|
||||
|
||||
return (
|
||||
<OpenAPIPathItem
|
||||
copyType="button"
|
||||
{...props}
|
||||
value={`${withServer ? selectedServer : ''}${path}`}
|
||||
value={`${withServer ? key : ''}${path}`}
|
||||
context={context}
|
||||
>
|
||||
{withServer ? (
|
||||
@@ -51,7 +49,7 @@ export function OpenAPIPathMultipleServers(
|
||||
className="openapi-select openapi-select-unstyled"
|
||||
items={items}
|
||||
itemClassName="openapi-select-item-column"
|
||||
stateKey={serversStateKey}
|
||||
stateKey={stateKey}
|
||||
placement="bottom start"
|
||||
icon={context.icons.chevronDown}
|
||||
defaultValue={defaultServer}
|
||||
|
||||
Reference in New Issue
Block a user