forked from camptocamp/ogc-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-utils.js
More file actions
101 lines (96 loc) · 2.95 KB
/
api-utils.js
File metadata and controls
101 lines (96 loc) · 2.95 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import API from './data/api.json';
export function formatClassToString(classObj) {
return classObj.name;
}
export function formatFunctionToString(functionObj) {
const params = (functionObj?.signatures?.[0]?.parameters || [])
.map(
(param) =>
`${param.flags?.isRest ? '...' : ''}${param.name}${
param.flags?.optional ? '?' : ''
}: ${formatTypeToString(param.type)}`
)
.join(', ');
let typeParams = '';
if (functionObj?.signatures?.[0]?.typeParameter?.length) {
typeParams = `<${functionObj.signatures[0].typeParameter
.map((t) => t.name)
.join(', ')}>`;
}
return `${functionObj.name}${typeParams}(${params})`;
}
export function formatConstructorToString(classObj, functionObj) {
const params = (functionObj?.signatures?.[0]?.parameters || [])
.map(
(param) =>
`${param.flags?.isRest ? '...' : ''}${param.name}: ${formatTypeToString(
param.type
)}`
)
.join(', ');
return `new ${classObj.name}(${params})`;
}
export function formatTypeToString(typeObj) {
if (!typeObj) return 'void';
if (typeObj.type === 'array') {
return `${formatTypeToString(typeObj.elementType)}[]`;
}
if (typeObj.type === 'union') {
return typeObj.types.map(formatTypeToString).join(' | ');
}
if (typeObj.type === 'intersection') {
return typeObj.types.map(formatTypeToString).join(' & ');
}
if (typeObj.type === 'literal') {
return `'${typeObj.value}'`;
}
if (
typeObj.type === 'reflection' &&
typeObj.declaration?.signatures?.length
) {
const returnType = typeObj.declaration?.signatures?.[0]?.type;
return `() => ${formatTypeToString(returnType)}`;
}
if (typeObj.type === 'reference') {
switch (typeObj.name) {
case 'Record':
return `Record\\<${formatTypeToString(
typeObj.typeArguments[0]
)}, ${formatTypeToString(typeObj.typeArguments[1])}\\>`;
case 'Response':
return `[Response](https://developer.mozilla.org/en-US/docs/Web/API/Response)`;
case 'Promise':
return `Promise<${formatTypeToString(typeObj.typeArguments[0])}>`;
}
const ref = API.children.find((el) => el.id === typeObj.target);
if (ref) {
return `[${ref.name}](#/api/${ref.name})`;
}
return typeObj.name;
}
if (typeObj.type === 'query') {
const ref = API.children.find((el) => el.id === typeObj.queryType.target);
if (ref) {
return ref;
}
return typeObj.queryType.name;
}
if (typeObj.type === 'indexedAccess') {
return `${formatTypeToString(typeObj.objectType)}[${formatTypeToString(
typeObj.indexType
)}]`;
}
if (typeObj.type === 'tuple') {
return `[${typeObj.elements.map(formatTypeToString).join(', ')}]`;
}
return typeObj.name;
}
export function getDescription(obj) {
return (
obj?.getSignature?.comment?.summary ||
obj?.signatures?.[0]?.comment?.summary ||
obj?.comment?.summary
)
?.map((s) => s.text)
.join('');
}