import {z} from 'zod' export type GetApiV2MessagesKakaoSendProfilesSendProfileIdTemplatesTemplateIdArgs = {sendProfileId: string, templateId: string}; export type GetApiV2MessagesKakaoSendProfilesSendProfileIdTemplatesTemplateIdOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const templateWebLinkButtonDto = z.object({name: z.string(), ordering: z.number().optional(), type: z.literal("WL"), urlMobile: z.string().optional(), urlPc: z.string().optional()}); export const templateAppLinkButtonDto = z.object({name: z.string(), ordering: z.number().optional(), type: z.literal("AL"), urlMobile: z.string().optional(), urlPc: z.string().optional(), schemeAndroid: z.string().optional(), schemeIos: z.string().optional()}); export const templateBasicButtonDto = z.object({name: z.string(), ordering: z.number().optional(), type: z.enum(["BK", "MD", "DS", "AC"])}); export const templateFallbackDto = z.object({senderNumber: z.string(), type: z.enum(["SMS", "LMS", "MMS"]), message: z.string().nullable(), title: z.string().nullable(), images: z.array(z.string()).nullable(), isAd: z.boolean().optional(), groupId: z.string().nullable()}); export const templateCommentDto = z.object({commentContent: z.string(), commentCreateAt: z.string(), commentSeqno: z.number(), commentStatus: z.string(), commentUserName: z.string(), regBy: z.string(), regDate: z.string().nullable(), updateBy: z.string().nullable(), updateDate: z.string().nullable().optional()}); export const templateDetailDto = z.object({profileId: z.string(), id: z.string(), templateName: z.string(), status: z.enum(["NEED_REVIEW", "IN_REVIEW", "APPROVED", "REJECTED", "DORMANT", "UNKNOWN", "DELETED"]), templateMessageType: z.enum(["BA", "EX", "AD", "MI"]), templateEmphasizeType: z.enum(["NONE", "TEXT", "IMAGE", "ITEM_LIST"]), templateContent: z.string(), buttons: z.array(z.discriminatedUnion("type", [templateWebLinkButtonDto, templateAppLinkButtonDto, templateBasicButtonDto])).optional(), fallback: templateFallbackDto.nullable(), securityFlag: z.boolean(), templateComments: z.array(templateCommentDto).nullable(), templateTitle: z.string().nullable(), templateSubtitle: z.string().nullable(), templateImageName: z.string().nullable(), templateImageUrl: z.string().nullable(), templateExtra: z.string().nullable(), createdAt: z.string().nullable(), updatedAt: z.string().nullable(), syncedAt: z.string().nullable(), lastUsedAt: z.string().nullable()}); export const getTemplateResponseDto = z.object({code: z.number(), message: z.string(), data: templateDetailDto}); 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 /v2/messages/kakao/send-profiles/{sendProfileId}/templates/{templateId} */ export const getApiV2MessagesKakaoSendProfilesSendProfileIdTemplatesTemplateId = async (args: GetApiV2MessagesKakaoSendProfilesSendProfileIdTemplatesTemplateIdArgs, options?: GetApiV2MessagesKakaoSendProfilesSendProfileIdTemplatesTemplateIdOptions) => { const {baseUrl = "https://api.sendon.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/messages/kakao/send-profiles/${encodeURIComponent(String(args.sendProfileId))}/templates/${encodeURIComponent(String(args.templateId))}${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(getTemplateResponseDto, await res.json()) };