bbuck
bbuck5mo ago

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;
};
/**
* 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.
No description
1 Reply
Alex / KATT 🐱
Can't see anything obvious, try adding a loggerlink before and/or after to verify For references you can search our source code for loggerLink, dedupeLink, retryLink