import {z} from 'zod' export type GetApiFingerprintsArgs = {deviceType?: string | undefined, osFamily?: string | undefined, browserProduct?: string | undefined, browserVersion?: string | undefined}; export type GetApiFingerprintsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const device = z.object({type: z.string(), name: z.string().nullable().optional()}); export const os = z.object({family: z.string(), version: z.string(), platform: z.string()}); export const browser = z.object({product: z.string(), major: z.number().int(), version: z.string()}); export const webglMeta = z.object({vendor: z.string(), renderer: z.string().nullable().optional()}); export const fingerprintPreview = z.object({id: z.string(), device: device, os: os, browser: browser, webglMeta: webglMeta, userAgent: z.string(), fontsCount: z.number().int()}); export const getApiFingerprintsResponse = z.array(fingerprintPreview); 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 }; /** * Retrieves a set of 25 fingerprints based on specified criteria, with the most recent browser versions at the beginning of the list. Note * that calling this method multiple times will return different sets of fingerprints. * * GET /fingerprints */ export const getApiFingerprints = async (args: GetApiFingerprintsArgs, options?: GetApiFingerprintsOptions) => { const {baseUrl = "http://localhost:5050", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.deviceType != null) query.append("deviceType", String(args.deviceType)) if (args.osFamily != null) query.append("osFamily", String(args.osFamily)) if (args.browserProduct != null) query.append("browserProduct", String(args.browserProduct)) if (args.browserVersion != null) query.append("browserVersion", String(args.browserVersion)) const url = `${baseUrl.replace(/\/+$/, '')}/fingerprints${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(getApiFingerprintsResponse, await res.json()) };