import {z} from 'zod' export type GetApiLookalikesArgs = {_filter?: string | undefined, _fields?: string | undefined, _offset?: number | undefined, _limit?: number | undefined, _page_token?: string | undefined, _order_by?: string | undefined}; export type GetApiLookalikesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const serviceLookalikeDomain = z.object({categories: z.array(z.string()).optional(), lookalike_domain: z.string().optional(), lookalike_host: z.string().optional(), malware: z.boolean().optional(), phishing: z.boolean().optional(), reason: z.string().optional(), registration_date: z.string().optional(), suspicious: z.boolean().optional(), target_domain: z.string().optional(), type: z.string().optional()}); export const serviceGetLookalikeDomainsResponse = z.object({results: z.array(serviceLookalikeDomain).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 /lookalikes */ export const getApiLookalikes = async (args: GetApiLookalikesArgs, options: GetApiLookalikesOptions) => { 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(/\/+$/, '')}/lookalikes${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(serviceGetLookalikeDomainsResponse, await res.json()) };