Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fetchWithEvent): allow customizing fetch impl type #414

Merged
merged 3 commits into from
Jun 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/utils/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,16 @@ export function getProxyRequestHeaders(event: H3Event) {
return headers;
}

export function fetchWithEvent(
export function fetchWithEvent<
T = unknown,
_R = any,
F extends (req: RequestInfo | URL, opts?: any) => any = typeof fetch
>(
event: H3Event,
req: RequestInfo | URL,
init?: RequestInit & { context?: H3EventContext },
options?: { fetch: typeof fetch }
) {
options?: { fetch: F }
): unknown extends T ? ReturnType<F> : T {
return _getFetch(options?.fetch)(req, <RequestInit>{
...init,
context: init?.context || event.context,
Expand All @@ -169,7 +173,7 @@ export function fetchWithEvent(

// -- internal utils --

function _getFetch(_fetch?: typeof fetch) {
function _getFetch<T = typeof fetch>(_fetch?: T) {
if (_fetch) {
return _fetch;
}
Expand Down