Skip to content

Commit 2154aa4

Browse files
committed
Use biome to format and lint code
1 parent 304682c commit 2154aa4

9 files changed

Lines changed: 168 additions & 94 deletions

File tree

code-generator/biome.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/2.4.10/schema.json",
3+
"formatter": {
4+
"indentStyle": "space"
5+
},
6+
"linter": {
7+
"rules": {
8+
"recommended": true
9+
}
10+
}
11+
}

code-generator/package.json

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
{
2-
"type": "module",
3-
"license": "MIT",
4-
"scripts": {
5-
"definitions": "rm -rf ../src/main/java/com/ringcentral/definitions/* && yarn tsx -r dotenv-override-true/config ./src/definitions.ts",
6-
"paths": "rm -rf ../src/main/java/com/ringcentral/paths/* && yarn tsx -r dotenv-override-true/config ./src/paths.ts",
7-
"samples": "yarn tsx -r dotenv-override-true/config ./src/samples.ts",
8-
"generate": "yarn definitions && yarn paths && yarn samples"
9-
},
10-
"dependencies": {
11-
"change-case": "4.1.2",
12-
"js-yaml": "^4.1.1",
13-
"lower-case-first": "^3.0.0",
14-
"openapi-types": "^12.1.3",
15-
"ramda": "^0.32.0",
16-
"ringcentral-open-api-parser": "^0.17.1"
17-
},
18-
"devDependencies": {
19-
"@types/js-yaml": "^4.0.9",
20-
"@types/node": "^25.5.2",
21-
"@types/ramda": "^0.31.1",
22-
"dotenv-override-true": "^6.2.2",
23-
"tsx": "^4.21.0",
24-
"typescript": "^6.0.2",
25-
"yarn-upgrade-all": "^0.7.5"
26-
},
27-
"yarn-upgrade-all": {
28-
"ignore": [
29-
"change-case"
30-
]
31-
},
32-
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
2+
"type": "module",
3+
"license": "MIT",
4+
"scripts": {
5+
"definitions": "rm -rf ../src/main/java/com/ringcentral/definitions/* && yarn tsx -r dotenv-override-true/config ./src/definitions.ts",
6+
"paths": "rm -rf ../src/main/java/com/ringcentral/paths/* && yarn tsx -r dotenv-override-true/config ./src/paths.ts",
7+
"samples": "yarn tsx -r dotenv-override-true/config ./src/samples.ts",
8+
"generate": "yarn definitions && yarn paths && yarn samples"
9+
},
10+
"dependencies": {
11+
"change-case": "4.1.2",
12+
"js-yaml": "^4.1.1",
13+
"lower-case-first": "^3.0.0",
14+
"openapi-types": "^12.1.3",
15+
"ramda": "^0.32.0",
16+
"ringcentral-open-api-parser": "^0.17.1"
17+
},
18+
"devDependencies": {
19+
"@biomejs/biome": "^2.4.10",
20+
"@types/js-yaml": "^4.0.9",
21+
"@types/node": "^25.5.2",
22+
"@types/ramda": "^0.31.1",
23+
"dotenv-override-true": "^6.2.2",
24+
"tsx": "^4.21.0",
25+
"typescript": "^6.0.2",
26+
"yarn-upgrade-all": "^0.7.5"
27+
},
28+
"yarn-upgrade-all": {
29+
"ignore": [
30+
"change-case"
31+
]
32+
},
33+
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
3334
}

code-generator/src/definitions.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import fs from "fs";
2-
import path from "path";
3-
import { fileURLToPath } from "url";
4-
import type { Field } from "ringcentral-open-api-parser/lib/types.js";
5-
6-
import { escapeJavaDoc } from "./utils.js";
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { fileURLToPath } from "node:url";
4+
import type { Field } from "ringcentral-open-api-parser";
75
import { parsed } from "./parser.js";
6+
import { escapeJavaDoc } from "./utils.js";
87

98
const __dirname = path.dirname(fileURLToPath(import.meta.url));
109

