Skip to content

Commit ccc7ae4

Browse files
committed
chore: generate CreateClientConfig type with ClientOptions
1 parent bb6d46a commit ccc7ae4

File tree

14 files changed

+184
-33
lines changed

14 files changed

+184
-33
lines changed

packages/client-axios/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export const mergeHeaders = (
281281
};
282282

283283
export const createConfig = <T extends ClientOptions = ClientOptions>(
284-
override: Config<ClientOptions & T> = {},
285-
): Config<Required<ClientOptions> & T> => ({
284+
override: Config<Omit<ClientOptions, keyof T> & T> = {},
285+
): Config<Omit<ClientOptions, keyof T> & T> => ({
286286
...override,
287287
});

packages/client-fetch/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ const defaultHeaders = {
393393
};
394394

395395
export const createConfig = <T extends ClientOptions = ClientOptions>(
396-
override: Config<ClientOptions & T> = {},
397-
): Config<Required<ClientOptions> & T> => ({
396+
override: Config<Omit<ClientOptions, keyof T> & T> = {},
397+
): Config<Omit<ClientOptions, keyof T> & T> => ({
398398
...jsonBodySerializer,
399399
headers: defaultHeaders,
400400
parseAs: 'auto',

packages/client-next/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,8 @@ const defaultHeaders = {
382382
};
383383

384384
export const createConfig = <T extends ClientOptions = ClientOptions>(
385-
override: Config<ClientOptions & T> = {},
386-
): Config<Required<ClientOptions> & T> => ({
385+
override: Config<Omit<ClientOptions, keyof T> & T> = {},
386+
): Config<Omit<ClientOptions, keyof T> & T> => ({
387387
...jsonBodySerializer,
388388
headers: defaultHeaders,
389389
parseAs: 'auto',

packages/client-nuxt/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ const defaultHeaders = {
303303
};
304304

305305
export const createConfig = <T extends ClientOptions = ClientOptions>(
306-
override: Config<ClientOptions & T> = {},
307-
): Config<Required<ClientOptions> & T> => ({
306+
override: Config<Omit<ClientOptions, keyof T> & T> = {},
307+
): Config<Omit<ClientOptions, keyof T> & T> => ({
308308
...jsonBodySerializer,
309309
headers: defaultHeaders,
310310
querySerializer: defaultQuerySerializer,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { handler } from '../client-core/plugin';
33
import type { Config } from './types';
44

55
export const defaultConfig: Plugin.Config<Config> = {
6+
_dependencies: ['@hey-api/typescript'],
67
_handler: handler,
78
_handlerLegacy: () => {},
89
_tags: ['client'],

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

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,102 @@
11
import { compiler } from '../../../compiler';
22
import { clientModulePath } from '../../../generate/client';
3+
import type { IR } from '../../../ir/types';
34
import { clientId } from '../client-core/utils';
5+
import { typesId } from '../typescript/ref';
46
import type { PluginHandler } from './types';
57

8+
const createClientConfigType = ({ context }: { context: IR.Context }) => {
9+
const file = context.file({ id: clientId })!;
10+
11+
const clientModule = clientModulePath({
12+
config: context.config,
13+
sourceOutput: file.nameWithoutExtension(),
14+
});
15+
const clientOptions = file.import({
16+
asType: true,
17+
module: file.relativePathToFile({ context, id: typesId }),
18+
name: 'ClientOptions',
19+
});
20+
const configType = file.import({
21+
asType: true,
22+
module: clientModule,
23+
name: 'Config',
24+
});
25+
const defaultClientOptions = file.import({
26+
alias: 'DefaultClientOptions',
27+
asType: true,
28+
module: clientModule,
29+
name: 'ClientOptions',
30+
});
31+
32+
const defaultClientOptionsType = compiler.typeReferenceNode({
33+
typeName: defaultClientOptions.name,
34+
});
35+
const tType = compiler.typeReferenceNode({ typeName: 'T' });
36+
37+
const typeCreateClientConfig = compiler.typeAliasDeclaration({
38+
comment: [
39+
'The `createClientConfig()` function will be called on client initialization',
40+
"and the returned object will become the client's initial configuration.",
41+
'',
42+
'You may want to initialize your client this way instead of calling',
43+
"`setConfig()`. This is useful for example if you're using Next.js",
44+
'to ensure your client always has the correct values.',
45+
],
46+
exportType: true,
47+
name: 'CreateClientConfig',
48+
type: compiler.functionTypeNode({
49+
parameters: [
50+
compiler.parameterDeclaration({
51+
name: 'override',
52+
required: false,
53+
type: compiler.typeReferenceNode({
54+
typeArguments: [
55+
compiler.typeIntersectionNode({
56+
types: [defaultClientOptionsType, tType],
57+
}),
58+
],
59+
typeName: configType.name,
60+
}),
61+
}),
62+
],
63+
returnType: compiler.typeReferenceNode({
64+
typeArguments: [
65+
compiler.typeIntersectionNode({
66+
types: [
67+
compiler.typeReferenceNode({
68+
typeArguments: [defaultClientOptionsType],
69+
typeName: 'Required',
70+
}),
71+
tType,
72+
],
73+
}),
74+
],
75+
typeName: configType.name,
76+
}),
77+
}),
78+
typeParameters: [
79+
{
80+
default: compiler.typeReferenceNode({ typeName: clientOptions.name }),
81+
extends: defaultClientOptionsType,
82+
name: 'T',
83+
},
84+
],
85+
});
86+
87+
file.add(typeCreateClientConfig);
88+
};
89+
690
export const handler: PluginHandler = ({ context, plugin }) => {
791
const file = context.createFile({
892
exportFromIndex: plugin.exportFromIndex,
993
id: clientId,
1094
path: plugin.output,
1195
});
12-
const clientOutput = file.nameWithoutExtension();
1396

1497
const clientModule = clientModulePath({
1598
config: context.config,
16-
sourceOutput: clientOutput,
99+
sourceOutput: file.nameWithoutExtension(),
17100
});
18101
const createClient = file.import({
19102
module: clientModule,
@@ -23,6 +106,11 @@ export const handler: PluginHandler = ({ context, plugin }) => {
23106
module: clientModule,
24107
name: 'createConfig',
25108
});
109+
const clientOptions = file.import({
110+
asType: true,
111+
module: file.relativePathToFile({ context, id: typesId }),
112+
name: 'ClientOptions',
113+
});
26114

27115
const createClientConfig = plugin.runtimeConfigPath
28116
? file.import({
@@ -49,9 +137,14 @@ export const handler: PluginHandler = ({ context, plugin }) => {
49137
})
50138
: undefined,
51139
],
140+
types: [compiler.typeReferenceNode({ typeName: clientOptions.name })],
52141
}),
53142
];
54143

144+
createClientConfigType({
145+
context,
146+
});
147+
55148
const statement = compiler.constVariable({
56149
exportConst: true,
57150
expression: compiler.callExpression({

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ export const clientId = 'client';
55

66
type Plugins = Required<Config>['plugins'];
77

8+
export const getClientBaseUrlKey = (config: Config) => {
9+
const client = getClientPlugin(config);
10+
if (
11+
client.name === '@hey-api/client-axios' ||
12+
client.name === '@hey-api/client-nuxt'
13+
) {
14+
return 'baseURL';
15+
}
16+
return 'baseUrl';
17+
};
18+
819
export const getClientPlugin = (
920
config: Config,
1021
): Required<Plugins>[PluginClientNames] => {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { handler } from '../client-core/plugin';
33
import type { Config } from './types';
44

55
export const defaultConfig: Plugin.Config<Config> = {
6+
_dependencies: ['@hey-api/typescript'],
67
_handler: handler,
78
_handlerLegacy: () => {},
89
_tags: ['client'],

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { handler } from '../client-core/plugin';
33
import type { Config } from './types';
44

55
export const defaultConfig: Plugin.Config<Config> = {
6+
_dependencies: ['@hey-api/typescript'],
67
_handler: handler,
78
_handlerLegacy: () => {},
89
_tags: ['client'],

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { handler } from '../client-core/plugin';
33
import type { Config } from './types';
44

55
export const defaultConfig: Plugin.Config<Config> = {
6+
_dependencies: ['@hey-api/typescript'],
67
_handler: handler,
78
_handlerLegacy: () => {},
89
_tags: ['client'],

0 commit comments

Comments
 (0)