Skip to content

Commit 4dbce29

Browse files
committed
feat: examples plugin
1 parent 2808f39 commit 4dbce29

File tree

23 files changed

+207
-62
lines changed

23 files changed

+207
-62
lines changed

.changeset/early-knives-kneel.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+
**client**: use `getBaseUrl()` function to determine default value

.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

.changeset/nasty-planes-cry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/shared": patch
3+
---
4+
5+
**utils**: export `getBaseUrl()` function

dev/inputs.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ const specsPath = path.join(__dirname, '..', 'specs');
55
export const inputs = {
66
circular: path.resolve(specsPath, '3.0.x', 'circular.yaml'),
77
full: path.resolve(specsPath, '3.1.x', 'full.yaml'),
8+
heyapi: 'hey-api/backend',
89
local: 'http://localhost:8000/openapi.json',
10+
mockers: path.resolve(specsPath, '3.1.x', 'mockers.yaml'),
911
opencode: path.resolve(specsPath, '3.1.x', 'opencode.yaml'),
1012
petstore:
1113
'https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml',
1214
redfish:
1315
'https://raw.githubusercontent.com/DMTF/Redfish-Publications/refs/heads/main/openapi/openapi.yaml',
16+
rpc: path.resolve(specsPath, '3.1.x', 'rpc.yaml'),
1417
scalar: 'scalar:@scalar/access-service',
1518
transformers: path.resolve(specsPath, '3.1.x', 'transformers.json'),
1619
validators: path.resolve(specsPath, '3.1.x', 'validators.yaml'),

packages/openapi-python/src/plugins/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type PluginClientNames =
44
| '@hey-api/client-requests'
55
| '@hey-api/client-urllib3';
66

7-
export type PluginMockNames = 'factory_boy' | 'faker' | 'mimesis';
7+
export type PluginSourceNames = 'factory_boy' | 'faker' | 'mimesis';
88

99
export type PluginTransformerNames = never;
1010

packages/openapi-ts-tests/orpc/v1/test/3.0.x.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ describe(`OpenAPI ${version}`, () => {
1717
const scenarios = [
1818
{
1919
config: createConfig({
20-
input: 'orpc.yaml',
20+
input: 'rpc.yaml',
2121
output: 'default',
2222
plugins: ['orpc', 'zod'],
2323
}),
2424
description: 'generate oRPC contracts with Zod schemas',
2525
},
2626
{
2727
config: createConfig({
28-
input: 'orpc.yaml',
28+
input: 'rpc.yaml',
2929
output: 'custom-names',
3030
plugins: [
3131
'valibot',

packages/openapi-ts-tests/orpc/v1/test/3.1.x.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ describe(`OpenAPI ${version}`, () => {
1717
const scenarios = [
1818
{
1919
config: createConfig({
20-
input: 'orpc.yaml',
20+
input: 'rpc.yaml',
2121
output: 'default',
2222
plugins: ['orpc', 'zod'],
2323
}),
2424
description: 'generate oRPC contracts with Zod schemas',
2525
},
2626
{
2727
config: createConfig({
28-
input: 'orpc.yaml',
28+
input: 'rpc.yaml',
2929
output: 'custom-names',
3030
plugins: [
3131
'valibot',
@@ -42,7 +42,7 @@ describe(`OpenAPI ${version}`, () => {
4242
},
4343
{
4444
config: createConfig({
45-
input: 'orpc.yaml',
45+
input: 'rpc.yaml',
4646
output: 'contracts-strategy-by-tags',
4747
plugins: [
4848
'zod',
@@ -58,7 +58,7 @@ describe(`OpenAPI ${version}`, () => {
5858
},
5959
{
6060
config: createConfig({
61-
input: 'orpc.yaml',
61+
input: 'rpc.yaml',
6262
output: 'contracts-strategy-single',
6363
plugins: [
6464
'zod',
@@ -75,7 +75,7 @@ describe(`OpenAPI ${version}`, () => {
7575
},
7676
{
7777
config: createConfig({
78-
input: 'orpc.yaml',
78+
input: 'rpc.yaml',
7979
output: 'contracts-nesting-id',
8080
plugins: [
8181
'zod',
@@ -92,7 +92,7 @@ describe(`OpenAPI ${version}`, () => {
9292
},
9393
{
9494
config: createConfig({
95-
input: 'orpc.yaml',
95+
input: 'rpc.yaml',
9696
output: 'contracts-custom-naming',
9797
plugins: [
9898
'zod',

packages/openapi-ts/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ declare module '@hey-api/codegen-core' {
4747
| 'arktype'
4848
| 'fastify'
4949
| 'json-schema'
50+
| 'msw'
5051
| 'sdk'
5152
| 'typescript'
5253
| 'valibot'
@@ -67,6 +68,7 @@ declare module '@hey-api/shared' {
6768
'@hey-api/client-next': Plugins.HeyApiClientNext.Types['Types'];
6869
'@hey-api/client-nuxt': Plugins.HeyApiClientNuxt.Types['Types'];
6970
'@hey-api/client-ofetch': Plugins.HeyApiClientOfetch.Types['Types'];
71+
'@hey-api/examples': Plugins.HeyApiExamples.Types['Types'];
7072
'@hey-api/schemas': Plugins.HeyApiSchemas.Types['Types'];
7173
'@hey-api/sdk': Plugins.HeyApiSdk.Types['Types'];
7274
'@hey-api/transformers': Plugins.HeyApiTransformers.Types['Types'];
@@ -126,6 +128,7 @@ import type {
126128
HeyApiClientOfetchPlugin,
127129
OfetchClient as OfetchClientImp,
128130
} from './plugins/@hey-api/client-ofetch';
131+
import type { HeyApiExamplesPlugin } from './plugins/@hey-api/examples';
129132
import type { HeyApiSchemasPlugin } from './plugins/@hey-api/schemas';
130133
import type { HeyApiSdkPlugin } from './plugins/@hey-api/sdk';
131134
import type { HeyApiTransformersPlugin } from './plugins/@hey-api/transformers';
@@ -247,6 +250,10 @@ export namespace Plugins {
247250
export type Types = HeyApiClientOfetchPlugin;
248251
}
249252

253+
export namespace HeyApiExamples {
254+
export type Types = HeyApiExamplesPlugin;
255+
}
256+
250257
export namespace HeyApiSchemas {
251258
export type Types = HeyApiSchemasPlugin;
252259
}

packages/openapi-ts/src/plugins/@faker-js/faker/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const defaultConfig: FakerJsFakerPlugin['Config'] = {
2424
value: plugin.config.definitions,
2525
});
2626
},
27-
tags: ['mocker'],
27+
tags: ['source'],
2828
};
2929

3030
/**

packages/openapi-ts/src/plugins/@hey-api/client-core/client.ts

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
1-
import { parseUrl } from '@hey-api/shared';
1+
import { getBaseUrl } from '@hey-api/shared';
22

33
import { getTypedConfig } from '../../../config/utils';
44
import { clientFolderAbsolutePath } from '../../../generate/client';
55
import { $ } from '../../../ts-dsl';
66
import type { PluginHandler } from './types';
77
import { getClientBaseUrlKey } from './utils';
88

9-
const resolveBaseUrlString = ({ plugin }: Parameters<PluginHandler>[0]): string | undefined => {
10-
const { baseUrl } = plugin.config;
11-
12-
if (baseUrl === false) {
13-
return;
14-
}
15-
16-
if (typeof baseUrl === 'string') {
17-
return baseUrl;
18-
}
19-
20-
const { servers } = plugin.context.ir;
21-
22-
if (!servers) {
23-
return;
24-
}
25-
26-
return servers[typeof baseUrl === 'number' ? baseUrl : 0]?.url;
27-
};
28-
299
export const createClient: PluginHandler = ({ plugin }) => {
3010
const clientModule = clientFolderAbsolutePath(getTypedConfig(plugin));
3111
const symbolCreateClient = plugin.symbol('createClient', {
@@ -47,26 +27,13 @@ export const createClient: PluginHandler = ({ plugin }) => {
4727
})
4828
: undefined;
4929

50-
const defaultVals = $.object();
30+
const baseUrl = getBaseUrl(plugin.config.baseUrl ?? true, plugin.context.ir);
5131

52-
const resolvedBaseUrl = resolveBaseUrlString({
53-
plugin: plugin as any,
54-
});
55-
if (resolvedBaseUrl) {
56-
const url = parseUrl(resolvedBaseUrl);
57-
if (url.protocol && url.host && !resolvedBaseUrl.includes('{')) {
58-
defaultVals.prop(getClientBaseUrlKey(getTypedConfig(plugin)), $.literal(resolvedBaseUrl));
59-
} else if (resolvedBaseUrl !== '/' && resolvedBaseUrl.startsWith('/')) {
60-
const baseUrl = resolvedBaseUrl.endsWith('/')
61-
? resolvedBaseUrl.slice(0, -1)
62-
: resolvedBaseUrl;
63-
defaultVals.prop(getClientBaseUrlKey(getTypedConfig(plugin)), $.literal(baseUrl));
64-
}
65-
}
66-
67-
if ('throwOnError' in plugin.config && plugin.config.throwOnError) {
68-
defaultVals.prop('throwOnError', $.literal(true));
69-
}
32+
const defaultVals = $.object()
33+
.$if(baseUrl, (o, v) => o.prop(getClientBaseUrlKey(getTypedConfig(plugin)), $.literal(v)))
34+
.$if('throwOnError' in plugin.config && plugin.config.throwOnError, (o) =>
35+
o.prop('throwOnError', $.literal(true)),
36+
);
7037

7138
const createConfigParameters = [
7239
$(symbolCreateConfig)

0 commit comments

Comments
 (0)