-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
127 lines (102 loc) · 3.71 KB
/
build.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import fetch from 'node-fetch'
import fs from 'node:fs/promises'
const OpenAPI = await fetch(
'https://raw.githubusercontent.com/zaunchat/documentation/master/openapi.json',
)
.then((r) => r.json());
const components = [];
const routes = [];
const isReference = (x) =>
typeof x === 'object' && '$ref' in x;
for (let [name, schema] of Object.entries(OpenAPI.components?.schemas)) {
if (isReference(schema)) {
schema = OpenAPI.components.schemas[schema.$ref.split('/').pop()];
}
components.push(`export type API${name} = ${getType(schema)}`);
}
function getType(schema) {
if (schema == null) return 'undefined';
if (isReference(schema)) {
return 'API' + schema.$ref.split('/').pop();
}
const nullable = (type) =>
type + (schema.nullable ? ' | null' : '');
const extractedTypes = {};
if (schema.items) {
if (Array.isArray(schema.items)) {
return '[' + schema.items.map((t) => getType(t)).join(', ') + ']';
}
return nullable(getType(schema.items) + '[]');
}
if (schema.allOf) {
return nullable(
'( ' + (schema.allOf).map((x) =>
getType(x)
).join(' & ') + ' )',
);
}
if (schema.anyOf) {
return '( ' + schema.anyOf.map((x) => getType(x)).join(' | ') + ' )';
}
if (schema.oneOf) {
return '( ' + schema.oneOf.map((x) => getType(x)).join(' | ') + ' )';
}
if (schema.enum) return schema.enum.map((e) => `'${e}'`).join(' | ');
if (
schema.additionalProperties &&
typeof schema.additionalProperties !== 'boolean'
) {
return `{ [key: string]: ${getType(schema.additionalProperties)} }`;
}
if (schema.properties) {
for (const [name, prop] of Object.entries(schema.properties)) {
extractedTypes[schema.required?.includes(name) ? name : `${name}?`] =
getType(prop);
}
}
if (schema.type === 'integer') schema.type = 'number';
if (schema.type === 'number' && schema.example != null) {
schema.type = schema.example;
}
if (schema.type && Object.keys(extractedTypes).length === 0) {
extractedTypes.type = schema.type;
if (schema.nullable) extractedTypes.type = nullable(extractedTypes.type);
return extractedTypes.type;
}
return '{ ' + Object.entries(extractedTypes).map(([key, value]) => {
return `${key.replace('r#', '')}: ${value},`;
}).join(' ') + ' }';
}
for (const [path, methods] of Object.entries(OpenAPI.paths)) {
for (const [method, data] of Object.entries(methods)) {
if (
!['GET', 'POST', 'DELETE', 'PATCH', 'PUT'].includes(method.toUpperCase())
) {
continue;
}
const schema = data.responses?.['200']?.content?.['application/json']
?.schema;
const typedResponse = getType(schema);
const typedPath = path.replace(
/\{(target|id|user|code|role_id|member|msg|server|message|username|_target|bot|group|channel|invite|session)(_id)?\}/g,
'${string}',
);
routes.push(`{
path: \`${typedPath}\`
parts: ${typedPath.split('/').length - 1}
method: '${method.toUpperCase()}'
response: ${typedResponse}
}`);
}
}
await fs.writeFile(
'src/lib.d.ts',
`
${components.join('\n\n')}
export type Routes = ${routes.join(' | ')}
export type GetRoutes = Extract<Routes, { method: 'GET' }>
export type DeleteRoutes = Extract<Routes, { method: 'DELETE' }>
export type PostRoutes = Extract<Routes, { method: 'POST' }>
export type PatchRoutes = Extract<Routes, { method: 'PATCH' }>
export type PutRoutes = Extract<Routes, { method: 'PUT' }>`,
);