Skip to content

Commit 462412f

Browse files
authored
Merge pull request #2513 from hey-api/refactor/client-sse
fix(client): move sse functions into their own namespace
2 parents 2745413 + 34fa59f commit 462412f

File tree

525 files changed

+9622
-7408
lines changed

Some content is hidden

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

525 files changed

+9622
-7408
lines changed

.changeset/grumpy-queens-own.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+
fix(client): move sse functions into their own namespace

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/body-response-text-plain/client/client.gen.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

33
import { createSseClient } from '../core/serverSentEvents.gen';
4+
import type { HttpMethod } from '../core/types.gen';
45
import type {
56
Client,
67
Config,
@@ -193,9 +194,12 @@ export const createClient = (config: Config = {}): Client => {
193194
};
194195
};
195196

196-
const makeMethod = (method: Required<Config>['method']) => {
197-
const fn = (options: RequestOptions) => request({ ...options, method });
198-
fn.sse = async (options: RequestOptions) => {
197+
const makeMethodFn =
198+
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
199+
request({ ...options, method });
200+
201+
const makeSseFn =
202+
(method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
199203
const { opts, url } = await beforeRequest(options);
200204
return createSseClient({
201205
...opts,
@@ -205,23 +209,32 @@ export const createClient = (config: Config = {}): Client => {
205209
url,
206210
});
207211
};
208-
return fn;
209-
};
210212

211213
return {
212214
buildUrl,
213-
connect: makeMethod('CONNECT'),
214-
delete: makeMethod('DELETE'),
215-
get: makeMethod('GET'),
215+
connect: makeMethodFn('CONNECT'),
216+
delete: makeMethodFn('DELETE'),
217+
get: makeMethodFn('GET'),
216218
getConfig,
217-
head: makeMethod('HEAD'),
219+
head: makeMethodFn('HEAD'),
218220
interceptors,
219-
options: makeMethod('OPTIONS'),
220-
patch: makeMethod('PATCH'),
221-
post: makeMethod('POST'),
222-
put: makeMethod('PUT'),
221+
options: makeMethodFn('OPTIONS'),
222+
patch: makeMethodFn('PATCH'),
223+
post: makeMethodFn('POST'),
224+
put: makeMethodFn('PUT'),
223225
request,
224226
setConfig,
225-
trace: makeMethod('TRACE'),
227+
sse: {
228+
connect: makeSseFn('CONNECT'),
229+
delete: makeSseFn('DELETE'),
230+
get: makeSseFn('GET'),
231+
head: makeSseFn('HEAD'),
232+
options: makeSseFn('OPTIONS'),
233+
patch: makeSseFn('PATCH'),
234+
post: makeSseFn('POST'),
235+
put: makeSseFn('PUT'),
236+
trace: makeSseFn('TRACE'),
237+
},
238+
trace: makeMethodFn('TRACE'),
226239
} as Client;
227240
};

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/body-response-text-plain/client/types.gen.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export interface ClientOptions {
155155
throwOnError?: boolean;
156156
}
157157

158-
type MethodFnBase = <
158+
type MethodFn = <
159159
TData = unknown,
160160
TError = unknown,
161161
ThrowOnError extends boolean = false,
@@ -164,7 +164,7 @@ type MethodFnBase = <
164164
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'>,
165165
) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
166166

167-
type MethodFnServerSentEvents = <
167+
type SseFn = <
168168
TData = unknown,
169169
TError = unknown,
170170
ThrowOnError extends boolean = false,
@@ -173,10 +173,6 @@ type MethodFnServerSentEvents = <
173173
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'>,
174174
) => Promise<ServerSentEventsResult<TData, TError>>;
175175

176-
type MethodFn = MethodFnBase & {
177-
sse: MethodFnServerSentEvents;
178-
};
179-
180176
type RequestFn = <
181177
TData = unknown,
182178
TError = unknown,
@@ -201,7 +197,13 @@ type BuildUrlFn = <
201197
options: Pick<TData, 'url'> & Options<TData>,
202198
) => string;
203199

