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 ReadMe API Registry input formats:
4// - @{organization}/{project}#{uuid}
5// - {uuid}
6const registryRegExp = /^(@([\w-]+)\/([\w\-.]+)#)?([\w-]+)$/;
7
8/**
9 * Creates a full ReadMe API Registry URL.
10 *
11 * @param uuid - ReadMe UUID
12 * @returns The full ReadMe API registry URL.
13 */
14export function getRegistryUrl(uuid: string): string {
15 return `https://dash.readme.com/api/v1/api-registry/${uuid}`;
16}
17
18export type Parsed = {
19 organization?: string;
20 project?: string;
21 uuid: string;
22};
23
24const namespace = 'readme';
25
26/**
27 * Parses a ReadMe input string and extracts components.
28 *
29 * @param shorthand - ReadMe format string (@org/project#uuid or uuid)
30 * @returns Parsed ReadMe input components
31 * @throws Error if the input format is invalid
32 */
33export function parseShorthand(shorthand: string): Parsed {
34 const match = shorthand.match(registryRegExp);
35
36 if (!match) {
37 throw new Error(
38 `Invalid ReadMe shorthand format. Expected "${namespace}:@organization/project#uuid" or "${namespace}:uuid", received: ${namespace}:${shorthand}`,
39 );
40 }
41
42 const [, , organization, project, uuid] = match;
43
44 if (!uuid) {
45 throw new Error('The ReadMe UUID cannot be empty.');
46 }
47
48 const result: Parsed = {
49 organization,
50 project,
51 uuid,
52 };
53
54 return result;
55}
56
57/**
58 * Transforms a ReadMe shorthand string to the corresponding API URL.
59 *
60 * @param input - ReadMe format string
61 * @returns The ReadMe API Registry URL
62 */
63export function inputToReadmePath(input: string): Partial<Input> {
64 const shorthand = input.slice(`${namespace}:`.length);
65 const parsed = parseShorthand(shorthand);
66 return {
67 ...parsed,
68 path: getRegistryUrl(parsed.uuid),
69 registry: 'readme',
70 };
71}