Skip to content

Commit

Permalink
Merge pull request #531 from hey-api/chore/clients-openapi-ts-changes
Browse files Browse the repository at this point in the history
chore: update openapi-ts for clients work
  • Loading branch information
mrlubos committed May 5, 2024
2 parents 76ca2cf + 423863a commit 8a20551
Show file tree
Hide file tree
Showing 11 changed files with 501 additions and 220 deletions.
39 changes: 7 additions & 32 deletions packages/openapi-ts/src/compiler/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const toAccessLevelModifiers = (access?: AccessLevel): ts.ModifierLike[] => {
* @param parameters - the parameters to conver to declarations
* @returns ts.ParameterDeclaration[]
*/
const toParameterDeclarations = (parameters: FunctionParameter[]) =>
export const toParameterDeclarations = (parameters: FunctionParameter[]) =>
parameters.map((p) => {
const modifiers = toAccessLevelModifiers(p.accessLevel);
if (p.isReadOnly) {
Expand Down Expand Up @@ -69,8 +69,8 @@ const toParameterDeclarations = (parameters: FunctionParameter[]) =>
* @returns ts.ConstructorDeclaration
*/
export const createConstructorDeclaration = ({
accessLevel = undefined,
comment = undefined,
accessLevel,
comment,
multiLine = true,
parameters = [],
statements = [],
Expand Down Expand Up @@ -105,13 +105,13 @@ export const createConstructorDeclaration = ({
* @returns ts.MethodDeclaration
*/
export const createMethodDeclaration = ({
accessLevel = undefined,
comment = undefined,
accessLevel,
comment,
isStatic = false,
multiLine = true,
name,
parameters = [],
returnType = undefined,
returnType,
statements = [],
}: {
accessLevel?: AccessLevel;
Expand Down Expand Up @@ -156,7 +156,7 @@ type ClassDecorator = {
* @returns ts.ClassDeclaration
*/
export const createClassDeclaration = ({
decorator = undefined,
decorator,
members = [],
name,
}: {
Expand Down Expand Up @@ -195,28 +195,3 @@ export const createClassDeclaration = ({
m,
);
};

/**
* Create a return function call. Example `return call(param);`.
* @param args - arguments to pass to the function.
* @param name - name of the function to call.
* @returns ts.ReturnStatement
*/
export const createReturnFunctionCall = ({
args = [],
name,
}: {
args: any[];
name: string;
}) =>
ts.factory.createReturnStatement(
ts.factory.createCallExpression(
ts.factory.createIdentifier(name),
undefined,
args
.map((arg) =>
ts.isExpression(arg) ? arg : ts.factory.createIdentifier(arg),
)
.filter(isType<ts.Identifier | ts.Expression>),
),
);
12 changes: 7 additions & 5 deletions packages/openapi-ts/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ts from 'typescript';

import * as classes from './classes';
import * as module from './module';
import * as _return from './return';
import * as typedef from './typedef';
import * as types from './types';
import { stringToTsNodes, tsNodeToString } from './utils';
Expand Down Expand Up @@ -55,9 +56,7 @@ export class TypeScriptFile {
this._items = [...this._items, ...nodes];
}

public addNamedImport(
...params: Parameters<typeof compiler.import.named>
): void {
public addImport(...params: Parameters<typeof compiler.import.named>): void {
this._imports = [...this._imports, compiler.import.named(...params)];
}

Expand Down Expand Up @@ -121,16 +120,18 @@ export const compiler = {
constructor: classes.createConstructorDeclaration,
create: classes.createClassDeclaration,
method: classes.createMethodDeclaration,
return: classes.createReturnFunctionCall,
},
export: {
all: module.createExportAllDeclaration,
asConst: module.createExportVariableAsConst,
const: module.createExportConstVariable,
named: module.createNamedExportDeclarations,
},
import: {
named: module.createNamedImportDeclarations,
},
return: {
functionCall: _return.createReturnFunctionCall,
},
typedef: {
alias: typedef.createTypeAliasDeclaration,
array: typedef.createTypeArrayNode,
Expand All @@ -144,6 +145,7 @@ export const compiler = {
types: {
array: types.createArrayType,
enum: types.createEnumDeclaration,
function: types.createFunction,
object: types.createObjectType,
},
utils: {
Expand Down
59 changes: 36 additions & 23 deletions packages/openapi-ts/src/compiler/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ts from 'typescript';

import { ots } from './utils';
import { addLeadingJSDocComment, type Comments, ots } from './utils';

/**
* Create export all declaration. Example: `export * from './y'`.
Expand Down Expand Up @@ -53,32 +53,45 @@ export const createNamedExportDeclarations = (
};

/**
* Create an export variable as const statement. Example: `export x = {} as const`.
* @param name - name of the variable.
* @param expression - expression for the variable.
* Create a const variable export. Optionally, it can use const assertion.
* Example: `export x = {} as const`.
* @param constAssertion use const assertion?
* @param expression expression for the variable.
* @param name name of the variable.
* @returns ts.VariableStatement
*/
export const createExportVariableAsConst = (
name: string,
expression: ts.Expression,
): ts.VariableStatement =>
ts.factory.createVariableStatement(
export const createExportConstVariable = ({
comment,
constAssertion = false,
expression,
name,
}: {
comment?: Comments;
constAssertion?: boolean;
expression: ts.Expression;
name: string;
}): ts.VariableStatement => {
const initializer = constAssertion
? ts.factory.createAsExpression(
expression,
ts.factory.createTypeReferenceNode('const'),
)
: expression;
const declaration = ts.factory.createVariableDeclaration(
ts.factory.createIdentifier(name),
undefined,
undefined,
initializer,
);
const statement = ts.factory.createVariableStatement(
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
ts.factory.createVariableDeclarationList(
[
ts.factory.createVariableDeclaration(
ts.factory.createIdentifier(name),
undefined,
undefined,
ts.factory.createAsExpression(
expression,
ts.factory.createTypeReferenceNode('const'),
),
),
],
ts.NodeFlags.Const,
),
ts.factory.createVariableDeclarationList([declaration], ts.NodeFlags.Const),
);
if (comment) {
addLeadingJSDocComment(statement, comment);
}
return statement;
};

/**
* Create a named import declaration. Example: `import { X } from './y'`.
Expand Down
33 changes: 33 additions & 0 deletions packages/openapi-ts/src/compiler/return.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import ts from 'typescript';

import { isType } from './utils';

/**
* Create a return function call statement.
* Example `return fn<string>(params)`.
* @param args arguments to pass to the function.
* @param name name of the function to call.
* @param types list of function types
* @returns ts.ReturnStatement
*/
export const createReturnFunctionCall = ({
args = [],
name,
types = [],
}: {
args: any[];
name: string;
types?: string[];
}) => {
const expression = ts.factory.createCallExpression(
ts.factory.createIdentifier(name),
types.map((type) => ts.factory.createTypeReferenceNode(type)),
args
.map((arg) =>
ts.isExpression(arg) ? arg : ts.factory.createIdentifier(arg),
)
.filter(isType<ts.Identifier | ts.Expression>),
);
const statement = ts.factory.createReturnStatement(expression);
return statement;
};
Loading

0 comments on commit 8a20551

Please sign in to comment.