import {z} from 'zod' export type GetApiV1DomainsIdArgs = {id?: (number | null) | undefined}; export type GetApiV1DomainsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1DomainsIdResponse = z.object({id: z.number(), name: z.string(), teamId: z.number(), status: z.enum(["NOT_STARTED", "PENDING", "SUCCESS", "FAILED", "TEMPORARY_FAILURE"]), region: z.string().optional(), clickTracking: z.boolean().optional(), openTracking: z.boolean().optional(), publicKey: z.string(), dkimStatus: z.string().nullable().optional(), spfDetails: z.string().nullable().optional(), createdAt: z.string(), updatedAt: z.string(), dmarcAdded: z.boolean().optional(), isVerifying: z.boolean().optional(), errorMessage: z.string().nullable().optional(), subdomain: z.string().nullable().optional(), verificationError: z.string().nullable().optional(), lastCheckedTime: z.string().nullable().optional(), dnsRecords: z.array(z.object({type: z.enum(["MX", "TXT"]), name: z.string(), value: z.string(), ttl: z.string(), priority: z.string().nullable().optional(), status: z.enum(["NOT_STARTED", "PENDING", "SUCCESS", "FAILED", "TEMPORARY_FAILURE"]), recommended: z.boolean().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 /v1/domains/{id} */ export const getApiV1DomainsId = async (args: GetApiV1DomainsIdArgs, options?: GetApiV1DomainsIdOptions) => { const {baseUrl = "https://app.usesend.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/domains/${encodeURIComponent(String(args.id))}${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(getApiV1DomainsIdResponse, await res.json()) };