Skip to content

Commit 2450e8f

Browse files
committed
fix(clients): preserve beforeRequest typing for fetch, next, ky, angular)
1 parent b1ee820 commit 2450e8f

4 files changed

Lines changed: 42 additions & 14 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,14 @@ export const createClient = (config: Config = {}): Client => {
9595
return { opts, req, url };
9696
};
9797

98-
const beforeRequest = async (options: RequestOptions) => {
98+
const beforeRequest = async <
99+
TData = unknown,
100+
TResponseStyle extends ResponseStyle = 'fields',
101+
ThrowOnError extends boolean = boolean,
102+
Url extends string = string,
103+
>(
104+
options: RequestOptions<TData, TResponseStyle, ThrowOnError, Url>,
105+
) => {
99106
const { opts, req, url } = requestOptions(options);
100107

101108
if (opts.security) {
@@ -113,7 +120,6 @@ export const createClient = (config: Config = {}): Client => {
113120
};
114121

115122
const request: Client['request'] = async (options) => {
116-
// @ts-expect-error
117123
const { opts, req: initialReq } = await beforeRequest(options);
118124

119125
let req = initialReq;

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ export const createClient = (config: Config = {}): Client => {
2929

3030
const interceptors = createInterceptors<Request, Response, unknown, ResolvedRequestOptions>();
3131

32-
const beforeRequest = async (options: RequestOptions) => {
32+
const beforeRequest = async <
33+
TData = unknown,
34+
TResponseStyle extends 'data' | 'fields' = 'fields',
35+
ThrowOnError extends boolean = boolean,
36+
Url extends string = string,
37+
>(
38+
options: RequestOptions<TData, TResponseStyle, ThrowOnError, Url>,
39+
) => {
3340
const opts = {
3441
..._config,
3542
...options,
@@ -58,13 +65,14 @@ export const createClient = (config: Config = {}): Client => {
5865
opts.headers.delete('Content-Type');
5966
}
6067

61-
const url = buildUrl(opts);
68+
const resolvedOpts = opts as typeof opts &
69+
ResolvedRequestOptions<TResponseStyle, ThrowOnError, Url>;
70+
const url = buildUrl(resolvedOpts);
6271

63-
return { opts, url };
72+
return { opts: resolvedOpts, url };
6473
};
6574

6675
const request: Client['request'] = async (options) => {
67-
// @ts-expect-error
6876
const { opts, url } = await beforeRequest(options);
6977
const requestInit: ReqInit = {
7078
redirect: 'follow',

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ export const createClient = (config: Config = {}): Client => {
2828

2929
const interceptors = createInterceptors<Request, Response, unknown, ResolvedRequestOptions>();
3030

31-
const beforeRequest = async (options: RequestOptions) => {
31+
const beforeRequest = async <
32+
TData = unknown,
33+
TResponseStyle extends 'data' | 'fields' = 'fields',
34+
ThrowOnError extends boolean = boolean,
35+
Url extends string = string,
36+
>(
37+
options: RequestOptions<TData, TResponseStyle, ThrowOnError, Url>,
38+
) => {
3239
const opts = {
3340
..._config,
3441
...options,
@@ -56,9 +63,11 @@ export const createClient = (config: Config = {}): Client => {
5663
opts.headers.delete('Content-Type');
5764
}
5865

59-
const url = buildUrl(opts);
66+
const resolvedOpts = opts as typeof opts &
67+
ResolvedRequestOptions<TResponseStyle, ThrowOnError, Url>;
68+
const url = buildUrl(resolvedOpts);
6069

61-
return { opts, url };
70+
return { opts: resolvedOpts, url };
6271
};
6372

6473
const parseErrorResponse = async (
@@ -105,7 +114,6 @@ export const createClient = (config: Config = {}): Client => {
105114
};
106115

107116
const request: Client['request'] = async (options) => {
108-
// @ts-expect-error
109117
const { opts, url } = await beforeRequest(options);
110118

111119
const kyInstance = opts.ky!;

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ export const createClient = (config: Config = {}): Client => {
2929

3030
const interceptors = createInterceptors<Response, unknown, ResolvedRequestOptions>();
3131

32-
const beforeRequest = async (options: RequestOptions) => {
32+
const beforeRequest = async <
33+
TData = unknown,
34+
ThrowOnError extends boolean = boolean,
35+
Url extends string = string,
36+
>(
37+
options: RequestOptions<TData, ThrowOnError, Url>,
38+
) => {
3339
const opts = {
3440
..._config,
3541
...options,
@@ -58,14 +64,14 @@ export const createClient = (config: Config = {}): Client => {
5864
opts.headers.delete('Content-Type');
5965
}
6066

61-
const url = buildUrl(opts);
67+
const resolvedOpts = opts as typeof opts & ResolvedRequestOptions<ThrowOnError, Url>;
68+
const url = buildUrl(resolvedOpts);
6269

63-
return { opts, url };
70+
return { opts: resolvedOpts, url };
6471
};
6572

6673
// @ts-expect-error
6774
const request: Client['request'] = async (options) => {
68-
// @ts-expect-error
6975
const { opts, url } = await beforeRequest(options);
7076

7177
for (const fn of interceptors.request.fns) {

0 commit comments

Comments
 (0)