import {z} from 'zod' export type GetApiApiV3ShoppingFepBusinessChatRoomsArgs = {filter?: ('ALL' | 'UNREAD' | 'UNANSWERED') | undefined, page?: number | undefined, size?: number | undefined, partnerName?: string | undefined}; export type GetApiApiV3ShoppingFepBusinessChatRoomsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const receiverFepResponse = z.object({type: z.string(), tossUserNo: z.number().int(), name: z.string(), profileImageUrl: z.string().optional()}); export const lastMessageFepResponse = z.object({messageId: z.number().int(), content: z.string(), senderType: z.string(), sentAt: z.string(), isRead: z.boolean()}); export const renderInfoFepResponse = z.object({thumbnail: z.string().optional(), link: z.string().optional(), title: z.string().optional(), subtitle: z.string().optional(), badge: z.string().optional()}); export const startCardFepResponse = z.object({chatEntryRefId: z.number().int().optional(), inquiryType: z.enum(["PRODUCT", "DELIVERY", "EXCHANGE", "RETURN", "CANCEL", "OTHER"]).optional(), entryRefType: z.enum(["PRODUCT", "ORDER_PRODUCT"]).optional(), entryRefIds: z.record(z.string(), z.object({})).optional(), renderInfo: renderInfoFepResponse.optional()}); export const sessionFepResponse = z.object({sessionId: z.number().int(), status: z.string(), startedAt: z.string(), endedAt: z.string().optional(), startCard: startCardFepResponse.optional()}); export const businessChatRoomSummaryFepResponse = z.object({roomId: z.number().int(), receiver: receiverFepResponse, lastMessage: lastMessageFepResponse.optional(), unreadCount: z.number().int(), sessionStatus: z.string(), lastSession: sessionFepResponse}); export const businessChatRoomsFepResponse = z.object({rooms: z.array(businessChatRoomSummaryFepResponse), hasNext: z.boolean()}); export const getApiApiV3ShoppingFepBusinessChatRoomsResponse = z.object({resultType: z.enum(["SUCCESS", "FAIL"]).optional(), error: z.object({errorCode: z.enum(["INVALID_REQUEST", "REQUEST_FAILED", "COMMON_ERROR"]).optional(), reason: z.string().optional()}).optional(), success: businessChatRoomsFepResponse.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * 채팅방 목록 조회 * * GET /api/v3/shopping-fep/business-chat/rooms */ export const getApiApiV3ShoppingFepBusinessChatRooms = async (args: GetApiApiV3ShoppingFepBusinessChatRoomsArgs, options?: GetApiApiV3ShoppingFepBusinessChatRoomsOptions) => { const {baseUrl = "https://oauth2.cert.toss.im", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter != null) query.append("filter", String(args.filter)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.partnerName != null) query.append("partnerName", String(args.partnerName)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/shopping-fep/business-chat/rooms${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiV3ShoppingFepBusinessChatRoomsResponse, await res.json()) };