204-
export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn> & {
200+
export type Client = CoreClient<
201+
RequestFn,
202+
Config,
203+
MethodFn,
204+
BuildUrlFn,
205+
SseFn
206+
> & {
205207
interceptors: Middleware<Request, Response, unknown, ResolvedRequestOptions>;
206208
};
207209

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/body-response-text-plain/core/types.gen.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,36 @@ import type {
77
QuerySerializerOptions,
88
} from './bodySerializer.gen';
99

10-
export interface Client<
10+
export type HttpMethod =
11+
| 'connect'
12+
| 'delete'
13+
| 'get'
14+
| 'head'
15+
| 'options'
16+
| 'patch'
17+
| 'post'
18+
| 'put'
19+
| 'trace';
20+
21+
export type Client<
1122
RequestFn = never,
1223
Config = unknown,
1324
MethodFn = never,
1425
BuildUrlFn = never,
15-
> {
26+
SseFn = never,
27+
> = {
1628
/**
1729
* Returns the final request URL.
1830
*/
1931
buildUrl: BuildUrlFn;
20-
connect: MethodFn;
21-
delete: MethodFn;
22-
get: MethodFn;
2332
getConfig: () => Config;
24-
head: MethodFn;
25-
options: MethodFn;
26-
patch: MethodFn;
27-
post: MethodFn;
28-
put: MethodFn;
2933
request: RequestFn;
3034
setConfig: (config: Config) => Config;
31-
trace: MethodFn;
32-
}
35+
} & {
36+
[K in HttpMethod]: MethodFn;
37+
} & ([SseFn] extends [never]
38+
? { sse?: never }
39+
: { sse: { [K in HttpMethod]: SseFn } });
3340

3441
export interface Config {
3542
/**
@@ -65,16 +72,7 @@ export interface Config {
6572
*
6673
* {@link https://developer.mozilla.org/docs/Web/API/fetch#method See more}
6774
*/
68-
method?:
69-
| 'CONNECT'
70-
| 'DELETE'
71-
| 'GET'
72-
| 'HEAD'
73-
| 'OPTIONS'
74-
| 'PATCH'
75-
| 'POST'
76-
| 'PUT'
77-
| 'TRACE';
75+
method?: Uppercase<HttpMethod>;
7876
/**
7977
* A function for serializing request query parameters. By default, arrays
8078
* will be exploded in form style, objects will be exploded in deepObject

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/form-data/client/client.gen.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

33
import { createSseClient } from '../core/serverSentEvents.gen';
4+
import type { HttpMethod } from '../core/types.gen';
45
import type {
56
Client,
67
Config,
@@ -193,9 +194,12 @@ export const createClient = (config: Config = {}): Client => {
193194
};
194195
};
195196

196-
const makeMethod = (method: Required<Config>['method']) => {
197-
const fn = (options: RequestOptions) => request({ ...options, method });
198-
fn.sse = async (options: RequestOptions) => {
197+
const makeMethodFn =
198+
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
199+
request({ ...options, method });
200+
201+
const makeSseFn =
202+
(method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
199203
const { opts, url } = await beforeRequest(options);
200204
return createSseClient({
201205
...opts,
@@ -205,23 +209,32 @@ export const createClient = (config: Config = {}): Client => {
205209
url,
206210
});
207211
};
208-
return fn;
209-
};
210212

211213
return {
212214
buildUrl,
213-
connect: makeMethod('CONNECT'),
214-
delete: makeMethod('DELETE'),
215-
get: makeMethod('GET'),
215+
connect: makeMethodFn('CONNECT'),
216+
delete: makeMethodFn('DELETE'),
217+
get: makeMethodFn('GET'),
216218
getConfig,
217-
head: makeMethod('HEAD'),
219+
head: makeMethodFn('HEAD'),
218220
interceptors,
219-
options: makeMethod('OPTIONS'),
220-
patch: makeMethod('PATCH'),
221-
post: makeMethod('POST'),
222-
put: makeMethod('PUT'),
221+
options: makeMethodFn('OPTIONS'),
222+
patch: makeMethodFn('PATCH'),
223+
post: makeMethodFn('POST'),
224+
put: makeMethodFn('PUT'),
223225
request,
224226
setConfig,
225-
trace: makeMethod('TRACE'),
227+
sse: {
228+
connect: makeSseFn('CONNECT'),
229+
delete: makeSseFn('DELETE'),
230+
get: makeSseFn('GET'),
231+
head: makeSseFn('HEAD'),
232+
options: makeSseFn('OPTIONS'),
233+
patch: makeSseFn('PATCH'),
234+
post: makeSseFn('POST'),
235+
put: makeSseFn('PUT'),
236+
trace: makeSseFn('TRACE'),
237+
},
238+
trace: makeMethodFn('TRACE'),
226239
} as Client;
227240
};

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/form-data/client/types.gen.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export interface ClientOptions {
155155
throwOnError?: boolean;
156156
}
157157

158-
type MethodFnBase = <
158+
type MethodFn = <
159159
TData = unknown,
160160
TError = unknown,
161161
ThrowOnError extends boolean = false,
@@ -164,7 +164,7 @@ type MethodFnBase = <
164164
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'>,
165165
) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
166166

167-
type MethodFnServerSentEvents = <
167+
type SseFn = <
168168
TData = unknown,
169169
TError = unknown,
170170
ThrowOnError extends boolean = false,
@@ -173,10 +173,6 @@ type MethodFnServerSentEvents = <
173173
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'>,
174174
) => Promise<ServerSentEventsResult<TData, TError>>;
175175

176-
type MethodFn = MethodFnBase & {
177-
sse: MethodFnServerSentEvents;
178-
};
179-
180176
type RequestFn = <
181177
TData = unknown,
182178
TError = unknown,
@@ -201,7 +197,13 @@ type BuildUrlFn = <
201197
options: Pick<TData, 'url'> & Options<TData>,
202198
) => string;
203199

204-
export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn> & {
200+
export type Client = CoreClient<
201+
RequestFn,
202+
Config,
203+
MethodFn,
204+
BuildUrlFn,
205+
SseFn
206+
> & {
205207
interceptors: Middleware<Request, Response, unknown, ResolvedRequestOptions>;
206208
};
207209

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/form-data/core/types.gen.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,36 @@ import type {
77
QuerySerializerOptions,
88
} from './bodySerializer.gen';
99

10-
export interface Client<
10+
export type HttpMethod =
11+
| 'connect'
12+
| 'delete'
13+
| 'get'
14+
| 'head'
15+
| 'options'
16+
| 'patch'
17+
| 'post'
18+
| 'put'
19+
| 'trace';
20+
21+
export type Client<
1122
RequestFn = never,
1223
Config = unknown,
1324
MethodFn = never,
1425
BuildUrlFn = never,
15-
> {
26+
SseFn = never,
27+
> = {
1628
/**
1729
* Returns the final request URL.
1830
*/
1931
buildUrl: BuildUrlFn;
20-
connect: MethodFn;
21-
delete: MethodFn;
22-
get: MethodFn;
2332
getConfig: () => Config;
24-
head: MethodFn;
25-
options: MethodFn;
26-
patch: MethodFn;
27-
post: MethodFn;
28-
put: MethodFn;
2933
request: RequestFn;
3034
setConfig: (config: Config) => Config;
31-
trace: MethodFn;
32-
}
35+
} & {
36+
[K in HttpMethod]: MethodFn;
37+
} & ([SseFn] extends [never]
38+
? { sse?: never }
39+
: { sse: { [K in HttpMethod]: SseFn } });
3340

3441
export interface Config {
3542
/**
@@ -65,16 +72,7 @@ export interface Config {
6572
*
6673
* {@link https://developer.mozilla.org/docs/Web/API/fetch#method See more}
6774
*/
68-
method?:
69-
| 'CONNECT'
70-
| 'DELETE'
71-
| 'GET'
72-
| 'HEAD'
73-
| 'OPTIONS'
74-
| 'PATCH'
75-
| 'POST'
76-
| 'PUT'
77-
| 'TRACE';
75+
method?: Uppercase<HttpMethod>;
7876
/**
7977
* A function for serializing request query parameters. By default, arrays
8078
* will be exploded in form style, objects will be exploded in deepObject

0 commit comments

Comments
 (0)