Update server choice based on selection

This commit is contained in:
Brett Jephson
2024-09-12 16:46:18 +01:00
parent dce39c4c0d
commit e866080867
3 changed files with 8 additions and 23 deletions
@@ -105,14 +105,14 @@ function parseModifiers(data: OpenAPIOperationData, params: Record<string, strin
if (!data) {
return;
}
const { servers } = params;
const { server: serverQueryParam } = params;
const serverIndex =
servers && !isNaN(Number(servers))
? Math.min(0, Math.max(Number(servers), servers.length - 1))
serverQueryParam && !isNaN(Number(serverQueryParam))
? Math.max(0, Math.min(Number(serverQueryParam), data.servers.length - 1))
: 0;
const server = data.servers[serverIndex];
if (server && server.variables) {
return Object.keys(server.variables).reduce<Record<string, number>>(
if (server) {
return Object.keys(server.variables ?? {}).reduce<Record<string, number>>(
(result, key) => {
const selection = Number(params[key]);
if (!isNaN(selection)) {
@@ -120,7 +120,7 @@ function parseModifiers(data: OpenAPIOperationData, params: Record<string, strin
}
return result;
},
{ servers: serverIndex },
{ server: serverIndex },
);
}
}
@@ -28,9 +28,8 @@ export async function OpenAPIOperation(props: {
enumSelectors: context.enumSelectors,
};
const config = await getConfiguration(context);
return (
<ScalarApiClient>
<ScalarApiClient serverUrl={getServersURL(data.servers, context.enumSelectors)}>
<div className={classNames('openapi-operation', className)}>
<div className="openapi-intro">
<h2 className="openapi-summary" id={context.id}>
@@ -72,17 +71,3 @@ export async function OpenAPIOperation(props: {
</ScalarApiClient>
);
}
async function getConfiguration(context: OpenAPIContextProps) {
const response = await fetch(context.specUrl);
const doc = await response.json();
return {
spec: {
content: {
...doc,
servers: [{ url: getServersURL(doc.servers, context.enumSelectors) }],
},
},
};
}
@@ -26,7 +26,7 @@ export function OpenAPIServerURL(props: {
return (
<ServerURLForm context={context} servers={servers} serverIndex={serverIndex}>
{parts.map((part, i) => {
if (part.kind === 'text') {
if (part.kind === 'text') {
return <span key={i}>{part.text}</span>;
} else {
if (!server.variables?.[part.name]) {