Skip to content

Commit

Permalink
watchのparamsを渡す場合、watchEventHandlerをextraOptionsに必須で渡す必要がある様にしました
Browse files Browse the repository at this point in the history
  • Loading branch information
kahirokunn committed Mar 13, 2024
1 parent 043e807 commit 1fda8bf
Show file tree
Hide file tree
Showing 8 changed files with 1,916 additions and 548 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kubernetes-typescript-client-codegen-openapi",
"version": "0.0.16",
"version": "0.0.20",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"author": "kahirokunn",
Expand Down
194 changes: 153 additions & 41 deletions packages/kubernetes-typescript-client-codegen-openapi/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,61 +77,173 @@ export function generateEndpointDefinition({
Response,
QueryArg,
queryFn,
isWatch,
}: {
operationName: string;
Response: ts.TypeReferenceNode;
QueryArg: ts.TypeReferenceNode;
queryFn: ts.ObjectLiteralExpression;
isWatch: boolean;
}) {
return factory.createVariableStatement(
[factory.createToken(ts.SyntaxKind.ExportKeyword)],
factory.createVariableDeclarationList(
[
factory.createVariableDeclaration(
factory.createIdentifier(operationName),
undefined,
undefined,
factory.createArrowFunction(
undefined,
undefined,
[
factory.createParameterDeclaration(
if (!isWatch) {
return [
factory.createVariableStatement(
[factory.createToken(ts.SyntaxKind.ExportKeyword)],
factory.createVariableDeclarationList(
[
factory.createVariableDeclaration(
factory.createIdentifier(operationName),
undefined,
undefined,
factory.createArrowFunction(
undefined,
undefined,
factory.createIdentifier('args'),
[
factory.createParameterDeclaration(
undefined,
undefined,
factory.createIdentifier('args'),
undefined,
QueryArg,
undefined
),
factory.createParameterDeclaration(
undefined,
undefined,
factory.createIdentifier('options'),
factory.createToken(ts.SyntaxKind.QuestionToken),
factory.createTypeReferenceNode(factory.createIdentifier('Options'), undefined),
undefined
),
],
undefined,
QueryArg,
undefined
),
factory.createParameterDeclaration(
factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
factory.createBlock(
[
factory.createReturnStatement(
factory.createCallExpression(
factory.createIdentifier('apiClient'),
[Response],
[queryFn, factory.createIdentifier('options')]
)
),
],
true
)
)
),
],
ts.NodeFlags.Const
)
),
];
}
return [
factory.createFunctionDeclaration(
[factory.createToken(ts.SyntaxKind.ExportKeyword)],
undefined,
factory.createIdentifier(operationName),
undefined,
[
factory.createParameterDeclaration(
undefined,
undefined,
factory.createIdentifier('args'),
undefined,
factory.createTypeReferenceNode(factory.createIdentifier('NoWatch'), [QueryArg]),
undefined
),
factory.createParameterDeclaration(
undefined,
undefined,
factory.createIdentifier('options'),
factory.createToken(ts.SyntaxKind.QuestionToken),
factory.createTypeReferenceNode(factory.createIdentifier('Options'), undefined),
undefined
),
],
factory.createTypeReferenceNode(factory.createIdentifier('Promise'), [Response]),
undefined
),
factory.createFunctionDeclaration(
[factory.createToken(ts.SyntaxKind.ExportKeyword)],
undefined,
factory.createIdentifier(operationName),
undefined,
[
factory.createParameterDeclaration(
undefined,
undefined,
factory.createIdentifier('args'),
undefined,
factory.createIntersectionTypeNode([
QueryArg,
factory.createTypeLiteralNode([
factory.createPropertySignature(
undefined,
factory.createIdentifier('watch'),
undefined,
factory.createIdentifier('options'),
factory.createToken(ts.SyntaxKind.QuestionToken),
factory.createTypeReferenceNode(factory.createIdentifier('Options'), undefined),
undefined
factory.createLiteralTypeNode(factory.createTrue())
),
],
undefined,
factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
factory.createBlock(
[
factory.createReturnStatement(
factory.createCallExpression(
factory.createIdentifier('apiClient'),
[Response],
[queryFn, factory.createIdentifier('options')]
)
),
],
true
)
)
]),
]),
undefined
),
factory.createParameterDeclaration(
undefined,
undefined,
factory.createIdentifier('options'),
undefined,
factory.createIntersectionTypeNode([
factory.createTypeReferenceNode(factory.createIdentifier('Options'), undefined),
factory.createTypeReferenceNode(factory.createIdentifier('WatchExtraOptions'), [Response]),
]),
undefined
),
],
ts.NodeFlags.Const
)
);
factory.createTypeReferenceNode(factory.createIdentifier('Promise'), [
factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword),
]),
undefined
),
factory.createFunctionDeclaration(
[factory.createToken(ts.SyntaxKind.ExportKeyword)],
undefined,
factory.createIdentifier(operationName),
undefined,
[
factory.createParameterDeclaration(
undefined,
undefined,
factory.createIdentifier('args'),
undefined,
factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
undefined
),
factory.createParameterDeclaration(
undefined,
undefined,
factory.createIdentifier('options'),
undefined,
factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
undefined
),
],
factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
factory.createBlock(
[
factory.createReturnStatement(
factory.createCallExpression(
factory.createIdentifier('apiClient'),
[Response],
[queryFn, factory.createIdentifier('options')]
)
),
],
true
)
),
];
}

export function generateTagTypes({ addTagTypes }: { addTagTypes: string[] }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export async function generateApi(
apiFile,
apiImport = 'apiClient',
optionsImport = 'Options',
watchExtraOptions = 'WatchExtraOptions',
argSuffix = 'ApiArg',
responseSuffix = 'ApiResponse',
outputFile,
Expand Down Expand Up @@ -119,12 +120,16 @@ export async function generateApi(
generateImportNode(apiFile, {
[apiImport]: { isTypeOnly: false, name: 'apiClient' },
[optionsImport]: { isTypeOnly: true, name: 'Options' },
[watchExtraOptions]: { isTypeOnly: true, name: 'WatchExtraOptions' }
}),
...operationDefinitions.map((operationDefinition) =>
generateRequester({
operationDefinition,
})
),
noWatch,
...operationDefinitions
.map((operationDefinition) =>
generateRequester({
operationDefinition,
})
)
.flat(),
...Object.values(interfaces),
...apiGen['aliases'],
],
Expand Down Expand Up @@ -265,6 +270,10 @@ export async function generateApi(
};

const queryArgValues = Object.values(queryArg);
const isWatch =
queryArgValues.findIndex(
(queryArg) => queryArg.origin === 'param' && queryArg.param.name === 'watch' && queryArg.param.in === 'query'
) !== -1;

const bodyType = queryArgValues.find((def) => def.origin === 'body')?.type;

Expand Down Expand Up @@ -310,6 +319,7 @@ export async function generateApi(
Response: ResponseTypeName,
QueryArg,
queryFn: generateQueryFn({ operationDefinition, queryArg }),
isWatch,
});
}

Expand Down Expand Up @@ -383,7 +393,12 @@ function accessProperty(rootObject: ts.Identifier, propertyName: string) {
: factory.createElementAccessExpression(rootObject, factory.createStringLiteral(propertyName));
}

function generatePathExpression(path: string, pathParameters: QueryArgDefinition[], rootObject: ts.Identifier, strict: boolean) {
function generatePathExpression(
path: string,
pathParameters: QueryArgDefinition[],
rootObject: ts.Identifier,
strict: boolean
) {
const expressions: Array<[string, string]> = [];

const head = path.replace(/\{(.*?)\}(.*?)(?=\{|$)/g, (_, expression, literal) => {
Expand Down Expand Up @@ -464,3 +479,35 @@ function getBodyNode(bodies: { [contentType: string]: ts.TypeNode }): ts.TypeNod
)
);
}

// type NoWatch<T> = Omit<T, 'watch'> & {
// watch?: false
// }
const noWatch = factory.createTypeAliasDeclaration(
undefined,
factory.createIdentifier("NoWatch"),
[factory.createTypeParameterDeclaration(
undefined,
factory.createIdentifier("T"),
undefined,
undefined
)],
factory.createIntersectionTypeNode([
factory.createTypeReferenceNode(
factory.createIdentifier("Omit"),
[
factory.createTypeReferenceNode(
factory.createIdentifier("T"),
undefined
),
factory.createLiteralTypeNode(factory.createStringLiteral("watch"))
]
),
factory.createTypeLiteralNode([factory.createPropertySignature(
undefined,
factory.createIdentifier("watch"),
factory.createToken(ts.SyntaxKind.QuestionToken),
factory.createLiteralTypeNode(factory.createFalse())
)])
])
)
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export interface CommonOptions {
* defaults to "Options"
*/
optionsImport?: string;
/**
* defaults to "WatchExtraOptions"
*/
watchExtraOptions?: string;
/**
* defaults to "enhancedApi"
*/
Expand Down
Loading

0 comments on commit 1fda8bf

Please sign in to comment.