import {z} from 'zod' export type StatefulSessionRequestRulesetSchema = {network_type?: ('wifi' | 'mobile' | 'any' | null) | undefined, country?: (string | null) | undefined, region?: (string | null) | undefined, city?: (string | null) | undefined, isp?: (string | null) | undefined, asn?: (string | null) | undefined, zip?: (string | null) | undefined, lookalike?: (boolean | null) | undefined, rotation_trigger?: (string | null) | undefined, error_handling?: (string | null) | undefined}; export type StatefulSessionCreateRequest = {name?: (string | null) | undefined, ruleset?: StatefulSessionRequestRulesetSchema | undefined, count?: number | undefined}; export type CreateApiV1ProxyPackagesPackageIdStatefulSessionsArgs = {package_id: string, body: StatefulSessionCreateRequest}; export type CreateApiV1ProxyPackagesPackageIdStatefulSessionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const statefulSessionStatus = z.enum(["enabled", "disabled"]); export const statefulSessionRulesetSchema = z.object({network_type: z.enum(["wifi", "mobile", "any"]).nullable().optional(), country: z.string().nullable().optional(), region: z.string().nullable().optional(), city: z.string().nullable().optional(), isp: z.string().nullable().optional(), asn: z.string().nullable().optional(), zip: z.string().nullable().optional(), lookalike: z.boolean().nullable().optional(), rotation_trigger: z.string().nullable().optional(), error_handling: z.string().nullable().optional()}); export const connectionStringSchema = z.object({uri: z.string(), username: z.string(), password: z.string().nullable().optional(), host: z.string(), port: z.number().int()}); export const statefulSessionResponse = z.object({id: z.string(), proxy_id: z.string(), name: z.string(), status: statefulSessionStatus, ruleset: statefulSessionRulesetSchema, exit_ip: z.string(), network_type: z.string(), country: z.string(), region: z.string().nullable().optional(), city: z.string().nullable().optional(), isp: z.string().nullable().optional(), asn: z.string().nullable().optional(), zip: z.string().nullable().optional(), bound_since: z.string().nullable().optional(), port: z.number().int().nullable().optional(), created_at: z.string(), updated_at: z.string(), connection_string: connectionStringSchema.nullable().optional()}); export const statefulSessionCreateResponse = z.object({data: z.array(statefulSessionResponse), failed: z.number().int(), reason: z.string().nullable().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 stateful sessions * * POST /v1/proxy/packages/{package_id}/stateful-sessions */ export const createApiV1ProxyPackagesPackageIdStatefulSessions = async (args: CreateApiV1ProxyPackagesPackageIdStatefulSessionsArgs, options?: CreateApiV1ProxyPackagesPackageIdStatefulSessionsOptions) => { const {baseUrl = "https://api.platform.soax.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/proxy/packages/${encodeURIComponent(String(args.package_id))}/stateful-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(statefulSessionCreateResponse, await res.json()) };