import {z} from 'zod' export type GetApiLookalikeDomainsArgs = {_filter?: string | undefined, _fields?: string | undefined, _offset?: number | undefined, _limit?: number | undefined, _page_token?: string | undefined, _order_by?: string | undefined}; export type GetApiLookalikeDomainsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const serviceLookalikeDomainDetection = z.object({detected_at: z.string().optional(), lookalike_domain: z.string().optional(), lookalike_host: z.string().optional(), reason: z.string().optional(), suspicious: z.boolean().optional(), target_domain: z.string().optional()}); export const serviceLookalikeDomainReadMultiResponse = z.object({results: z.array(serviceLookalikeDomainDetection).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 }; /** * Lookalike Domain Lists. * * GET /lookalike_domains */ export const getApiLookalikeDomains = async (args: GetApiLookalikeDomainsArgs, options: GetApiLookalikeDomainsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args._filter != null) query.append("_filter", String(args._filter)) if (args._fields != null) query.append("_fields", String(args._fields)) if (args._offset != null) query.append("_offset", String(args._offset)) if (args._limit != null) query.append("_limit", String(args._limit)) if (args._page_token != null) query.append("_page_token", String(args._page_token)) if (args._order_by != null) query.append("_order_by", String(args._order_by)) const url = `${baseUrl.replace(/\/+$/, '')}/lookalike_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(serviceLookalikeDomainReadMultiResponse, await res.json()) };