Skip to content

Commit a2b179c

Browse files
committed
feat: enhance content type handling and add post-bump check
This commit updates the Axios and React Query generators to prioritize `application/json` over `application/ld+json` for request and response bodies. Additionally, it introduces a new hook in the release-it configuration to run checks after version bumping, ensuring code quality is maintained.
1 parent 7451a8a commit a2b179c

File tree

5 files changed

+22
-22
lines changed

5 files changed

+22
-22
lines changed

.release-it.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"hooks": {
1616
"before:init": ["npm run build", "npm run check"],
17+
"after:bump": ["npm run check"],
1718
"after:release": "echo Successfully released ${name} v${version} to npm."
1819
}
1920
}

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
"license": "MIT",
55
"description": "Generate Axios API clients and React Query options from OpenAPI specifications",
66
"exports": "./dist/index.js",
7-
"files": [
8-
"src",
9-
"dist"
10-
],
7+
"files": ["src", "dist"],
118
"author": {
129
"name": "Oliver Winter",
1310
"email": "[email protected]"

src/generator/clientGenerator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
5151
const responseObj = response as OpenAPIV3.ResponseObject;
5252
const desc = "description" in responseObj ? responseObj.description : "";
5353
const contentType =
54-
responseObj.content?.["application/json"]?.schema ??
5554
responseObj.content?.["application/ld+json"]?.schema ??
55+
responseObj.content?.["application/json"]?.schema ??
5656
responseObj.content?.["application/octet-stream"]?.schema;
5757

5858
const typeName = pascalCase(`${operationId}Response${code}`);
@@ -80,8 +80,8 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
8080

8181
const content =
8282
requestBody && "content" in requestBody
83-
? (requestBody.content?.["application/json"]?.schema ??
84-
requestBody.content?.["application/ld+json"]?.schema ??
83+
? (requestBody.content?.["application/ld+json"]?.schema ??
84+
requestBody.content?.["application/json"]?.schema ??
8585
requestBody.content?.["application/octet-stream"]?.schema)
8686
: undefined;
8787

@@ -138,7 +138,7 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
138138
requestBodySchema?.properties
139139
? `const bodyData = {
140140
${Object.entries(requestBodySchema.properties)
141-
.map(([key]) => `${key}: data.${key}`)
141+
.map(([key]) => `["${key}"]: data["${key}"]`)
142142
.join(",\n ")}
143143
};`
144144
: "",

src/generator/reactQueryGenerator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document
2222

2323
const content =
2424
requestBody && "content" in requestBody
25-
? (requestBody.content?.["application/json"]?.schema ??
26-
requestBody.content?.["application/ld+json"]?.schema ??
25+
? (requestBody.content?.["application/ld+json"]?.schema ??
26+
requestBody.content?.["application/json"]?.schema ??
2727
requestBody.content?.["application/octet-stream"]?.schema)
2828
: undefined;
2929
// Get required parameter names from both parameters and request body

src/generator/schemaGenerator.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,28 @@ function getTypeFromSchema(
1919
const refType = schema.$ref.split("/").pop();
2020
return sanitizeTypeName(refType as string);
2121
}
22+
const nullable = schema.nullable ? " | null" : "";
2223

2324
// Handle enum types properly
2425
if (schema.enum) {
25-
return schema.enum.map((e) => (typeof e === "string" ? `'${e}'` : e)).join(" | ");
26+
return schema.enum.map((e) => (typeof e === "string" ? `'${e}'` : e)).join(" | ") + nullable;
2627
}
2728

2829
switch (schema.type) {
2930
case "string":
3031
if ("format" in schema && schema.format === "binary") {
31-
return "string | { name?: string; type?: string; uri: string }";
32+
return `string | { name?: string; type?: string; uri: string }${nullable}`;
3233
}
33-
return "string";
34+
35+
return `string${nullable}`;
3436
case "number":
3537
case "integer":
36-
return "number";
38+
return `number${nullable}`;
3739
case "boolean":
38-
return "boolean";
40+
return `boolean${nullable}`;
3941
case "array": {
4042
const itemType = getTypeFromSchema(schema.items, context);
41-
return `Array<${itemType}>`;
43+
return `Array<${itemType}>${nullable}`;
4244
}
4345
case "object":
4446
if (schema.properties) {
@@ -50,18 +52,18 @@ function getTypeFromSchema(
5052
return ` ${safeName}${isRequired ? "" : "?"}: ${propertyType};`;
5153
})
5254
.join("\n");
53-
return `{\n${properties}\n}`;
55+
return `{${properties}\n}${nullable}`;
5456
}
5557
if (schema.additionalProperties) {
5658
const valueType =
5759
typeof schema.additionalProperties === "boolean"
5860
? "any"
5961
: getTypeFromSchema(schema.additionalProperties, context);
60-
return `Record<string, ${valueType}>`;
62+
return `Record<string, ${valueType}>${nullable}`;
6163
}
62-
return "Record<string, any>";
64+
return `Record<string, any>${nullable}`;
6365
default:
64-
return "any";
66+
return `any${nullable}`;
6567
}
6668
}
6769

@@ -114,9 +116,9 @@ export function generateTypeDefinitions(spec: OpenAPIV3.Document): string {
114116
if (operationObject.requestBody) {
115117
const content = (operationObject.requestBody as OpenAPIV3.RequestBodyObject).content;
116118
const jsonContent =
119+
content["application/ld+json"] ??
117120
content["application/json"] ??
118121
content["multipart/form-data"] ??
119-
content["application/ld+json"] ??
120122
content["application/octet-stream"];
121123
if (jsonContent?.schema) {
122124
const typeName = `${operationId}Request`;
@@ -129,8 +131,8 @@ export function generateTypeDefinitions(spec: OpenAPIV3.Document): string {
129131
for (const [code, response] of Object.entries(operationObject.responses)) {
130132
const responseObj = response as OpenAPIV3.ResponseObject;
131133
const content =
132-
responseObj.content?.["application/json"] ??
133134
responseObj.content?.["application/ld+json"] ??
135+
responseObj.content?.["application/json"] ??
134136
responseObj.content?.["application/octet-stream"];
135137
if (content?.schema) {
136138
const typeName = `${operationId}Response${code}`;

0 commit comments

Comments
 (0)