Skip to content

Commit a749c67

Browse files
authored
Extract and remove unused code around OpenAPI servers (#2867)
1 parent c808bb1 commit a749c67

File tree

5 files changed

+107
-89
lines changed

5 files changed

+107
-89
lines changed

packages/react-openapi/src/OpenAPICodeSample.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { CodeSampleInput, codeSampleGenerators } from './code-samples';
22
import { generateMediaTypeExample, generateSchemaExample } from './generateSchemaExample';
33
import { InteractiveSection } from './InteractiveSection';
4-
import { getServersURL } from './OpenAPIServerURL';
54
import type { OpenAPIContextProps, OpenAPIOperationData } from './types';
65
import { createStateKey } from './utils';
76
import { stringifyOpenAPI } from './stringifyOpenAPI';
87
import { OpenAPITabs, OpenAPITabsList, OpenAPITabsPanels } from './OpenAPITabs';
98
import { checkIsReference } from './utils';
9+
import { getDefaultServerURL } from './util/server';
1010

1111
/**
1212
* Display code samples to execute the operation.
@@ -53,7 +53,7 @@ export function OpenAPICodeSample(props: {
5353

5454
const input: CodeSampleInput = {
5555
url:
56-
getServersURL(data.servers) +
56+
getDefaultServerURL(data.servers) +
5757
data.path +
5858
(searchParams.size ? `?${searchParams.toString()}` : ''),
5959
method: data.method,

packages/react-openapi/src/OpenAPIServerURL.tsx

Lines changed: 0 additions & 73 deletions
This file was deleted.

packages/react-openapi/src/OpenAPIServerURLVariable.tsx

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { describe, it, expect } from 'bun:test';
2+
import { interpolateServerURL, getDefaultServerURL } from './server';
3+
import { OpenAPIV3 } from '@gitbook/openapi-parser';
4+
5+
describe('#interpolateServerURL', () => {
6+
it('interpolates the server URL with the default values of the variables', () => {
7+
const server: OpenAPIV3.ServerObject = {
8+
url: 'https://{username}.example.com/{basePath}',
9+
variables: {
10+
username: { default: 'user' },
11+
basePath: { default: 'v1' },
12+
},
13+
};
14+
const result = interpolateServerURL(server);
15+
expect(result).toBe('https://user.example.com/v1');
16+
});
17+
18+
it('returns the URL with placeholders if no variables are provided', () => {
19+
const server: OpenAPIV3.ServerObject = {
20+
url: 'https://{username}.example.com/{basePath}',
21+
};
22+
const result = interpolateServerURL(server);
23+
expect(result).toBe('https://{username}.example.com/{basePath}');
24+
});
25+
26+
it('returns the URL with mixed placeholders and default values', () => {
27+
const server: OpenAPIV3.ServerObject = {
28+
url: 'https://{username}.example.com/{basePath}',
29+
variables: {
30+
basePath: { default: 'v1' },
31+
},
32+
};
33+
const result = interpolateServerURL(server);
34+
expect(result).toBe('https://{username}.example.com/v1');
35+
});
36+
});
37+
38+
describe('#getDefaultServerURL', () => {
39+
it('returns the default server URL', () => {
40+
const servers: OpenAPIV3.ServerObject[] = [
41+
{
42+
url: 'https://{username}.example.com/{basePath}',
43+
variables: {
44+
username: { default: 'user' },
45+
basePath: { default: 'v1' },
46+
},
47+
},
48+
];
49+
const result = getDefaultServerURL(servers);
50+
expect(result).toBe('https://user.example.com/v1');
51+
});
52+
53+
it('returns null if no servers are provided', () => {
54+
const servers: OpenAPIV3.ServerObject[] = [];
55+
const result = getDefaultServerURL(servers);
56+
expect(result).toBeNull();
57+
});
58+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { OpenAPIV3 } from '@gitbook/openapi-parser';
2+
3+
/**
4+
* Get the default URL for the server.
5+
*/
6+
export function getDefaultServerURL(servers: OpenAPIV3.ServerObject[]): null | string {
7+
const server = servers[0];
8+
if (!server) {
9+
return null;
10+
}
11+
12+
return interpolateServerURL(server);
13+
}
14+
15+
/**
16+
* Interpolate the server URL with the default values of the variables.
17+
*/
18+
export function interpolateServerURL(server: OpenAPIV3.ServerObject) {
19+
const parts = parseServerURL(server?.url ?? '');
20+
21+
return parts
22+
.map((part) => {
23+
if (part.kind === 'text') {
24+
return part.text;
25+
} else {
26+
return server.variables?.[part.name]?.default ?? `{${part.name}}`;
27+
}
28+
})
29+
.join('');
30+
}
31+
32+
function parseServerURL(url: string) {
33+
const parts = url.split(/{([^}]+)}/g);
34+
const result: Array<{ kind: 'variable'; name: string } | { kind: 'text'; text: string }> = [];
35+
for (let i = 0; i < parts.length; i++) {
36+
const part = parts[i];
37+
if (!part) {
38+
continue;
39+
}
40+
if (i % 2 === 0) {
41+
result.push({ kind: 'text', text: part });
42+
} else {
43+
result.push({ kind: 'variable', name: part });
44+
}
45+
}
46+
return result;
47+
}

0 commit comments

Comments
 (0)