1import { filter, pipe, tap } from 'wonka';
2import type { ExchangeIO, ExchangeInput } from '../types';
3
4/** Used by the `Client` as the last exchange to warn about unhandled operations.
5 *
6 * @remarks
7 * In a normal setup, some operations may go unhandled when a {@link Client} isn’t set up
8 * with the right exchanges.
9 * For instance, a `Client` may be missing a fetch exchange, or an exchange handling subscriptions.
10 * This {@link Exchange} is added by the `Client` automatically to log warnings about unhandled
11 * {@link Operaiton | Operations} in development.
12 */
13export const fallbackExchange: ({
14 dispatchDebug,
15}: Pick<ExchangeInput, 'dispatchDebug'>) => ExchangeIO =
16 ({ dispatchDebug }) =>
17 ops$ => {
18 if (process.env.NODE_ENV !== 'production') {
19 ops$ = pipe(
20 ops$,
21 tap(operation => {
22 if (
23 operation.kind !== 'teardown' &&
24 process.env.NODE_ENV !== 'production'
25 ) {
26 const message = `No exchange has handled operations of kind "${operation.kind}". Check whether you've added an exchange responsible for these operations.`;
27
28 dispatchDebug({
29 type: 'fallbackCatch',
30 message,
31 operation,
32 });
33 console.warn(message);
34 }
35 })
36 );
37 }
38
39 // All operations that skipped through the entire exchange chain should be filtered from the output
40 return filter((_x): _x is never => false)(ops$);
41 };