@@ -21,9 +20,11 @@ const normalizeField = (f: Field): Field => {
2120
} else if (f.type === "integer") {
2221
f.type = "Long";
2322
} else if (f.type === "array") {
24-
f.type = `${normalizeField(f.items!).type}[]`;
23+
const itemType = f.items ? normalizeField(f.items).type : "Object";
24+
f.type = `${itemType}[]`;
2525
} else if (f.type === "dict") {
26-
f.type = `java.util.Map<String, ${normalizeField(f.items!).type}>`;
26+
const itemType = f.items ? normalizeField(f.items).type : "Object";
27+
f.type = `java.util.Map<String, ${itemType}>`;
2728
} else if (f.type === "boolean") {
2829
f.type = "Boolean";
2930
} else if (f.type === "string") {
@@ -45,7 +46,7 @@ const generateField = (f: Field, modelName: string) => {
4546
p += ` @SerializedName("${f.name}")\n`;
4647
f.name = f.name.replace(
4748
/-([a-z])/g,
48-
(match, p1: string) => p1.toUpperCase(),
49+
(_match, p1: string) => p1.toUpperCase(),
4950
);
5051
}
5152
if (f.name.includes(":")) {
@@ -60,8 +61,8 @@ const generateField = (f: Field, modelName: string) => {
6061
}`;
6162

6263
p = ` */\n${p}`;
63-
if (f.enum || (f.items || {}).enum) {
64-
p = ` * Enum: ${(f.enum || (f.items || {}).enum)!.join(", ")}\n ${p}`;
64+
if (f.enum || f.items?.enum) {
65+
p = ` * Enum: ${(f.enum || f.items?.enum)?.join(", ")}\n ${p}`;
6566
}
6667
if (f.default) {
6768
p = ` * Default: ${f.default}\n ${p}`;
@@ -83,8 +84,8 @@ const generateField = (f: Field, modelName: string) => {
8384
}
8485
if (f.description) {
8586
p = ` * ${
86-
escapeJavaDoc(f.description)!
87-
.trim()
87+
escapeJavaDoc(f.description)
88+
?.trim()
8889
.split("\n")
8990
.join("\n * ")
9091
}\n ${p}`;
@@ -99,7 +100,7 @@ parsed.models.forEach((model) => {
99100
? "\n /**\n" +
100101
model.description
101102
.split("\n")
102-
.map((line) => "* " + line)
103+
.map((line) => `* ${line}`)
103104
.join("\n") +
104105
"\n*/"
105106
: ""
@@ -109,8 +110,8 @@ public class ${model.name}
109110
${model.fields.map((f) => generateField(f, model.name)).join("\n\n ")}
110111
}`;
111112
if (code.includes("@SerializedName(")) {
112-
code = "import com.google.gson.annotations.SerializedName;\n\n" + code;
113+
code = `import com.google.gson.annotations.SerializedName;\n\n${code}`;
113114
}
114-
code = "package com.ringcentral.definitions;\n\n" + code;
115+
code = `package com.ringcentral.definitions;\n\n${code}`;
115116
fs.writeFileSync(path.join(outputDir, `${model.name}.java`), code);
116117
});

code-generator/src/parser.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
import { prepareSpec } from "ringcentral-open-api-parser";
22

3-
export const parsed = prepareSpec(process.env.spec_file_path!);
3+
const specFilePath = process.env.spec_file_path;
4+
5+
if (!specFilePath) {
6+
throw new Error("Missing required env var: spec_file_path");
7+
}
8+
9+
export const parsed = prepareSpec(specFilePath);

code-generator/src/paths.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import fs from "fs";
2-
import path from "path";
1+
import fs from "node:fs";
2+
import path from "node:path";
33
import { camelCase, capitalCase, pascalCase } from "change-case";
44
import * as R from "ramda";
5-
import type { Operation } from "ringcentral-open-api-parser/lib/types.js";
6-
7-
import { capitalizeFirstLetter, escapeJavaDoc, patchSrcFile } from "./utils.js";
5+
import type { Operation } from "ringcentral-open-api-parser";
86
import { parsed } from "./parser.js";
7+
import { capitalizeFirstLetter, escapeJavaDoc, patchSrcFile } from "./utils.js";
98

109
const outputDir = "../src/main/java/com/ringcentral/paths";
1110

