Skip to content

Commit 2032222

Browse files
authored
Merge pull request #2542 from hey-api/fix/sse-fetch
2 parents d3923a8 + c19459e commit 2032222

File tree

459 files changed

+6502
-362
lines changed

Some content is hidden

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

459 files changed

+6502
-362
lines changed

.changeset/cyan-crabs-vanish.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): pass fetch option to sse client

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,15 @@ export const createClient = (config: Config = {}): Client => {
224224
body: opts.body as BodyInit | null | undefined,
225225
headers: opts.headers as unknown as Record<string, string>,
226226
method,
227+
onRequest: async (url, init) => {
228+
let request = new Request(url, init);
229+
for (const fn of interceptors.request._fns) {
230+
if (fn) {
231+
request = await fn(request, opts);
232+
}
233+
}
234+
return request;
235+
},
227236
url,
228237
});
229238
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface Config<T extends ClientOptions = ClientOptions>
2626
*
2727
* @default globalThis.fetch
2828
*/
29-
fetch?: (request: Request) => ReturnType<typeof fetch>;
29+
fetch?: typeof fetch;
3030
/**
3131
* Please don't use the Fetch client for Next.js applications. The `next`
3232
* options won't have any effect.

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
77
'method'
88
> &
99
Pick<Config, 'method' | 'responseTransformer' | 'responseValidator'> & {
10+
/**
11+
* Fetch API implementation. You can use this option to provide a custom
12+
* fetch instance.
13+
*
14+
* @default globalThis.fetch
15+
*/
16+
fetch?: typeof fetch;
17+
/**
18+
* Implementing clients can call request interceptors inside this hook.
19+
*/
20+
onRequest?: (url: string, init: RequestInit) => Promise<Request>;
1021
/**
1122
* Callback invoked when a network or parsing error occurs during streaming.
1223
*
@@ -24,6 +35,7 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
2435
* @returns Nothing (void).
2536
*/
2637
onSseEvent?: (event: StreamEvent<TData>) => void;
38+
serializedBody?: RequestInit['body'];
2739
/**
2840
* Default retry delay in milliseconds.
2941
*
@@ -75,6 +87,7 @@ export type ServerSentEventsResult<
7587
};
7688

7789
export const createSseClient = <TData = unknown>({
90+
onRequest,
7891
onSseError,
7992
onSseEvent,
8093
responseTransformer,
@@ -112,7 +125,21 @@ export const createSseClient = <TData = unknown>({
112125
}
113126

114127
try {
115-
const response = await fetch(url, { ...options, headers, signal });
128+
const requestInit: RequestInit = {
129+
redirect: 'follow',
130+
...options,
131+
body: options.serializedBody,
132+
headers,
133+
signal,
134+
};
135+
let request = new Request(url, requestInit);
136+
if (onRequest) {
137+
request = await onRequest(url, requestInit);
138+
}
139+
// fetch must be assigned here, otherwise it would throw the error:
140+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
141+
const _fetch = options.fetch ?? globalThis.fetch;
142+
const response = await _fetch(request);
116143

117144
if (!response.ok)
118145
throw new Error(

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,15 @@ export const createClient = (config: Config = {}): Client => {
224224
body: opts.body as BodyInit | null | undefined,
225225
headers: opts.headers as unknown as Record<string, string>,
226226
method,
227+
onRequest: async (url, init) => {
228+
let request = new Request(url, init);
229+
for (const fn of interceptors.request._fns) {
230+
if (fn) {
231+
request = await fn(request, opts);
232+
}
233+
}
234+
return request;
235+
},
227236
url,
228237
});
229238
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface Config<T extends ClientOptions = ClientOptions>
2626
*
2727
* @default globalThis.fetch
2828
*/
29-
fetch?: (request: Request) => ReturnType<typeof fetch>;
29+
fetch?: typeof fetch;
3030
/**
3131
* Please don't use the Fetch client for Next.js applications. The `next`
3232
* options won't have any effect.

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
77
'method'
88
> &
99
Pick<Config, 'method' | 'responseTransformer' | 'responseValidator'> & {
10+
/**
11+
* Fetch API implementation. You can use this option to provide a custom
12+
* fetch instance.
13+
*
14+
* @default globalThis.fetch
15+
*/
16+
fetch?: typeof fetch;
17+
/**
18+
* Implementing clients can call request interceptors inside this hook.
19+
*/
20+
onRequest?: (url: string, init: RequestInit) => Promise<Request>;
1021
/**
1122
* Callback invoked when a network or parsing error occurs during streaming.
1223
*
@@ -24,6 +35,7 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
2435
* @returns Nothing (void).
2536
*/
2637
onSseEvent?: (event: StreamEvent<TData>) => void;
38+
serializedBody?: RequestInit['body'];
2739
/**
2840
* Default retry delay in milliseconds.
2941
*
@@ -75,6 +87,7 @@ export type ServerSentEventsResult<
7587
};
7688

7789
export const createSseClient = <TData = unknown>({
90+
onRequest,
7891
onSseError,
7992
onSseEvent,
8093
responseTransformer,
@@ -112,7 +125,21 @@ export const createSseClient = <TData = unknown>({
112125
}
113126

114127
try {
115-
const response = await fetch(url, { ...options, headers, signal });
128+
const requestInit: RequestInit = {
129+
redirect: 'follow',
130+
...options,
131+
body: options.serializedBody,
132+
headers,
133+
signal,
134+
};
135+
let request = new Request(url, requestInit);
136+
if (onRequest) {
137+
request = await onRequest(url, requestInit);
138+
}
139+
// fetch must be assigned here, otherwise it would throw the error:
140+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
141+
const _fetch = options.fetch ?? globalThis.fetch;
142+
const response = await _fetch(request);
116143

117144
if (!response.ok)
118145
throw new Error(

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/client-fetch/sdk-nested-classes-instance/client/client.gen.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,15 @@ export const createClient = (config: Config = {}): Client => {
224224
body: opts.body as BodyInit | null | undefined,
225225
headers: opts.headers as unknown as Record<string, string>,
226226
method,
227+
onRequest: async (url, init) => {
228+
let request = new Request(url, init);
229+
for (const fn of interceptors.request._fns) {
230+
if (fn) {
231+
request = await fn(request, opts);
232+
}
233+
}
234+
return request;
235+
},
227236
url,
228237
});
229238
};

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/client-fetch/sdk-nested-classes-instance/client/types.gen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface Config<T extends ClientOptions = ClientOptions>
2626
*
2727
* @default globalThis.fetch
2828
*/
29-
fetch?: (request: Request) => ReturnType<typeof fetch>;
29+
fetch?: typeof fetch;
3030
/**
3131
* Please don't use the Fetch client for Next.js applications. The `next`
3232
* options won't have any effect.

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/client-fetch/sdk-nested-classes-instance/core/serverSentEvents.gen.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
77
'method'
88
> &
99
Pick<Config, 'method' | 'responseTransformer' | 'responseValidator'> & {
10+
/**
11+
* Fetch API implementation. You can use this option to provide a custom
12+
* fetch instance.
13+
*
14+
* @default globalThis.fetch
15+
*/
16+
fetch?: typeof fetch;
17+
/**
18+
* Implementing clients can call request interceptors inside this hook.
19+
*/
20+
onRequest?: (url: string, init: RequestInit) => Promise<Request>;
1021
/**
1122
* Callback invoked when a network or parsing error occurs during streaming.
1223
*
@@ -24,6 +35,7 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
2435
* @returns Nothing (void).
2536
*/
2637
onSseEvent?: (event: StreamEvent<TData>) => void;
38+
serializedBody?: RequestInit['body'];
2739
/**
2840
* Default retry delay in milliseconds.
2941
*
@@ -75,6 +87,7 @@ export type ServerSentEventsResult<
7587
};
7688

7789
export const createSseClient = <TData = unknown>({
90+
onRequest,
7891
onSseError,
7992
onSseEvent,
8093
responseTransformer,
@@ -112,7 +125,21 @@ export const createSseClient = <TData = unknown>({
112125
}
113126

114127
try {
115-
const response = await fetch(url, { ...options, headers, signal });
128+
const requestInit: RequestInit = {
129+
redirect: 'follow',
130+
...options,
131+
body: options.serializedBody,
132+
headers,
133+
signal,
134+
};
135+
let request = new Request(url, requestInit);
136+
if (onRequest) {
137+
request = await onRequest(url, requestInit);
138+
}
139+
// fetch must be assigned here, otherwise it would throw the error:
140+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
141+
const _fetch = options.fetch ?? globalThis.fetch;
142+
const response = await _fetch(request);
116143

117144
if (!response.ok)
118145
throw new Error(

0 commit comments

Comments
 (0)