import {z} from 'zod' export type GetApiApiV3ShoppingFepMerchantsPenaltySummaryArgs = {partnerName?: string | undefined}; export type GetApiApiV3ShoppingFepMerchantsPenaltySummaryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const merchantPenaltySummaryFepResponse = z.object({score: z.number().int(), count: z.number().int(), isPenalized: z.boolean()}); export const getApiApiV3ShoppingFepMerchantsPenaltySummaryResponse = z.object({resultType: z.enum(["SUCCESS", "FAIL"]).optional(), error: z.object({errorCode: z.enum(["REQUEST_FAILED", "COMMON_ERROR"]).optional(), reason: z.string().optional()}).optional(), success: merchantPenaltySummaryFepResponse.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/merchants/penalty/summary */ export const getApiApiV3ShoppingFepMerchantsPenaltySummary = async (args: GetApiApiV3ShoppingFepMerchantsPenaltySummaryArgs, options?: GetApiApiV3ShoppingFepMerchantsPenaltySummaryOptions) => { 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.partnerName != null) query.append("partnerName", String(args.partnerName)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/shopping-fep/merchants/penalty/summary${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(getApiApiV3ShoppingFepMerchantsPenaltySummaryResponse, await res.json()) };