Skip to content

Commit

Permalink
fix: update types for custom plugins so defineConfig does not throw
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Dec 18, 2024
1 parent f1dfeb4 commit 1eb2cfa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-knives-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: update types for custom plugins so defineConfig does not throw
20 changes: 11 additions & 9 deletions packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { parseExperimental, parseLegacy } from './openApi';
import type { ClientPlugins, UserPlugins } from './plugins';
import { defaultPluginConfigs } from './plugins';
import type {
AnyPluginName,
DefaultPluginConfigs,
PluginContext,
PluginNames,
Expand Down Expand Up @@ -190,30 +191,30 @@ const getPluginsConfig = ({
userPluginsConfig,
}: {
pluginConfigs: DefaultPluginConfigs<ClientPlugins>;
userPlugins: ReadonlyArray<PluginNames>;
userPlugins: ReadonlyArray<AnyPluginName>;
userPluginsConfig: Config['plugins'];
}): Pick<Config, 'plugins' | 'pluginOrder'> => {
const circularReferenceTracker = new Set<PluginNames>();
const pluginOrder = new Set<PluginNames>();
const circularReferenceTracker = new Set<AnyPluginName>();
const pluginOrder = new Set<AnyPluginName>();
const plugins: Config['plugins'] = {};

const dfs = (name: PluginNames) => {
const dfs = (name: AnyPluginName) => {
if (circularReferenceTracker.has(name)) {
throw new Error(`Circular reference detected at '${name}'`);
}

if (!pluginOrder.has(name)) {
circularReferenceTracker.add(name);

const pluginConfig = pluginConfigs[name];
const pluginConfig = pluginConfigs[name as PluginNames];
if (!pluginConfig) {
throw new Error(
`🚫 unknown plugin dependency "${name}" - do you need to register a custom plugin with this name?`,
);
}

const defaultOptions = defaultPluginConfigs[name];
const userOptions = userPluginsConfig[name];
const defaultOptions = defaultPluginConfigs[name as PluginNames];
const userOptions = userPluginsConfig[name as PluginNames];
if (userOptions && defaultOptions) {
const nativePluginOption = Object.keys(userOptions).find((key) =>
key.startsWith('_'),
Expand Down Expand Up @@ -243,7 +244,8 @@ const getPluginsConfig = ({
},
pluginByTag: (tag) => {
for (const userPlugin of userPlugins) {
const defaultConfig = defaultPluginConfigs[userPlugin];
const defaultConfig =
defaultPluginConfigs[userPlugin as PluginNames];
if (
defaultConfig &&
defaultConfig._tags?.includes(tag) &&
Expand Down Expand Up @@ -274,7 +276,7 @@ const getPluginsConfig = ({
}

return {
pluginOrder: Array.from(pluginOrder),
pluginOrder: Array.from(pluginOrder) as ReadonlyArray<PluginNames>,
plugins,
};
};
Expand Down
20 changes: 15 additions & 5 deletions packages/openapi-ts/src/plugins/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ export type PluginNames =
| 'fastify'
| 'zod';

// eslint-disable-next-line @typescript-eslint/ban-types
export type AnyPluginName = PluginNames | (string & {});

type PluginTag = 'transformer' | 'validator';

export interface PluginContext {
ensureDependency: (name: PluginNames | true) => void;
pluginByTag: (tag: PluginTag) => PluginNames | undefined;
pluginByTag: (tag: PluginTag) => AnyPluginName | undefined;
}

interface BaseConfig {
Expand All @@ -35,8 +38,7 @@ interface BaseConfig {
* barrel file?
*/
exportFromIndex?: boolean;
// eslint-disable-next-line @typescript-eslint/ban-types
name: PluginNames | (string & {});
name: AnyPluginName;
output?: string;
}

Expand All @@ -45,7 +47,7 @@ interface Meta<Config extends BaseConfig> {
* Dependency plugins will be always processed, regardless of whether user
* explicitly defines them in their `plugins` config.
*/
_dependencies?: ReadonlyArray<PluginNames>;
_dependencies?: ReadonlyArray<AnyPluginName>;
/**
* Allows overriding config before it's sent to the parser. An example is
* defining `validator` as `true` and the plugin figures out which plugin
Expand Down Expand Up @@ -83,7 +85,15 @@ export namespace Plugin {

export type DefineConfig<Config extends BaseConfig> = (
config?: Plugin.UserConfig<Config>,
) => Plugin.Config<Config>;
) => Omit<Plugin.Config<Config>, 'name'> & {
/**
* Cast name to `any` so it doesn't throw type error in `plugins` array.
* We could allow any `string` as plugin `name` in the object syntax, but
* that TypeScript trick would cause all string methods to appear as
* suggested auto completions, which is undesirable.
*/
name: any;
};

/**
* Plugin implementation for experimental parser.
Expand Down
4 changes: 0 additions & 4 deletions packages/openapi-ts/test/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ for (const version of versions) {
experimentalParser: true,
input: path.join(__dirname, 'spec', '3.1.x', 'full.json'),
output: path.join(outputDir, myPlugin.name, 'default'),
// @ts-expect-error
plugins: [myPlugin],
});

Expand All @@ -273,7 +272,6 @@ for (const version of versions) {
name: string;
output: string;
}> = {
// @ts-expect-error
_dependencies: ['@hey-api/oops'],
_handler: vi.fn(),
_handlerLegacy: vi.fn(),
Expand All @@ -290,7 +288,6 @@ for (const version of versions) {
level: 'silent',
},
output: path.join(outputDir, myPlugin.name, 'default'),
// @ts-expect-error
plugins: [myPlugin],
}),
).rejects.toThrowError(/unknown plugin/g);
Expand Down Expand Up @@ -319,7 +316,6 @@ for (const version of versions) {
level: 'silent',
},
output: path.join(outputDir, myPlugin.name, 'default'),
// @ts-expect-error
plugins: [myPlugin],
}),
).rejects.toThrowError(/cannot register plugin/g);
Expand Down

0 comments on commit 1eb2cfa

Please sign in to comment.