import {z} from 'zod' export type AirtopProxyConfiguration = {country?: string | undefined, sticky?: boolean | undefined}; export type ProxyCredentials = {password?: string | undefined, url: string, username?: string | undefined}; export type Proxy = string | AirtopProxyConfiguration | ProxyCredentials; export type ProxyConfigurationKind = {domainPattern: string, relay: Proxy}; export type SessionConfigV1Proxy = boolean | Proxy | Array; export type SessionConfigV1 = {baseProfileId?: string | undefined, extensionIds?: Array | undefined, persistProfile?: boolean | undefined, profileName?: string | undefined, proxy?: SessionConfigV1Proxy | undefined, record?: boolean | undefined, solveCaptcha?: boolean | undefined, timeoutMinutes?: number | undefined}; export type CreateApiV1SessionsArgs = {body: {configuration?: SessionConfigV1 | undefined}}; export type CreateApiV1SessionsOptions = {baseUrl?: string | undefined, defaultValue?: string | undefined, fetch?: typeof fetch | undefined}; export const externalSessionConfig = z.object({baseProfileId: z.string().optional(), persistProfile: z.boolean().optional(), timeoutMinutes: z.number().int().gte(1).lte(1440).optional()}); export const externalSessionWithConnectionInfo = z.object({cdpUrl: z.string().optional(), cdpWsUrl: z.string().optional(), chromedriverUrl: z.string().optional(), configuration: externalSessionConfig, currentUsage: z.number().int().optional(), dateCreated: z.string().optional(), id: z.string(), lastActivity: z.string().optional(), profileId: z.string().optional(), status: z.string(), viceUrl: z.string().optional()}); export const issue = z.object({code: z.string().optional(), details: z.record(z.string(), z.unknown()).optional(), message: z.string(), reason: z.string().optional()}); export const envelopeDefaultMeta = z.object({requestId: z.string().optional()}); export const sessionResponse = z.object({data: externalSessionWithConnectionInfo, errors: z.array(issue).optional(), meta: envelopeDefaultMeta, warnings: z.array(issue).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 }; /** * Create a session * * POST /v1/sessions */ export const createApiV1Sessions = async (args: CreateApiV1SessionsArgs, options?: CreateApiV1SessionsOptions) => { const {baseUrl = "https://api.airtop.ai/api", fetch: fetchFn = fetch, defaultValue} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (defaultValue !== undefined) headers.set('authorization', `Bearer ${defaultValue}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/sessions${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(sessionResponse, await res.json()) };