fork of hey-api/openapi-ts because I need some additional things
1import type { Context } from '../../../ir/context';
2import { parseUrl } from '../../../utils/url';
3
4export const parseServers = ({ context }: { context: Context }) => {
5 let schemes: ReadonlyArray<string> = context.spec.schemes ?? [];
6 let host = context.spec.host ?? '';
7 const path = context.spec.basePath ?? '';
8
9 for (const input of context.config.input) {
10 if (typeof input.path === 'string') {
11 const url = parseUrl(input.path);
12
13 if (!schemes.length) {
14 if (url.protocol) {
15 schemes = [url.protocol] as typeof schemes;
16 }
17 }
18
19 if (!host) {
20 host = `${url.host}${url.port ? `:${url.port}` : ''}`;
21 }
22 }
23 }
24
25 if (!schemes.length) {
26 schemes = [''];
27 }
28
29 const servers = schemes
30 .map((scheme) => `${scheme ? `${scheme}://` : ''}${host}${path}`)
31 .filter(Boolean);
32
33 if (servers.length) {
34 context.ir.servers = servers.map((url) => ({
35 url,
36 }));
37 }
38};