这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 5 additions & 0 deletions .changeset/cyan-crabs-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix(client): pass fetch option to sse client
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ export const createClient = (config: Config = {}): Client => {
body: opts.body as BodyInit | null | undefined,
headers: opts.headers as unknown as Record<string, string>,
method,
onRequest: async (url, init) => {
let request = new Request(url, init);
for (const fn of interceptors.request._fns) {
if (fn) {
request = await fn(request, opts);
}
}
return request;
},
url,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface Config<T extends ClientOptions = ClientOptions>
*
* @default globalThis.fetch
*/
fetch?: (request: Request) => ReturnType<typeof fetch>;
fetch?: typeof fetch;
/**
* Please don't use the Fetch client for Next.js applications. The `next`
* options won't have any effect.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
'method'
> &
Pick<Config, 'method' | 'responseTransformer' | 'responseValidator'> & {
/**
* Fetch API implementation. You can use this option to provide a custom
* fetch instance.
*
* @default globalThis.fetch
*/
fetch?: typeof fetch;
/**
* Implementing clients can call request interceptors inside this hook.
*/
onRequest?: (url: string, init: RequestInit) => Promise<Request>;
/**
* Callback invoked when a network or parsing error occurs during streaming.
*
Expand All @@ -24,6 +35,7 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
* @returns Nothing (void).
*/
onSseEvent?: (event: StreamEvent<TData>) => void;
serializedBody?: RequestInit['body'];
/**
* Default retry delay in milliseconds.
*
Expand Down Expand Up @@ -75,6 +87,7 @@ export type ServerSentEventsResult<
};

export const createSseClient = <TData = unknown>({
onRequest,
onSseError,
onSseEvent,
responseTransformer,
Expand Down Expand Up @@ -112,7 +125,21 @@ export const createSseClient = <TData = unknown>({
}

try {
const response = await fetch(url, { ...options, headers, signal });
const requestInit: RequestInit = {
redirect: 'follow',
...options,
body: options.serializedBody,
headers,
signal,
};
let request = new Request(url, requestInit);
if (onRequest) {
request = await onRequest(url, requestInit);
}
// fetch must be assigned here, otherwise it would throw the error:
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
const _fetch = options.fetch ?? globalThis.fetch;
const response = await _fetch(request);

if (!response.ok)
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ export const createClient = (config: Config = {}): Client => {
body: opts.body as BodyInit | null | undefined,
headers: opts.headers as unknown as Record<string, string>,
method,
onRequest: async (url, init) => {
let request = new Request(url, init);
for (const fn of interceptors.request._fns) {
if (fn) {
request = await fn(request, opts);
}
}
return request;
},
url,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface Config<T extends ClientOptions = ClientOptions>
*
* @default globalThis.fetch
*/
fetch?: (request: Request) => ReturnType<typeof fetch>;
fetch?: typeof fetch;
/**
* Please don't use the Fetch client for Next.js applications. The `next`
* options won't have any effect.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
'method'
> &
Pick<Config, 'method' | 'responseTransformer' | 'responseValidator'> & {
/**
* Fetch API implementation. You can use this option to provide a custom
* fetch instance.
*
* @default globalThis.fetch
*/
fetch?: typeof fetch;
/**
* Implementing clients can call request interceptors inside this hook.
*/
onRequest?: (url: string, init: RequestInit) => Promise<Request>;
/**
* Callback invoked when a network or parsing error occurs during streaming.
*
Expand All @@ -24,6 +35,7 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
* @returns Nothing (void).
*/
onSseEvent?: (event: StreamEvent<TData>) => void;
serializedBody?: RequestInit['body'];
/**
* Default retry delay in milliseconds.
*
Expand Down Expand Up @@ -75,6 +87,7 @@ export type ServerSentEventsResult<
};

export const createSseClient = <TData = unknown>({
onRequest,
onSseError,
onSseEvent,
responseTransformer,
Expand Down Expand Up @@ -112,7 +125,21 @@ export const createSseClient = <TData = unknown>({
}

try {
const response = await fetch(url, { ...options, headers, signal });
const requestInit: RequestInit = {
redirect: 'follow',
...options,
body: options.serializedBody,
headers,
signal,
};
let request = new Request(url, requestInit);
if (onRequest) {
request = await onRequest(url, requestInit);
}
// fetch must be assigned here, otherwise it would throw the error:
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
const _fetch = options.fetch ?? globalThis.fetch;
const response = await _fetch(request);

if (!response.ok)
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ export const createClient = (config: Config = {}): Client => {
body: opts.body as BodyInit | null | undefined,
headers: opts.headers as unknown as Record<string, string>,
method,
onRequest: async (url, init) => {
let request = new Request(url, init);
for (const fn of interceptors.request._fns) {
if (fn) {
request = await fn(request, opts);
}
}
return request;
},
url,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface Config<T extends ClientOptions = ClientOptions>
*
* @default globalThis.fetch
*/
fetch?: (request: Request) => ReturnType<typeof fetch>;
fetch?: typeof fetch;
/**
* Please don't use the Fetch client for Next.js applications. The `next`
* options won't have any effect.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
'method'
> &
Pick<Config, 'method' | 'responseTransformer' | 'responseValidator'> & {
/**
* Fetch API implementation. You can use this option to provide a custom
* fetch instance.
*
* @default globalThis.fetch
*/
fetch?: typeof fetch;
/**
* Implementing clients can call request interceptors inside this hook.
*/
onRequest?: (url: string, init: RequestInit) => Promise<Request>;
/**
* Callback invoked when a network or parsing error occurs during streaming.
*
Expand All @@ -24,6 +35,7 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
* @returns Nothing (void).
*/
onSseEvent?: (event: StreamEvent<TData>) => void;
serializedBody?: RequestInit['body'];
/**
* Default retry delay in milliseconds.
*
Expand Down Expand Up @@ -75,6 +87,7 @@ export type ServerSentEventsResult<
};

export const createSseClient = <TData = unknown>({
onRequest,
onSseError,
onSseEvent,
responseTransformer,
Expand Down Expand Up @@ -112,7 +125,21 @@ export const createSseClient = <TData = unknown>({
}

try {
const response = await fetch(url, { ...options, headers, signal });
const requestInit: RequestInit = {
redirect: 'follow',
...options,
body: options.serializedBody,
headers,
signal,
};
let request = new Request(url, requestInit);
if (onRequest) {
request = await onRequest(url, requestInit);
}
// fetch must be assigned here, otherwise it would throw the error:
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
const _fetch = options.fetch ?? globalThis.fetch;
const response = await _fetch(request);

if (!response.ok)
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ export const createClient = (config: Config = {}): Client => {
body: opts.body as BodyInit | null | undefined,
headers: opts.headers as unknown as Record<string, string>,
method,
onRequest: async (url, init) => {
let request = new Request(url, init);
for (const fn of interceptors.request._fns) {
if (fn) {
request = await fn(request, opts);
}
}
return request;
},
url,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface Config<T extends ClientOptions = ClientOptions>
*
* @default globalThis.fetch
*/
fetch?: (request: Request) => ReturnType<typeof fetch>;
fetch?: typeof fetch;
/**
* Please don't use the Fetch client for Next.js applications. The `next`
* options won't have any effect.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
'method'
> &
Pick<Config, 'method' | 'responseTransformer' | 'responseValidator'> & {
/**
* Fetch API implementation. You can use this option to provide a custom
* fetch instance.
*
* @default globalThis.fetch
*/
fetch?: typeof fetch;
/**
* Implementing clients can call request interceptors inside this hook.
*/
onRequest?: (url: string, init: RequestInit) => Promise<Request>;
/**
* Callback invoked when a network or parsing error occurs during streaming.
*
Expand All @@ -24,6 +35,7 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
* @returns Nothing (void).
*/
onSseEvent?: (event: StreamEvent<TData>) => void;
serializedBody?: RequestInit['body'];
/**
* Default retry delay in milliseconds.
*
Expand Down Expand Up @@ -75,6 +87,7 @@ export type ServerSentEventsResult<
};

export const createSseClient = <TData = unknown>({
onRequest,
onSseError,
onSseEvent,
responseTransformer,
Expand Down Expand Up @@ -112,7 +125,21 @@ export const createSseClient = <TData = unknown>({
}

try {
const response = await fetch(url, { ...options, headers, signal });
const requestInit: RequestInit = {
redirect: 'follow',
...options,
body: options.serializedBody,
headers,
signal,
};
let request = new Request(url, requestInit);
if (onRequest) {
request = await onRequest(url, requestInit);
}
// fetch must be assigned here, otherwise it would throw the error:
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
const _fetch = options.fetch ?? globalThis.fetch;
const response = await _fetch(request);

if (!response.ok)
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ export const createClient = (config: Config = {}): Client => {
body: opts.body as BodyInit | null | undefined,
headers: opts.headers as unknown as Record<string, string>,
method,
onRequest: async (url, init) => {
let request = new Request(url, init);
for (const fn of interceptors.request._fns) {
if (fn) {
request = await fn(request, opts);
}
}
return request;
},
url,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface Config<T extends ClientOptions = ClientOptions>
*
* @default globalThis.fetch
*/
fetch?: (request: Request) => ReturnType<typeof fetch>;
fetch?: typeof fetch;
/**
* Please don't use the Fetch client for Next.js applications. The `next`
* options won't have any effect.
Expand Down
Loading
Loading