import {z} from 'zod' export type TemplateName = 'successful outbound fax' | 'unsuccessful outbound fax' | 'successful inbound fax' | 'unsuccessful inbound fax'; export type GetApiV3ProjectsProjectIdServicesServiceIdDomainsDomainNameTemplatesTemplateNameArgs = {serviceId: string, domainName: string, templateName: TemplateName}; export type GetApiV3ProjectsProjectIdServicesServiceIdDomainsDomainNameTemplatesTemplateNameOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const templateName = z.enum(["successful outbound fax", "unsuccessful outbound fax", "successful inbound fax", "unsuccessful inbound fax"]); export const templateByNameResponse = z.object({template: z.object({name: templateName.optional(), createdAt: z.string().optional(), id: z.string().optional(), version: z.object({tag: z.string().optional(), template: z.string().optional(), createdAt: z.string().optional(), comment: z.string().optional(), active: z.boolean().optional(), id: z.string().optional(), headers: z.object({}).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 }; /** * Get template * * GET /v3/projects/{projectId}/services/{serviceId}/domains/{domainName}/templates/{templateName} * * Also accepts auth: OAuth2 */ export const getApiV3ProjectsProjectIdServicesServiceIdDomainsDomainNameTemplatesTemplateName = async (args: GetApiV3ProjectsProjectIdServicesServiceIdDomainsDomainNameTemplatesTemplateNameArgs, options?: GetApiV3ProjectsProjectIdServicesServiceIdDomainsDomainNameTemplatesTemplateNameOptions) => { const {baseUrl = "https://fax.api.sinch.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/projects/${encodeURIComponent(String(args.projectId))}/services/${encodeURIComponent(String(args.serviceId))}/domains/${encodeURIComponent(String(args.domainName))}/templates/${encodeURIComponent(String(args.templateName))}${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(templateByNameResponse, await res.json()) };