fork of hey-api/openapi-ts because I need some additional things
1import type { Input } from '../../config/input/types';
2
3// Regular expression to match Scalar API Registry input formats:
4// - @{organization}/{project}
5const registryRegExp = /^(@[\w-]+)\/([\w.-]+)$/;
6
7/**
8 * Creates a full Scalar API Registry URL.
9 *
10 * @param organization - Scalar organization slug
11 * @param project - Scalar project slug
12 * @returns The full Scalar API registry URL.
13 */
14export function getRegistryUrl(organization: string, project: string): string {
15 return `https://registry.scalar.com/${organization}/apis/${project}/latest?format=json`;
16}
17
18export type Parsed = {
19 organization: string;
20 project: string;
21};
22
23const namespace = 'scalar';
24
25/**
26 * Parses a Scalar input string and extracts components.
27 *
28 * @param shorthand - Scalar format string (@org/project)
29 * @returns Parsed Scalar input components
30 * @throws Error if the input format is invalid
31 */
32export function parseShorthand(shorthand: string): Parsed {
33 const match = shorthand.match(registryRegExp);
34
35 if (!match) {
36 throw new Error(
37 `Invalid Scalar shorthand format. Expected "${namespace}:@organization/project", received: ${namespace}:${shorthand}`,
38 );
39 }
40
41 const [, organization, project] = match;
42
43 if (!organization) {
44 throw new Error('The Scalar organization cannot be empty.');
45 }
46
47 if (!project) {
48 throw new Error('The Scalar project cannot be empty.');
49 }
50
51 const result: Parsed = {
52 organization,
53 project,
54 };
55
56 return result;
57}
58
59/**
60 * Transforms a Scalar shorthand string to the corresponding API URL.
61 *
62 * @param input - Scalar format string
63 * @returns The Scalar API Registry URL
64 */
65export function inputToScalarPath(input: string): Partial<Input> {
66 const shorthand = input.slice(`${namespace}:`.length);
67 const parsed = parseShorthand(shorthand);
68 return {
69 ...parsed,
70 path: getRegistryUrl(parsed.organization, parsed.project),
71 registry: 'scalar',
72 };
73}