Skip to content

Commit 559b09d

Browse files
committed
fix: improve destructuring of input and add query support
1 parent 929d167 commit 559b09d

1 file changed

Lines changed: 70 additions & 28 deletions

File tree

lib/process-document.ts

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function createUnion(...types: (string | undefined)[]) {
4343
return type1 && type2 ? Writers.unionType(type1, type2, ...typeX) : type1;
4444
}
4545

46-
function isVoid(type: TypeAliasDeclaration | null) {
46+
function isVoidKeyword(type: TypeAliasDeclaration) {
4747
return type?.getTypeNode()?.getKindName() === 'VoidKeyword';
4848
}
4949

@@ -81,6 +81,10 @@ export async function processOpenApiDocument(
8181
},
8282
);
8383

84+
const clientFile = project.createSourceFile(join(outputDir, 'main.ts'), '', {
85+
overwrite: true,
86+
});
87+
8488
const outputTypes: (InterfaceDeclaration | TypeAliasDeclaration)[] = [];
8589

8690
const refs = await $RefParser.default.resolve(schema);
@@ -428,7 +432,7 @@ export async function processOpenApiDocument(
428432

429433
const inputBodyType = typesFile.addTypeAlias({
430434
name: pascalCase(`${classDeclaration.getName() || 'INVALID'}Body`),
431-
type: createIntersection(bodyType?.getName(), queryType?.getName()),
435+
type: bodyType?.getName() || 'void', // createIntersection(bodyType?.getName(), queryType?.getName()),
432436
isExported: true,
433437
});
434438

@@ -438,15 +442,24 @@ export async function processOpenApiDocument(
438442
// CommandInput
439443
classDeclaration
440444
.getExtends()
441-
?.addTypeArgument(isVoid(inputType) ? 'void' : inputType.getName());
445+
?.addTypeArgument(
446+
isVoidKeyword(inputType) ? 'void' : inputType.getName(),
447+
);
442448

443-
if (!isVoid(inputType)) {
449+
if (!isVoidKeyword(inputType)) {
444450
ctor.addParameter({
445451
name: 'input',
446452
type: inputType.getName(),
447453
});
448454
}
449455

456+
// if (queryType && !isVoidKeyword(queryType)) {
457+
// ctor.addParameter({
458+
// name: 'query',
459+
// type: queryType.getName(),
460+
// });
461+
// }
462+
450463
for (const queryParam of queryParameters) {
451464
const queryParameterName = camelcase(queryParam.name);
452465

@@ -546,10 +559,17 @@ export async function processOpenApiDocument(
546559
});
547560
}
548561
}
562+
549563
// body
550564
classDeclaration
551565
.getExtends()
552566
?.addTypeArgument(inputBodyType.getName());
567+
568+
// query
569+
if (queryType) {
570+
classDeclaration.getExtends()?.addTypeArgument(queryType.getName());
571+
}
572+
553573
const pathname = `\`${path
554574
.replaceAll(/\{(\w+)\}/g, camelcase)
555575
.replaceAll(/{/g, '${')}\``;
@@ -577,40 +597,59 @@ export async function processOpenApiDocument(
577597
);
578598
});
579599

580-
const restName = 'rest';
600+
const bodyName = 'body';
581601
const ctorArgName = ctor.getParameters()[0]?.getName() || 'never';
582-
const hasParams = !isVoid(paramsType);
583-
const hasRest = !isVoid(inputBodyType);
602+
// const hasParams = paramsType && !isVoidKeyword(paramsType);
603+
const hasBody = !isVoidKeyword(inputBodyType);
604+
const hasQuery = queryType && !isVoidKeyword(queryType);
605+
606+
const queryParameterNames = queryParameters.map((q) => q.name);
607+
608+
const varsToDestructure = [
609+
...pathParameterNames,
610+
...queryParameterNames,
611+
];
584612

585613
ctor.addStatements([
586-
!isVoid(inputType)
614+
!isVoidKeyword(inputType)
587615
? {
588616
kind: StructureKind.VariableStatement,
589617
declarationKind: VariableDeclarationKind.Const,
590618
declarations: [
591619
{
592620
kind: StructureKind.VariableDeclaration,
593621
initializer: ctorArgName,
594-
name: hasParams
595-
? `{${[
596-
...pathParameterNames,
597-
hasRest ? `...${restName}` : '',
598-
].join(',')} }`
599-
: restName,
622+
name:
623+
varsToDestructure.length > 0
624+
? `{${[
625+
...varsToDestructure,
626+
hasBody || hasQuery ? `...${bodyName}` : '',
627+
].join(',')} }`
628+
: bodyName,
600629
},
601630
],
602631
}
603-
: '//no input parameters',
632+
: '// no input parameters',
604633
'super();',
605634
]);
606635

607636
const superKeyword = ctor.getFirstDescendantByKind(
608637
SyntaxKind.SuperKeyword,
609638
);
639+
610640
const callExpr = superKeyword?.getParentIfKindOrThrow(
611641
SyntaxKind.CallExpression,
612642
);
613-
callExpr?.addArguments(hasRest ? [pathname, restName] : [pathname]);
643+
644+
callExpr?.addArguments(
645+
hasBody || hasQuery
646+
? [
647+
pathname,
648+
hasBody ? bodyName : 'undefined',
649+
...(hasQuery ? [`{${queryParameterNames.join(', ')}}`] : []),
650+
]
651+
: [pathname],
652+
);
614653
}
615654
}
616655
}
@@ -621,17 +660,12 @@ export async function processOpenApiDocument(
621660
// const isOutput = (t: string) => t.endsWith('Output');
622661

623662
const inputTypes = typesFile.getTypeAliases().filter((t) => isInput(t));
624-
625-
clientFile.addImportDeclaration({
626-
moduleSpecifier: typesModuleSpecifier,
627-
namedImports: [...new Set([...inputTypes, ...outputTypes])].map((t) => ({
628-
name: t.getName(),
629-
isTypeOnly: true,
630-
})),
631-
});
632-
633-
const inputUnion = createUnion(...inputTypes.map((t) => t.getName()));
634-
const outputUnion = createUnion(...outputTypes.map((t) => t.getName()));
663+
const inputUnion = createUnion(
664+
...new Set(inputTypes.map((t) => t.getName())),
665+
);
666+
const outputUnion = createUnion(
667+
...new Set(outputTypes.map((t) => t.getName())),
668+
);
635669

636670
const allInputs = inputUnion
637671
? clientFile.addTypeAlias({
@@ -665,6 +699,14 @@ export async function processOpenApiDocument(
665699
},
666700
]);
667701

702+
clientFile.addImportDeclaration({
703+
moduleSpecifier: typesModuleSpecifier,
704+
namedImports: [...new Set([...inputTypes, ...outputTypes])].map((t) => ({
705+
name: t.getName(),
706+
isTypeOnly: true,
707+
})),
708+
});
709+
668710
const clientClassDeclaration = clientFile.addClass({
669711
name: pascalCase(schema.info.title, 'RestClient'),
670712
isExported: true,
@@ -706,5 +748,5 @@ export async function processOpenApiDocument(
706748
configParam.getName(),
707749
]);
708750

709-
return { entryFile: commandsFile, typesFile, clientFile };
751+
return { commandsFile, typesFile, clientFile };
710752
}

0 commit comments

Comments
 (0)