All files / src/libs orpcClient.ts

100% Statements 21/21
100% Branches 7/7
100% Functions 3/3
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 581x 1x 1x 1x                       1x                                                 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x  
import { createORPCClient } from '@orpc/client';
import { RPCLink } from '@orpc/client/fetch';
import { BatchLinkPlugin } from '@orpc/client/plugins';
import { DurableEventIteratorLinkPlugin } from '@orpc/experimental-durable-event-iterator/client';
 
// Types
import {
  Meal,
  LunchPlansResponse,
  SubscribeEvent,
  MealPlansResponse,
} from '@/types/meal';
import { StatisticsResponse } from '@/types/statistic';
 
// Constants
import { ENV } from '@/constants/env';
 
export type ORPCClient = {
  lunchMenus: {
    list: (input: {}) => Promise<{ data: Meal[] }>;
    like: (input: { lunchMenuId: string }) => Promise<{ success: true }>;
    dislike: (input: { lunchMenuId: string }) => Promise<{ success: true }>;
  };
  lunchPlans: {
    list: (input: {}) => Promise<{ data: LunchPlansResponse[] }>;
  };
  lunchStats: {
    list: (input: {}) => Promise<{ data: StatisticsResponse[] }>;
  };
  mealPlans: {
    list: (input: {}) => Promise<{ data: MealPlansResponse[] }>;
  };
  socket: {
    subscribeLunchMenu: (
      input?: { lunchMenuId: string },
      options?: { signal?: AbortSignal },
    ) => AsyncGenerator<SubscribeEvent>;
  };
};
 
export const createOrpcClient = (
  token?: string,
  currentOrigin: string = '',
) => {
  const link = new RPCLink({
    url: `${currentOrigin || ENV.API_ENDPOINT}/rpc`,
    headers: () => (token ? { Authorization: `Bearer ${token}` } : {}),
    plugins: [
      new BatchLinkPlugin({ groups: [{ condition: () => true, context: {} }] }),
      new DurableEventIteratorLinkPlugin({
        url: `${currentOrigin || ENV.API_ENDPOINT}/ws`,
      }),
    ],
  });
 
  return createORPCClient(link) as unknown as ORPCClient;
};