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 StatefulSessionStatus = 'enabled' | 'disabled'; export type StatefulSessionUpdateRequest = {name?: (string | null) | undefined, ruleset?: (StatefulSessionRequestRulesetSchema | null) | undefined, status?: (StatefulSessionStatus | null) | undefined}; export type PatchApiV1ProxyPackagesPackageIdStatefulSessionsSessionIdArgs = {package_id: string, session_id: string, body: StatefulSessionUpdateRequest}; export type PatchApiV1ProxyPackagesPackageIdStatefulSessionsSessionIdOptions = {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 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 }; /** * Update a stateful session * * PATCH /v1/proxy/packages/{package_id}/stateful-sessions/{session_id} */ export const patchApiV1ProxyPackagesPackageIdStatefulSessionsSessionId = async (args: PatchApiV1ProxyPackagesPackageIdStatefulSessionsSessionIdArgs, options?: PatchApiV1ProxyPackagesPackageIdStatefulSessionsSessionIdOptions) => { 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/${encodeURIComponent(String(args.session_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(statefulSessionResponse, await res.json()) };