Skip to content

Commit e436bc5

Browse files
committed
chore: add tests
1 parent 766ad64 commit e436bc5

File tree

178 files changed

+32087
-409
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+32087
-409
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import type { Plugin } from '../../types';
2+
import { clientDefaultConfig } from '../client-core/config';
23
import { handler } from '../client-core/plugin';
34
import type { Config } from './types';
45

56
export const defaultConfig: Plugin.Config<Config> = {
6-
_dependencies: ['@hey-api/typescript'],
7+
...clientDefaultConfig,
78
_handler: handler,
89
_handlerLegacy: () => {},
9-
_tags: ['client'],
10-
bundle: false,
1110
name: '@hey-api/client-axios',
12-
output: 'client',
1311
throwOnError: false,
1412
};
1513

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { Client, Plugin } from '../../types';
1+
import type { Plugin } from '../../types';
2+
import type { Client } from '../client-core/types';
23

34
export interface Config
45
extends Plugin.Name<'@hey-api/client-axios'>,
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { compiler } from '../../../compiler';
2+
import { clientModulePath } from '../../../generate/client';
3+
import { parseUrl } from '../../../utils/url';
4+
import { clientId, getClientBaseUrlKey } from '../client-core/utils';
5+
import { typesId } from '../typescript/ref';
6+
import type { PluginHandler } from './types';
7+
8+
const resolveBaseUrlString: PluginHandler<string | undefined> = ({
9+
context,
10+
plugin,
11+
}) => {
12+
const { baseUrl } = plugin;
13+
14+
if (baseUrl === false) {
15+
return;
16+
}
17+
18+
if (typeof baseUrl === 'string') {
19+
return baseUrl;
20+
}
21+
22+
const { servers } = context.ir;
23+
24+
if (!servers) {
25+
return;
26+
}
27+
28+
return servers[typeof baseUrl === 'number' ? baseUrl : 0]?.url;
29+
};
30+
31+
export const createClient: PluginHandler = ({ context, plugin }) => {
32+
const file = context.file({ id: clientId })!;
33+
34+
const clientModule = clientModulePath({
35+
config: context.config,
36+
sourceOutput: file.nameWithoutExtension(),
37+
});
38+
const createClient = file.import({
39+
module: clientModule,
40+
name: 'createClient',
41+
});
42+
const createConfig = file.import({
43+
module: clientModule,
44+
name: 'createConfig',
45+
});
46+
const clientOptions = file.import({
47+
asType: true,
48+
module: file.relativePathToFile({ context, id: typesId }),
49+
name: 'ClientOptions',
50+
});
51+
52+
const createClientConfig = plugin.runtimeConfigPath
53+
? file.import({
54+
module: file.relativePathToFile({
55+
context,
56+
id: plugin.runtimeConfigPath,
57+
}),
58+
name: 'createClientConfig',
59+
})
60+
: undefined;
61+
62+
const defaultValues: Array<unknown> = [];
63+
64+
const resolvedBaseUrl = resolveBaseUrlString({ context, plugin });
65+
if (resolvedBaseUrl) {
66+
const url = parseUrl(resolvedBaseUrl);
67+
if (url.protocol && url.host && !resolvedBaseUrl.includes('{')) {
68+
defaultValues.push({
69+
key: getClientBaseUrlKey(context.config),
70+
value: resolvedBaseUrl,
71+
});
72+
}
73+
}
74+
75+
if ('throwOnError' in plugin && plugin.throwOnError) {
76+
defaultValues.push({
77+
key: 'throwOnError',
78+
value: true,
79+
});
80+
}
81+
82+
const createConfigParameters = [
83+
compiler.callExpression({
84+
functionName: createConfig.name,
85+
parameters: defaultValues.length
86+
? [compiler.objectExpression({ obj: defaultValues })]
87+
: undefined,
88+
types: [compiler.typeReferenceNode({ typeName: clientOptions.name })],
89+
}),
90+
];
91+
92+
const statement = compiler.constVariable({
93+
exportConst: true,
94+
expression: compiler.callExpression({
95+
functionName: createClient.name,
96+
parameters: createClientConfig
97+
? [
98+
compiler.callExpression({
99+
functionName: createClientConfig.name,
100+
parameters: createConfigParameters,
101+
}),
102+
]
103+
: createConfigParameters,
104+
}),
105+
name: 'client',
106+
});
107+
file.add(statement);
108+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const clientDefaultConfig = {
2+
_dependencies: ['@hey-api/typescript'],
3+
_tags: ['client'],
4+
baseUrl: true,
5+
bundle: false,
6+
output: 'client',
7+
} as const;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { compiler } from '../../../compiler';
2+
import { clientModulePath } from '../../../generate/client';
3+
import { clientId } from '../client-core/utils';
4+
import { typesId } from '../typescript/ref';
5+
import type { PluginHandler } from './types';
6+
7+
export const createClientConfigType: PluginHandler = ({ context }) => {
8+
const file = context.file({ id: clientId })!;
9+
10+
const clientModule = clientModulePath({
11+
config: context.config,
12+
sourceOutput: file.nameWithoutExtension(),
13+
});
14+
const clientOptions = file.import({
15+
asType: true,
16+
module: file.relativePathToFile({ context, id: typesId }),
17+
name: 'ClientOptions',
18+
});
19+
const configType = file.import({
20+
asType: true,
21+
module: clientModule,
22+
name: 'Config',
23+
});
24+
const defaultClientOptions = file.import({
25+
alias: 'DefaultClientOptions',
26+
asType: true,
27+
module: clientModule,
28+
name: 'ClientOptions',
29+
});
30+
31+
const defaultClientOptionsType = compiler.typeReferenceNode({
32+
typeName: defaultClientOptions.name,
33+
});
34+
const tType = compiler.typeReferenceNode({ typeName: 'T' });
35+
36+
const typeCreateClientConfig = compiler.typeAliasDeclaration({
37+
comment: [
38+
'The `createClientConfig()` function will be called on client initialization',
39+
"and the returned object will become the client's initial configuration.",
40+
'',
41+
'You may want to initialize your client this way instead of calling',
42+
"`setConfig()`. This is useful for example if you're using Next.js",
43+
'to ensure your client always has the correct values.',
44+
],
45+
exportType: true,
46+
name: 'CreateClientConfig',
47+
type: compiler.functionTypeNode({
48+
parameters: [
49+
compiler.parameterDeclaration({
50+
name: 'override',
51+
required: false,
52+
type: compiler.typeReferenceNode({
53+
typeArguments: [
54+
compiler.typeIntersectionNode({
55+
types: [defaultClientOptionsType, tType],
56+
}),
57+
],
58+
typeName: configType.name,
59+
}),
60+
}),
61+
],
62+
returnType: compiler.typeReferenceNode({
63+
typeArguments: [
64+
compiler.typeIntersectionNode({
65+
types: [
66+
compiler.typeReferenceNode({
67+
typeArguments: [defaultClientOptionsType],
68+
typeName: 'Required',
69+
}),
70+
tType,
71+
],
72+
}),
73+
],
74+
typeName: configType.name,
75+
}),
76+
}),
77+
typeParameters: [
78+
{
79+
default: compiler.typeReferenceNode({ typeName: clientOptions.name }),
80+
extends: defaultClientOptionsType,
81+
name: 'T',
82+
},
83+
],
84+
});
85+
86+
file.add(typeCreateClientConfig);
87+
};
Lines changed: 5 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,15 @@
1-
import { compiler } from '../../../compiler';
2-
import { clientModulePath } from '../../../generate/client';
3-
import type { IR } from '../../../ir/types';
41
import { clientId } from '../client-core/utils';
5-
import { typesId } from '../typescript/ref';
2+
import { createClient } from './client';
3+
import { createClientConfigType } from './createClientConfig';
64
import type { PluginHandler } from './types';
75

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-
906
export const handler: PluginHandler = ({ context, plugin }) => {
91-
const file = context.createFile({
7+
context.createFile({
928
exportFromIndex: plugin.exportFromIndex,
939
id: clientId,
9410
path: plugin.output,
9511
});
9612

97-
const clientModule = clientModulePath({
98-
config: context.config,
99-
sourceOutput: file.nameWithoutExtension(),
100-
});
101-
const createClient = file.import({
102-
module: clientModule,
103-
name: 'createClient',
104-
});
105-
const createConfig = file.import({
106-
module: clientModule,
107-
name: 'createConfig',
108-
});
109-
const clientOptions = file.import({
110-
asType: true,
111-
module: file.relativePathToFile({ context, id: typesId }),
112-
name: 'ClientOptions',
113-
});
114-
115-
const createClientConfig = plugin.runtimeConfigPath
116-
? file.import({
117-
module: file.relativePathToFile({
118-
context,
119-
id: plugin.runtimeConfigPath,
120-
}),
121-
name: 'createClientConfig',
122-
})
123-
: undefined;
124-
125-
const createConfigParameters = [
126-
compiler.callExpression({
127-
functionName: createConfig.name,
128-
parameters: [
129-
'throwOnError' in plugin && plugin.throwOnError
130-
? compiler.objectExpression({
131-
obj: [
132-
{
133-
key: 'throwOnError',
134-
value: true,
135-
},
136-
],
137-
})
138-
: undefined,
139-
],
140-
types: [compiler.typeReferenceNode({ typeName: clientOptions.name })],
141-
}),
142-
];
143-
144-
createClientConfigType({
145-
context,
146-
});
147-
148-
const statement = compiler.constVariable({
149-
exportConst: true,
150-
expression: compiler.callExpression({
151-
functionName: createClient.name,
152-
parameters: createClientConfig
153-
? [
154-
compiler.callExpression({
155-
functionName: createClientConfig.name,
156-
parameters: createConfigParameters,
157-
}),
158-
]
159-
: createConfigParameters,
160-
}),
161-
name: 'client',
162-
});
163-
file.add(statement);
13+
createClientConfigType({ context, plugin });
14+
createClient({ context, plugin });
16415
};

0 commit comments

Comments
 (0)