All files / src/libs orpcClient.ts

97.29% Statements 36/37
91.66% Branches 11/12
100% Functions 4/4
97.29% Lines 36/37

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 1181x 1x 1x 1x                                       1x                                                                                                                     1x 5x 5x 3x 3x 1x 1x   1x 5x 5x 5x 5x 5x 5x   5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 5x 5x 5x   5x 5x  
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,
  DailyCommentCounts,
} from '@/types/meal';
import { StatisticsResponse } from '@/types/statistic';
import {
  CommentListResponse,
  CreateFeedbackRequest,
  FeedbackResponse,
  SetReadStatusResponse,
  UpdateFeedbackRequest,
} from '@/types/menuFeedback';
 
// 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 }>;
    comments: {
      list: (input: {
        fromDate: string;
        toDate: string;
        limit?: number;
        sentiment?: string;
        isRead?: boolean;
        cursor?: string;
      }) => Promise<CommentListResponse>;
      create: (
        input: CreateFeedbackRequest,
      ) => Promise<{ data: FeedbackResponse }>;
      get: (input: {
        lunchMenuId: string;
      }) => Promise<{ data: FeedbackResponse }>;
      update: (
        input: UpdateFeedbackRequest,
      ) => Promise<{ data: FeedbackResponse }>;
      delete: (input: { lunchMenuId: string }) => Promise<{ success: true }>;
      setReadStatus: (input: {
        lunchMenuId: string;
        commentId: string;
        isRead: boolean;
      }) => Promise<{ data: SetReadStatusResponse }>;
      summaryByDay: (input: { fromDate: string; toDate: string }) => Promise<{
        data: DailyCommentCounts[];
      }>;
    };
  };
  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 type CreateOrpcClientOptions = {
  /** When false, skips BatchLinkPlugin (use for comment get/create/update/delete). */
  batch?: boolean;
};
 
/** Server-side calls use ENV.API_ENDPOINT; browser calls use same-origin /rpc proxy. */
const resolveRpcBase = (currentOrigin: string) => {
  if (currentOrigin) return currentOrigin;
  if (typeof window !== 'undefined' && window.location?.origin) {
    return window.location.origin;
  }
  return ENV.API_ENDPOINT;
};
 
export const createOrpcClient = (
  token?: string,
  currentOrigin: string = '',
  options: CreateOrpcClientOptions = {},
) => {
  const { batch = true } = options;
  const rpcBase = resolveRpcBase(currentOrigin);
 
  const link = new RPCLink({
    url: `${rpcBase}/rpc`,
    headers: () => (token ? { Authorization: `Bearer ${token}` } : {}),
    plugins: [
      ...(batch
        ? [
            new BatchLinkPlugin({
              groups: [{ condition: () => true, context: {} }],
            }),
          ]
        : []),
      new DurableEventIteratorLinkPlugin({
        url: `${rpcBase}/ws`,
      }),
    ],
  });
 
  return createORPCClient(link) as unknown as ORPCClient;
};