import {z} from 'zod' export type GetApiV1TemplateArgs = {has_tags_any?: string | undefined, has_channels_any?: string | undefined, is_active?: boolean | undefined, is_archived?: boolean | undefined}; export type GetApiV1TemplateOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1TemplateResponse = z.object({meta: z.object({count: z.number().int().optional(), limit: z.number().int().optional(), offset: z.number().int().optional()}).optional(), results: z.array(z.object({id: z.number().int().optional(), name: z.string().optional(), slug: z.string().optional(), description: z.string().nullable().optional(), is_active: z.boolean().optional(), default_language: z.object({name: z.string().optional(), slug: z.string().optional()}).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), updated_by: z.object({name: z.string().optional(), email: z.string().optional()}).optional(), last_triggered_at: z.string().optional(), is_auto_translate_enabled: z.boolean().optional(), enabled_languages: z.array(z.string()).optional(), channels: z.array(z.object({})).optional(), tags: z.array(z.object({id: z.string().optional(), name: z.string().optional()})).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 List * * GET /v1/template/ */ export const getApiV1Template = async (args: GetApiV1TemplateArgs, options?: GetApiV1TemplateOptions) => { 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 (args.has_tags_any != null) query.append("has_tags_any", String(args.has_tags_any)) if (args.has_channels_any != null) query.append("has_channels_any", String(args.has_channels_any)) if (args.is_active != null) query.append("is_active", String(args.is_active)) if (args.is_archived != null) query.append("is_archived", String(args.is_archived)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/template/${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(getApiV1TemplateResponse, await res.json()) };