Skip to content

Commit

Permalink
Merge pull request #1125 from hey-api/chore/clean-up
Browse files Browse the repository at this point in the history
chore: rename isStandaloneClient function to isLegacyClient
  • Loading branch information
mrlubos authored Oct 5, 2024
2 parents 4afe9bd + 7615fd9 commit e908593
Show file tree
Hide file tree
Showing 25 changed files with 176 additions and 122 deletions.
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/generate/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { copyFileSync } from 'node:fs';
import path from 'node:path';

import { getConfig, isStandaloneClient } from '../utils/config';
import { getConfig, isLegacyClient } from '../utils/config';
import { ensureDirSync, relativeModulePath } from './utils';

export const clientModulePath = ({
Expand Down Expand Up @@ -35,7 +35,7 @@ export const generateClient = async (
) => {
const config = getConfig();

if (!isStandaloneClient(config) || !config.client.bundle) {
if (isLegacyClient(config) || !config.client.bundle) {
return;
}

Expand Down
10 changes: 4 additions & 6 deletions packages/openapi-ts/src/generate/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path';
import { TypeScriptFile } from '../compiler';
import type { Client } from '../types/client';
import type { Files } from '../types/utils';
import { getConfig, isStandaloneClient } from '../utils/config';
import { getConfig, isLegacyClient } from '../utils/config';

export const generatePlugins = async ({
client,
Expand All @@ -14,10 +14,8 @@ export const generatePlugins = async ({
}) => {
const config = getConfig();

const isStandalone = isStandaloneClient(config);

if (!isStandalone) {
// plugins work only with standalone clients
if (isLegacyClient(config)) {
// plugins do not work with legacy clients
return;
}

Expand All @@ -34,7 +32,7 @@ export const generatePlugins = async ({
plugin.handler({
client,
files,
plugin,
plugin: plugin as never,
});
}
};
28 changes: 14 additions & 14 deletions packages/openapi-ts/src/generate/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
} from '../types/client';
import type { Files } from '../types/utils';
import { camelCase } from '../utils/camelCase';
import { getConfig, isStandaloneClient } from '../utils/config';
import { getConfig, isLegacyClient } from '../utils/config';
import { escapeComment, escapeName } from '../utils/escape';
import { reservedWordsRegExp } from '../utils/regexp';
import { transformServiceName } from '../utils/transform';
Expand Down Expand Up @@ -115,7 +115,7 @@ const toOperationParamType = (

const isRequired = isOperationParameterRequired(operation.parameters);

if (isStandaloneClient(config)) {
if (!isLegacyClient(config)) {
return [
{
isRequired,
Expand Down Expand Up @@ -205,7 +205,7 @@ const toOperationReturnType = (client: Client, operation: Operation) => {
const toOperationComment = (operation: Operation): Comments => {
const config = getConfig();

if (isStandaloneClient(config)) {
if (!isLegacyClient(config)) {
const comment = [
operation.deprecated && '@deprecated',
operation.summary && escapeComment(operation.summary),
Expand Down Expand Up @@ -273,7 +273,7 @@ const toRequestOptions = (
onImport(responseTransformerName);
}

if (isStandaloneClient(config)) {
if (!isLegacyClient(config)) {
let obj: ObjectValue[] = [
{
spread: 'options',
Expand Down Expand Up @@ -490,7 +490,7 @@ const toOperationStatements = (

const options = toRequestOptions(client, operation, onImport, onClientImport);

if (isStandaloneClient(config)) {
if (!isLegacyClient(config)) {
const errorType = setUniqueTypeName({
client,
meta: {
Expand Down Expand Up @@ -573,7 +573,7 @@ const processService = ({
}) => {
const config = getConfig();

const isStandalone = isStandaloneClient(config);
const isLegacy = isLegacyClient(config);

for (const operation of service.operations) {
if (operation.parameters.length) {
Expand All @@ -590,7 +590,7 @@ const processService = ({
});
}

if (isStandalone) {
if (!isLegacy) {
generateImport({
client,
meta: {
Expand Down Expand Up @@ -632,7 +632,7 @@ const processService = ({
for (const operation of service.operations) {
const compileFunctionParams = {
parameters: toOperationParamType(client, operation),
returnType: isStandalone
returnType: !isLegacy
? undefined
: toOperationReturnType(client, operation),
statements: toOperationStatements(
Expand All @@ -641,7 +641,7 @@ const processService = ({
onImport,
onClientImport,
),
types: isStandalone ? [throwOnErrorTypeGeneric] : undefined,
types: !isLegacy ? [throwOnErrorTypeGeneric] : undefined,
};
const expression =
config.client.name === 'legacy/angular'
Expand All @@ -666,7 +666,7 @@ const processService = ({
config.name === undefined && config.client.name !== 'legacy/angular',
name: toOperationName(operation, false),
parameters: toOperationParamType(client, operation),
returnType: isStandalone
returnType: !isLegacy
? undefined
: toOperationReturnType(client, operation),
statements: toOperationStatements(
Expand All @@ -675,7 +675,7 @@ const processService = ({
onImport,
onClientImport,
),
types: isStandalone ? [throwOnErrorTypeGeneric] : undefined,
types: !isLegacy ? [throwOnErrorTypeGeneric] : undefined,
});
return node;
});
Expand Down Expand Up @@ -759,7 +759,7 @@ export const generateServices = async ({

checkPrerequisites({ files });

const isStandalone = isStandaloneClient(config);
const isLegacy = isLegacyClient(config);

const servicesOutput = 'services';

Expand All @@ -769,7 +769,7 @@ export const generateServices = async ({
});

// Import required packages and core files.
if (isStandalone) {
if (!isLegacy) {
files.services.import({
module: clientModulePath({ sourceOutput: servicesOutput }),
name: 'createClient',
Expand Down Expand Up @@ -838,7 +838,7 @@ export const generateServices = async ({
}

// define client first
if (isStandalone) {
if (!isLegacy) {
const statement = compiler.constVariable({
exportConst: true,
expression: compiler.callExpression({
Expand Down
12 changes: 6 additions & 6 deletions packages/openapi-ts/src/generate/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { isOperationParameterRequired } from '../openApi';
import type { Method, Model, OperationParameter } from '../types/client';
import type { Client } from '../types/client';
import type { Files } from '../types/utils';
import { getConfig, isStandaloneClient } from '../utils/config';
import { getConfig, isLegacyClient } from '../utils/config';
import { enumEntry, enumUnionType } from '../utils/enum';
import { escapeComment } from '../utils/escape';
import { sortByName, sorterByName } from '../utils/sort';
Expand Down Expand Up @@ -301,7 +301,7 @@ const processServiceTypes = ({
return;
}

const isStandalone = isStandaloneClient(config);
const isLegacy = isLegacyClient(config);

for (const service of client.services) {
for (const operation of service.operations) {
Expand Down Expand Up @@ -382,8 +382,8 @@ const processServiceTypes = ({
),
),
mediaType: null,
name: isStandalone ? 'headers' : 'header',
prop: isStandalone ? 'headers' : 'header',
name: isLegacy ? 'header' : 'headers',
prop: isLegacy ? 'header' : 'headers',
properties: operation.parameters
.filter((parameter) => parameter.in === 'header')
.sort(sorterByName),
Expand Down Expand Up @@ -416,7 +416,7 @@ const processServiceTypes = ({
.filter((parameter) => parameter.in === 'query')
.sort(sorterByName),
};
const operationProperties = isStandalone
const operationProperties = !isLegacy
? [
bodyParameters,
headerParameters,
Expand Down Expand Up @@ -479,7 +479,7 @@ const processServiceTypes = ({
response.responseTypes.includes('error'),
);

if (isStandalone) {
if (!isLegacy) {
// create type export for operation error
generateType({
client,
Expand Down
7 changes: 3 additions & 4 deletions packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { defaultPluginConfigs } from './plugins';
import type { Client } from './types/client';
import type { ClientConfig, Config, UserConfig } from './types/config';
import { CLIENTS } from './types/config';
import { getConfig, isStandaloneClient, setConfig } from './utils/config';
import { getConfig, isLegacyClient, setConfig } from './utils/config';
import { getOpenApiSpec } from './utils/getOpenApiSpec';
import { registerHandlebarTemplates } from './utils/handlebars';
import {
Expand Down Expand Up @@ -146,7 +146,7 @@ const getPlugins = (userConfig: ClientConfig): Config['plugins'] => {
...defaultPluginConfigs[plugin.name],
...(plugin as (typeof defaultPluginConfigs)[(typeof plugin)['name']]),
},
);
) as unknown as Config['plugins'];
return plugins;
};

Expand Down Expand Up @@ -293,8 +293,7 @@ const initConfigs = async (userConfig: UserConfig): Promise<Config[]> => {
debug,
dryRun,
experimental_parser,
exportCore:
isStandaloneClient(client) || !client.name ? false : exportCore,
exportCore: isLegacyClient(client) ? exportCore : false,
input,
name,
output,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ vi.mock('../../../../utils/config', () => {
};
return {
getConfig: () => config,
isStandaloneClient: vi.fn().mockReturnValue(false),
isLegacyClient: vi.fn().mockReturnValue(true),
};
});

Expand Down
10 changes: 7 additions & 3 deletions packages/openapi-ts/src/plugins/@hey-api/schemas/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PluginDefinition } from '../../types';
import type { PluginHandler } from '../../types';

export interface PluginConfig extends PluginDefinition {
interface Config {
/**
* Generate Hey API schemas from the provided input.
*/
Expand All @@ -12,4 +12,8 @@ export interface PluginConfig extends PluginDefinition {
output?: string;
}

export interface UserConfig extends Pick<PluginConfig, 'name'> {}
export interface PluginConfig extends Config {
handler: PluginHandler<Config>;
}

export interface UserConfig extends Omit<Config, 'output'> {}
10 changes: 7 additions & 3 deletions packages/openapi-ts/src/plugins/@hey-api/services/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PluginDefinition } from '../../types';
import type { PluginHandler } from '../../types';

export interface PluginConfig extends PluginDefinition {
interface Config {
/**
* Generate Hey API services from the provided input.
*/
Expand All @@ -12,4 +12,8 @@ export interface PluginConfig extends PluginDefinition {
output?: string;
}

export interface UserConfig extends Pick<PluginConfig, 'name'> {}
export interface PluginConfig extends Config {
handler: PluginHandler<Config>;
}

export interface UserConfig extends Omit<Config, 'output'> {}
10 changes: 7 additions & 3 deletions packages/openapi-ts/src/plugins/@hey-api/types/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PluginDefinition } from '../../types';
import type { PluginHandler } from '../../types';

export interface PluginConfig extends PluginDefinition {
interface Config {
/**
* Generate Hey API types from the provided input.
*/
Expand All @@ -12,4 +12,8 @@ export interface PluginConfig extends PluginDefinition {
output?: string;
}

export interface UserConfig extends Pick<PluginConfig, 'name'> {}
export interface PluginConfig extends Config {
handler: PluginHandler<Config>;
}

export interface UserConfig extends Omit<Config, 'output'> {}
26 changes: 11 additions & 15 deletions packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ import type {
import type { Files } from '../../../types/utils';
import { getConfig } from '../../../utils/config';
import { transformServiceName } from '../../../utils/transform';
import type { PluginDefinition } from '../../types';
import type { PluginHandler } from '../../types';
import type { PluginConfig as ReactQueryPluginConfig } from '../react-query';
import type { PluginConfig as SolidQueryPluginConfig } from '../solid-query';
import type { PluginConfig as SvelteQueryPluginConfig } from '../svelte-query';
import type { PluginConfig as VueQueryPluginConfig } from '../vue-query';

const toInfiniteQueryOptionsName = (operation: Operation) =>
`${toOperationName(operation, false)}InfiniteOptions`;
Expand Down Expand Up @@ -641,20 +645,12 @@ const createQueryKeyLiteral = ({
return queryKeyLiteral;
};

export const handler: PluginDefinition['handler'] = ({
client,
files,
plugin,
}) => {
if (
plugin.name !== '@tanstack/react-query' &&
plugin.name !== '@tanstack/solid-query' &&
plugin.name !== '@tanstack/svelte-query' &&
plugin.name !== '@tanstack/vue-query'
) {
return;
}

export const handler: PluginHandler<
| ReactQueryPluginConfig
| SolidQueryPluginConfig
| SvelteQueryPluginConfig
| VueQueryPluginConfig
> = ({ client, files, plugin }) => {
checkPrerequisites({ files });

const config = getConfig();
Expand Down
14 changes: 7 additions & 7 deletions packages/openapi-ts/src/plugins/@tanstack/react-query/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PluginDefinition } from '../../types';
import type { PluginHandler } from '../../types';

export interface PluginConfig extends PluginDefinition {
interface Config {
/**
* Generate {@link https://tanstack.com/query/v5/docs/framework/react/reference/infiniteQueryOptions `infiniteQueryOptions()`} helpers? These will be generated from GET and POST requests where a pagination parameter is detected.
* @default true
Expand Down Expand Up @@ -28,8 +28,8 @@ export interface PluginConfig extends PluginDefinition {
queryOptions?: boolean;
}

export interface UserConfig
extends Pick<
PluginConfig,
'infiniteQueryOptions' | 'mutationOptions' | 'name' | 'queryOptions'
> {}
export interface PluginConfig extends Config {
handler: PluginHandler<Config>;
}

export interface UserConfig extends Omit<Config, 'output'> {}
Loading

0 comments on commit e908593

Please sign in to comment.