Skip to content

Commit 9eb241f

Browse files
authored
fix(hooks): prefer to use global fetch when available (#1826)
2 parents df9b0ff + 55c6fd6 commit 9eb241f

File tree

16 files changed

+36
-20
lines changed

16 files changed

+36
-20
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zenstack-monorepo",
3-
"version": "2.7.4",
3+
"version": "2.7.5",
44
"description": "",
55
"scripts": {
66
"build": "pnpm -r build",

packages/ide/jetbrains/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99
}
1010

1111
group = "dev.zenstack"
12-
version = "2.7.4"
12+
version = "2.7.5"
1313

1414
repositories {
1515
mavenCentral()

packages/ide/jetbrains/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jetbrains",
3-
"version": "2.7.4",
3+
"version": "2.7.5",
44
"displayName": "ZenStack JetBrains IDE Plugin",
55
"description": "ZenStack JetBrains IDE plugin",
66
"homepage": "https://zenstack.dev",

packages/language/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/language",
3-
"version": "2.7.4",
3+
"version": "2.7.5",
44
"displayName": "ZenStack modeling language compiler",
55
"description": "ZenStack modeling language compiler",
66
"homepage": "https://zenstack.dev",

packages/misc/redwood/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/redwood",
33
"displayName": "ZenStack RedwoodJS Integration",
4-
"version": "2.7.4",
4+
"version": "2.7.5",
55
"description": "CLI and runtime for integrating ZenStack with RedwoodJS projects.",
66
"repository": {
77
"type": "git",

packages/plugins/openapi/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/openapi",
33
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
4-
"version": "2.7.4",
4+
"version": "2.7.5",
55
"description": "ZenStack plugin and runtime supporting OpenAPI",
66
"main": "index.js",
77
"repository": {

packages/plugins/swr/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/swr",
33
"displayName": "ZenStack plugin for generating SWR hooks",
4-
"version": "2.7.4",
4+
"version": "2.7.5",
55
"description": "ZenStack plugin for generating SWR hooks",
66
"main": "index.js",
77
"repository": {

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/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/tanstack-query",
33
"displayName": "ZenStack plugin for generating tanstack-query hooks",
4-
"version": "2.7.4",
4+
"version": "2.7.5",
55
"description": "ZenStack plugin for generating tanstack-query hooks",
66
"main": "index.js",
77
"exports": {

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)