import {z} from 'zod' export type PaginationState = {pageIndex?: number | undefined, pageSize?: number | undefined}; export type SortingState = {id: string, desc?: boolean | undefined}; export type ColumnFilterState = {id: string, value: unknown}; export type TableRequest = {pagination?: PaginationState | undefined, sorting?: Array | undefined, columnFilters?: Array | undefined, globalFilter?: (string | null) | undefined}; export type CreateApiProfilesSearchArgs = {body: TableRequest}; export type CreateApiProfilesSearchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const proxyConnectionType = z.enum(["none", "http", "socks5", "ssh"]); export const server = z.object({host: z.string(), port: z.number().int(), id: z.string().nullable().optional(), secret: z.string().nullable().optional()}); export const proxyChoice = z.object({value: proxyConnectionType, extra: server.nullable().optional()}); 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 profileLifetimeState = z.enum(["created", "starting", "running", "terminating", "terminated", "locked", "loading", "unknown"]); export const statusResponse = z.object({lifetimeState: profileLifetimeState}); export const profileStorageLocation = z.enum(["local", "cloud"]); export const profilePreview = z.object({id: z.string(), name: z.string(), tags: z.array(z.string()), proxy: proxyChoice, createdAt: z.string(), device: device, os: os, browser: browser, language: z.string(), status: statusResponse, storage: profileStorageLocation.optional(), folderId: z.string().nullable().optional()}); export const profilePreviewPagedResponse = z.object({items: z.array(profilePreview), totalCount: z.number().int(), pageCount: z.number().int(), pageIndex: z.number().int(), pageSize: z.number().int()}); 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 }; /** * Retrieve a page of profile previews with server-side filtering and sorting to optimize querying profiles. * Only the properties of ProfilePreview can be filtered and sorted. * * POST /profiles/search */ export const createApiProfilesSearch = async (args: CreateApiProfilesSearchArgs, options?: CreateApiProfilesSearchOptions) => { const {baseUrl = "http://localhost:5050", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/profiles/search${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(profilePreviewPagedResponse, await res.json()) };