@@ -162,13 +161,17 @@ const generateOperationMethod = (
162161
if (operation.bodyType) {
163162
methodParams.push(
164163
`${
165-
capitalizeFirstLetter(operation.bodyType)
164+
capitalizeFirstLetter(
165+
operation.bodyType,
166+
)
166167
} ${operation.bodyParameters}`,
167168
);
168169
} else {
169170
methodParams.push(
170171
`${
171-
capitalizeFirstLetter(operation.bodyParameters)
172+
capitalizeFirstLetter(
173+
operation.bodyParameters,
174+
)
172175
} ${operation.bodyParameters}`,
173176
);
174177
}
@@ -210,7 +213,9 @@ const generateOperationMethod = (
210213
}\n`;
211214
}
212215
result += ` okhttp3.ResponseBody rb = this.rc.${operation.method}(${
213-
requestParams.join(", ")
216+
requestParams.join(
217+
", ",
218+
)
214219
});`;
215220
if (responseType === "String") {
216221
result += "\n return rb.string();";
@@ -255,6 +260,11 @@ for (const item of parsed.paths) {
255260
return p;
256261
})
257262
.map((p) => pascalCase(p));
263+
const lastPath = R.last(item.paths);
264+
const lastItemPath = R.last(itemPaths);
265+
if (!lastPath || !lastItemPath) {
266+
continue;
267+
}
258268
const code = `
259269
package com.ringcentral.paths.${
260270
itemPaths
@@ -278,7 +288,7 @@ public class Index
278288
${
279289
generatePathMethod(
280290
item.parameter,
281-
R.last(item.paths)!,
291+
lastPath,
282292
itemPaths.length > 1,
283293
item.noParentParameter,
284294
)
@@ -300,7 +310,7 @@ ${
300310
itemPaths
301311
.join(".")
302312
.toLowerCase()
303-
}.Index ${camelCase(R.last(itemPaths)!)}(${
313+
}.Index ${camelCase(lastItemPath)}(${
304314
item.parameter ? `String ${item.parameter}` : ""
305315
})
306316
{
@@ -316,9 +326,9 @@ ${
316326
itemPaths
317327
.join(".")
318328
.toLowerCase()
319-
}.Index ${camelCase(R.last(itemPaths)!)}()
329+
}.Index ${camelCase(lastItemPath)}()
320330
{
321-
return this.${camelCase(R.last(itemPaths)!)}(${
331+
return this.${camelCase(lastItemPath)}(${
322332
item.defaultParameter ? `"${item.defaultParameter}"` : "null"
323333
});
324334
}

code-generator/src/samples.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import fs from "fs";
2-
import path from "path";
3-
import { fileURLToPath } from "url";
4-
import * as R from "ramda";
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { fileURLToPath } from "node:url";
54
import { camelCase } from "change-case";
6-
7-
import { capitalizeFirstLetter } from "./utils.js";
85
import { parsed } from "./parser.js";
6+
import { capitalizeFirstLetter } from "./utils.js";
97

108
const __dirname = path.dirname(fileURLToPath(import.meta.url));
119

1210
const markdown = ["# RingCentral Java SDK Code Samples"];
1311

14-
const paths = R.sortBy(
15-
R.path(["operations", 0, "endpoint"]) as any,
16-
parsed.paths,
17-
);
12+
const paths = [...parsed.paths].sort((a, b) => {
13+
const endpointA = a.operations[0]?.endpoint ?? "";
14+
const endpointB = b.operations[0]?.endpoint ?? "";
15+
return endpointA.localeCompare(endpointB);
16+
});
1817

1918
const buildPath = (s: string): string => {
2019
const tokens = s.split("/").filter((t) => t.length > 0);
@@ -65,9 +64,9 @@ rc.authorize(jwtToken);`,
6564
responseType = "byte[]";
6665
}
6766
markdown.push(
68-
`${responseType} result = rc${
69-
buildPath(operation.endpoint)
70-
}.${operation.method2}(${parameters.join(", ")});`,
67+
`${responseType} result = rc${buildPath(
68+
operation.endpoint,
69+
)}.${operation.method2}(${parameters.join(", ")});`,
7170
);
7271
markdown.push("rc.revoke();");
7372
markdown.push("```\n");
@@ -95,15 +94,11 @@ rc.authorize(jwtToken);`,
9594

9695
for (const parameter of parameters) {
9796
markdown.push(
98-
`- \`${parameter}\` is of type [${
99-
capitalizeFirstLetter(
100-
parameter,
101-
)
102-
}](./src/main/java/com/ringcentral/definitions/${
103-
capitalizeFirstLetter(
104-
parameter,
105-
)
106-
}.java)`,
97+
`- \`${parameter}\` is of type [${capitalizeFirstLetter(
98+
parameter,
99+
)}](./src/main/java/com/ringcentral/definitions/${capitalizeFirstLetter(
100+
parameter,
101+
)}.java)`,
107102
);
108103
}
109104

@@ -124,12 +119,10 @@ rc.authorize(jwtToken);`,
124119
}
125120

126121
markdown.push(
127-
`\n[Try it out](https://developer.ringcentral.com/api-reference#${
128-
operation.tags![0].replace(
129-
/ /g,
130-
"-",
131-
)
132-
}-${operation.operationId}) in API Explorer.`,
122+
`\n[Try it out](https://developer.ringcentral.com/api-reference#${operation.tags?.[0].replace(
123+
/ /g,
124+
"-",
125+
)}-${operation.operationId}) in API Explorer.`,
133126
);
134127
}
135128
}

code-generator/src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import path from "path";
2-
import fs from "fs";
3-
import { fileURLToPath } from "url";
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { fileURLToPath } from "node:url";
44

55
const __dirname = path.dirname(fileURLToPath(import.meta.url));
66

code-generator/tsconfig.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,5 @@
88
"forceConsistentCasingInFileNames": true,
99
"skipLibCheck": true
1010
},
11-
"include": [
12-
"src/**/*.ts"
13-
]
11+
"include": ["src/**/*.ts"]
1412
}

0 commit comments

Comments
 (0)