kuunkoK
tRPC2y ago
2 replies
kuunko

tRPC mutate call firing two requests with custom link

Node: 18.16
tRPC: 10.38.0
pnpm

What's wrong with this custom link that's causing it's addition to a tRPC client to make two requests?

/**
 * Checks tRPC errors, if UNAUTHORIZED it notifies the front-end the user's
 * token is no longer valid.
 */
export const unauthorizedLink =
  <TRouter extends AnyRouter>(): TRPCLink<TRouter> =>
  () =>
  ({ op, next }) => {
    const observable = next(op);
    observable.subscribe({
      error: (error) => {
        if (error.shape?.data.httpStatus === StatusCodes.UNAUTHORIZED) {
          useEnduvoToken.getState().clearToken();
        }
      },
    });

    return observable;
  };


There isn't a lot of documentation about custom links.
image.png
Was this page helpful?