diff --git a/scripts/build.ts b/scripts/build.ts index 7ac80a95..f43c63d6 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -1,9 +1,9 @@ import "./clean"; +import { cherryPick } from "./tools/cherry-pick"; import { copyPackageSet } from "./tools/copyPackageSet"; import { generateExportsField } from "./tools/dualPackageSupport"; import { shell } from "./tools/shell"; -import { cherryPick } from "./tools/cherry-pick"; const main = async () => { await Promise.all([ diff --git a/scripts/testCodeGen.ts b/scripts/testCodeGen.ts index 98a4a0b2..3b267994 100644 --- a/scripts/testCodeGen.ts +++ b/scripts/testCodeGen.ts @@ -1,7 +1,7 @@ import * as fs from "fs"; import { posix as path } from "path"; -import { CodeGenerator, GeneratorTemplate } from "../lib"; +import { CodeGenerator, CustomCodeGenerator } from "../lib"; import * as Templates from "../lib/templates"; const writeText = (filename: string, text: string): void => { @@ -34,7 +34,7 @@ const generateTemplateCodeOnly = ( }); } - const apiClientGeneratorTemplate: GeneratorTemplate = { + const apiClientGeneratorTemplate: CustomCodeGenerator = { generator: Templates.ApiClient.generator, option: option, }; @@ -58,11 +58,7 @@ const generateTypedefWithTemplateCode = ( } const code = codeGenerator.generateTypeDefinition([ - { - generator: () => { - return codeGenerator.getAdditionalTypeStatements(); - }, - }, + codeGenerator.getAdditionalTypeDefinitionCustomCodeGenerator(), { generator: Templates.ApiClient.generator, option: option, @@ -75,9 +71,9 @@ const generateTypedefWithTemplateCode = ( const generateSplitCode = (inputFilename: string, outputDir: string) => { const codeGenerator = new CodeGenerator(inputFilename); - const apiClientGeneratorTemplate: GeneratorTemplate = { + const apiClientGeneratorTemplate: CustomCodeGenerator = { generator: Templates.ApiClient.generator, - option: { sync: false }, + option: { sync: false, additionalMethodComment: true }, }; const typeDefCode = codeGenerator.generateTypeDefinition(); @@ -87,11 +83,7 @@ const generateSplitCode = (inputFilename: string, outputDir: string) => { return [`import { Schemas } from "./types";`]; }, }, - { - generator: () => { - return codeGenerator.getAdditionalTypeStatements(); - }, - }, + codeGenerator.getAdditionalTypeDefinitionCustomCodeGenerator(), apiClientGeneratorTemplate, ]); @@ -99,6 +91,11 @@ const generateSplitCode = (inputFilename: string, outputDir: string) => { writeText(path.join(outputDir, "apiClient.ts"), apiClientCode); }; +const generateParameter = (inputFilename: string, outputFilename: string) => { + const codeGenerator = new CodeGenerator(inputFilename); + writeText(outputFilename, JSON.stringify(codeGenerator.getCodeGeneratorParamsArray(), null, 2)); +}; + const main = () => { generateTypedefCodeOnly("test/api.test.domain/index.yml", "test/code/typedef-only/api.test.domain.ts", true); generateTypedefCodeOnly("test/infer.domain/index.yml", "test/code/typedef-only/infer.domain.ts", false); @@ -116,6 +113,9 @@ const main = () => { generateTypedefWithTemplateCode("test/infer.domain/index.yml", "test/code/typedef-with-template/infer.domain.ts", false, { sync: false }); generateSplitCode("test/api.test.domain/index.yml", "test/code/split"); + + generateParameter("test/api.test.domain/index.yml", "test/code/parameter/api.test.domain.json"); + generateParameter("test/infer.domain/index.yml", "test/code/parameter/infer.domain.json"); }; main(); diff --git a/src/api.ts b/src/api.ts index e204d180..8f4108cf 100644 --- a/src/api.ts +++ b/src/api.ts @@ -3,4 +3,3 @@ export * as OpenApiTools from "./internal/OpenApiTools"; export { FileSystem } from "./internal/FileSystem"; export * as ResolveReference from "./internal/ResolveReference"; export * as Validator from "./internal/Validator"; - diff --git a/src/code-templates/api-client/ApiClientClass/ApiClientInterface.ts b/src/code-templates/api-client/ApiClientClass/ApiClientInterface.ts index d876c423..d9852c45 100644 --- a/src/code-templates/api-client/ApiClientClass/ApiClientInterface.ts +++ b/src/code-templates/api-client/ApiClientClass/ApiClientInterface.ts @@ -2,6 +2,7 @@ import ts from "typescript"; import type { TsGenerator } from "../../../api"; import type { CodeGenerator } from "../../../types"; +import type { Option } from "../types"; const httpMethodList: string[] = ["GET", "PUT", "POST", "DELETE", "OPTIONS", "HEAD", "PATCH", "TRACE"]; @@ -104,7 +105,7 @@ const createObjectLikeInterface = (factory: TsGenerator.Factory.Type) => { }); }; -export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Params[], option: { sync?: boolean }): ts.Statement[] => { +export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Params[], option: Option): ts.Statement[] => { const objectLikeOrAnyType = factory.UnionTypeNode.create({ typeNodes: [ factory.TypeReferenceNode.create({ diff --git a/src/code-templates/api-client/ApiClientClass/Method.ts b/src/code-templates/api-client/ApiClientClass/Method.ts index ab37dd99..70827b85 100644 --- a/src/code-templates/api-client/ApiClientClass/Method.ts +++ b/src/code-templates/api-client/ApiClientClass/Method.ts @@ -1,7 +1,10 @@ +import { EOL } from "os"; + import ts from "typescript"; import type { TsGenerator } from "../../../api"; import type { CodeGenerator } from "../../../types"; +import type { Option } from "../types"; import * as MethodBody from "./MethodBody"; export { MethodBody }; @@ -36,7 +39,7 @@ const generateResponseReturnType = ( factory: TsGenerator.Factory.Type, successResponseNameList: string[], successResponseContentTypeList: string[], - option: { sync?: boolean }, + option: Option, ) => { let objectType: ts.TypeNode = factory.TypeNode.create({ type: "void", @@ -121,7 +124,7 @@ const methodTypeParameters = (factory: TsGenerator.Factory.Type, params: CodeGen * * } */ -export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.Params, option: { sync?: boolean }): ts.MethodDeclaration => { +export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.Params, option: Option): ts.MethodDeclaration => { const typeParameters: ts.TypeParameterDeclaration[] = methodTypeParameters(factory, params); const methodArguments: ts.ParameterDeclaration[] = []; const hasParamsArguments = @@ -148,7 +151,9 @@ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator. name: params.functionName, async: !option.sync, parameters: methodArguments, - comment: params.comment, + comment: option.additionalMethodComment + ? [params.comment, `operationId: ${params.operationId}`, `Request URI: ${params.rawRequestUri}`].filter(t => !!t).join(EOL) + : params.comment, deprecated: params.deprecated, type: returnType, typeParameters: typeParameters, diff --git a/src/code-templates/api-client/ApiClientClass/MethodBody/PathParameter.ts b/src/code-templates/api-client/ApiClientClass/MethodBody/PathParameter.ts index 7719689b..763f8879 100644 --- a/src/code-templates/api-client/ApiClientClass/MethodBody/PathParameter.ts +++ b/src/code-templates/api-client/ApiClientClass/MethodBody/PathParameter.ts @@ -11,7 +11,10 @@ export const isPathParameter = (params: any): params is CodeGenerator.PickedPara /** * const url = this.baseUrl + `[head]${params.parameter.[parameterName]}`; */ -const generateUrlVariableStatement = (factory: TsGenerator.Factory.Type, urlTemplate: Utils.Params$TemplateExpression): ts.VariableStatement => { +const generateUrlVariableStatement = ( + factory: TsGenerator.Factory.Type, + urlTemplate: Utils.Params$TemplateExpression, +): ts.VariableStatement => { return factory.VariableStatement.create({ declarationList: factory.VariableDeclarationList.create({ declarations: [ @@ -93,7 +96,11 @@ export const generateUrlTemplateExpression = ( return urlTemplate; }; -export const create = (factory: TsGenerator.Factory.Type, requestUri: string, pathParameters: CodeGenerator.PickedParameter[]): ts.VariableStatement => { +export const create = ( + factory: TsGenerator.Factory.Type, + requestUri: string, + pathParameters: CodeGenerator.PickedParameter[], +): ts.VariableStatement => { if (pathParameters.length > 0) { const urlTemplate = generateUrlTemplateExpression(factory, requestUri, pathParameters); return generateUrlVariableStatement(factory, urlTemplate); diff --git a/src/code-templates/api-client/ApiClientClass/index.ts b/src/code-templates/api-client/ApiClientClass/index.ts index f246b84e..a0d9afb0 100644 --- a/src/code-templates/api-client/ApiClientClass/index.ts +++ b/src/code-templates/api-client/ApiClientClass/index.ts @@ -2,6 +2,7 @@ import ts from "typescript"; import type { TsGenerator } from "../../../api"; import type { CodeGenerator } from "../../../types"; +import type { Option } from "../types"; import * as ApiClientInterface from "./ApiClientInterface"; import * as Class from "./Class"; import * as Constructor from "./Constructor"; @@ -9,7 +10,7 @@ import * as Method from "./Method"; export { Method }; -export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Params[], option: { sync?: boolean }): ts.Statement[] => { +export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Params[], option: Option): ts.Statement[] => { const methodList = list.map(params => { return Method.create(factory, params, option); }); diff --git a/src/code-templates/api-client/index.ts b/src/code-templates/api-client/index.ts index 170f46e9..9cac5c2e 100644 --- a/src/code-templates/api-client/index.ts +++ b/src/code-templates/api-client/index.ts @@ -4,10 +4,9 @@ import { TsGenerator } from "../../api"; import type { CodeGenerator } from "../../types"; import * as ApiClientArgument from "./ApiClientArgument"; import * as ApiClientClass from "./ApiClientClass"; +import type { Option } from "./types"; -export interface Option { - sync?: boolean; -} +export { Option }; export const generator: CodeGenerator.GenerateFunction