Skip to content

Commit

Permalink
prepare package usage
Browse files Browse the repository at this point in the history
  • Loading branch information
inkognitro committed Apr 18, 2024
1 parent 4619118 commit 7ee8be6
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 23 deletions.
51 changes: 42 additions & 9 deletions src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,48 @@
import {DefaultCodeGenerator} from '@oas3/codegen/ts/generator';
const fs = require('fs');

export function generate() {
const filePath = './oasTestSpecs.json';
const dataStr = fs.readFileSync(filePath, 'utf-8').toString();
const data = JSON.parse(dataStr);
const codeGenerator = new DefaultCodeGenerator(data);
codeGenerator.generate({
targetFolderPath: './generated-files',
predefinedFolderPaths: [['booking', 'core']],
type OutputPath = string[];

export type GenerateConfig = {
type: 'oas3';
getSpecification: () => Promise<object>;
outputFolderPath: string;
importRootAlias?: string;
predefinedFolderOutputPaths?: OutputPath[];
};

export function generate(config: GenerateConfig) {
switch (config.type) {
case 'oas3':
// eslint-disable-next-line no-case-declarations
config.getSpecification().then(data => {
const codeGenerator = new DefaultCodeGenerator(data);
codeGenerator.generate({
outputFolderPath: config.outputFolderPath,
predefinedFolderOutputPaths: config.predefinedFolderOutputPaths,
});
});
break;
default:
console.error('');
}
}

// todo: move this code to the outside of the package
export function usePackage() {
generate({
type: 'oas3',
getSpecification: () => {
return new Promise<object>(resolve => {
const filePath = './oasTestSpecs.json';
const dataStr = fs.readFileSync(filePath, 'utf-8').toString();
const data = JSON.parse(dataStr);
resolve(data);
});
},
outputFolderPath: './generated-files',
predefinedFolderOutputPaths: [['booking', 'core']],
});
}

generate();
usePackage();
7 changes: 7 additions & 0 deletions src/oas3/codegen/ts/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export function areOutputPathsEqual(a: OutputPath, b: OutputPath): boolean {
);
}

export function containsOutputPath(
outputPaths: OutputPath[],
outputPath: OutputPath
): boolean {
return !!outputPaths.find(p => areOutputPathsEqual(p, outputPath));
}

export function doesOutputPathStartWithOtherOutputPath(
outputPath: OutputPath,
otherOutputPath: OutputPath
Expand Down
7 changes: 1 addition & 6 deletions src/oas3/codegen/ts/endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import {CodeGenerator, OutputType, OutputPath, DefinitionOutput} from './core';
import {Request} from '@oas3/specification';
import {applyResponseByStatusCodeMap} from './response';
import {
templateRequestHandlerType,
templateRequestResultType,
templateRequestType,
} from './template';
import {templateRequestResultType, templateRequestType} from './template';

export const responseOutputPathPart = 'response6b3a7814';
export const requestResultOutputPathPart = 'requestResult6b3a7814';
Expand Down Expand Up @@ -95,7 +91,6 @@ export function applyEndpointCallerFunction(
endpointIdConstDefinition.path,
templateRequestType.path,
requestResultTypeDefinition.path,
templateRequestHandlerType.path,
],
};
codeGenerator.addOutput(funcDefinition);
Expand Down
8 changes: 4 additions & 4 deletions src/oas3/codegen/ts/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ const componentResponsesFileNameOutputPathPart = 'responses6b3a7814';
const componentSchemasFileNameOutputPathPart = 'schemas6b3a7814';

type GenerateConfig = {
targetFolderPath: string;
outputFolderPath: string;
importRootAlias?: string;
predefinedFolderPaths?: OutputPath[];
predefinedFolderOutputPaths?: OutputPath[];
};

export class DefaultCodeGenerator implements CodeGenerator {
Expand Down Expand Up @@ -124,7 +124,7 @@ export class DefaultCodeGenerator implements CodeGenerator {
}

private initializeOperationFolderOutputPaths(config: GenerateConfig) {
this.operationFolderOutputPaths = config.predefinedFolderPaths ?? [];
this.operationFolderOutputPaths = config.predefinedFolderOutputPaths ?? [];
for (const path in this.oas3Specs.paths) {
const requestByMethodMap = this.oas3Specs.paths[path];
for (const method in requestByMethodMap) {
Expand All @@ -141,7 +141,7 @@ export class DefaultCodeGenerator implements CodeGenerator {
fileOutputByFilePath: FileOutputByFilePath,
config: GenerateConfig
) {
const cleanTargetFolderPath = cleanUpFolderPath(config.targetFolderPath);
const cleanTargetFolderPath = cleanUpFolderPath(config.outputFolderPath);
fs.cpSync(__dirname + '../../../../templates/ts', cleanTargetFolderPath, {
recursive: true,
});
Expand Down
26 changes: 22 additions & 4 deletions src/oas3/codegen/ts/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
OutputType,
OutputPath,
DefinitionOutput,
containsOutputPath,
} from './core';
import {applyComponentRefSchema, applySchema} from './schema';
import {
Expand Down Expand Up @@ -58,30 +59,44 @@ export function applyResponseByStatusCodeMap(
path: OutputPath
): DefinitionOutput {
const statusCodeResponseOutputs: CodeGenerationOutput[] = [];
const requiredOutputPaths: OutputPath[] = [];
for (const statusCode in schema) {
const responseOutputPath: OutputPath = [...path, statusCode];
const responseOrRef = schema[statusCode];
if (isResponseComponentRef(responseOrRef)) {
const responseBodyOutput = applyComponentRefSchema(
const jsonResponseBody = applyComponentRefSchema(
codeGenerator,
responseOrRef,
[...responseOutputPath, 'body']
);
if (!containsOutputPath(requiredOutputPaths, templateResponseType.path)) {
requiredOutputPaths.push(templateResponseType.path);
}
if (
!containsOutputPath(requiredOutputPaths, templateStatusCodeEnum.path)
) {
requiredOutputPaths.push(templateStatusCodeEnum.path);
}
if (
!containsOutputPath(requiredOutputPaths, templateStatusCodeEnum.path)
) {
requiredOutputPaths.push(jsonResponseBody.path);
}
const responseOutput: CodeGenerationOutput = {
createCode: referencingPath => {
const responseType = templateResponseType.createName(path);
const statusCodeEnum = templateStatusCodeEnum.createName(path);
const statusCodeEnumEntry = getTemplateResponseStatusCodeEnumEntry(
parseInt(statusCode)
);
const bodyCode = responseBodyOutput.createCode(referencingPath);
const bodyCode = jsonResponseBody.createCode(referencingPath);
return `${responseType}<${statusCodeEnum}.${statusCodeEnumEntry}, ${bodyCode}>`;
},
path: responseOutputPath,
requiredOutputPaths: [
templateResponseType.path,
templateStatusCodeEnum.path,
responseBodyOutput.path,
jsonResponseBody.path,
],
};
statusCodeResponseOutputs.push(responseOutput);
Expand All @@ -98,6 +113,9 @@ export function applyResponseByStatusCodeMap(
parseInt(statusCode),
jsonResponseBody
);
if (!containsOutputPath(requiredOutputPaths, statusCodeResponseType.path)) {
requiredOutputPaths.push(statusCodeResponseType.path);
}
statusCodeResponseOutputs.push({
createCode: referencingPath => {
return statusCodeResponseType.createName(referencingPath);
Expand All @@ -123,7 +141,7 @@ export function applyResponseByStatusCodeMap(
});
return `${codeParts.join('\n')}`;
},
requiredOutputPaths: statusCodeResponseOutputs.map(o => o.path),
requiredOutputPaths,
};
codeGenerator.addOutput(responseTypeDefinition);
return responseTypeDefinition;
Expand Down

0 comments on commit 7ee8be6

Please sign in to comment.