fork of hey-api/openapi-ts because I need some additional things
1import type { MaybeArray } from '@hey-api/types';
2
3import { inputToApiRegistry } from '../../utils/input';
4import { heyApiRegistryBaseUrl } from '../../utils/input/heyApi';
5import type { Input, UserInput, UserWatch, Watch } from './types';
6
7const defaultWatch: Watch = {
8 enabled: false,
9 interval: 1_000,
10 timeout: 60_000,
11};
12
13// watch only remote files
14function getWatch(input: Pick<Input, 'path' | 'watch'>): Watch {
15 let watch = { ...defaultWatch };
16
17 // we cannot watch spec passed as an object
18 if (typeof input.path !== 'string') {
19 return watch;
20 }
21
22 if (typeof input.watch === 'boolean') {
23 watch.enabled = input.watch;
24 } else if (typeof input.watch === 'number') {
25 watch.enabled = true;
26 watch.interval = input.watch;
27 } else if (input.watch) {
28 watch = {
29 ...watch,
30 ...input.watch,
31 };
32 }
33
34 return watch;
35}
36
37export function getInput(userConfig: {
38 input: MaybeArray<UserInput | Required<UserInput>['path']>;
39 watch?: UserWatch;
40}): ReadonlyArray<Input> {
41 const userInputs = userConfig.input instanceof Array ? userConfig.input : [userConfig.input];
42
43 const inputs: Array<Input> = [];
44
45 for (const userInput of userInputs) {
46 let input: Input = {
47 path: '',
48 watch: defaultWatch,
49 };
50
51 if (typeof userInput === 'string') {
52 input.path = userInput;
53 } else if (
54 userInput &&
55 (userInput.path !== undefined || userInput.organization !== undefined)
56 ) {
57 // @ts-expect-error
58 input = {
59 ...input,
60 path: heyApiRegistryBaseUrl,
61 ...userInput,
62 };
63
64 if (input.watch !== undefined) {
65 input.watch = getWatch(input);
66 }
67 } else {
68 input = {
69 ...input,
70 path: userInput,
71 };
72 }
73
74 if (typeof input.path === 'string') {
75 inputToApiRegistry(input as Input & { path: string });
76 }
77
78 if (
79 userConfig.watch !== undefined &&
80 input.watch.enabled === defaultWatch.enabled &&
81 input.watch.interval === defaultWatch.interval &&
82 input.watch.timeout === defaultWatch.timeout
83 ) {
84 input.watch = getWatch({
85 path: input.path,
86 // @ts-expect-error
87 watch: userConfig.watch,
88 });
89 }
90
91 if (input.path) {
92 inputs.push(input);
93 }
94 }
95
96 return inputs;
97}