Skip to content

Commit dccae0e

Browse files
committed
fix: improve generated files quality
1 parent 6301bee commit dccae0e

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

src/schema-to-typescript/common/core/common-http-client.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ async function convertResponseBody(
219219
throw new Error('Invalid response body type.');
220220
}
221221

222-
type ResponseByMediaType<T extends CommonHttpClientResponse<unknown>, K extends string> = T extends any
222+
/**
223+
* `extends unknown` is a trick to split the union into individual types.
224+
*/
225+
type ResponseByMediaType<T extends CommonHttpClientResponse<unknown>, K extends string> = T extends unknown
223226
? ((a: T) => void) extends (a: {
224227
mediaType: K;
225228
status: infer _Status;
@@ -387,10 +390,8 @@ export class CommonHttpClient {
387390
response.headers.forEach((value, key) => {
388391
headers[key] = value;
389392
});
390-
if (response.headers.has('set-cookie')) {
391-
if ('getSetCookie' in response.headers) {
392-
headers['set-cookie'] = (response.headers as {getSetCookie(): string[]}).getSetCookie();
393-
}
393+
if (response.headers.has('set-cookie') && 'getSetCookie' in response.headers) {
394+
headers['set-cookie'] = (response.headers as {getSetCookie(): string[]}).getSetCookie();
394395
}
395396
return {
396397
status: response.status,

src/schema-to-typescript/common/core/common-http-service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ export class CommonHttpService {
55
constructor(getClientInstance: () => commonHttpClient.CommonHttpClient) {
66
this.getClientInstance = () => {
77
const classInstance = this.constructor as typeof CommonHttpService;
8-
if (!classInstance.initialized) {
8+
if (classInstance.initialized !== true) {
99
classInstance.initialized = true;
10-
if (classInstance.initialize) {
10+
if (classInstance.initialize !== undefined) {
1111
classInstance.initialize();
1212
}
1313
}
@@ -16,5 +16,5 @@ export class CommonHttpService {
1616
};
1717
}
1818
protected static initialized: boolean | undefined;
19-
protected static initialize: () => void | undefined;
19+
protected static initialize: (() => void) | undefined;
2020
}

src/schema-to-typescript/common/validation-providers/zod-validation-provider/index.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import {
1010
Expression,
1111
expressionStatement,
1212
identifier,
13+
IfStatement,
1314
ifStatement,
1415
isIdentifier,
1516
isImportSpecifier,
1617
isVariableDeclaration,
18+
logicalExpression,
1719
memberExpression,
1820
newExpression,
1921
numericLiteral,
@@ -555,16 +557,26 @@ export class ZodValidationProvider extends ValidationProvider {
555557
otherwise = undefined;
556558
}
557559
caseEntries = Object.entries(cases);
558-
let prefIfStatement = ifStatement(
559-
buildCondition(caseEntries[0][0]),
560-
buildStatement(caseEntries[0][1], caseEntries[0][0])
561-
);
560+
const ifBlockInfos: {condition: Expression; statement: Statement}[] = caseEntries.map(([key, value]) => ({
561+
condition: buildCondition(key),
562+
statement: buildStatement(value, key)
563+
}));
564+
const resultingIfBlocks: IfStatement[] = [];
565+
while (ifBlockInfos.length > 0) {
566+
const info = ifBlockInfos.shift()!;
567+
for (let i = 0; i < ifBlockInfos.length; i++) {
568+
if (R.equals(ifBlockInfos[i].statement, info.statement)) {
569+
info.condition = logicalExpression('||', info.condition, ifBlockInfos[i].condition);
570+
ifBlockInfos.splice(i, 1);
571+
i--;
572+
}
573+
}
574+
resultingIfBlocks.push(ifStatement(info.condition, info.statement));
575+
}
576+
let prefIfStatement = resultingIfBlocks.shift()!;
562577
const result = prefIfStatement;
563-
for (let i = 1; i < caseEntries.length; i++) {
564-
const newIfStatement = ifStatement(
565-
buildCondition(caseEntries[i][0]),
566-
buildStatement(caseEntries[i][1], caseEntries[i][0])
567-
);
578+
while (resultingIfBlocks.length > 0) {
579+
const newIfStatement = resultingIfBlocks.shift()!;
568580
prefIfStatement.alternate = newIfStatement;
569581
prefIfStatement = newIfStatement;
570582
}

0 commit comments

Comments
 (0)