import {z} from 'zod' export type GetApiApiTemplatesArgs = {companyId?: string | undefined, templateType?: ('sms' | 'email' | 'form' | 'eSignature' | 'pdf' | 'blockDocument') | undefined, aptletUuid?: string | undefined, archived?: boolean | undefined}; export type GetApiApiTemplatesOptions = {baseUrl?: string | undefined, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const template = z.object({_id: z.string().optional(), companyId: z.string().optional(), name: z.string().optional(), templateType: z.enum(["sms", "email", "form", "eSignature", "pdf", "blockDocument"]).optional(), archived: z.boolean().optional(), createdAt: z.string().optional(), createdBy: z.string().optional(), updatedAt: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const getApiApiTemplatesResponse = z.object({data: z.array(template).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 }; /** * List templates * * GET /api/templates * * Also accepts auth: DelegateToken, PartnerBearer */ export const getApiApiTemplates = async (args: GetApiApiTemplatesArgs, options?: GetApiApiTemplatesOptions) => { const {baseUrl = "https://core-api.getaptly.com", fetch: fetchFn = fetch, ApiKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.companyId != null) query.append("companyId", String(args.companyId)) if (args.templateType != null) query.append("templateType", String(args.templateType)) if (args.aptletUuid != null) query.append("aptletUuid", String(args.aptletUuid)) if (args.archived != null) query.append("archived", String(args.archived)) if (ApiKeyHeader !== undefined) headers.set("x-token", ApiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/templates${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(getApiApiTemplatesResponse, await res.json()) };