-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcomplex.ts
More file actions
60 lines (52 loc) · 1.64 KB
/
complex.ts
File metadata and controls
60 lines (52 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import type {
ObjectSchema,
RefSchema,
RefUnionSchema,
XrpcParametersSchema,
} from '../schema.js';
import {
toNamespace,
toUpper,
writeJsdoc,
sortName,
sortPropertyKeys,
} from '../../utils/index.js';
import { resolveType } from './type.js';
export function resolveRefType(def: RefSchema): string {
const [ns, ref] = def.ref.split('#');
return (ns ? `${toNamespace(ns)}.` : '') + (ref ? toUpper(ref) : 'Main');
}
export function resolveUnionType(def: RefUnionSchema): string {
// empty refs ([]) never has any type
if (def.refs.length === 0) {
return 'never';
}
const refs = def.refs.toSorted(sortName).map((raw) => {
const [ns, ref] = raw.split('#');
return (ns ? `${toNamespace(ns)}.` : '') + (ref ? toUpper(ref) : 'Main');
});
return `TypeUnion<${refs.join('|')}>`;
}
export function resolveObjectType(
def: ObjectSchema | XrpcParametersSchema,
type: 'object' | 'params',
nsid: string,
): { value: string; descriptions: string[] } {
const required = def.required;
const nullable = type === 'object' && 'nullable' in def ? def.nullable : [];
const properties = def.properties;
const propKeys = sortPropertyKeys(Object.keys(properties), required);
let chunk = '{';
for (const prop of propKeys) {
const isOptional = !required || !required.includes(prop);
const isNullable = nullable?.includes(prop);
const { value, descriptions } = resolveType(
`${nsid}/${prop}`,
properties[prop],
);
chunk += writeJsdoc(descriptions);
chunk += `${prop}${isOptional ? '?' : ''}:${value}${isNullable ? '| null' : ''};`;
}
chunk += '}';
return { value: chunk, descriptions: [] };
}