import {z} from 'zod' export type GetApiV4AccountSubAccountIdDatacenterSharedPeriodsArgs = {subAccountId: string}; export type GetApiV4AccountSubAccountIdDatacenterSharedPeriodsOptions = {baseUrl?: string | undefined, apiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV4AccountSubAccountIdDatacenterSharedPeriodsResponse = z.union([z.object({periods_supported: z.literal(false)}), z.object({current_period: z.object({start: z.number().int().optional(), end: z.number().int().optional(), bandwidth_bytes: z.number().int().optional(), bandwidth_used_bytes: z.number().int().nullable().optional(), bandwidth_remaining_bytes: z.number().int().nullable().optional()}).nullable(), remaining_periods: z.array(z.object({days: z.number().int().optional(), estimated_start: z.number().int().optional(), estimated_end: z.number().int().optional()})), total_periods_remaining: 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 }; /** * Datacenter bandwidth periods * * GET /v4/account/{subAccountId}/datacenter_shared/periods */ export const getApiV4AccountSubAccountIdDatacenterSharedPeriods = async (args: GetApiV4AccountSubAccountIdDatacenterSharedPeriodsArgs, options?: GetApiV4AccountSubAccountIdDatacenterSharedPeriodsOptions) => { 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 (apiToken !== undefined) headers.set("api-token", apiToken) const url = `${baseUrl.replace(/\/+$/, '')}/v4/account/${encodeURIComponent(String(args.subAccountId))}/datacenter_shared/periods${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(getApiV4AccountSubAccountIdDatacenterSharedPeriodsResponse, await res.json()) };