Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/calm-cups-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@hey-api/openapi-ts': patch
---

Add new configuration toggles to reduce generated TypeScript artifacts and improve parser read/write controls.

For `@hey-api/openapi-ts` (`@hey-api/typescript` plugin):

- allow disabling request aliases with `requests: false`
- allow disabling response aliases with `responses: false`
- allow disabling error aliases with `errors: false`
- allow disabling `ClientOptions` with `clientOptions: false`
12 changes: 12 additions & 0 deletions packages/openapi-ts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @hey-api/openapi-ts

## Unreleased

### Patch Changes

- **parser**: allow disabling read/write schema split variants independently via `parser.transforms.readWrite.requests = false` and `parser.transforms.readWrite.responses = false`

- **plugin(@hey-api/typescript)**: allow disabling generated operation aliases and client options via:
- `requests: false`
- `responses: false`
- `errors: false`
- `clientOptions: false`

## 0.97.2

### Patch Changes
Expand Down
83 changes: 83 additions & 0 deletions packages/openapi-ts/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,87 @@ describe('createClient', () => {

expect(results.length).toBeGreaterThanOrEqual(1);
});

it('can disable readWrite split and TypeScript request/response/error/client options artifacts', async () => {
const results = await createClient({
dryRun: true,
input: {
components: {
schemas: {
Demo: {
properties: {
id: { readOnly: true, type: 'string' },
secret: { type: 'string', writeOnly: true },
value: { type: 'string' },
},
required: ['value'],
type: 'object',
},
},
},
info: { title: 'disable-artifacts-test', version: '1.0.0' },
openapi: '3.1.0',
paths: {
'/demo': {
get: {
operationId: 'getDemo',
responses: {
200: {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Demo',
},
},
},
description: 'ok',
},
400: {
content: {
'application/json': {
schema: {
type: 'object',
},
},
},
description: 'bad request',
},
},
},
},
},
},
logs: { level: 'silent' },
output: 'out',
parser: {
transforms: {
readWrite: false,
},
},
plugins: [
{
clientOptions: false,
errors: false,
name: '@hey-api/typescript',
requests: false,
responses: false,
},
],
});

expect(results).toHaveLength(1);

const output = results[0]!.gen
.render()
.map((file) => file.content)
.join('\n');

