fork of hey-api/openapi-ts because I need some additional things
1import colors from 'ansi-colors';
2
3import type { Input } from './types';
4
5export function compileInputPath(input: Omit<Input, 'watch'>) {
6 const result: Pick<
7 Partial<Input>,
8 | 'api_key'
9 | 'branch'
10 | 'commit_sha'
11 | 'organization'
12 | 'project'
13 | 'registry'
14 | 'tags'
15 | 'version'
16 > &
17 Pick<Input, 'path'> = {
18 ...input,
19 path: '',
20 };
21
22 if (input.path && (typeof input.path !== 'string' || input.registry !== 'hey-api')) {
23 result.path = input.path;
24 return result;
25 }
26
27 const [basePath, baseQuery] = input.path.split('?');
28 const queryParts = (baseQuery || '').split('&');
29 const queryPath = queryParts.map((part) => part.split('='));
30
31 let path = basePath || '';
32 if (path.endsWith('/')) {
33 path = path.slice(0, path.length - 1);
34 }
35
36 const [, pathUrl] = path.split('://');
37 const [baseUrl, organization, project] = (pathUrl || '').split('/');
38 result.organization = organization || input.organization;
39 result.project = project || input.project;
40
41 const queryParams: Array<string> = [];
42
43 const kApiKey = 'api_key';
44 result.api_key =
45 queryPath.find(([key]) => key === kApiKey)?.[1] || input.api_key || process.env.HEY_API_TOKEN;
46 if (result.api_key) {
47 queryParams.push(`${kApiKey}=${result.api_key}`);
48 }
49
50 const kBranch = 'branch';
51 result.branch = queryPath.find(([key]) => key === kBranch)?.[1] || input.branch;
52 if (result.branch) {
53 queryParams.push(`${kBranch}=${result.branch}`);
54 }
55
56 const kCommitSha = 'commit_sha';
57 result.commit_sha = queryPath.find(([key]) => key === kCommitSha)?.[1] || input.commit_sha;
58 if (result.commit_sha) {
59 queryParams.push(`${kCommitSha}=${result.commit_sha}`);
60 }
61
62 const kTags = 'tags';
63 result.tags = queryPath.find(([key]) => key === kTags)?.[1]?.split(',') || input.tags;
64 if (result.tags?.length) {
65 queryParams.push(`${kTags}=${result.tags.join(',')}`);
66 }
67
68 const kVersion = 'version';
69 result.version = queryPath.find(([key]) => key === kVersion)?.[1] || input.version;
70 if (result.version) {
71 queryParams.push(`${kVersion}=${result.version}`);
72 }
73
74 if (!result.organization) {
75 throw new Error(
76 'missing organization - from which Hey API Platform organization do you want to generate your output?',
77 );
78 }
79
80 if (!result.project) {
81 throw new Error(
82 'missing project - from which Hey API Platform project do you want to generate your output?',
83 );
84 }
85
86 const query = queryParams.join('&');
87 const platformUrl = baseUrl || 'get.heyapi.dev';
88 const isLocalhost = platformUrl.startsWith('localhost');
89 const platformUrlWithProtocol = [isLocalhost ? 'http' : 'https', platformUrl].join('://');
90 const compiledPath = isLocalhost
91 ? [platformUrlWithProtocol, 'v1', 'get', result.organization, result.project].join('/')
92 : [platformUrlWithProtocol, result.organization, result.project].join('/');
93 result.path = query ? `${compiledPath}?${query}` : compiledPath;
94
95 return result;
96}
97
98export function logInputPaths(
99 inputPaths: ReadonlyArray<ReturnType<typeof compileInputPath>>,
100 jobIndex: number,
101): void {
102 const lines: Array<string> = [];
103
104 const jobPrefix = colors.gray(`[Job ${jobIndex + 1}] `);
105 const count = inputPaths.length;
106 const baseString = colors.cyan(`Generating from ${count} ${count === 1 ? 'input' : 'inputs'}:`);
107 lines.push(`${jobPrefix}⏳ ${baseString}`);
108
109 inputPaths.forEach((inputPath, index) => {
110 const itemPrefixStr = ` [${index + 1}] `;
111 const itemPrefix = colors.cyan(itemPrefixStr);
112 const detailIndent = ' '.repeat(itemPrefixStr.length);
113
114 if (typeof inputPath.path !== 'string') {
115 lines.push(`${jobPrefix}${itemPrefix}raw OpenAPI specification`);
116 return;
117 }
118
119 switch (inputPath.registry) {
120 case 'hey-api': {
121 const baseInput = [inputPath.organization, inputPath.project].filter(Boolean).join('/');
122 lines.push(`${jobPrefix}${itemPrefix}${baseInput}`);
123 if (inputPath.branch) {
124 lines.push(
125 `${jobPrefix}${detailIndent}${colors.gray('branch:')} ${colors.green(
126 inputPath.branch,
127 )}`,
128 );
129 }
130 if (inputPath.commit_sha) {
131 lines.push(
132 `${jobPrefix}${detailIndent}${colors.gray('commit:')} ${colors.green(
133 inputPath.commit_sha,
134 )}`,
135 );
136 }
137 if (inputPath.tags?.length) {
138 lines.push(
139 `${jobPrefix}${detailIndent}${colors.gray('tags:')} ${colors.green(
140 inputPath.tags.join(', '),
141 )}`,
142 );
143 }
144 if (inputPath.version) {
145 lines.push(
146 `${jobPrefix}${detailIndent}${colors.gray('version:')} ${colors.green(
147 inputPath.version,
148 )}`,
149 );
150 }
151 lines.push(
152 `${jobPrefix}${detailIndent}${colors.gray('registry:')} ${colors.green('Hey API')}`,
153 );
154 break;
155 }
156 case 'readme': {
157 const baseInput = [inputPath.organization, inputPath.project].filter(Boolean).join('/');
158 if (!baseInput) {
159 lines.push(`${jobPrefix}${itemPrefix}${inputPath.path}`);
160 } else {
161 lines.push(`${jobPrefix}${itemPrefix}${baseInput}`);
162 }
163 // @ts-expect-error
164 if (inputPath.uuid) {
165 lines.push(
166 `${jobPrefix}${detailIndent}${colors.gray('uuid:')} ${colors.green(
167 // @ts-expect-error
168 inputPath.uuid,
169 )}`,
170 );
171 }
172 lines.push(
173 `${jobPrefix}${detailIndent}${colors.gray('registry:')} ${colors.green('ReadMe')}`,
174 );
175 break;
176 }
177 case 'scalar': {
178 const baseInput = [inputPath.organization, inputPath.project].filter(Boolean).join('/');
179 lines.push(`${jobPrefix}${itemPrefix}${baseInput}`);
180 lines.push(
181 `${jobPrefix}${detailIndent}${colors.gray('registry:')} ${colors.green('Scalar')}`,
182 );
183 break;
184 }
185 default:
186 lines.push(`${jobPrefix}${itemPrefix}${inputPath.path}`);
187 break;
188 }
189 });
190
191 for (const line of lines) {
192 console.log(line);
193 }
194}