Skip to content

Commit

Permalink
Merge pull request #1211 from hey-api/fix/legacy-name-ignore
Browse files Browse the repository at this point in the history
fix: ignore name option when not used with legacy clients to avoid producing broken output
  • Loading branch information
mrlubos authored Oct 29, 2024
2 parents ad33092 + c8a3e3d commit d2a039b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-garlics-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: ignore name option when not used with legacy clients to avoid producing broken output
6 changes: 3 additions & 3 deletions packages/openapi-ts/src/generate/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path';

import type { OpenApi } from '../openApi';
import type { Client } from '../types/client';
import { getConfig } from '../utils/config';
import { getConfig, legacyNameFromConfig } from '../utils/config';
import { getHttpRequestName } from '../utils/getHttpRequestName';
import type { Templates } from '../utils/handlebars';
import { sortByName } from '../utils/sort';
Expand Down Expand Up @@ -34,10 +34,10 @@ export const generateLegacyClientClass = async (
services: sortByName(client.services),
});

if (config.name) {
if (legacyNameFromConfig(config)) {
ensureDirSync(outputPath);
writeFileSync(
path.resolve(outputPath, `${config.name}.ts`),
path.resolve(outputPath, `${legacyNameFromConfig(config)}.ts`),
templateResult,
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/generate/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import path from 'node:path';

import type { Client } from '../types/client';
import { getConfig } from '../utils/config';
import { getConfig, legacyNameFromConfig } from '../utils/config';
import { getHttpRequestName } from '../utils/getHttpRequestName';
import type { Templates } from '../utils/handlebars';

Expand Down Expand Up @@ -85,7 +85,7 @@ export const generateLegacyCore = async (
}),
);

if (config.name) {
if (legacyNameFromConfig(config)) {
await writeFileSync(
path.resolve(outputPath, 'BaseHttpRequest.ts'),
templates.core.baseHttpRequest({
Expand Down
10 changes: 5 additions & 5 deletions packages/openapi-ts/src/generate/indexFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { compiler } from '../compiler';
import type { Files } from '../types/utils';
import { getConfig } from '../utils/config';
import { getConfig, legacyNameFromConfig } from '../utils/config';
import { TypeScriptFile } from './files';

export const generateIndexFile = ({ files }: { files: Files }): void => {
Expand All @@ -11,11 +11,11 @@ export const generateIndexFile = ({ files }: { files: Files }): void => {
name: 'index.ts',
});

if (config.name) {
if (legacyNameFromConfig(config)) {
files.index.add(
compiler.exportNamedDeclaration({
exports: config.name,
module: `./${config.name}`,
exports: legacyNameFromConfig(config)!,
module: `./${legacyNameFromConfig(config)}`,
}),
);
}
Expand All @@ -35,7 +35,7 @@ export const generateIndexFile = ({ files }: { files: Files }): void => {
}),
);
}
if (config.name) {
if (legacyNameFromConfig(config)) {
files.index.add(
compiler.exportNamedDeclaration({
exports: 'BaseHttpRequest',
Expand Down
12 changes: 10 additions & 2 deletions packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import type { PluginNames } from './plugins/types';
import type { Client } from './types/client';
import type { ClientConfig, Config, UserConfig } from './types/config';
import { CLIENTS } from './types/config';
import { isLegacyClient, setConfig } from './utils/config';
import {
isLegacyClient,
legacyNameFromConfig,
setConfig,
} from './utils/config';
import { getOpenApiSpec } from './utils/getOpenApiSpec';
import { registerHandlebarTemplates } from './utils/handlebars';
import { Performance, PerformanceReport } from './utils/performance';
Expand Down Expand Up @@ -351,7 +355,11 @@ export async function createClient(
operationParameter: operationParameterNameFn,
},
};
if (config.experimentalParser && !isLegacyClient(config) && !config.name) {
if (
config.experimentalParser &&
!isLegacyClient(config) &&
!legacyNameFromConfig(config)
) {
context = parseExperimental({
config,
parserConfig,
Expand Down
22 changes: 15 additions & 7 deletions packages/openapi-ts/src/plugins/@hey-api/services/plugin-legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import type {
} from '../../../types/client';
import type { Config } from '../../../types/config';
import { camelCase } from '../../../utils/camelCase';
import { getConfig, isLegacyClient } from '../../../utils/config';
import {
getConfig,
isLegacyClient,
legacyNameFromConfig,
} from '../../../utils/config';
import { escapeComment, escapeName } from '../../../utils/escape';
import { reservedWordsRegExp } from '../../../utils/regexp';
import { transformServiceName } from '../../../utils/transform';
Expand Down Expand Up @@ -555,7 +559,7 @@ const toOperationStatements = (
];
}

if (config.name) {
if (legacyNameFromConfig(config)) {
return [
compiler.returnFunctionCall({
args: [options],
Expand Down Expand Up @@ -651,7 +655,10 @@ const processService = ({
name: 'ThrowOnError',
};

if (!config.plugins['@hey-api/services']?.asClass && !config.name) {
if (
!config.plugins['@hey-api/services']?.asClass &&
!legacyNameFromConfig(config)
) {
for (const operation of service.operations) {
const compileFunctionParams = {
parameters: toOperationParamType(client, operation),
Expand Down Expand Up @@ -691,7 +698,8 @@ const processService = ({
accessLevel: 'public',
comment: toOperationComment(operation),
isStatic:
config.name === undefined && config.client.name !== 'legacy/angular',
legacyNameFromConfig(config) === undefined &&
config.client.name !== 'legacy/angular',
name: serviceFunctionIdentifier({
config,
id: operation.name,
Expand All @@ -717,7 +725,7 @@ const processService = ({
}

// Push constructor to front if needed
if (config.name) {
if (legacyNameFromConfig(config)) {
members = [
compiler.constructorDeclaration({
multiLine: false,
Expand Down Expand Up @@ -803,7 +811,7 @@ export const handlerLegacy: PluginLegacyHandler<any> = ({ client, files }) => {
name: 'Injectable',
});

if (!config.name) {
if (!legacyNameFromConfig(config)) {
files.services.import({
module: '@angular/common/http',
name: 'HttpClient',
Expand Down Expand Up @@ -831,7 +839,7 @@ export const handlerLegacy: PluginLegacyHandler<any> = ({ client, files }) => {
});
}

if (config.name) {
if (legacyNameFromConfig(config)) {
files.services.import({
asType: config.client.name !== 'legacy/angular',
module: './core/BaseHttpRequest',
Expand Down
11 changes: 11 additions & 0 deletions packages/openapi-ts/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ export const isLegacyClient = (config: Config | Config['client']) => {
const client = 'client' in config ? config.client.name : config.name;
return client.startsWith('legacy/');
};

/**
* Wrap legacy `name` option so we don't use it when not using legacy clients.
*/
export const legacyNameFromConfig = (config: Config) => {
if (!isLegacyClient(config)) {
return;
}

return config.name;
};
5 changes: 3 additions & 2 deletions packages/openapi-ts/src/utils/postprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Client as ParserClient, Model } from '../openApi';
import { sanitizeNamespaceIdentifier } from '../openApi';
import type { Client, Operation, Service } from '../types/client';
import { camelCase } from './camelCase';
import { getConfig } from './config';
import { getConfig, legacyNameFromConfig } from './config';
import { sort } from './sort';
import { unique } from './unique';

Expand Down Expand Up @@ -47,7 +47,8 @@ const postProcessOperations = (
operations.forEach((parserOperation) => {
const tags =
parserOperation.tags?.length &&
(config.plugins['@hey-api/services']?.asClass || config.name)
(config.plugins['@hey-api/services']?.asClass ||
legacyNameFromConfig(config))
? parserOperation.tags.filter(unique)
: ['Default'];
tags.forEach((tag) => {
Expand Down

0 comments on commit d2a039b

Please sign in to comment.