expect(output).not.toContain('type ClientOptions');
expect(output).not.toContain('type GetDemoData');
expect(output).not.toContain('type GetDemoResponses');
expect(output).not.toContain('type GetDemoResponse');
expect(output).not.toContain('type GetDemoErrors');
expect(output).not.toContain('type GetDemoError');
expect(output).not.toContain('Writable');
});
});
7 changes: 7 additions & 0 deletions packages/openapi-ts/src/plugins/@hey-api/typescript/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const defaultConfig: HeyApiTypeScriptPlugin['Config'] = {
api: new Api(),
config: {
case: 'PascalCase',
clientOptions: true,
comments: true,
includeInEntry: true,
topType: 'unknown',
Expand Down Expand Up @@ -44,10 +45,12 @@ export const defaultConfig: HeyApiTypeScriptPlugin['Config'] = {
plugin.config.errors = context.valueToObject({
defaultValue: {
case: plugin.config.case ?? 'PascalCase',
enabled: true,
error: '{{name}}Error',
name: '{{name}}Errors',
},
mappers: {
boolean: (enabled) => ({ enabled }),
function: (name) => ({ name }),
string: (name) => ({ name }),
},
Expand All @@ -57,9 +60,11 @@ export const defaultConfig: HeyApiTypeScriptPlugin['Config'] = {
plugin.config.requests = context.valueToObject({
defaultValue: {
case: plugin.config.case ?? 'PascalCase',
enabled: true,
name: '{{name}}Data',
},
mappers: {
boolean: (enabled) => ({ enabled }),
function: (name) => ({ name }),
string: (name) => ({ name }),
},
Expand All @@ -69,10 +74,12 @@ export const defaultConfig: HeyApiTypeScriptPlugin['Config'] = {
plugin.config.responses = context.valueToObject({
defaultValue: {
case: plugin.config.case ?? 'PascalCase',
enabled: true,
name: '{{name}}Responses',
response: '{{name}}Response',
},
mappers: {
boolean: (enabled) => ({ enabled }),
function: (name) => ({ name }),
string: (name) => ({ name }),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,44 +103,46 @@ export const operationToType = ({
data.required = dataRequired;
}

const dataResult = processor.process({
export: false,
meta: {
resource: 'operation',
resourceId: operation.id,
},
naming: plugin.config.definitions,
path: [...path, operation.id, 'data'],
plugin,
schema: data,
});

const dataSymbol = plugin.registerSymbol(
buildSymbolIn({
if (plugin.config.requests.enabled) {
const dataResult = processor.process({
export: false,
meta: {
category: 'type',
path,
resource: 'operation',
resourceId: operation.id,
role: 'data',
tags,
tool: 'typescript',
},
name: operation.id,
naming: plugin.config.requests,
operation,
naming: plugin.config.definitions,
path: [...path, operation.id, 'data'],
plugin,
}),
);
const dataNode = $.type
.alias(dataSymbol)
.export()
.type(dataResult?.type ?? $.type('never'));
plugin.node(dataNode);
schema: data,
});

const dataSymbol = plugin.registerSymbol(
buildSymbolIn({
meta: {
category: 'type',
path,
resource: 'operation',
resourceId: operation.id,
role: 'data',
tags,
tool: 'typescript',
},
name: operation.id,
naming: plugin.config.requests,
operation,
plugin,
}),
);
const dataNode = $.type
.alias(dataSymbol)
.export()
.type(dataResult?.type ?? $.type('never'));
plugin.node(dataNode);
}

const { error, errors, response, responses } = operationResponsesMap(operation);

if (errors) {
if (plugin.config.errors.enabled && errors) {
const errorsResult = processor.process({
export: false,
meta: {
Expand Down Expand Up @@ -205,7 +207,7 @@ export const operationToType = ({
}
}

if (responses) {
if (plugin.config.responses.enabled && responses) {
const responsesResult = processor.process({
export: false,
meta: {
Expand Down
59 changes: 46 additions & 13 deletions packages/openapi-ts/src/plugins/@hey-api/typescript/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export type UserConfig = Plugin.Name<'@hey-api/typescript'> &
* @default 'PascalCase'
*/
case?: Exclude<Casing, 'SCREAMING_SNAKE_CASE'>;
/**
* Generate `ClientOptions` type.
*
* @default true
*/
clientOptions?: boolean;
/**
* Configuration for reusable schema definitions.
*
Expand Down Expand Up @@ -104,6 +110,7 @@ export type UserConfig = Plugin.Name<'@hey-api/typescript'> &
* @default '{{name}}Errors'
*/
errors?:
| boolean
| NameTransformer
| {
/**
Expand All @@ -112,6 +119,12 @@ export type UserConfig = Plugin.Name<'@hey-api/typescript'> &
* @default 'PascalCase'
*/
case?: Casing;
/**
* Whether this feature is enabled.
*
* @default true
*/
enabled?: boolean;
/**
* Naming pattern for generated names.
*
Expand All @@ -138,6 +151,7 @@ export type UserConfig = Plugin.Name<'@hey-api/typescript'> &
* @default '{{name}}Data'
*/
requests?:
| boolean
| NameTransformer
| {
/**
Expand All @@ -146,6 +160,12 @@ export type UserConfig = Plugin.Name<'@hey-api/typescript'> &
* @default 'PascalCase'
*/
case?: Casing;
/**
* Whether this feature is enabled.
*
* @default true
*/
enabled?: boolean;
/**
* Naming pattern for generated names.
*
Expand All @@ -165,6 +185,7 @@ export type UserConfig = Plugin.Name<'@hey-api/typescript'> &
* @default '{{name}}Responses'
*/
responses?:
| boolean
| NameTransformer
| {
/**
Expand All @@ -173,6 +194,12 @@ export type UserConfig = Plugin.Name<'@hey-api/typescript'> &
* @default 'PascalCase'
*/
case?: Casing;
/**
* Whether this feature is enabled.
*
* @default true
*/
enabled?: boolean;
/**
* Naming pattern for generated names.
*
Expand Down Expand Up @@ -240,6 +267,10 @@ export type Config = Plugin.Name<'@hey-api/typescript'> &
* Casing convention for generated names.
*/
case: Exclude<Casing, 'SCREAMING_SNAKE_CASE'>;
/**
* Generate `ClientOptions` type.
*/
clientOptions: boolean;
/**
* Configuration for reusable schema definitions.
*
Expand Down Expand Up @@ -290,30 +321,32 @@ export type Config = Plugin.Name<'@hey-api/typescript'> &
* - `string` or `function`: Shorthand for `{ name: string | function }`
* - `object`: Full configuration object
*/
errors: NamingOptions & {
/**
* Naming pattern for generated names.
*/
error: NameTransformer;
};
errors: NamingOptions &
FeatureToggle & {
/**
* Naming pattern for generated names.
*/
error: NameTransformer;
};
Comment thread
pullfrog[bot] marked this conversation as resolved.
/**
* Configuration for request-specific types.
*
* Controls generation of types for request bodies, query parameters, path
* parameters, and headers.
*/
requests: NamingOptions;
requests: NamingOptions & FeatureToggle;
/**
* Configuration for response-specific types.
*
* Controls generation of types for response bodies and status codes.
*/
responses: NamingOptions & {
/**
* Naming pattern for generated names.
*/
response: NameTransformer;
};
responses: NamingOptions &
FeatureToggle & {
/**
* Naming pattern for generated names.
*/
response: NameTransformer;
};
/**
* The top type to use for untyped or unspecified schema values.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ export const handlerV1: HeyApiTypeScriptPlugin['Handler'] = ({ plugin }) => {
},
);

createClientOptions({ nodeIndex: nodeClientIndex, plugin, servers });
if (plugin.config.clientOptions) {
createClientOptions({ nodeIndex: nodeClientIndex, plugin, servers });
}

if (webhooks.length) {
const symbol = plugin.registerSymbol(
Expand Down
Loading
Loading