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: Rework how fetch and AC are accessed from global #3860

Merged
merged 4 commits into from
Feb 26, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 14 additions & 12 deletions packages/client/src/getFetch.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { getWindow } from './internals/fetchHelpers';
import { FetchEsque, NativeFetchEsque } from './internals/types';

export function getFetch(f?: FetchEsque | NativeFetchEsque): FetchEsque {
if (f) {
return f as FetchEsque;
export function getFetch(
customFetchImpl?: FetchEsque | NativeFetchEsque,
): FetchEsque {
if (customFetchImpl) {
return customFetchImpl as FetchEsque;
}

const win = getWindow();
const globalFetch = win.fetch;
if (globalFetch) {
return (
typeof globalFetch.bind === 'function'
? globalFetch.bind(win)
: globalFetch
) as FetchEsque;
if (typeof window !== 'undefined' && typeof window.fetch === 'function') {
return window.fetch.bind(window) as FetchEsque;
}

if (
typeof globalThis !== 'undefined' &&
typeof globalThis.fetch === 'function'
) {
return globalThis.fetch.bind(globalThis) as FetchEsque;
}

throw new Error('No fetch implementation found');
Expand Down
14 changes: 0 additions & 14 deletions packages/client/src/internals/fetchHelpers.ts

This file was deleted.

20 changes: 20 additions & 0 deletions packages/client/src/internals/getAbortController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Maybe } from '@trpc/server';
import { AbortControllerEsque } from './types';

export function getAbortController(
customAbortControllerImpl: Maybe<AbortControllerEsque>,
): AbortControllerEsque | null {
if (customAbortControllerImpl) {
return customAbortControllerImpl;
}

if (typeof window !== 'undefined' && window.AbortController) {
return window.AbortController;
}

if (typeof globalThis !== 'undefined' && globalThis.AbortController) {
return globalThis.AbortController;
}

return null;
}
2 changes: 1 addition & 1 deletion packages/client/src/internals/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { fetch as undiciFetch } from 'undici';
import { createTRPCProxyClient } from '../createTRPCClientProxy';
import { getFetch } from '../getFetch';
import { httpBatchLink } from '../links/httpBatchLink';
import { getAbortController } from './fetchHelpers';
import { getAbortController } from './getAbortController';
import {
AbortControllerEsque,
AbortControllerInstanceEsque,
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/links/internals/httpUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ProcedureType } from '@trpc/server';
import { TRPCResponse } from '@trpc/server/rpc';
import { getFetch } from '../../getFetch';
import { getAbortController } from '../../internals/fetchHelpers';
import { getAbortController } from '../../internals/getAbortController';
import {
AbortControllerEsque,
FetchEsque,
Expand Down
42 changes: 25 additions & 17 deletions packages/tests/server/clientInternals.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getFetch } from '@trpc/client/src';
import { getAbortController } from '@trpc/client/src/internals/fetchHelpers';
import { getAbortController } from '@trpc/client/src/internals/getAbortController';

describe('getAbortController() from..', () => {
test('passed', () => {
Expand All @@ -8,17 +8,22 @@ describe('getAbortController() from..', () => {
});
test('window', () => {
const sym: any = Symbol('test');

(global as any).AbortController = undefined;
(global as any).window = {};
(global as any).window.AbortController = sym;
(global as any).window = {
AbortController: sym,
};
expect(getAbortController(null)).toBe(sym);
});
test('global', () => {
const sym: any = Symbol('test');

(global as any).AbortController = sym;
delete (global as any).window;
(global as any).window = undefined;
expect(getAbortController(null)).toBe(sym);
});
test('window w. undefined AbortController -> global', () => {
const sym: any = Symbol('test');
(global as any).AbortController = sym;
(global as any).window = {};
expect(getAbortController(null)).toBe(sym);
});
test('neither', () => {
Expand All @@ -30,23 +35,26 @@ describe('getAbortController() from..', () => {

describe('getFetch() from...', () => {
test('passed', () => {
const sym: any = Symbol('test');
expect(getFetch(sym)).toBe(sym);
const customImpl: any = () => true;
expect(getFetch(customImpl)).toBe(customImpl);
});
test('window', () => {
const sym: any = Symbol('test');

(global as any).fetch = undefined;
(global as any).window = {};
(global as any).window.fetch = sym;
expect(getFetch()).toBe(sym);
(global as any).window = {
fetch: () => 42,
};
expect(getFetch()('')).toBe(42);
});
test('global', () => {
const sym: any = Symbol('test');

(global as any).fetch = sym;
(global as any).fetch = () => 1337;
(global as any).window = undefined;
delete (global as any).window;
expect(getFetch()).toBe(sym);
expect(getFetch()('')).toBe(1337);
});
test('window w. undefined fetch -> global', () => {
(global as any).fetch = () => 808;
(global as any).window = {};
expect(getFetch()('')).toBe(808);
});
test('neither -> throws', () => {
(global as any).fetch = undefined;
Expand Down
44 changes: 26 additions & 18 deletions packages/tests/server/interop/clientInternals.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getFetch } from '@trpc/client/src';
import { getAbortController } from '@trpc/client/src/internals/fetchHelpers';
import { getAbortController } from '@trpc/client/src/internals/getAbortController';

describe('getAbortController() from..', () => {
test('passed', () => {
Expand All @@ -8,17 +8,22 @@ describe('getAbortController() from..', () => {
});
test('window', () => {
const sym: any = Symbol('test');

(global as any).AbortController = undefined;
(global as any).window = {};
(global as any).window = AbortController = sym;
expect(getAbortController(null)).toBe(null);
(global as any).window = {
AbortController: sym,
};
expect(getAbortController(null)).toBe(sym);
});
test('global', () => {
const sym: any = Symbol('test');

(global as any).AbortController = sym;
delete (global as any).window;
(global as any).window = undefined;
expect(getAbortController(null)).toBe(sym);
});
test('window w. undefined AbortController -> global', () => {
const sym: any = Symbol('test');
(global as any).AbortController = sym;
(global as any).window = {};
expect(getAbortController(null)).toBe(sym);
});
test('neither', () => {
Expand All @@ -30,23 +35,26 @@ describe('getAbortController() from..', () => {

describe('getFetch() from...', () => {
test('passed', () => {
const sym: any = Symbol('test');
expect(getFetch(sym)).toBe(sym);
const customImpl: any = () => true;
expect(getFetch(customImpl)).toBe(customImpl);
});
test('window', () => {
const sym: any = Symbol('test');

(global as any).fetch = undefined;
(global as any).window = {};
(global as any).window.fetch = sym;
expect(getFetch()).toBe(sym);
(global as any).window = {
fetch: () => 42,
};
expect(getFetch()('')).toBe(42);
});
test('global', () => {
const sym: any = Symbol('test');

(global as any).fetch = sym;
(global as any).fetch = () => 1337;
(global as any).window = undefined;
delete (global as any).window;
expect(getFetch()).toBe(sym);
expect(getFetch()('')).toBe(1337);
});
test('window w. undefined fetch -> global', () => {
(global as any).fetch = () => 808;
(global as any).window = {};
expect(getFetch()('')).toBe(808);
});
test('neither -> throws', () => {
(global as any).fetch = undefined;
Expand Down