Skip to content

Commit 3b18362

Browse files
committed
Merge remote-tracking branch 'origin/main' into dev
2 parents f16225c + 9eb241f commit 3b18362

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

packages/plugins/swr/src/runtime/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
type ModelMeta,
88
type PrismaWriteActionType,
99
} from '@zenstackhq/runtime/cross';
10-
import * as crossFetch from 'cross-fetch';
1110
import { lowerCaseFirst } from 'lower-case-first';
1211
import { createContext, useContext } from 'react';
1312
import type { Cache, Fetcher, SWRConfiguration, SWRResponse } from 'swr';
@@ -376,10 +375,19 @@ export function useInvalidation(model: string, modelMeta: ModelMeta): Invalidato
376375
export async function fetcher<R, C extends boolean>(
377376
url: string,
378377
options?: RequestInit,
379-
fetch?: FetchFn,
378+
customFetch?: FetchFn,
380379
checkReadBack?: C
381380
): Promise<C extends true ? R | undefined : R> {
382-
const _fetch = fetch ?? crossFetch.fetch;
381+
// Note: 'cross-fetch' is supposed to handle fetch compatibility
382+
// but it doesn't work for cloudflare workers
383+
const _fetch =
384+
customFetch ??
385+
// check if fetch is available globally
386+
(typeof fetch === 'function'
387+
? fetch
388+
: // fallback to 'cross-fetch' if otherwise
389+
(await import('cross-fetch')).default);
390+
383391
const res = await _fetch(url, options);
384392
if (!res.ok) {
385393
const errData = unmarshal(await res.text());

packages/plugins/tanstack-query/src/runtime/common.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
type ModelMeta,
99
type PrismaWriteActionType,
1010
} from '@zenstackhq/runtime/cross';
11-
import * as crossFetch from 'cross-fetch';
1211

1312
/**
1413
* The default query endpoint.
@@ -133,10 +132,19 @@ export type APIContext = {
133132
export async function fetcher<R, C extends boolean>(
134133
url: string,
135134
options?: RequestInit,
136-
fetch?: FetchFn,
135+
customFetch?: FetchFn,
137136
checkReadBack?: C
138137
): Promise<C extends true ? R | undefined : R> {
139-
const _fetch = fetch ?? crossFetch.fetch;
138+
// Note: 'cross-fetch' is supposed to handle fetch compatibility
139+
// but it doesn't work for cloudflare workers
140+
const _fetch =
141+
customFetch ??
142+
// check if fetch is available globally
143+
(typeof fetch === 'function'
144+
? fetch
145+
: // fallback to 'cross-fetch' if otherwise
146+
(await import('cross-fetch')).default);
147+
140148
const res = await _fetch(url, options);
141149
if (!res.ok) {
142150
const errData = unmarshal(await res.text());

0 commit comments

Comments
 (0)