speedruncom.js 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,70 @@
1
+ import { Project, SourceFile, InterfaceDeclaration, OptionalKind, MethodDeclarationStructure } from 'ts-morph';
2
+
3
+ const project = new Project({
4
+ tsConfigFilePath: "tsconfig.json",
5
+ });
6
+
7
+ const isInterfaceEmpty = (interfaceName: string, sourceFile: SourceFile) => {
8
+ const declarations = sourceFile.getExportedDeclarations().get(interfaceName);
9
+ if (!declarations || declarations.length === 0) return false;
10
+
11
+ const decl = declarations[0];
12
+ if (!decl || !decl.getKindName || decl.getKindName() !== "InterfaceDeclaration") return false;
13
+
14
+ const props = (decl as InterfaceDeclaration).getProperties();
15
+ return props.length === 0;
16
+ };
17
+
18
+ const isInterfaceAllOptional = (interfaceName: string, sourceFile: SourceFile) => {
19
+ const interfaceDecl = sourceFile.getInterfaceOrThrow(interfaceName);
20
+ return interfaceDecl.getProperties().every(prop => prop.hasQuestionToken());
21
+ };
22
+
23
+ const baseClient = project.getSourceFileOrThrow('src/BaseClient.ts');
24
+ const clientFile = project.createSourceFile('src/Client.ts', baseClient.getFullText(), { overwrite: true });
25
+ const clientClass = clientFile.getClasses()[1];
26
+
27
+ const getEndpointsFile = project.getSourceFileOrThrow('src/endpoints/endpoints.get.ts');
28
+ const postEndpointsFile = project.getSourceFileOrThrow('src/endpoints/endpoints.post.ts');
29
+ const responsesFile = project.getSourceFileOrThrow('src/responses.ts');
30
+
31
+ const getEndpointNames = Array.from(getEndpointsFile.getExportedDeclarations().keys());
32
+ const postEndpointNames = Array.from(postEndpointsFile.getExportedDeclarations().keys());
33
+
34
+ const responseNames = new Set(Array.from(responsesFile.getExportedDeclarations().keys()));
35
+
36
+ const makeMethod = (name: string, isStatic: boolean, returnType: string, isEmpty: boolean, interfaces: string, isOptional: boolean) => {
37
+ const method: OptionalKind<MethodDeclarationStructure> = {
38
+ name,
39
+ isStatic,
40
+ isAsync: true,
41
+ returnType,
42
+ statements: [`return await this.request('${name}', params);`]
43
+ }
44
+ if (!isEmpty) method.parameters = [{
45
+ name: 'params',
46
+ type: `${interfaces}.${name}`,
47
+ hasQuestionToken: isOptional
48
+ }];
49
+
50
+ clientClass.addMethod(method);
51
+ }
52
+
53
+ for (const endpointName of getEndpointNames) {
54
+ const returnType = responseNames.has(endpointName) ? `Promise<Readonly<Responses.${endpointName}>>` : 'Promise<void>';
55
+ const isEmpty = isInterfaceEmpty(endpointName, getEndpointsFile);
56
+ const isAllOptional = isInterfaceAllOptional(endpointName, getEndpointsFile);
57
+
58
+ makeMethod(endpointName, false, returnType, isEmpty, 'GetEndpoints', isAllOptional);
59
+ makeMethod(endpointName, true, returnType, isEmpty, 'GetEndpoints', isAllOptional);
60
+ }
61
+
62
+ for (const endpointName of postEndpointNames) {
63
+ const returnType = responseNames.has(endpointName) ? `Promise<Readonly<Responses.${endpointName}>>` : 'Promise<void>';
64
+ const isEmpty = isInterfaceEmpty(endpointName, postEndpointsFile);
65
+ const isAllOptional = isInterfaceAllOptional(endpointName, postEndpointsFile);
66
+
67
+ makeMethod(endpointName, false, returnType, isEmpty, 'PostEndpoints', isAllOptional);
68
+ }
69
+
70
+ clientFile.saveSync();