-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(functional): make the functional api client a pure function. #103
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,8 @@ import ts from "typescript"; | |
|
||
import type { TsGenerator } from "../../../api"; | ||
import type { CodeGenerator } from "../../../types"; | ||
import type { Option } from "../../_shared/types"; | ||
import * as MethodBody from "../../_shared/MethodBody"; | ||
import type { Option } from "../../_shared/types"; | ||
|
||
export { MethodBody }; | ||
|
||
|
@@ -121,14 +121,35 @@ const methodTypeParameters = (factory: TsGenerator.Factory.Type, { convertedPara | |
*/ | ||
export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.Params, option: Option): ts.ArrowFunction => { | ||
const { convertedParams } = params; | ||
const typeParameters: ts.TypeParameterDeclaration[] = methodTypeParameters(factory, params); | ||
const typeParameters: ts.TypeParameterDeclaration[] = []; | ||
typeParameters.push( | ||
factory.TypeParameterDeclaration.create({ | ||
name: "RequestOption", | ||
}), | ||
); | ||
typeParameters.push(...methodTypeParameters(factory, params)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. each functions are now accept the |
||
|
||
const methodArguments: ts.ParameterDeclaration[] = []; | ||
methodArguments.push( | ||
factory.ParameterDeclaration.create({ | ||
name: "apiClient", | ||
modifiers: undefined, | ||
type: factory.TypeReferenceNode.create({ | ||
name: "ApiClient", | ||
typeArguments: [ | ||
factory.TypeReferenceNode.create({ | ||
name: "RequestOption", | ||
}), | ||
], | ||
}), | ||
}), | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the |
||
|
||
const hasParamsArguments = | ||
convertedParams.hasParameter || | ||
convertedParams.hasRequestBody || | ||
convertedParams.has2OrMoreSuccessResponseContentTypes || | ||
convertedParams.has2OrMoreRequestContentTypes; | ||
|
||
if (hasParamsArguments) { | ||
methodArguments.push(generateParams(factory, params)); | ||
} | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,23 @@ | ||
import { EOL } from "os"; | ||
import ts from "typescript"; | ||
|
||
import type { TsGenerator } from "../../../api"; | ||
import type { CodeGenerator } from "../../../types"; | ||
import type { Option } from "../../_shared/types"; | ||
import * as ArrowFunction from "./ArrowFunction"; | ||
|
||
export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Params[], option: Option): ts.VariableStatement => { | ||
const properties = list.map(params => { | ||
return factory.PropertyAssignment.create({ | ||
name: params.convertedParams.functionName, | ||
initializer: ArrowFunction.create(factory, params, option), | ||
comment: option.additionalMethodComment | ||
? [params.operationParams.comment, `operationId: ${params.operationId}`, `Request URI: ${params.operationParams.requestUri}`] | ||
.filter(t => !!t) | ||
.join(EOL) | ||
: params.operationParams.comment, | ||
}); | ||
}); | ||
|
||
const returnValue = factory.ReturnStatement.create({ | ||
expression: factory.ObjectLiteralExpression.create({ | ||
properties, | ||
multiLine: true, | ||
}), | ||
}); | ||
|
||
const arrowFunction = factory.ArrowFunction.create({ | ||
typeParameters: [ | ||
factory.TypeParameterDeclaration.create({ | ||
name: "RequestOption", | ||
}), | ||
], | ||
parameters: [ | ||
factory.ParameterDeclaration.create({ | ||
name: "apiClient", | ||
type: factory.TypeReferenceNode.create({ | ||
name: "ApiClient", | ||
typeArguments: [ | ||
factory.TypeReferenceNode.create({ | ||
name: "RequestOption", | ||
}), | ||
], | ||
}), | ||
}), | ||
factory.ParameterDeclaration.create({ | ||
name: "baseUrl", | ||
type: ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword), | ||
}), | ||
], | ||
body: factory.Block.create({ | ||
statements: [ | ||
factory.VariableStatement.create({ | ||
declarationList: factory.VariableDeclarationList.create({ | ||
flag: "const", | ||
declarations: [ | ||
factory.VariableDeclaration.create({ | ||
name: "_baseUrl", | ||
initializer: factory.CallExpression.create({ | ||
expression: factory.PropertyAccessExpression.create({ | ||
expression: "baseUrl", | ||
name: "replace", | ||
}), | ||
argumentsArray: [factory.RegularExpressionLiteral.create({ text: "/\\/$/" }), factory.StringLiteral.create({ text: "" })], | ||
}), | ||
}), | ||
], | ||
export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Params[], option: Option): ts.VariableStatement[] => { | ||
return list.map(params => { | ||
return factory.VariableStatement.create({ | ||
modifiers: [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], | ||
declarationList: factory.VariableDeclarationList.create({ | ||
declarations: [ | ||
factory.VariableDeclaration.create({ | ||
name: params.convertedParams.functionName, | ||
initializer: ArrowFunction.create(factory, params, option), | ||
}), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the functional api client will be |
||
}), | ||
returnValue, | ||
], | ||
multiLine: true, | ||
}), | ||
}); | ||
|
||
return factory.VariableStatement.create({ | ||
modifiers: [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], | ||
declarationList: factory.VariableDeclarationList.create({ | ||
declarations: [ | ||
factory.VariableDeclaration.create({ | ||
name: "createClient", | ||
initializer: arrowFunction, | ||
}), | ||
], | ||
flag: "const", | ||
}), | ||
], | ||
flag: "const", | ||
}), | ||
}); | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export const Name = "@himenon/openapi-typescript-code-generator"; | ||
export const Version = "0.22.2"; | ||
export const Version = "0.22.3"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't understand the reason but this code isn't worked.
The
class
part works fine butfunction
part will be wrong template string.