Skip to content

Commit

Permalink
fix: allow not generating types tree with types.tree = false
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Jul 15, 2024
1 parent 8bd45bf commit 668eb81
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-pants-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: allow not generating types tree with types.tree = false
28 changes: 15 additions & 13 deletions packages/openapi-ts/src/generate/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface TypesProps {
onRemoveNode?: VoidFunction;
}

const serviceExportedNamespace = () => '$OpenApiTs';
const treeName = '$OpenApiTs';

export const emptyModel: Model = {
$refs: [],
Expand Down Expand Up @@ -489,18 +489,20 @@ const processServiceTypes = ({
return pathKey;
});

generateType({
client,
meta: {
$ref: '@hey-api/openapi-ts',
name: serviceExportedNamespace(),
},
onNode,
type: toType({
...emptyModel,
properties,
}),
});
if (config.types.tree) {
generateType({
client,
meta: {
$ref: '@hey-api/openapi-ts',
name: treeName,
},
onNode,
type: toType({
...emptyModel,
properties,
}),
});
}
};

export const generateTypes = async ({
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ const getTypes = (userConfig: ClientConfig): Config['types'] => {
enums: false,
export: true,
name: 'preserve',
tree: true,
};
if (typeof userConfig.types === 'boolean') {
types.export = userConfig.types;
Expand Down
6 changes: 6 additions & 0 deletions packages/openapi-ts/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ export interface ClientConfig {
* @default 'preserve'
*/
name?: 'PascalCase' | 'preserve';
/**
* Generate a tree of types containing all operations? It will be named
* $OpenApiTs and is generated by default.
* @default true
*/
tree?: boolean;
};
/**
* Use options or arguments functions
Expand Down
14 changes: 13 additions & 1 deletion packages/openapi-ts/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,21 @@ describe('OpenAPI v3', () => {
services: false,
types: {},
}),
description: 'generate only types',
description: 'generate only types with default settings',
name: 'v3_types',
},
{
config: createConfig({
exportCore: false,
schemas: false,
services: false,
types: {
tree: false,
},
}),
description: 'generate only types without tree',
name: 'v3_types_no_tree',
},
];

it.each(clientScenarios.concat(allScenarios))(
Expand Down

0 comments on commit 668eb81

Please sign in to comment.