# Conflicts: # app/game-api/src/server.ts # app/game-frontend/e2e/mainNavigation.spec.ts # app/game-frontend/src/utils/trpc.ts
30 lines
1.1 KiB
TypeScript
30 lines
1.1 KiB
TypeScript
import { REALTIME_ACCESS_GRANT_HEADER, trpcJsonBodyHttpClientOptions } from '@sammo-ts/common';
|
|
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
|
|
import type { AppRouter } from '@sammo-ts/game-api';
|
|
import { resolveBatchRealtimeAccessGrant } from './realtimeAccessGrant';
|
|
|
|
const getGameToken = (): string | null => {
|
|
if (typeof window === 'undefined') {
|
|
return null;
|
|
}
|
|
|
|
return window.localStorage.getItem('sammo-game-token');
|
|
};
|
|
|
|
export const trpc = createTRPCProxyClient<AppRouter>({
|
|
links: [
|
|
httpBatchLink({
|
|
url: import.meta.env.VITE_GAME_API_URL ?? '/api/trpc',
|
|
...trpcJsonBodyHttpClientOptions,
|
|
headers({ opList }) {
|
|
const token = getGameToken();
|
|
const refreshGrant = resolveBatchRealtimeAccessGrant(opList);
|
|
return {
|
|
...(token ? { authorization: `Bearer ${token}` } : {}),
|
|
...(refreshGrant ? { [REALTIME_ACCESS_GRANT_HEADER]: refreshGrant } : {}),
|
|
};
|
|
},
|
|
}),
|
|
],
|
|
});
|