import {z} from 'zod' export type GetApiV1DistributionDomainsArgs = {page?: number | undefined, per_page?: number | undefined, names?: (Array | null) | undefined, is_tollbit?: (boolean | null) | undefined, publisher?: (boolean | null) | undefined, full_text?: (boolean | null) | undefined}; export type GetApiV1DistributionDomainsOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const readDomainResponse = z.object({id: z.string(), name: z.string(), owner: z.string().nullable().optional(), is_tollbit: z.boolean().optional(), publisher: z.boolean().optional(), full_text: z.boolean().optional(), premium_multiplier: z.number().int().optional(), created_at: z.string(), updated_at: z.string().nullable().optional()}); export const paginatedResponseReadDomainResponse = z.object({items: z.array(readDomainResponse), count: z.number().int(), next_page: z.number().int().nullable(), previous_page: z.number().int().nullable()}); 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 }; /** * Find domains * * GET /v1/distribution/domains */ export const getApiV1DistributionDomains = async (args: GetApiV1DistributionDomainsArgs, options?: GetApiV1DistributionDomainsOptions) => { const {baseUrl = "https://api.asknews.app", fetch: fetchFn = fetch, APIKey} = 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 (args.per_page != null) query.append("per_page", String(args.per_page)) for (const item of args.names ?? []) query.append("names", String(item)) if (args.is_tollbit != null) query.append("is_tollbit", String(args.is_tollbit)) if (args.publisher != null) query.append("publisher", String(args.publisher)) if (args.full_text != null) query.append("full_text", String(args.full_text)) if (APIKey !== undefined) headers.set("Authorization", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/distribution/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(paginatedResponseReadDomainResponse, await res.json()) };