import {z} from 'zod' export type GetApiV4AccountSubAccountIdDatacenterSharedWhitelistArgs = {subAccountId: string, type: 'get' | 'add' | 'remove' | 'set', 'ip[]'?: Array | undefined, labels?: string | undefined}; export type GetApiV4AccountSubAccountIdDatacenterSharedWhitelistOptions = {baseUrl?: string | undefined, apiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV4AccountSubAccountIdDatacenterSharedWhitelistResponse = z.union([z.object({status: z.string(), maxips: z.number().int(), whitelisted: z.array(z.string()), labels: z.record(z.string(), z.string()).optional()}), z.object({status: z.string(), info: z.string()})]); 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 }; /** * Manage IP whitelist * * GET /v4/account/{subAccountId}/datacenter_shared/whitelist */ export const getApiV4AccountSubAccountIdDatacenterSharedWhitelist = async (args: GetApiV4AccountSubAccountIdDatacenterSharedWhitelistArgs, options?: GetApiV4AccountSubAccountIdDatacenterSharedWhitelistOptions) => { const {baseUrl = "https://api.proxyscrape.com", fetch: fetchFn = fetch, apiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) for (const item of args['ip[]'] ?? []) query.append("ip[]", String(item)) if (args.labels != null) query.append("labels", String(args.labels)) if (apiToken !== undefined) headers.set("api-token", apiToken) const url = `${baseUrl.replace(/\/+$/, '')}/v4/account/${encodeURIComponent(String(args.subAccountId))}/datacenter_shared/whitelist${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(getApiV4AccountSubAccountIdDatacenterSharedWhitelistResponse, await res.json()) };