Skip to content

Commit

Permalink
feat: changed so that options can be specified on a case-by-case basis (
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon committed Apr 7, 2021
1 parent 6961bab commit 108ce3a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
20 changes: 9 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ export class CodeGenerator {
constructor(private readonly entryPoint: string, option?: Option) {
this.rootSchema = Api.FileSystem.loadJsonOrYaml(entryPoint);
this.resolvedReferenceDocument = Api.ResolveReference.resolve(entryPoint, entryPoint, JSON.parse(JSON.stringify(this.rootSchema)));
this.parser = this.createParser(option?.allowOperationIds);
this.parser = this.createParser();
}

private createParser(allowOperationIds?: string[]): Api.OpenApiTools.Parser {
return new Api.OpenApiTools.Parser(this.entryPoint, this.rootSchema, this.resolvedReferenceDocument, {
allowOperationIds: allowOperationIds,
});
private createParser(): Api.OpenApiTools.Parser {
return new Api.OpenApiTools.Parser(this.entryPoint, this.rootSchema, this.resolvedReferenceDocument);
}

/**
Expand All @@ -40,11 +38,11 @@ export class CodeGenerator {
* @param generatorTemplate Template for when you want to change the code following a type definition
* @returns String of generated code
*/
public generateTypeDefinition(generatorTemplates?: Types.CodeGenerator.CustomGenerator<any>[]): string {
public generateTypeDefinition(generatorTemplates?: Types.CodeGenerator.CustomGenerator<any>[], allowOperationIds?: string[]): string {
const create = () => {
const statements = this.parser.getOpenApiTypeDefinitionStatements();
generatorTemplates?.forEach(generatorTemplate => {
const payload = this.parser.getCodeGeneratorParamsArray();
const payload = this.parser.getCodeGeneratorParamsArray(allowOperationIds);
const extraStatements = Api.TsGenerator.Utils.convertIntermediateCodes(generatorTemplate.generator(payload, generatorTemplate.option));
statements.push(...extraStatements);
});
Expand All @@ -59,8 +57,8 @@ export class CodeGenerator {
* @param generatorTemplate
* @returns String of generated code
*/
public generateCode(generatorTemplates: Types.CodeGenerator.CustomGenerator<any>[]): string {
const payload = this.parser.getCodeGeneratorParamsArray();
public generateCode(generatorTemplates: Types.CodeGenerator.CustomGenerator<any>[], allowOperationIds?: string[]): string {
const payload = this.parser.getCodeGeneratorParamsArray(allowOperationIds);
const create = () => {
return generatorTemplates
.map(generatorTemplate => {
Expand All @@ -74,8 +72,8 @@ export class CodeGenerator {
/**
* Provides parameters extracted from OpenApi Schema
*/
public getCodeGeneratorParamsArray(): Types.CodeGenerator.Params[] {
return this.parser.getCodeGeneratorParamsArray();
public getCodeGeneratorParamsArray(allowOperationIds?: string[]): Types.CodeGenerator.Params[] {
return this.parser.getCodeGeneratorParamsArray(allowOperationIds);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/internal/OpenApiTools/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ import * as Paths from "./paths";
import { Store } from "./store";
import * as TypeNodeContext from "./TypeNodeContext";

export interface Option {
/**
* List of operationId to be used
*/
allowOperationIds?: string[];
}

export class Parser {
private currentPoint: string;
private convertContext: ConvertContext.Types;
Expand All @@ -29,7 +22,6 @@ export class Parser {
private entryPoint: string,
private rootSchema: OpenApi.Document,
noReferenceOpenApiSchema: OpenApi.Document,
private option: Option,
) {
this.currentPoint = entryPoint;
this.convertContext = ConvertContext.create();
Expand Down Expand Up @@ -126,8 +118,8 @@ export class Parser {
}
}

public getCodeGeneratorParamsArray(): CodeGenerator.Params[] {
return Extractor.generateCodeGeneratorParamsArray(this.store, this.convertContext, this.option.allowOperationIds);
public getCodeGeneratorParamsArray(allowOperationIds?: string[]): CodeGenerator.Params[] {
return Extractor.generateCodeGeneratorParamsArray(this.store, this.convertContext, allowOperationIds);
}

public getOpenApiTypeDefinitionStatements(): ts.Statement[] {
Expand Down

0 comments on commit 108ce3a

Please sign in to comment.