@trpc/client 10.12.0 → 10.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  import { FetchEsque, NativeFetchEsque } from './internals/types';
2
- export declare function getFetch(f?: FetchEsque | NativeFetchEsque): FetchEsque;
2
+ export declare function getFetch(customFetchImpl?: FetchEsque | NativeFetchEsque): FetchEsque;
3
3
  //# sourceMappingURL=getFetch.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getFetch.d.ts","sourceRoot":"","sources":["../src/getFetch.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAEjE,wBAAgB,QAAQ,CAAC,CAAC,CAAC,EAAE,UAAU,GAAG,gBAAgB,GAAG,UAAU,CAgBtE"}
1
+ {"version":3,"file":"getFetch.d.ts","sourceRoot":"","sources":["../src/getFetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAEjE,wBAAgB,QAAQ,CACtB,eAAe,CAAC,EAAE,UAAU,GAAG,gBAAgB,GAC9C,UAAU,CAiBZ"}
@@ -1,23 +1,27 @@
1
- function getWindow() {
2
- if (typeof window !== 'undefined') {
3
- return window;
1
+ function getFetch(customFetchImpl) {
2
+ if (customFetchImpl) {
3
+ return customFetchImpl;
4
4
  }
5
- return globalThis;
6
- }
7
- function getAbortController(ac) {
8
- return ac ?? getWindow().AbortController ?? null;
5
+ if (typeof window !== 'undefined' && typeof window.fetch === 'function') {
6
+ return window.fetch.bind(window);
7
+ }
8
+ if (typeof globalThis !== 'undefined' && typeof globalThis.fetch === 'function') {
9
+ return globalThis.fetch.bind(globalThis);
10
+ }
11
+ throw new Error('No fetch implementation found');
9
12
  }
10
13
 
11
- function getFetch(f) {
12
- if (f) {
13
- return f;
14
+ function getAbortController(customAbortControllerImpl) {
15
+ if (customAbortControllerImpl) {
16
+ return customAbortControllerImpl;
14
17
  }
15
- const win = getWindow();
16
- const globalFetch = win.fetch;
17
- if (globalFetch) {
18
- return typeof globalFetch.bind === 'function' ? globalFetch.bind(win) : globalFetch;
18
+ if (typeof window !== 'undefined' && window.AbortController) {
19
+ return window.AbortController;
19
20
  }
20
- throw new Error('No fetch implementation found');
21
+ if (typeof globalThis !== 'undefined' && globalThis.AbortController) {
22
+ return globalThis.AbortController;
23
+ }
24
+ return null;
21
25
  }
22
26
 
23
27
  function resolveHTTPLinkOptions(opts) {
@@ -77,7 +81,7 @@ function httpRequest(opts) {
77
81
  const body = getBody(opts);
78
82
  const meta = {};
79
83
  Promise.resolve(opts.headers()).then((headers)=>{
80
- /* istanbul ignore if */ if (type === 'subscription') {
84
+ /* istanbul ignore if -- @preserve */ if (type === 'subscription') {
81
85
  throw new Error('Subscriptions should use wsLink');
82
86
  }
83
87
  return opts.fetch(url, {
@@ -1,25 +1,29 @@
1
1
  'use strict';
2
2
 
3
- function getWindow() {
4
- if (typeof window !== 'undefined') {
5
- return window;
3
+ function getFetch(customFetchImpl) {
4
+ if (customFetchImpl) {
5
+ return customFetchImpl;
6
6
  }
7
- return globalThis;
8
- }
9
- function getAbortController(ac) {
10
- return ac ?? getWindow().AbortController ?? null;
7
+ if (typeof window !== 'undefined' && typeof window.fetch === 'function') {
8
+ return window.fetch.bind(window);
9
+ }
10
+ if (typeof globalThis !== 'undefined' && typeof globalThis.fetch === 'function') {
11
+ return globalThis.fetch.bind(globalThis);
12
+ }
13
+ throw new Error('No fetch implementation found');
11
14
  }
12
15
 
13
- function getFetch(f) {
14
- if (f) {
15
- return f;
16
+ function getAbortController(customAbortControllerImpl) {
17
+ if (customAbortControllerImpl) {
18
+ return customAbortControllerImpl;
16
19
  }
17
- const win = getWindow();
18
- const globalFetch = win.fetch;
19
- if (globalFetch) {
20
- return typeof globalFetch.bind === 'function' ? globalFetch.bind(win) : globalFetch;
20
+ if (typeof window !== 'undefined' && window.AbortController) {
21
+ return window.AbortController;
21
22
  }
22
- throw new Error('No fetch implementation found');
23
+ if (typeof globalThis !== 'undefined' && globalThis.AbortController) {
24
+ return globalThis.AbortController;
25
+ }
26
+ return null;
23
27
  }
24
28
 
25
29
  function resolveHTTPLinkOptions(opts) {
@@ -79,7 +83,7 @@ function httpRequest(opts) {
79
83
  const body = getBody(opts);
80
84
  const meta = {};
81
85
  Promise.resolve(opts.headers()).then((headers)=>{
82
- /* istanbul ignore if */ if (type === 'subscription') {
86
+ /* istanbul ignore if -- @preserve */ if (type === 'subscription') {
83
87
  throw new Error('Subscriptions should use wsLink');
84
88
  }
85
89
  return opts.fetch(url, {
@@ -0,0 +1,120 @@
1
+ function getFetch(customFetchImpl) {
2
+ if (customFetchImpl) {
3
+ return customFetchImpl;
4
+ }
5
+ if (typeof window !== 'undefined' && typeof window.fetch === 'function') {
6
+ return window.fetch.bind(window);
7
+ }
8
+ if (typeof globalThis !== 'undefined' &&
9
+ typeof globalThis.fetch === 'function') {
10
+ return globalThis.fetch.bind(globalThis);
11
+ }
12
+ throw new Error('No fetch implementation found');
13
+ }
14
+
15
+ function getAbortController(customAbortControllerImpl) {
16
+ if (customAbortControllerImpl) {
17
+ return customAbortControllerImpl;
18
+ }
19
+ if (typeof window !== 'undefined' && window.AbortController) {
20
+ return window.AbortController;
21
+ }
22
+ if (typeof globalThis !== 'undefined' && globalThis.AbortController) {
23
+ return globalThis.AbortController;
24
+ }
25
+ return null;
26
+ }
27
+
28
+ function resolveHTTPLinkOptions(opts) {
29
+ const headers = opts.headers || (() => ({}));
30
+ return {
31
+ url: opts.url,
32
+ fetch: getFetch(opts.fetch),
33
+ AbortController: getAbortController(opts.AbortController),
34
+ headers: typeof headers === 'function' ? headers : () => headers,
35
+ };
36
+ }
37
+ // https://github.com/trpc/trpc/pull/669
38
+ function arrayToDict(array) {
39
+ const dict = {};
40
+ for (let index = 0; index < array.length; index++) {
41
+ const element = array[index];
42
+ dict[index] = element;
43
+ }
44
+ return dict;
45
+ }
46
+ const METHOD = {
47
+ query: 'GET',
48
+ mutation: 'POST',
49
+ };
50
+ function getInput(opts) {
51
+ return 'input' in opts
52
+ ? opts.runtime.transformer.serialize(opts.input)
53
+ : arrayToDict(opts.inputs.map((_input) => opts.runtime.transformer.serialize(_input)));
54
+ }
55
+ function getUrl(opts) {
56
+ let url = opts.url + '/' + opts.path;
57
+ const queryParts = [];
58
+ if ('inputs' in opts) {
59
+ queryParts.push('batch=1');
60
+ }
61
+ if (opts.type === 'query') {
62
+ const input = getInput(opts);
63
+ if (input !== undefined) {
64
+ queryParts.push(`input=${encodeURIComponent(JSON.stringify(input))}`);
65
+ }
66
+ }
67
+ if (queryParts.length) {
68
+ url += '?' + queryParts.join('&');
69
+ }
70
+ return url;
71
+ }
72
+ function getBody(opts) {
73
+ if (opts.type === 'query') {
74
+ return undefined;
75
+ }
76
+ const input = getInput(opts);
77
+ return input !== undefined ? JSON.stringify(input) : undefined;
78
+ }
79
+ function httpRequest(opts) {
80
+ const { type } = opts;
81
+ const ac = opts.AbortController ? new opts.AbortController() : null;
82
+ const promise = new Promise((resolve, reject) => {
83
+ const url = getUrl(opts);
84
+ const body = getBody(opts);
85
+ const meta = {};
86
+ Promise.resolve(opts.headers())
87
+ .then((headers) => {
88
+ /* istanbul ignore if -- @preserve */
89
+ if (type === 'subscription') {
90
+ throw new Error('Subscriptions should use wsLink');
91
+ }
92
+ return opts.fetch(url, {
93
+ method: METHOD[type],
94
+ signal: ac?.signal,
95
+ body: body,
96
+ headers: {
97
+ 'content-type': 'application/json',
98
+ ...headers,
99
+ },
100
+ });
101
+ })
102
+ .then((_res) => {
103
+ meta.response = _res;
104
+ return _res.json();
105
+ })
106
+ .then((json) => {
107
+ resolve({
108
+ json: json,
109
+ meta,
110
+ });
111
+ })
112
+ .catch(reject);
113
+ });
114
+ const cancel = () => {
115
+ ac?.abort();
116
+ };
117
+ return { promise, cancel };
118
+ }
119
+
120
+ export { getUrl as a, getFetch as g, httpRequest as h, resolveHTTPLinkOptions as r };
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ var observable = require('@trpc/server/observable');
6
6
  var transformResult = require('./transformResult-e7a1e69e.js');
7
7
  var links_splitLink = require('./splitLink-f29e84be.js');
8
8
  var shared = require('@trpc/server/shared');
9
- var httpUtils = require('./httpUtils-30a43613.js');
9
+ var httpUtils = require('./httpUtils-ce40966a.js');
10
10
  var links_httpBatchLink = require('./links/httpBatchLink.js');
11
11
  var links_httpLink = require('./links/httpLink.js');
12
12
  var links_loggerLink = require('./links/loggerLink.js');
package/dist/index.mjs CHANGED
@@ -4,7 +4,7 @@ export { T as TRPCClientError } from './transformResult-6fb67924.mjs';
4
4
  import { c as createChain } from './splitLink-4c75f7be.mjs';
5
5
  export { s as splitLink } from './splitLink-4c75f7be.mjs';
6
6
  import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared';
7
- export { g as getFetch } from './httpUtils-9322dc79.mjs';
7
+ export { g as getFetch } from './httpUtils-24d9ee59.mjs';
8
8
  export { httpBatchLink } from './links/httpBatchLink.mjs';
9
9
  export { httpLink } from './links/httpLink.mjs';
10
10
  export { loggerLink } from './links/loggerLink.mjs';
@@ -0,0 +1,4 @@
1
+ import { Maybe } from '@trpc/server';
2
+ import { AbortControllerEsque } from './types';
3
+ export declare function getAbortController(customAbortControllerImpl: Maybe<AbortControllerEsque>): AbortControllerEsque | null;
4
+ //# sourceMappingURL=getAbortController.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getAbortController.d.ts","sourceRoot":"","sources":["../../src/internals/getAbortController.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAE/C,wBAAgB,kBAAkB,CAChC,yBAAyB,EAAE,KAAK,CAAC,oBAAoB,CAAC,GACrD,oBAAoB,GAAG,IAAI,CAc7B"}
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var observable = require('@trpc/server/observable');
6
6
  var transformResult = require('../transformResult-e7a1e69e.js');
7
- var httpUtils = require('../httpUtils-30a43613.js');
7
+ var httpUtils = require('../httpUtils-ce40966a.js');
8
8
 
9
9
  /* eslint-disable @typescript-eslint/no-non-null-assertion */ /**
10
10
  * A function that should never be called unless we messed something up.
@@ -1,6 +1,6 @@
1
1
  import { observable } from '@trpc/server/observable';
2
2
  import { t as transformResult, T as TRPCClientError } from '../transformResult-6fb67924.mjs';
3
- import { r as resolveHTTPLinkOptions, a as getUrl, h as httpRequest } from '../httpUtils-9322dc79.mjs';
3
+ import { r as resolveHTTPLinkOptions, a as getUrl, h as httpRequest } from '../httpUtils-24d9ee59.mjs';
4
4
 
5
5
  /* eslint-disable @typescript-eslint/no-non-null-assertion */ /**
6
6
  * A function that should never be called unless we messed something up.
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var observable = require('@trpc/server/observable');
6
6
  var transformResult = require('../transformResult-e7a1e69e.js');
7
- var httpUtils = require('../httpUtils-30a43613.js');
7
+ var httpUtils = require('../httpUtils-ce40966a.js');
8
8
 
9
9
  function httpLink(opts) {
10
10
  const resolvedOpts = httpUtils.resolveHTTPLinkOptions(opts);
@@ -1,6 +1,6 @@
1
1
  import { observable } from '@trpc/server/observable';
2
2
  import { t as transformResult, T as TRPCClientError } from '../transformResult-6fb67924.mjs';
3
- import { r as resolveHTTPLinkOptions, h as httpRequest } from '../httpUtils-9322dc79.mjs';
3
+ import { r as resolveHTTPLinkOptions, h as httpRequest } from '../httpUtils-24d9ee59.mjs';
4
4
 
5
5
  function httpLink(opts) {
6
6
  const resolvedOpts = resolveHTTPLinkOptions(opts);
@@ -5,11 +5,11 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var observable = require('@trpc/server/observable');
6
6
  var transformResult = require('../transformResult-e7a1e69e.js');
7
7
 
8
- /* istanbul ignore next */ const retryDelay = (attemptIndex)=>attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
8
+ /* istanbul ignore next -- @preserve */ const retryDelay = (attemptIndex)=>attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
9
9
 
10
10
  function createWSClient(opts) {
11
11
  const { url , WebSocket: WebSocketImpl = WebSocket , retryDelayMs: retryDelayFn = retryDelay , onOpen , onClose } = opts;
12
- /* istanbul ignore next */ if (!WebSocketImpl) {
12
+ /* istanbul ignore next -- @preserve */ if (!WebSocketImpl) {
13
13
  throw new Error("No WebSocket implementation found - you probably don't want to use this on the server, but if you do you need to pass a `WebSocket`-ponyfill");
14
14
  }
15
15
  /**
@@ -79,7 +79,7 @@ function createWSClient(opts) {
79
79
  clearTimeout(connectTimer);
80
80
  connectTimer = null;
81
81
  conn.addEventListener('open', ()=>{
82
- /* istanbul ignore next */ if (conn !== activeConnection) {
82
+ /* istanbul ignore next -- @preserve */ if (conn !== activeConnection) {
83
83
  return;
84
84
  }
85
85
  connectAttempt = 0;
@@ -1,11 +1,11 @@
1
1
  import { observable } from '@trpc/server/observable';
2
2
  import { T as TRPCClientError, t as transformResult } from '../transformResult-6fb67924.mjs';
3
3
 
4
- /* istanbul ignore next */ const retryDelay = (attemptIndex)=>attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
4
+ /* istanbul ignore next -- @preserve */ const retryDelay = (attemptIndex)=>attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
5
5
 
6
6
  function createWSClient(opts) {
7
7
  const { url , WebSocket: WebSocketImpl = WebSocket , retryDelayMs: retryDelayFn = retryDelay , onOpen , onClose } = opts;
8
- /* istanbul ignore next */ if (!WebSocketImpl) {
8
+ /* istanbul ignore next -- @preserve */ if (!WebSocketImpl) {
9
9
  throw new Error("No WebSocket implementation found - you probably don't want to use this on the server, but if you do you need to pass a `WebSocket`-ponyfill");
10
10
  }
11
11
  /**
@@ -75,7 +75,7 @@ function createWSClient(opts) {
75
75
  clearTimeout(connectTimer);
76
76
  connectTimer = null;
77
77
  conn.addEventListener('open', ()=>{
78
- /* istanbul ignore next */ if (conn !== activeConnection) {
78
+ /* istanbul ignore next -- @preserve */ if (conn !== activeConnection) {
79
79
  return;
80
80
  }
81
81
  connectAttempt = 0;
@@ -0,0 +1,41 @@
1
+ import { observable } from '@trpc/server/observable';
2
+
3
+ /** @internal */
4
+ function createChain(opts) {
5
+ return observable((observer) => {
6
+ function execute(index = 0, op = opts.op) {
7
+ const next = opts.links[index];
8
+ if (!next) {
9
+ throw new Error('No more links to execute - did you forget to add an ending link?');
10
+ }
11
+ const subscription = next({
12
+ op,
13
+ next(nextOp) {
14
+ const nextObserver = execute(index + 1, nextOp);
15
+ return nextObserver;
16
+ },
17
+ });
18
+ return subscription;
19
+ }
20
+ const obs$ = execute();
21
+ return obs$.subscribe(observer);
22
+ });
23
+ }
24
+
25
+ function asArray(value) {
26
+ return Array.isArray(value) ? value : [value];
27
+ }
28
+ function splitLink(opts) {
29
+ return (runtime) => {
30
+ const yes = asArray(opts.true).map((link) => link(runtime));
31
+ const no = asArray(opts.false).map((link) => link(runtime));
32
+ return (props) => {
33
+ return observable((observer) => {
34
+ const links = opts.condition(props.op) ? yes : no;
35
+ return createChain({ op: props.op, links }).subscribe(observer);
36
+ });
37
+ };
38
+ };
39
+ }
40
+
41
+ export { createChain as c, splitLink as s };
@@ -0,0 +1,86 @@
1
+ class TRPCClientError extends Error {
2
+ constructor(message, opts) {
3
+ const cause = opts?.cause;
4
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
5
+ // @ts-ignore https://github.com/tc39/proposal-error-cause
6
+ super(message, { cause });
7
+ this.meta = opts?.meta;
8
+ this.cause = cause;
9
+ this.shape = opts?.result?.error;
10
+ this.data = opts?.result?.error.data;
11
+ this.name = 'TRPCClientError';
12
+ Object.setPrototypeOf(this, TRPCClientError.prototype);
13
+ }
14
+ static from(cause, opts = {}) {
15
+ if (!(cause instanceof Error)) {
16
+ return new TRPCClientError(cause.error.message ?? '', {
17
+ ...opts,
18
+ cause: undefined,
19
+ result: cause,
20
+ });
21
+ }
22
+ if (cause.name === 'TRPCClientError') {
23
+ return cause;
24
+ }
25
+ return new TRPCClientError(cause.message, {
26
+ ...opts,
27
+ cause,
28
+ result: null,
29
+ });
30
+ }
31
+ }
32
+
33
+ // FIXME:
34
+ // - the generics here are probably unnecessary
35
+ // - the RPC-spec could probably be simplified to combine HTTP + WS
36
+ /** @internal */
37
+ function transformResultInner(response, runtime) {
38
+ if ('error' in response) {
39
+ const error = runtime.transformer.deserialize(response.error);
40
+ return {
41
+ ok: false,
42
+ error: {
43
+ ...response,
44
+ error,
45
+ },
46
+ };
47
+ }
48
+ const result = {
49
+ ...response.result,
50
+ ...((!response.result.type || response.result.type === 'data') && {
51
+ type: 'data',
52
+ data: runtime.transformer.deserialize(response.result.data),
53
+ }),
54
+ };
55
+ return { ok: true, result };
56
+ }
57
+ function isObject(value) {
58
+ // check that value is object
59
+ return !!value && !Array.isArray(value) && typeof value === 'object';
60
+ }
61
+ /**
62
+ * Transforms and validates that the result is a valid TRPCResponse
63
+ * @internal
64
+ */
65
+ function transformResult(response, runtime) {
66
+ let result;
67
+ try {
68
+ // Use the data transformers on the JSON-response
69
+ result = transformResultInner(response, runtime);
70
+ }
71
+ catch (err) {
72
+ throw new TRPCClientError('Unable to transform response from server');
73
+ }
74
+ // check that output of the transformers is a valid TRPCResponse
75
+ if (!result.ok &&
76
+ (!isObject(result.error.error) ||
77
+ typeof result.error.error.code !== 'number')) {
78
+ throw new TRPCClientError('Badly formatted response from server');
79
+ }
80
+ if (result.ok && !isObject(result.result)) {
81
+ throw new TRPCClientError('Badly formatted response from server');
82
+ }
83
+ return result;
84
+ }
85
+
86
+ export { TRPCClientError as T, transformResult as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/client",
3
- "version": "10.12.0",
3
+ "version": "10.13.0",
4
4
  "description": "tRPC Client lib",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -69,22 +69,24 @@
69
69
  "links"
70
70
  ],
71
71
  "peerDependencies": {
72
- "@trpc/server": "10.12.0"
72
+ "@trpc/server": "10.13.0"
73
73
  },
74
74
  "devDependencies": {
75
- "@testing-library/dom": "^8.18.1",
76
- "@trpc/server": "10.12.0",
75
+ "@testing-library/dom": "^9.0.0",
76
+ "@trpc/server": "10.13.0",
77
77
  "@types/isomorphic-fetch": "^0.0.36",
78
78
  "@types/node": "^18.7.20",
79
79
  "eslint": "^8.30.0",
80
80
  "expect-type": "^0.15.0",
81
81
  "isomorphic-fetch": "^3.0.0",
82
+ "node-fetch": "^3.3.0",
82
83
  "rollup": "^2.79.1",
83
84
  "tsx": "^3.9.0",
84
- "undici": "^5.14.0"
85
+ "undici": "^5.14.0",
86
+ "vitest": "^0.28.5"
85
87
  },
86
88
  "publishConfig": {
87
89
  "access": "public"
88
90
  },
89
- "gitHead": "5f5d1f937dcae4addd850d320f3c0273559fd2ab"
91
+ "gitHead": "1214c2d1d239c6864a32c7df24651899551be8f8"
90
92
  }
package/src/getFetch.ts CHANGED
@@ -1,19 +1,21 @@
1
- import { getWindow } from './internals/fetchHelpers';
2
1
  import { FetchEsque, NativeFetchEsque } from './internals/types';
3
2
 
4
- export function getFetch(f?: FetchEsque | NativeFetchEsque): FetchEsque {
5
- if (f) {
6
- return f as FetchEsque;
3
+ export function getFetch(
4
+ customFetchImpl?: FetchEsque | NativeFetchEsque,
5
+ ): FetchEsque {
6
+ if (customFetchImpl) {
7
+ return customFetchImpl as FetchEsque;
7
8
  }
8
9
 
9
- const win = getWindow();
10
- const globalFetch = win.fetch;
11
- if (globalFetch) {
12
- return (
13
- typeof globalFetch.bind === 'function'
14
- ? globalFetch.bind(win)
15
- : globalFetch
16
- ) as FetchEsque;
10
+ if (typeof window !== 'undefined' && typeof window.fetch === 'function') {
11
+ return window.fetch.bind(window) as FetchEsque;
12
+ }
13
+
14
+ if (
15
+ typeof globalThis !== 'undefined' &&
16
+ typeof globalThis.fetch === 'function'
17
+ ) {
18
+ return globalThis.fetch.bind(globalThis) as FetchEsque;
17
19
  }
18
20
 
19
21
  throw new Error('No fetch implementation found');
@@ -0,0 +1,20 @@
1
+ import { Maybe } from '@trpc/server';
2
+ import { AbortControllerEsque } from './types';
3
+
4
+ export function getAbortController(
5
+ customAbortControllerImpl: Maybe<AbortControllerEsque>,
6
+ ): AbortControllerEsque | null {
7
+ if (customAbortControllerImpl) {
8
+ return customAbortControllerImpl;
9
+ }
10
+
11
+ if (typeof window !== 'undefined' && window.AbortController) {
12
+ return window.AbortController;
13
+ }
14
+
15
+ if (typeof globalThis !== 'undefined' && globalThis.AbortController) {
16
+ return globalThis.AbortController;
17
+ }
18
+
19
+ return null;
20
+ }
@@ -1,3 +1,3 @@
1
- /* istanbul ignore next */
1
+ /* istanbul ignore next -- @preserve */
2
2
  export const retryDelay = (attemptIndex: number) =>
3
3
  attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
@@ -5,7 +5,7 @@ import type { fetch as undiciFetch } from 'undici';
5
5
  import { createTRPCProxyClient } from '../createTRPCClientProxy';
6
6
  import { getFetch } from '../getFetch';
7
7
  import { httpBatchLink } from '../links/httpBatchLink';
8
- import { getAbortController } from './fetchHelpers';
8
+ import { getAbortController } from './getAbortController';
9
9
  import {
10
10
  AbortControllerEsque,
11
11
  AbortControllerInstanceEsque,
@@ -57,7 +57,7 @@ describe('fetch', () => {
57
57
 
58
58
  test('NativeFetchEsque', () => {
59
59
  getFetch(isomorphicFetch);
60
- getFetch(nodeFetch);
60
+ getFetch(nodeFetch as any);
61
61
 
62
62
  // Passing in undiciFetch directly in Node v18.7.0 gives:
63
63
  // ReferenceError: TextEncoder is not defined
@@ -6,8 +6,8 @@ import { dedupeLink } from './dedupeLink';
6
6
  import { createChain } from './internals/createChain';
7
7
 
8
8
  test('dedupeLink', async () => {
9
- const endingLinkTriggered = jest.fn();
10
- const timerTriggered = jest.fn();
9
+ const endingLinkTriggered = vi.fn();
10
+ const timerTriggered = vi.fn();
11
11
  const links: OperationLink<AnyRouter, any, any>[] = [
12
12
  // "dedupe link"
13
13
  dedupeLink()(null as any),
@@ -52,7 +52,7 @@ test('dedupeLink', async () => {
52
52
  context: {},
53
53
  },
54
54
  });
55
- const next = jest.fn();
55
+ const next = vi.fn();
56
56
  call1.subscribe({ next });
57
57
  call2.subscribe({ next });
58
58
 
@@ -66,8 +66,8 @@ test('dedupeLink', async () => {
66
66
  });
67
67
 
68
68
  test('dedupe - cancel one does not cancel the other', async () => {
69
- const endingLinkTriggered = jest.fn();
70
- const timerTriggered = jest.fn();
69
+ const endingLinkTriggered = vi.fn();
70
+ const timerTriggered = vi.fn();
71
71
  const links: OperationLink<AnyRouter, any, any>[] = [
72
72
  // "dedupe link"
73
73
  dedupeLink()(null as any),
@@ -113,7 +113,7 @@ test('dedupe - cancel one does not cancel the other', async () => {
113
113
  context: {},
114
114
  },
115
115
  });
116
- const next = jest.fn();
116
+ const next = vi.fn();
117
117
  const call1$ = call1.subscribe({ next });
118
118
  call2.subscribe({ next });
119
119
  call1$.unsubscribe();
@@ -1,4 +1,4 @@
1
- /* istanbul ignore file */
1
+ /* istanbul ignore file -- @preserve */
2
2
  // We're not actually exporting this link
3
3
  import { AnyRouter } from '@trpc/server';
4
4
  import { Observable, observable, share } from '@trpc/server/observable';
@@ -38,7 +38,7 @@ describe('chain', () => {
38
38
  },
39
39
  });
40
40
 
41
- const next = jest.fn();
41
+ const next = vi.fn();
42
42
 
43
43
  result$.subscribe({ next });
44
44
  // console.log(next.mock.calls);
@@ -85,7 +85,7 @@ describe('chain', () => {
85
85
  },
86
86
  });
87
87
 
88
- const next = jest.fn();
88
+ const next = vi.fn();
89
89
 
90
90
  result$.subscribe({ next });
91
91
 
@@ -1,7 +1,7 @@
1
1
  import { ProcedureType } from '@trpc/server';
2
2
  import { TRPCResponse } from '@trpc/server/rpc';
3
3
  import { getFetch } from '../../getFetch';
4
- import { getAbortController } from '../../internals/fetchHelpers';
4
+ import { getAbortController } from '../../internals/getAbortController';
5
5
  import {
6
6
  AbortControllerEsque,
7
7
  FetchEsque,
@@ -130,7 +130,7 @@ export function httpRequest(
130
130
  const meta = {} as HTTPResult['meta'];
131
131
  Promise.resolve(opts.headers())
132
132
  .then((headers) => {
133
- /* istanbul ignore if */
133
+ /* istanbul ignore if -- @preserve */
134
134
  if (type === 'subscription') {
135
135
  throw new Error('Subscriptions should use wsLink');
136
136
  }
@@ -1,4 +1,4 @@
1
- /* istanbul ignore file */
1
+ /* istanbul ignore file -- @preserve */
2
2
  // We're not actually exporting this link
3
3
  import { AnyRouter } from '@trpc/server';
4
4
  import { Unsubscribable, observable } from '@trpc/server/observable';
@@ -21,7 +21,7 @@ export function retryLink<TRouter extends AnyRouter = AnyRouter>(opts: {
21
21
  next$?.unsubscribe();
22
22
  next$ = next(op).subscribe({
23
23
  error(error) {
24
- /* istanbul ignore if */
24
+ /* istanbul ignore if -- @preserve */
25
25
  if (attempts >= opts.attempts) {
26
26
  observer.error(error);
27
27
  return;
@@ -5,12 +5,12 @@ import { AnyRouter } from '../../../server/src';
5
5
  import { createChain } from '../links/internals/createChain';
6
6
 
7
7
  test('splitLink', () => {
8
- const wsLinkSpy = jest.fn();
8
+ const wsLinkSpy = vi.fn();
9
9
  const wsLink: TRPCLink<any> = () => () =>
10
10
  observable(() => {
11
11
  wsLinkSpy();
12
12
  });
13
- const httpLinkSpy = jest.fn();
13
+ const httpLinkSpy = vi.fn();
14
14
  const httpLink: TRPCLink<any> = () => () =>
15
15
  observable(() => {
16
16
  httpLinkSpy();
@@ -38,7 +38,7 @@ test('splitLink', () => {
38
38
  }).subscribe({});
39
39
  expect(httpLinkSpy).toHaveBeenCalledTimes(1);
40
40
  expect(wsLinkSpy).toHaveBeenCalledTimes(0);
41
- jest.resetAllMocks();
41
+ vi.resetAllMocks();
42
42
 
43
43
  createChain({
44
44
  links,
@@ -38,7 +38,7 @@ export function createWSClient(opts: WebSocketClientOptions) {
38
38
  onOpen,
39
39
  onClose,
40
40
  } = opts;
41
- /* istanbul ignore next */
41
+ /* istanbul ignore next -- @preserve */
42
42
  if (!WebSocketImpl) {
43
43
  throw new Error(
44
44
  "No WebSocket implementation found - you probably don't want to use this on the server, but if you do you need to pass a `WebSocket`-ponyfill",
@@ -134,7 +134,7 @@ export function createWSClient(opts: WebSocketClientOptions) {
134
134
  connectTimer = null;
135
135
 
136
136
  conn.addEventListener('open', () => {
137
- /* istanbul ignore next */
137
+ /* istanbul ignore next -- @preserve */
138
138
  if (conn !== activeConnection) {
139
139
  return;
140
140
  }
@@ -1,5 +0,0 @@
1
- import { Maybe } from '@trpc/server';
2
- import { AbortControllerEsque } from './types';
3
- export declare function getWindow(): typeof globalThis;
4
- export declare function getAbortController(ac: Maybe<AbortControllerEsque>): AbortControllerEsque | null;
5
- //# sourceMappingURL=fetchHelpers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fetchHelpers.d.ts","sourceRoot":"","sources":["../../src/internals/fetchHelpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAE/C,wBAAgB,SAAS,sBAKxB;AACD,wBAAgB,kBAAkB,CAChC,EAAE,EAAE,KAAK,CAAC,oBAAoB,CAAC,GAC9B,oBAAoB,GAAG,IAAI,CAE7B"}
@@ -1,14 +0,0 @@
1
- import { Maybe } from '@trpc/server';
2
- import { AbortControllerEsque } from './types';
3
-
4
- export function getWindow() {
5
- if (typeof window !== 'undefined') {
6
- return window;
7
- }
8
- return globalThis;
9
- }
10
- export function getAbortController(
11
- ac: Maybe<AbortControllerEsque>,
12
- ): AbortControllerEsque | null {
13
- return ac ?? getWindow().AbortController ?? null;
14
- }