import {z} from 'zod' export type CreateApiV1LeaksExportArgs = {format?: ('json' | 'csv') | undefined, 'X-Team-Id'?: string | undefined, body: {type?: ('all' | 'personal' | 'employee' | 'customer' | 'external_vendor_leaks' | 'organization_leaks') | undefined, domain?: string | undefined, email?: string | undefined, search?: string | undefined, limit?: number | undefined, page_number?: number | undefined, start_date?: string | undefined, time_range?: ('all_time' | 'current_month' | 'last_month' | 'last_3_months' | 'last_6_months' | 'last_12_months') | undefined, end_date?: string | undefined, sort_by?: ('url' | 'username' | 'log_date' | 'country' | 'device_ip' | 'host_username' | 'hostname' | 'os' | 'hardware_id' | 'malware_path') | undefined, sort_order?: ('asc' | 'desc') | undefined, status?: ('fixed' | 'open') | undefined, group_by?: ('url' | 'country' | 'device_ip' | 'hostname' | 'email' | 'hardware_id') | undefined, url?: string | undefined, country?: string | undefined, device_ip?: string | undefined, hostname?: string | undefined, hardware_id?: string | undefined}}; export type CreateApiV1LeaksExportOptions = {baseUrl?: string | undefined, XAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1LeaksExportResponse = z.object({data: z.array(z.object({id: z.string().optional(), url: z.string().optional(), username: z.string().optional(), password: z.string().optional(), device_ip: z.string().optional(), hostname: z.string().optional(), os: z.string().optional(), malware_path: z.string().optional(), country: z.string().optional(), log_date: z.string().optional(), hardware_id: z.string().optional(), domain: z.string().optional(), email_domain: z.string().optional(), url_domain: z.string().optional(), fetched_at: z.string().optional(), status: z.string().optional(), user_type: z.string().optional()})).optional(), total_leaks: z.number().optional(), total_pages: z.number().optional(), total_count: z.number().optional(), summary: z.object({total_leaks: z.number().optional(), personal_leaks: z.number().optional(), employee_leaks: z.number().optional(), customer_leaks: z.number().optional(), external_vendor_leaks: z.number().optional(), organization_leaks: z.number().optional()}).optional(), group_summary: z.array(z.record(z.string(), z.unknown())).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 }; /** * Export leaked credentials * * POST /v1/leaks/export */ export const createApiV1LeaksExport = async (args: CreateApiV1LeaksExportArgs, options?: CreateApiV1LeaksExportOptions) => { const {baseUrl = "https://api.projectdiscovery.io", fetch: fetchFn = fetch, XAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.format != null) query.append("format", String(args.format)) if (args['X-Team-Id'] != null) headers.set("X-Team-Id", String(args['X-Team-Id'])) if (XAPIKey !== undefined) headers.set("X-API-Key", XAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/leaks/export${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1LeaksExportResponse, await res.json()) };