Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
inkognitro committed Mar 23, 2024
1 parent 624983d commit 18f5a35
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/oas3/codegen/ts/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function applyEndpointCallerFunction(
},
createCode: () => {
const requestResult = requestResultTypeDefinition.createName(path);
return `(): Promise<${requestResult}> => { throw new Error('implement me!'); }`; // todo: implement
return `(): Promise<${requestResult}> { throw new Error('implement me!'); }`; // todo: implement
},
path,
requiredOutputPaths: [
Expand Down
41 changes: 22 additions & 19 deletions src/oas3/codegen/ts/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '@oas3/specification';
import {applyEndpointCallerFunction} from '@oas3/codegen/ts/endpoint';
import {mkdirp} from 'mkdirp';
import {findTemplateOutput} from '@oas3/codegen/ts/template';
const fs = require('fs');

async function writeFile(path: string, content: string) {
Expand Down Expand Up @@ -118,7 +119,7 @@ export class DefaultCodeGenerator implements CodeGenerator {
switch (o.definitionType) {
case 'function':
definitionContents.push(
`export function ${o.createName(o.path)} ${o.createCode(o.path)}`
`export function ${o.createName(o.path)}${o.createCode(o.path)}`
);
break;
case 'const':
Expand All @@ -142,9 +143,9 @@ export class DefaultCodeGenerator implements CodeGenerator {
});
let content = importContents.join('\n');
if (importContents.length && definitionContents.length) {
content += '\n\n';
content += '\n\n\n';
}
content += definitionContents.join('\n');
content += definitionContents.join('\n\n');
return content;
}

Expand Down Expand Up @@ -182,21 +183,17 @@ export class DefaultCodeGenerator implements CodeGenerator {
return `/${convertToKebabCase(folders.join('/'))}/${fileName}.ts`;
}

private createImportPath(
private createRelativeImportPath(
localOutputPath: OutputPath,
referencingOutputPath: OutputPath
): string {
return this.createFilePathFromOutputPath(localOutputPath); // todo: reference relatively
}

private isSameFileContext(
outputPath: OutputPath,
referencingPath: OutputPath
) {
return areOutputPathsEqual(
outputPath.slice(0, 2),
referencingPath.slice(0, 2)
);
private isSameOutputFile(outputPath1: OutputPath, outputPath2: OutputPath) {
const filePath1 = this.createFilePathFromOutputPath(outputPath1);
const filePath2 = this.createFilePathFromOutputPath(outputPath2);
return filePath1 === filePath2;
}

private addImportsToFileOutputByFilePath(
Expand All @@ -206,17 +203,23 @@ export class DefaultCodeGenerator implements CodeGenerator {
): FileOutput {
let nextFileOutput = fileOutput;
output.requiredOutputPaths.forEach(requiredOutputPath => {
const requiredOutput = availableOutputs.find(o =>
let requiredOutput = availableOutputs.find(o =>
areOutputPathsEqual(o.path, requiredOutputPath)
);
if (!requiredOutput) {
requiredOutput = findTemplateOutput(requiredOutputPath);
}
if (!requiredOutput) {
console.log(
'could not find output by path: ' + requiredOutputPath.join('.')
);
return;
}
if (this.isSameFileContext(requiredOutput.path, output.path)) {
if (this.isSameOutputFile(requiredOutput.path, output.path)) {
return;
}
const importName = requiredOutput.createName(requiredOutput.path);
const importPath = this.createImportPath(
const importPath = this.createRelativeImportPath(
requiredOutput.path,
output.path
);
Expand Down Expand Up @@ -353,23 +356,23 @@ export class DefaultCodeGenerator implements CodeGenerator {
}

createTypeName(outputPath: OutputPath, referencingPath: OutputPath): string {
const parts = this.isSameFileContext(outputPath, referencingPath)
const parts = this.isSameOutputFile(outputPath, referencingPath)
? this.getOutputPathWithoutDomainPathPart(outputPath)
: outputPath;
const pascalCaseParts = parts.map(p => capitalizeFirstLetter(p));
return pascalCaseParts.join('');
}

createEnumName(outputPath: OutputPath, referencingPath: OutputPath): string {
const parts = this.isSameFileContext(outputPath, referencingPath)
const parts = this.isSameOutputFile(outputPath, referencingPath)
? this.getOutputPathWithoutDomainPathPart(outputPath)
: outputPath;
const pascalCaseParts = parts.map(p => capitalizeFirstLetter(p));
return pascalCaseParts.join('');
}

createConstName(outputPath: OutputPath, referencingPath: OutputPath): string {
const parts = this.isSameFileContext(outputPath, referencingPath)
const parts = this.isSameOutputFile(outputPath, referencingPath)
? this.getOutputPathWithoutDomainPathPart(outputPath)
: outputPath;
const pascalCaseParts = parts.map(p => capitalizeFirstLetter(p));
Expand All @@ -380,7 +383,7 @@ export class DefaultCodeGenerator implements CodeGenerator {
outputPath: OutputPath,
referencingPath: OutputPath
): string {
const parts = this.isSameFileContext(outputPath, referencingPath)
const parts = this.isSameOutputFile(outputPath, referencingPath)
? this.getOutputPathWithoutDomainPathPart(outputPath)
: outputPath;
const pascalCaseParts = parts.map(p => capitalizeFirstLetter(p));
Expand Down
32 changes: 24 additions & 8 deletions src/oas3/codegen/ts/template.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import {DefinitionOutput, OutputType} from '@oas3/codegen/ts/core';

const templateOutputPathPart = 'template-5acf7fae';
import {
areOutputPathsEqual,
DefinitionOutput,
Output,
OutputPath,
OutputType,
} from '@oas3/codegen/ts/core';

const createCodeErrorMessage =
'case not supported: code should be referenced from template folder instead';

export const templateRequestType: DefinitionOutput = {
type: OutputType.DEFINITION,
definitionType: 'type',
path: [templateOutputPathPart, 'core', 'request'],
path: ['core', 'request'],
createName: () => {
return 'Request';
},
Expand All @@ -21,7 +25,7 @@ export const templateRequestType: DefinitionOutput = {
export const templateResponseType: DefinitionOutput = {
type: OutputType.DEFINITION,
definitionType: 'type',
path: [templateOutputPathPart, 'core', 'response'],
path: ['core', 'response'],
createName: () => {
return 'Response';
},
Expand All @@ -34,7 +38,7 @@ export const templateResponseType: DefinitionOutput = {
export const templateStatusCodeEnum: DefinitionOutput = {
type: OutputType.DEFINITION,
definitionType: 'enum',
path: [templateOutputPathPart, 'core', 'statusCode'],
path: ['core', 'statusCode'],
createName: () => {
return 'StatusCode';
},
Expand Down Expand Up @@ -68,7 +72,7 @@ export function getTemplateResponseStatusCodeEnumEntry(
export const templateRequestResultType: DefinitionOutput = {
type: OutputType.DEFINITION,
definitionType: 'type',
path: [templateOutputPathPart, 'core', 'requestResult'],
path: ['core', 'requestResult'],
createName: () => {
return 'RequestResult';
},
Expand All @@ -81,7 +85,7 @@ export const templateRequestResultType: DefinitionOutput = {
export const templateRequestHandlerType: DefinitionOutput = {
type: OutputType.DEFINITION,
definitionType: 'type',
path: [templateOutputPathPart, 'core', 'requestHandler'],
path: ['core', 'requestHandler'],
createName: () => {
return 'RequestHandler';
},
Expand All @@ -90,3 +94,15 @@ export const templateRequestHandlerType: DefinitionOutput = {
},
requiredOutputPaths: [],
};

const templateOutputs: Output[] = [
templateRequestType,
templateResponseType,
templateStatusCodeEnum,
templateRequestResultType,
templateRequestHandlerType,
];

export function findTemplateOutput(outputPath: OutputPath): undefined | Output {
return templateOutputs.find(o => areOutputPathsEqual(o.path, outputPath));
}

0 comments on commit 18f5a35

Please sign in to comment.