forked from AlroviOfficial/RoZod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzodios_endpoints.js
119 lines (105 loc) · 4.29 KB
/
zodios_endpoints.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
const fs = require('fs');
const path = require('path');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const pLimit = require('p-limit');
const { generateZodClientFromOpenAPI } = require('@alexop/openapi-zod-client');
const SwaggerParser = require('@apidevtools/swagger-parser');
const limit = pLimit(2);
const FOLDER_OPENAPI = 'openapi';
const FOLDER_ZODIOS = 'src/endpoints';
const handlebars = require('handlebars').create();
handlebars.registerHelper("ifeq", function (a, b, options) {
if (a === b) {
return options.fn(this);
}
return options.inverse(this);
});
handlebars.registerHelper("ifNotEmptyObj", function (obj, options) {
if (typeof obj === "object" && Object.keys(obj).length > 0) {
return options.fn(this);
}
return options.inverse(this);
});
handlebars.registerHelper("toCamelCase", function (input) {
if (/^[a-z][a-zA-Z0-9]*$/.test(input)) {
return input
}
const words = input.split(/[\s_-]/);
return words
.map((word, index) => {
if (index === 0) {
return word.toLowerCase();
}
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
})
.join("");
});
handlebars.registerHelper('toUpperCase', (str) => str.toUpperCase());
console.log('Generating OpenAPI files from Swagger files...');
const urls = fs.readFileSync('urls.txt', 'utf-8')
.split('\n')
.filter((url) => url.trim() !== '');
Promise.all(
urls.map((url) =>
limit(async () => {
if (url.trim()) {
console.log(`Converting ${url}`);
const [, subdomain, domain] = url.match(/https:\/\/([^\.]+)\.(.+)\//);
const apiName = url.split('/').slice(-1)[0];
await util.promisify(exec)(
`java -jar swagger-codegen-cli-3.0.42.jar generate -l openapi-yaml -i ${url} -o "${FOLDER_OPENAPI}/${subdomain}${apiName}"`,
);
}
}),
),
).then(() => {
console.log('Generating Zodios endpoints...');
const openApiFiles = fs.readdirSync(FOLDER_OPENAPI).filter((file) => {
const folderContents = fs.readdirSync(`${FOLDER_OPENAPI}/${file}`);
return folderContents.some((file) => file.endsWith('.yaml'));
});
openApiFiles.forEach(async (folder) => {
console.log(`Generating Zodios for ${folder}`);
const fileName = path.basename(folder, '.yaml');
const matchingUrl = urls.find((url) => {
const apiVersion = url.match(/\/v(\d+)/)[1];
return url.includes(`${fileName.replace(/v\d+$/, '')}.roblox.com/docs/json/v${apiVersion}`);
});
if (matchingUrl) {
const [, subdomain, domain] = matchingUrl.match(/https?:\/\/([^\.]+)\.([^\/]+)/);
const openApiDoc = await SwaggerParser.parse(`${FOLDER_OPENAPI}/${folder}/openapi.yaml`);
generateZodClientFromOpenAPI({
openApiDoc: openApiDoc,
templatePath: './template.hbs',
distPath: `${FOLDER_ZODIOS}/${fileName}.ts`,
handlebars,
options: {
baseUrl: `https://${subdomain}.${domain}`,
withDeprecatedEndpoints: true,
withImplicitRequiredProps: true,
withDefaultValues: true,
withAlias: (path, method, OpenAPIOperation) => {
// Returns a string that will be used as the alias for the endpoint
// Example: GET /v1/private-servers/enabled-in-universe/:universeId => getPrivateServersEnabledInUniverse
const url = path.replace(/\/v\d+\//, '/').replace(/\/:(\w+)/g, '/$1');
const urlParts = url.split(/[/-]/).filter((part) => part !== '');
const methodParts = method.split(' ');
const methodPart = methodParts[0].toLowerCase();
const endpointName = urlParts
.map((part) => part.replace(/(\w)(\w*)/, (g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase()))
.join('')
.replace(/[{}]/g, '');
return `${methodPart}${endpointName}`;
},
},
});
}
});
// Take all the endpoint files and move them to ./lib/endpoints but renamed to their name plus .d.ts
const endpointFiles = fs.readdirSync(FOLDER_ZODIOS).filter((file) => file.endsWith('.ts'));
endpointFiles.forEach((file) => {
const fileName = path.basename(file, '.ts');
fs.writeFileSync(`./lib/endpoints/${fileName}.d.ts`, fs.readFileSync(`./src/endpoints/${fileName}.ts`, 'utf-8'));
});
});