import {z} from 'zod' export type GetApiV1TemplateTemplateSlugChannelChannelSlugArgs = {template_slug: string, channel_slug: string}; export type GetApiV1TemplateTemplateSlugChannelChannelSlugOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1TemplateTemplateSlugChannelChannelSlugResponse = z.object({id: z.number().int().optional(), channel: z.object({name: z.string().optional(), slug: z.string().optional(), is_template_approval_needed: z.boolean().optional()}).optional(), is_active: z.boolean().optional(), is_enabled: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), disabled_languages: z.array(z.string()).optional(), versions: z.array(z.object({id: z.number().int().optional(), templates: z.array(z.object({id: z.number().int().optional(), language: z.object({name: z.string().optional(), slug: z.string().optional()}).optional(), is_enabled: z.boolean().optional(), approval_status: z.string().optional(), content: z.object({}).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), updated_by: z.object({name: z.string().optional(), email: z.string().optional()}).optional(), approval_cycle: z.string().nullable().optional(), is_approval_needed: z.boolean().optional(), is_cloned_from_last_version: z.boolean().optional()})).optional(), status: z.string().optional(), version_tag: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), updated_by: z.object({}).optional(), version_tag_user: z.string().optional(), published_languages: z.array(z.string()).optional(), apparent_published_languages: z.array(z.string()).optional(), system_approval_info: z.object({}).optional()})).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 }; /** * Fetch Template content for a channel * * GET /v1/template/{template_slug}/channel/{channel_slug}/ */ export const getApiV1TemplateTemplateSlugChannelChannelSlug = async (args: GetApiV1TemplateTemplateSlugChannelChannelSlugArgs, options?: GetApiV1TemplateTemplateSlugChannelChannelSlugOptions) => { const {baseUrl = "https://hub.suprsend.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/template/${encodeURIComponent(String(args.template_slug))}/channel/${encodeURIComponent(String(args.channel_slug))}/${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(getApiV1TemplateTemplateSlugChannelChannelSlugResponse, await res.json()) };