Skip to content

Commit 9e6bdde

Browse files
committed
chore: clean up
1 parent 5aa22e8 commit 9e6bdde

6 files changed

Lines changed: 30 additions & 20 deletions

File tree

‎.changeset/five-pianos-jump.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
---
4+
5+
**plugin(@hey-api/examples)**: initial release

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { defaultConfig as tanStackSvelteQuery } from './@tanstack/svelte-query';
2323
import { defaultConfig as tanStackVueQuery } from './@tanstack/vue-query';
2424
import { defaultConfig as arktype } from './arktype';
2525
import { defaultConfig as fastify } from './fastify';
26+
import { defaultConfig as msw } from './msw';
2627
import { defaultConfig as nestjs } from './nestjs';
2728
import { defaultConfig as orpc } from './orpc';
2829
import { defaultConfig as swr } from './swr';

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@ export const defaultConfig: MswPlugin['Config'] = {
99
comments: true,
1010
includeInEntry: false,
1111
responseFallback: 'error',
12-
valueSources: ['example'],
12+
source: ['@hey-api/examples'],
1313
},
1414
dependencies: ['@hey-api/typescript'],
1515
handler,
1616
name: 'msw',
17-
tags: ['mocker'],
17+
resolveConfig: (plugin) => {
18+
if (!(plugin.config.source instanceof Array)) {
19+
plugin.config.source = plugin.config.source ? [plugin.config.source] : [];
20+
}
21+
for (const source of plugin.config.source) {
22+
plugin.dependencies.add(source);
23+
}
24+
},
25+
tags: ['handler'],
1826
};
1927

2028
/**
21-
* Type helper for `msw` plugin, returns {@link Plugin.Config} object
29+
* Type helper for MSW plugin, returns {@link Plugin.Config} object
2230
*/
2331
export const defineConfig = definePluginConfig(defaultConfig);

‎packages/openapi-ts/src/plugins/msw/shared/handler.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,10 @@ function createHandlerNode({
153153

154154
export function getHandler({
155155
baseUrl,
156-
examples,
157156
operation,
158157
plugin,
159158
}: {
160159
baseUrl: string | undefined;
161-
examples: boolean;
162160
operation: IR.OperationObject;
163161
plugin: MswPlugin['Instance'];
164162
}): Symbol {
@@ -212,8 +210,8 @@ export function getHandler({
212210
// omit response type to avoid DefaultBodyType constraint issues
213211
);
214212

215-
// When examples are disabled, strip the example from the dominant response
216-
if (!examples) {
213+
if (!plugin.config.source.includes('@hey-api/examples')) {
214+
// examples are disabled, strip the example from the dominant response
217215
response.example = undefined;
218216
}
219217

‎packages/openapi-ts/src/plugins/msw/types.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import type { DefinePlugin, Plugin } from '@hey-api/shared';
2+
import type { MaybeArray } from '@hey-api/types';
3+
4+
import type { PluginSourceNames } from '../types';
25

36
export type UserConfig = Plugin.Name<'msw'> &
47
Plugin.Hooks &
@@ -28,14 +31,14 @@ export type UserConfig = Plugin.Name<'msw'> &
2831
*/
2932
responseFallback?: 'error' | 'passthrough';
3033
/**
31-
* Sources for default parameter values in handler factories. Order determines
32-
* priority (earlier entries take precedence).
34+
* Where to pull mock data from. When an array, earlier entries take
35+
* precedence.
3336
*
34-
* - `'example'` - use OpenAPI example values
37+
* - `'example'` - example values defined in the input
3538
*
36-
* @default ['example']
39+
* @default ['@hey-api/examples']
3740
*/
38-
valueSources?: ReadonlyArray<'example'>;
41+
source?: MaybeArray<PluginSourceNames>;
3942
};
4043

4144
export type Config = Plugin.Name<'msw'> &
@@ -46,8 +49,8 @@ export type Config = Plugin.Name<'msw'> &
4649
baseUrl: string | number | boolean;
4750
/** Behavior when a response cannot be generated. */
4851
responseFallback: 'error' | 'passthrough';
49-
/** Sources for default parameter values in handler factories. */
50-
valueSources: ReadonlyArray<'example'>;
52+
/** Where to pull mock data from. */
53+
source: ReadonlyArray<PluginSourceNames>;
5154
};
5255

5356
export type MswPlugin = DefinePlugin<UserConfig, Config>;

‎packages/openapi-ts/src/plugins/msw/v2/plugin.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,7 @@ export const handlerV2: MswPlugin['Handler'] = ({ plugin }) => {
5050
plugin.forEach(
5151
'operation',
5252
({ operation }) => {
53-
const symbolHandler = getHandler({
54-
baseUrl,
55-
examples: plugin.config.valueSources?.includes('example') ?? true,
56-
operation,
57-
plugin,
58-
});
53+
const symbolHandler = getHandler({ baseUrl, operation, plugin });
5954

6055
const name = operation.id;
6156
pickType.prop(name, (p) =>

0 commit comments

Comments
 (0)