Skip to content

Commit bb6d46a

Browse files
committed
chore: make createConfig, CreateClientConfig, and Config accept ClientOptions generic
1 parent 5fa4572 commit bb6d46a

File tree

17 files changed

+151
-92
lines changed

17 files changed

+151
-92
lines changed

.changeset/unlucky-moles-dance.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@hey-api/client-axios': patch
3+
'@hey-api/client-fetch': patch
4+
'@hey-api/client-next': patch
5+
'@hey-api/client-nuxt': patch
6+
---
7+
8+
fix: make createConfig, CreateClientConfig, and Config accept ClientOptions generic

packages/client-axios/src/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export const createClient = (config: Config = {}): Client => {
5959
const { auth, ...optsWithoutAuth } = opts;
6060
const response = await _axios({
6161
...optsWithoutAuth,
62+
baseURL: opts.baseURL as string,
6263
data: opts.body,
6364
headers: opts.headers as RawAxiosRequestHeaders,
6465
// let `paramsSerializer()` handle query params if it exists

packages/client-axios/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export { createClient } from './client';
22
export type {
33
Client,
4+
ClientOptions,
45
Config,
56
CreateClientConfig,
67
Options,

packages/client-axios/src/types.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import type {
1111
CreateAxiosDefaults,
1212
} from 'axios';
1313

14-
export interface Config<ThrowOnError extends boolean = boolean>
15-
extends Omit<CreateAxiosDefaults, 'auth' | 'headers' | 'method'>,
14+
export interface Config<T extends ClientOptions = ClientOptions>
15+
extends Omit<CreateAxiosDefaults, 'auth' | 'baseURL' | 'headers' | 'method'>,
1616
CoreConfig {
1717
/**
1818
* Axios implementation. You can use this option to provide a custom
@@ -21,6 +21,10 @@ export interface Config<ThrowOnError extends boolean = boolean>
2121
* @default axios
2222
*/
2323
axios?: AxiosStatic;
24+
/**
25+
* Base URL for all requests made by this client.
26+
*/
27+
baseURL?: T['baseURL'];
2428
/**
2529
* An object containing any HTTP headers that you want to pre-populate your
2630
* `Headers` object with.
@@ -44,13 +48,15 @@ export interface Config<ThrowOnError extends boolean = boolean>
4448
*
4549
* @default false
4650
*/
47-
throwOnError?: ThrowOnError;
51+
throwOnError?: T['throwOnError'];
4852
}
4953

5054
export interface RequestOptions<
5155
ThrowOnError extends boolean = boolean,
5256
Url extends string = string,
53-
> extends Config<ThrowOnError> {
57+
> extends Config<{
58+
throwOnError: ThrowOnError;
59+
}> {
5460
/**
5561
* Any body that you want to add to your request.
5662
*
@@ -77,6 +83,11 @@ export type RequestResult<
7783
| (AxiosError<TError> & { data: undefined; error: TError })
7884
>;
7985

86+
export interface ClientOptions {
87+
baseURL?: string;
88+
throwOnError?: boolean;
89+
}
90+
8091
type MethodFn = <
8192
TData = unknown,
8293
TError = unknown,
@@ -117,7 +128,9 @@ export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn> & {
117128
* `setConfig()`. This is useful for example if you're using Next.js
118129
* to ensure your client always has the correct values.
119130
*/
120-
export type CreateClientConfig = (override?: Config) => Config;
131+
export type CreateClientConfig<T extends ClientOptions = ClientOptions> = (
132+
override?: Config<ClientOptions & T>,
133+
) => Config<Required<ClientOptions> & T>;
121134

122135
export interface TDataShape {
123136
body?: unknown;

packages/client-axios/src/utils.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ import {
1010
serializePrimitiveParam,
1111
} from '@hey-api/client-core';
1212

13-
import type {
14-
Client,
15-
Config,
16-
CreateClientConfig,
17-
RequestOptions,
18-
} from './types';
13+
import type { Client, ClientOptions, Config, RequestOptions } from './types';
1914

2015
interface PathSerializer {
2116
path: Record<string, unknown>;
@@ -285,7 +280,8 @@ export const mergeHeaders = (
285280
return mergedHeaders;
286281
};
287282

288-
export const createConfig: CreateClientConfig = (override = {}) => ({
289-
baseURL: '',
283+
export const createConfig = <T extends ClientOptions = ClientOptions>(
284+
override: Config<ClientOptions & T> = {},
285+
): Config<Required<ClientOptions> & T> => ({
290286
...override,
291287
});

packages/client-fetch/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export { createClient } from './client';
22
export type {
33
Client,
4+
ClientOptions,
45
Config,
56
CreateClientConfig,
67
Options,

packages/client-fetch/src/types.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import type {
66

77
import type { Middleware } from './utils';
88

9-
export interface Config<ThrowOnError extends boolean = boolean>
9+
export interface Config<T extends ClientOptions = ClientOptions>
1010
extends Omit<RequestInit, 'body' | 'headers' | 'method'>,
1111
CoreConfig {
1212
/**
1313
* Base URL for all requests made by this client.
14-
*
15-
* @default ''
1614
*/
17-
baseUrl?: string;
15+
baseUrl?: T['baseUrl'];
1816
/**
1917
* Fetch API implementation. You can use this option to provide a custom
2018
* fetch instance.
@@ -36,13 +34,15 @@ export interface Config<ThrowOnError extends boolean = boolean>
3634
*
3735
* @default false
3836
*/
39-
throwOnError?: ThrowOnError;
37+
throwOnError?: T['throwOnError'];
4038
}
4139

4240
export interface RequestOptions<
4341
ThrowOnError extends boolean = boolean,
4442
Url extends string = string,
45-
> extends Config<ThrowOnError> {
43+
> extends Config<{
44+
throwOnError: ThrowOnError;
45+
}> {
4646
/**
4747
* Any body that you want to add to your request.
4848
*
@@ -83,6 +83,11 @@ export type RequestResult<
8383
}
8484
>;
8585

86+
export interface ClientOptions {
87+
baseUrl?: string;
88+
throwOnError?: boolean;
89+
}
90+
8691
type MethodFn = <
8792
TData = unknown,
8893
TError = unknown,
@@ -123,7 +128,9 @@ export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn> & {
123128
* `setConfig()`. This is useful for example if you're using Next.js
124129
* to ensure your client always has the correct values.
125130
*/
126-
export type CreateClientConfig = (override?: Config) => Config;
131+
export type CreateClientConfig<T extends ClientOptions = ClientOptions> = (
132+
override?: Config<ClientOptions & T>,
133+
) => Config<Required<ClientOptions> & T>;
127134

128135
export interface TDataShape {
129136
body?: unknown;

packages/client-fetch/src/utils.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ import {
1010
serializePrimitiveParam,
1111
} from '@hey-api/client-core';
1212

13-
import type {
14-
Client,
15-
Config,
16-
CreateClientConfig,
17-
RequestOptions,
18-
} from './types';
13+
import type { Client, ClientOptions, Config, RequestOptions } from './types';
1914

2015
interface PathSerializer {
2116
path: Record<string, unknown>;
@@ -234,7 +229,7 @@ export const setAuthParams = async ({
234229

235230
export const buildUrl: Client['buildUrl'] = (options) => {
236231
const url = getUrl({
237-
baseUrl: options.baseUrl ?? '',
232+
baseUrl: options.baseUrl as string,
238233
path: options.path,
239234
query: options.query,
240235
querySerializer:
@@ -253,14 +248,14 @@ export const getUrl = ({
253248
querySerializer,
254249
url: _url,
255250
}: {
256-
baseUrl: string;
251+
baseUrl?: string;
257252
path?: Record<string, unknown>;
258253
query?: Record<string, unknown>;
259254
querySerializer: QuerySerializer;
260255
url: string;
261256
}) => {
262257
const pathUrl = _url.startsWith('/') ? _url : `/${_url}`;
263-
let url = baseUrl + pathUrl;
258+
let url = (baseUrl ?? '') + pathUrl;
264259
if (path) {
265260
url = defaultPathSerializer({ path, url });
266261
}
@@ -397,9 +392,10 @@ const defaultHeaders = {
397392
'Content-Type': 'application/json',
398393
};
399394

400-
export const createConfig: CreateClientConfig = (override = {}) => ({
395+
export const createConfig = <T extends ClientOptions = ClientOptions>(
396+
override: Config<ClientOptions & T> = {},
397+
): Config<Required<ClientOptions> & T> => ({
401398
...jsonBodySerializer,
402-
baseUrl: '',
403399
headers: defaultHeaders,
404400
parseAs: 'auto',
405401
querySerializer: defaultQuerySerializer,

packages/client-next/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export { createClient } from './client';
22
export type {
33
Client,
4+
ClientOptions,
45
Config,
56
CreateClientConfig,
67
Options,

packages/client-next/src/types.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import type {
66

77
import type { Middleware } from './utils';
88

9-
export interface Config<ThrowOnError extends boolean = boolean>
9+
export interface Config<T extends ClientOptions = ClientOptions>
1010
extends Omit<RequestInit, 'body' | 'headers' | 'method'>,
1111
CoreConfig {
1212
/**
1313
* Base URL for all requests made by this client.
14-
*
15-
* @default ''
1614
*/
17-
baseUrl?: string;
15+
baseUrl?: T['baseUrl'];
1816
/**
1917
* Fetch API implementation. You can use this option to provide a custom
2018
* fetch instance.
@@ -36,13 +34,15 @@ export interface Config<ThrowOnError extends boolean = boolean>
3634
*
3735
* @default false
3836
*/
39-
throwOnError?: ThrowOnError;
37+
throwOnError?: T['throwOnError'];
4038
}
4139

4240
export interface RequestOptions<
4341
ThrowOnError extends boolean = boolean,
4442
Url extends string = string,
45-
> extends Config<ThrowOnError> {
43+
> extends Config<{
44+
throwOnError: ThrowOnError;
45+
}> {
4646
/**
4747
* Any body that you want to add to your request.
4848
*
@@ -81,6 +81,11 @@ export type RequestResult<
8181
}
8282
>;
8383

84+
export interface ClientOptions {
85+
baseUrl?: string;
86+
throwOnError?: boolean;
87+
}
88+
8489
type MethodFn = <
8590
TData = unknown,
8691
TError = unknown,
@@ -121,7 +126,9 @@ export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn> & {
121126
* `setConfig()`. This is useful for example if you're using Next.js
122127
* to ensure your client always has the correct values.
123128
*/
124-
export type CreateClientConfig = (override?: Config) => Config;
129+
export type CreateClientConfig<T extends ClientOptions = ClientOptions> = (
130+
override?: Config<ClientOptions & T>,
131+
) => Config<Required<ClientOptions> & T>;
125132

126133
export interface TDataShape {
127134
body?: unknown;

0 commit comments

Comments
 (0)