import {z} from 'zod' export type TemplateWebLinkButtonDto = {name: string, ordering?: number | undefined, type: 'WL', urlMobile?: string | undefined, urlPc?: string | undefined}; export type TemplateAppLinkButtonDto = {name: string, ordering?: number | undefined, type: 'AL', urlMobile?: string | undefined, urlPc?: string | undefined, schemeAndroid?: string | undefined, schemeIos?: string | undefined}; export type TemplateBasicButtonDto = {name: string, ordering?: number | undefined, type: 'BK' | 'MD' | 'DS' | 'AC'}; export type UpdateTemplateRequestDto = {templateName?: string | undefined, templateContent?: string | undefined, templateEmphasizeType?: ('NONE' | 'TEXT' | 'IMAGE' | 'ITEM_LIST') | undefined, templateExtra?: string | undefined, templateTitle?: string | undefined, templateSubtitle?: string | undefined, securityFlag?: boolean | undefined, templateImageName?: string | undefined, templateImageUrl?: string | undefined, buttons?: (Array) | undefined}; export type CreateApiV2MessagesKakaoSendProfilesSendProfileIdTemplatesTemplateIdUpdateArgs = {sendProfileId: string, templateId: string, body: UpdateTemplateRequestDto}; export type CreateApiV2MessagesKakaoSendProfilesSendProfileIdTemplatesTemplateIdUpdateOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const kakaoActionResponseDto = z.object({code: z.number(), message: z.string()}); 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 }; /** * 템플릿 수정 * * POST /v2/messages/kakao/send-profiles/{sendProfileId}/templates/{templateId}/update */ export const createApiV2MessagesKakaoSendProfilesSendProfileIdTemplatesTemplateIdUpdate = async (args: CreateApiV2MessagesKakaoSendProfilesSendProfileIdTemplatesTemplateIdUpdateArgs, options?: CreateApiV2MessagesKakaoSendProfilesSendProfileIdTemplatesTemplateIdUpdateOptions) => { const {baseUrl = "https://api.sendon.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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))}/update${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(kakaoActionResponseDto, await res.json()) };