import {z} from 'zod' export type GetApiV1DomainsArgs = {page?: number | undefined}; export type GetApiV1DomainsOptions = {baseUrl: string, ApiKey?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const error = z.object({code: z.string().nullable().optional(), name: z.string().nullable().optional(), message: z.string().nullable().optional(), description: z.string().nullable().optional()}); export const networkType = z.enum(["NotSet", "Information", "Sports", "Entertainment", "Lifestyle", "News", "Business", "Shopping", "Miscellaneous", "General"]); export const domainResponse = z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional(), domain: z.string().nullable().optional(), subDomains: z.array(z.string()).nullable().optional(), maxHeight: z.number().int().nullable().optional(), maxWidth: z.number().int().nullable().optional(), isSquare: z.boolean().optional(), logoUrl: z.string().nullable().optional(), networkType: networkType.optional()}); export const domainResponsePagedResponse = z.object({items: z.array(domainResponse).nullable().optional(), total: z.number().int().optional(), page: z.number().int().optional(), pageSize: z.number().int().optional(), totalPages: z.number().int().optional()}); export const domainResponsePagedResponseResponse = z.object({timestamp: z.string().optional(), errors: z.array(error).nullable().optional(), data: domainResponsePagedResponse.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 */ export const getApiV1Domains = async (args: GetApiV1DomainsArgs, options: GetApiV1DomainsOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (ApiKey !== undefined) headers.set("X-Api-Key", ApiKey) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/domains${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(domainResponsePagedResponseResponse, await res.json()) };