Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: export Plugin API namespace #1368

Merged
merged 1 commit into from
Dec 1, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/sharp-dogs-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: export Plugin API namespace
16 changes: 8 additions & 8 deletions docs/openapi-ts/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Featured community plugins.
## Custom

::: warning
Plugins API is in development. The interface might change before it becomes stable. We encourage you to leave feedback on [GitHub](https://github.com/hey-api/openapi-ts/issues).
Plugin API is in development. The interface might change before it becomes stable. We encourage you to leave feedback on [GitHub](https://github.com/hey-api/openapi-ts/issues).
:::

If the existing plugins do not handle your use case or you're working with proprietary packages, you might want to create your own plugin.
Expand Down Expand Up @@ -79,17 +79,17 @@ export interface Config {

:::

`config.ts` contains the runtime configuration for your plugin. It must implement the `Config` interface from `types.d.ts` and additional plugin metadata defined in the `PluginConfig` interface.
`config.ts` contains the runtime configuration for your plugin. It must implement the `Config` interface from `types.d.ts` and additional plugin metadata defined in the `Plugin.Config` interface.

::: code-group

```ts [config.ts]
import type { Plugins } from '@hey-api/openapi-ts';
import type { Plugin } from '@hey-api/openapi-ts';

import { handler } from './plugin';
import type { Config } from './types';

export const defaultConfig: Plugins.PluginConfig<Config> = {
export const defaultConfig: Plugin.Config<Config> = {
_dependencies: ['@hey-api/typescript'],
_handler: handler,
_handlerLegacy: () => {},
Expand All @@ -98,9 +98,9 @@ export const defaultConfig: Plugins.PluginConfig<Config> = {
};

/**
* Type helper for `my-plugin` plugin, returns {@link Plugins.PluginConfig} object
* Type helper for `my-plugin` plugin, returns {@link Plugin.Config} object
*/
export const defineConfig: Plugins.DefineConfig<Config> = (config) => ({
export const defineConfig: Plugin.DefineConfig<Config> = (config) => ({
...defaultConfig,
...config,
});
Expand All @@ -115,11 +115,11 @@ Lastly, we define the `_handler` method which will be responsible for generating
::: code-group

```ts [plugin.ts]
import type { Plugins } from '@hey-api/openapi-ts';
import type { Plugin } from '@hey-api/openapi-ts';

import type { Config } from './types';

export const handler: Plugins.PluginHandler<Config> = ({ context, plugin }) => {
export const handler: Plugin.Handler<Config> = ({ context, plugin }) => {
// create a file for our output
const file = context.createFile({
id: plugin.name,
Expand Down
6 changes: 3 additions & 3 deletions packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { IRContext } from './ir/context';
import { parseExperimental, parseLegacy } from './openApi';
import type { ClientPlugins } from './plugins';
import { defaultPluginConfigs } from './plugins';
import type { DefaultPluginConfigsMap, PluginNames } from './plugins/types';
import type { DefaultPluginConfigs, PluginNames } from './plugins/types';
import type { Client } from './types/client';
import type {
ClientConfig,
Expand Down Expand Up @@ -167,7 +167,7 @@ const getPluginOrder = ({
pluginConfigs,
userPlugins,
}: {
pluginConfigs: DefaultPluginConfigsMap<ClientPlugins>;
pluginConfigs: DefaultPluginConfigs<ClientPlugins>;
userPlugins: ReadonlyArray<PluginNames>;
}): Config['pluginOrder'] => {
const circularReferenceTracker = new Set<PluginNames>();
Expand Down Expand Up @@ -486,5 +486,5 @@ export default {

export type { OpenApiV3_0_X } from './openApi/3.0.x';
export type { OpenApiV3_1_X } from './openApi/3.1.x';
export type * as Plugins from './plugins/types';
export type { Plugin } from './plugins/types';
export type { UserConfig } from './types/config';
8 changes: 4 additions & 4 deletions packages/openapi-ts/src/plugins/@hey-api/schemas/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { DefineConfig, PluginConfig } from '../../types';
import type { Plugin } from '../../types';
import { handler } from './plugin';
import { handlerLegacy } from './plugin-legacy';
import type { Config } from './types';

export const defaultConfig: PluginConfig<Config> = {
export const defaultConfig: Plugin.Config<Config> = {
_handler: handler,
_handlerLegacy: handlerLegacy,
name: '@hey-api/schemas',
Expand All @@ -13,9 +13,9 @@ export const defaultConfig: PluginConfig<Config> = {
};

/**
* Type helper for `@hey-api/schemas` plugin, returns {@link PluginConfig} object
* Type helper for `@hey-api/schemas` plugin, returns {@link Plugin.Config} object
*/
export const defineConfig: DefineConfig<Config> = (config) => ({
export const defineConfig: Plugin.DefineConfig<Config> = (config) => ({
...defaultConfig,
...config,
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TypeScriptFile } from '../../../generate/files';
import type { OpenApiV2Schema, OpenApiV3Schema } from '../../../openApi';
import { ensureValidTypeScriptJavaScriptIdentifier } from '../../../openApi';
import { getConfig } from '../../../utils/config';
import type { PluginLegacyHandler } from '../../types';
import type { Plugin } from '../../types';
import type { Config } from './types';

const ensureValidSchemaOutput = (
Expand Down Expand Up @@ -69,7 +69,7 @@ const toSchemaName = (
return `${validName}Schema`;
};

export const handlerLegacy: PluginLegacyHandler<Config> = ({
export const handlerLegacy: Plugin.LegacyHandler<Config> = ({
files,
openApi,
}) => {
Expand Down
16 changes: 8 additions & 8 deletions packages/openapi-ts/src/plugins/@hey-api/schemas/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
} from '../../../openApi/3.0.x/types/spec';
import type { OpenApiV3_1_X } from '../../../openApi/3.1.x';
import type { SchemaObject as OpenApiV3_1_XSchemaObject } from '../../../openApi/3.1.x/types/spec';
import type { Plugin, PluginHandler } from '../../types';
import type { Plugin } from '../../types';
import type { Config } from './types';

const schemasId = 'schemas';
Expand All @@ -17,7 +17,7 @@
plugin,
schema,
}: {
plugin: Plugin<Config>;
plugin: Plugin.Instance<Config>;
schema: OpenApiV3_0_XSchemaObject | OpenApiV3_1_XSchemaObject;
}) => {
if (plugin.type === 'form') {
Expand Down Expand Up @@ -49,7 +49,7 @@
schema: _schema,
}: {
context: IRContext;
plugin: Plugin<Config>;
plugin: Plugin.Instance<Config>;

Check warning on line 52 in packages/openapi-ts/src/plugins/@hey-api/schemas/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/@hey-api/schemas/plugin.ts#L52

Added line #L52 was not covered by tests
schema: OpenApiV3_0_XSchemaObject | OpenApiV3_0_XReferenceObject;
}): object => {
if (Array.isArray(_schema)) {
Expand Down Expand Up @@ -145,7 +145,7 @@
schema: _schema,
}: {
context: IRContext;
plugin: Plugin<Config>;
plugin: Plugin.Instance<Config>;
schema: OpenApiV3_1_XSchemaObject;
}): object => {
if (Array.isArray(_schema)) {
Expand Down Expand Up @@ -250,7 +250,7 @@
schema,
}: {
name: string;
plugin: Plugin<Config>;
plugin: Plugin.Instance<Config>;
schema:
| OpenApiV3_0_XReferenceObject
| OpenApiV3_0_XSchemaObject
Expand All @@ -270,7 +270,7 @@
plugin,
}: {
context: IRContext<OpenApiV3_0_X>;
plugin: Plugin<Config>;
plugin: Plugin.Instance<Config>;

Check warning on line 273 in packages/openapi-ts/src/plugins/@hey-api/schemas/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/@hey-api/schemas/plugin.ts#L273

Added line #L273 was not covered by tests
}) => {
if (!context.spec.components) {
return;
Expand Down Expand Up @@ -298,7 +298,7 @@
plugin,
}: {
context: IRContext<OpenApiV3_1_X>;
plugin: Plugin<Config>;
plugin: Plugin.Instance<Config>;
}) => {
if (!context.spec.components) {
return;
Expand All @@ -321,7 +321,7 @@
}
};

export const handler: PluginHandler<Config> = ({ context, plugin }) => {
export const handler: Plugin.Handler<Config> = ({ context, plugin }) => {
context.createFile({
id: schemasId,
path: plugin.output,
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/plugins/@hey-api/schemas/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import type {
SchemaObject as OpenApiV3_0_XSchemaObject,
} from '../../../openApi/3.0.x/types/spec';
import type { SchemaObject as OpenApiV3_1_XSchemaObject } from '../../../openApi/3.1.x/types/spec';
import type { PluginName } from '../../types';
import type { Plugin } from '../../types';

export interface Config extends PluginName<'@hey-api/schemas'> {
export interface Config extends Plugin.Name<'@hey-api/schemas'> {
/**
* Customise the schema name. By default, `{{name}}Schema` is used. `name` is a
* valid JavaScript/TypeScript identifier, e.g. if your schema name is
Expand Down
8 changes: 4 additions & 4 deletions packages/openapi-ts/src/plugins/@hey-api/sdk/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { DefineConfig, PluginConfig } from '../../types';
import type { Plugin } from '../../types';
import { handler } from './plugin';
import { handlerLegacy } from './plugin-legacy';
import type { Config } from './types';

export const defaultConfig: PluginConfig<Config> = {
export const defaultConfig: Plugin.Config<Config> = {
_dependencies: ['@hey-api/typescript'],
_handler: handler,
_handlerLegacy: handlerLegacy,
Expand All @@ -17,9 +17,9 @@ export const defaultConfig: PluginConfig<Config> = {
};

/**
* Type helper for `@hey-api/sdk` plugin, returns {@link PluginConfig} object
* Type helper for `@hey-api/sdk` plugin, returns {@link Plugin.Config} object
*/
export const defineConfig: DefineConfig<Config> = (config) => ({
export const defineConfig: Plugin.DefineConfig<Config> = (config) => ({
...defaultConfig,
...config,
});
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/plugins/@hey-api/sdk/plugin-legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { stringCase } from '../../../utils/stringCase';
import { transformServiceName } from '../../../utils/transform';
import { setUniqueTypeName } from '../../../utils/type';
import { unique } from '../../../utils/unique';
import type { PluginLegacyHandler } from '../../types';
import type { Plugin } from '../../types';

type OnNode = (node: Node) => void;
type OnImport = (name: string) => void;
Expand Down Expand Up @@ -768,7 +768,7 @@ const processService = ({
onNode(statement);
};

export const handlerLegacy: PluginLegacyHandler<any> = ({ client, files }) => {
export const handlerLegacy: Plugin.LegacyHandler<any> = ({ client, files }) => {
const config = getConfig();

if (!config.client.name) {
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/plugins/@hey-api/sdk/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getServiceName } from '../../../utils/postprocess';
import { irRef } from '../../../utils/ref';
import { stringCase } from '../../../utils/stringCase';
import { transformServiceName } from '../../../utils/transform';
import type { PluginHandler } from '../../types';
import type { Plugin } from '../../types';
import { operationTransformerIrRef } from '../transformers/plugin';
import { serviceFunctionIdentifier } from './plugin-legacy';
import type { Config } from './types';
Expand Down Expand Up @@ -386,7 +386,7 @@ const generateFlatSdk = ({ context }: { context: IRContext }) => {
});
};

export const handler: PluginHandler<Config> = ({ context, plugin }) => {
export const handler: Plugin.Handler<Config> = ({ context, plugin }) => {
if (!context.config.client.name) {
throw new Error(
'🚫 client needs to be set to generate SDKs - which HTTP client do you want to use?',
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/plugins/@hey-api/sdk/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { IROperationObject } from '../../../ir/ir';
import type { Operation } from '../../../types/client';
import type { PluginName } from '../../types';
import type { Plugin } from '../../types';

export interface Config extends PluginName<'@hey-api/sdk'> {
export interface Config extends Plugin.Name<'@hey-api/sdk'> {
/**
* Group operation methods into classes? When enabled, you can
* select which classes to export with `sdk.include` and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { DefineConfig, PluginConfig } from '../../types';
import type { Plugin } from '../../types';
import { handler } from './plugin';
import { handlerLegacy } from './plugin-legacy';
import type { Config } from './types';

export const defaultConfig: PluginConfig<Config> = {
export const defaultConfig: Plugin.Config<Config> = {
_dependencies: ['@hey-api/typescript'],
_handler: handler,
_handlerLegacy: handlerLegacy,
Expand All @@ -13,9 +13,9 @@ export const defaultConfig: PluginConfig<Config> = {
};

/**
* Type helper for `@hey-api/transformers`, returns {@link PluginConfig} object
* Type helper for `@hey-api/transformers`, returns {@link Plugin.Config} object
*/
export const defineConfig: DefineConfig<Config> = (config) => ({
export const defineConfig: Plugin.DefineConfig<Config> = (config) => ({
...defaultConfig,
...config,
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getOperationKey } from '../../../openApi/common/parser/operation';
import type { ModelMeta, OperationResponse } from '../../../types/client';
import { getConfig } from '../../../utils/config';
import { isModelDate, unsetUniqueTypeName } from '../../../utils/type';
import type { PluginLegacyHandler } from '../../types';
import type { Plugin } from '../../types';
import {
modelResponseTransformerTypeName,
operationResponseTransformerTypeName,
Expand Down Expand Up @@ -246,7 +246,7 @@ const generateResponseTransformer = ({
};

// handles only response transformers for now
export const handlerLegacy: PluginLegacyHandler<Config> = ({
export const handlerLegacy: Plugin.LegacyHandler<Config> = ({
client,
files,
}) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { IRSchemaObject } from '../../../ir/ir';
import { operationResponsesMap } from '../../../ir/operation';
import { irRef } from '../../../utils/ref';
import { stringCase } from '../../../utils/stringCase';
import type { PluginHandler } from '../../types';
import type { Plugin } from '../../types';
import { operationIrRef } from '../sdk/plugin';
import type { Config } from './types';

Expand Down Expand Up @@ -365,7 +365,7 @@ const processSchemaType = ({
};

// handles only response transformers for now
export const handler: PluginHandler<Config> = ({ context, plugin }) => {
export const handler: Plugin.Handler<Config> = ({ context, plugin }) => {
const file = context.createFile({
id: transformersId,
path: plugin.output,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PluginName } from '../../types';
import type { Plugin } from '../../types';

export interface Config extends PluginName<'@hey-api/transformers'> {
export interface Config extends Plugin.Name<'@hey-api/transformers'> {
/**
* Convert date strings into Date objects?
* @default false
Expand Down
8 changes: 4 additions & 4 deletions packages/openapi-ts/src/plugins/@hey-api/typescript/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { DefineConfig, PluginConfig } from '../../types';
import type { Plugin } from '../../types';
import { handler } from './plugin';
import { handlerLegacy } from './plugin-legacy';
import type { Config } from './types';

export const defaultConfig: PluginConfig<Config> = {
export const defaultConfig: Plugin.Config<Config> = {
_handler: handler,
_handlerLegacy: handlerLegacy,
enums: false,
Expand All @@ -17,9 +17,9 @@ export const defaultConfig: PluginConfig<Config> = {
};

/**
* Type helper for `@hey-api/typescript` plugin, returns {@link PluginConfig} object
* Type helper for `@hey-api/typescript` plugin, returns {@link Plugin.Config} object
*/
export const defineConfig: DefineConfig<Config> = (config) => ({
export const defineConfig: Plugin.DefineConfig<Config> = (config) => ({
...defaultConfig,
...config,
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
type SetUniqueTypeNameResult,
toType,
} from '../../../utils/type';
import type { PluginLegacyHandler } from '../../types';
import type { Plugin } from '../../types';
import {
operationDataTypeName,
operationErrorTypeName,
Expand Down Expand Up @@ -606,7 +606,7 @@ const processServiceTypes = ({
}
};

export const handlerLegacy: PluginLegacyHandler<Config> = ({
export const handlerLegacy: Plugin.LegacyHandler<Config> = ({
client,
files,
}) => {
Expand Down
Loading