Skip to content

Commit

Permalink
fix(hooks): prefer to use global fetch when available (#1826)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Nov 3, 2024
2 parents df9b0ff + 55c6fd6 commit 9eb241f
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-monorepo",
"version": "2.7.4",
"version": "2.7.5",
"description": "",
"scripts": {
"build": "pnpm -r build",
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/jetbrains/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "dev.zenstack"
version = "2.7.4"
version = "2.7.5"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/jetbrains/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jetbrains",
"version": "2.7.4",
"version": "2.7.5",
"displayName": "ZenStack JetBrains IDE Plugin",
"description": "ZenStack JetBrains IDE plugin",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/language/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/language",
"version": "2.7.4",
"version": "2.7.5",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/misc/redwood/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/redwood",
"displayName": "ZenStack RedwoodJS Integration",
"version": "2.7.4",
"version": "2.7.5",
"description": "CLI and runtime for integrating ZenStack with RedwoodJS projects.",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/openapi/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/openapi",
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
"version": "2.7.4",
"version": "2.7.5",
"description": "ZenStack plugin and runtime supporting OpenAPI",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/swr/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/swr",
"displayName": "ZenStack plugin for generating SWR hooks",
"version": "2.7.4",
"version": "2.7.5",
"description": "ZenStack plugin for generating SWR hooks",
"main": "index.js",
"repository": {
Expand Down
14 changes: 11 additions & 3 deletions packages/plugins/swr/src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
type ModelMeta,
type PrismaWriteActionType,
} from '@zenstackhq/runtime/cross';
import * as crossFetch from 'cross-fetch';
import { lowerCaseFirst } from 'lower-case-first';
import { createContext, useContext } from 'react';
import type { Cache, Fetcher, SWRConfiguration, SWRResponse } from 'swr';
Expand Down Expand Up @@ -376,10 +375,19 @@ export function useInvalidation(model: string, modelMeta: ModelMeta): Invalidato
export async function fetcher<R, C extends boolean>(
url: string,
options?: RequestInit,
fetch?: FetchFn,
customFetch?: FetchFn,
checkReadBack?: C
): Promise<C extends true ? R | undefined : R> {
const _fetch = fetch ?? crossFetch.fetch;
// Note: 'cross-fetch' is supposed to handle fetch compatibility
// but it doesn't work for cloudflare workers
const _fetch =
customFetch ??
// check if fetch is available globally
(typeof fetch === 'function'
? fetch
: // fallback to 'cross-fetch' if otherwise
(await import('cross-fetch')).default);

const res = await _fetch(url, options);
if (!res.ok) {
const errData = unmarshal(await res.text());
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/tanstack-query/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/tanstack-query",
"displayName": "ZenStack plugin for generating tanstack-query hooks",
"version": "2.7.4",
"version": "2.7.5",
"description": "ZenStack plugin for generating tanstack-query hooks",
"main": "index.js",
"exports": {
Expand Down
14 changes: 11 additions & 3 deletions packages/plugins/tanstack-query/src/runtime/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
type ModelMeta,
type PrismaWriteActionType,
} from '@zenstackhq/runtime/cross';
import * as crossFetch from 'cross-fetch';

/**
* The default query endpoint.
Expand Down Expand Up @@ -133,10 +132,19 @@ export type APIContext = {
export async function fetcher<R, C extends boolean>(
url: string,
options?: RequestInit,
fetch?: FetchFn,
customFetch?: FetchFn,
checkReadBack?: C
): Promise<C extends true ? R | undefined : R> {
const _fetch = fetch ?? crossFetch.fetch;
// Note: 'cross-fetch' is supposed to handle fetch compatibility
// but it doesn't work for cloudflare workers
const _fetch =
customFetch ??
// check if fetch is available globally
(typeof fetch === 'function'
? fetch
: // fallback to 'cross-fetch' if otherwise
(await import('cross-fetch')).default);

const res = await _fetch(url, options);
if (!res.ok) {
const errData = unmarshal(await res.text());
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/trpc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/trpc",
"displayName": "ZenStack plugin for tRPC",
"version": "2.7.4",
"version": "2.7.5",
"description": "ZenStack plugin for tRPC",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/runtime",
"displayName": "ZenStack Runtime Library",
"version": "2.7.4",
"version": "2.7.5",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack Language Tools",
"description": "FullStack enhancement for Prisma ORM: seamless integration from database to UI",
"version": "2.7.4",
"version": "2.7.5",
"author": {
"name": "ZenStack Team"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/sdk",
"version": "2.7.4",
"version": "2.7.5",
"description": "ZenStack plugin development SDK",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/server",
"version": "2.7.4",
"version": "2.7.5",
"displayName": "ZenStack Server-side Adapters",
"description": "ZenStack server-side adapters",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/testtools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/testtools",
"version": "2.7.4",
"version": "2.7.5",
"description": "ZenStack Test Tools",
"main": "index.js",
"private": true,
Expand Down

0 comments on commit 9eb241f

Please sign in to comment.