Skip to content

Commit 41215a2

Browse files
committed
feat: use log.warn from codegen-core and handle function values in dedup
1 parent adcfc47 commit 41215a2

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

packages/openapi-ts/src/__tests__/index.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,45 @@ describe('createClient', () => {
413413

414414
warnSpy.mockRestore();
415415
});
416+
417+
it('does not warn when function-valued options have identical source', async () => {
418+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
419+
const transform = (s: string) => s.toUpperCase();
420+
421+
await createClient({
422+
...baseConfig,
423+
plugins: [
424+
{ definitions: { name: transform }, name: '@hey-api/typescript' },
425+
{ definitions: { name: transform }, name: '@hey-api/typescript' },
426+
],
427+
});
428+
429+
expect(conflictWarnings(warnSpy)).toHaveLength(0);
430+
431+
warnSpy.mockRestore();
432+
});
433+
434+
it('warns when function-valued options differ', async () => {
435+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
436+
437+
await createClient({
438+
...baseConfig,
439+
plugins: [
440+
{
441+
definitions: { name: (s: string) => s.toUpperCase() },
442+
name: '@hey-api/typescript',
443+
},
444+
{
445+
definitions: { name: (s: string) => s.toLowerCase() },
446+
name: '@hey-api/typescript',
447+
},
448+
],
449+
});
450+
451+
expect(conflictWarnings(warnSpy)).toHaveLength(1);
452+
453+
warnSpy.mockRestore();
454+
});
416455
});
417456

418457
it('executes @angular/common HttpRequest builder path', async () => {

packages/openapi-ts/src/config/plugins.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { log } from '@hey-api/codegen-core';
12
import type { AnyPluginName, PluginContext, PluginNames } from '@hey-api/shared';
23
import { dependencyFactory, valueToObject } from '@hey-api/shared';
3-
import colors from 'ansi-colors';
44

55
import { defaultPluginConfigs } from '../plugins/config';
66
import type { Config, UserConfig } from './types';
@@ -150,24 +150,27 @@ export function getPlugins({
150150
const seenPlugins = new Map<string, string>();
151151

152152
const stableStringify = (value: unknown): string =>
153-
JSON.stringify(value, (_, v) =>
154-
v && typeof v === 'object' && !Array.isArray(v)
155-
? Object.fromEntries(
156-
Object.entries(v as Record<string, unknown>).sort(([a], [b]) => a.localeCompare(b)),
157-
)
158-
: v,
159-
);
160-
161-
const warnConflictingPlugin = (name: string) =>
162-
console.warn(
163-
`⚙️ ${colors.yellow('Warning:')} Plugin ${colors.cyan(`"${name}"`)} is configured more than once with conflicting options. Only the last occurrence will take effect.`,
164-
);
153+
JSON.stringify(value, (_, v) => {
154+
if (typeof v === 'function') {
155+
return `[function:${(v as () => unknown).toString()}]`;
156+
}
157+
if (v && typeof v === 'object' && !Array.isArray(v)) {
158+
return Object.fromEntries(
159+
Object.entries(v as Record<string, unknown>).sort(([a], [b]) =>
160+
a.localeCompare(b),
161+
),
162+
);
163+
}
164+
return v;
165+
});
165166

166167
const checkDuplicate = (name: string, config: Record<string, unknown>) => {
167168
const serialized = stableStringify(config);
168169
const previous = seenPlugins.get(name);
169170
if (previous !== undefined && previous !== serialized) {
170-
warnConflictingPlugin(name);
171+
log.warn(
172+
`Plugin "${name}" is configured more than once with conflicting options. Only the last occurrence will take effect.`,
173+
);
171174
}
172175
seenPlugins.set(name, serialized);
173176
};

0 commit comments

Comments
 (0)