import {z} from 'zod' export type GetApiPlansIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const timestamp = z.string(); export const plan = z.object({href: z.string().optional(), id: z.string().optional(), name: z.string().optional(), attrs: z.object({adcpools: z.array(z.number()).optional(), application_testing_allocated_users: z.number().optional(), application_testing_allocated_virtual_user_minutes: z.number().optional(), application_testing_virtual_user_minute_price: z.number().optional(), data_transfer_allowance: z.number().optional(), data_transfer_allowance_period: z.number().optional(), enable_application_testing: z.boolean().optional(), enable_app_shielding: z.boolean().optional(), enable_autoscaler: z.boolean().optional(), enable_cdn: z.boolean().optional(), enable_custom_logs: z.boolean().optional(), enable_geolocation: z.boolean().optional(), enable_logs: z.boolean().optional(), enable_optimization: z.boolean().optional(), enable_page_control_actions: z.array(z.string()).optional(), enable_page_control_conditions: z.array(z.string()).optional(), enable_ranges: z.array(z.string()).optional(), enable_security_monitoring: z.boolean().optional(), enable_site_cache: z.boolean().optional(), enable_rate_limit: z.boolean().optional(), site_cache_max_metadata_entries: z.number().optional(), enable_threats_portlet: z.boolean().optional(), enable_views: z.array(z.string()).optional(), hosting_allowance: z.array(z.object({category: z.string(), count: z.number(), type: z.array(z.string())})).optional(), log_retention: z.array(z.object({type: z.enum(["default", "cdn", "csp-reports", "pageviews", "proxy-logs", "events", "custom"]), online_days: z.number().optional(), archive_days: z.number().optional(), online_space_allowance: z.number().optional(), archive_space_allowance: z.number().optional()})).optional(), maintenance_mode: z.boolean().optional(), max_adc_count: z.number().optional(), alias_count: z.number().optional(), monitoring_available: z.array(z.string()).optional(), monthly_bill: z.number().optional(), monthly_data_transfer_limit: z.number().optional(), num_page_controls: z.number().optional(), primary: z.boolean().optional(), protections_available: z.array(z.string()).optional(), site_cache_space: z.number().optional(), target_adc_count: z.number().optional(), shared_data_planes: z.array(z.string()).optional(), dedicated_data_plane_count: z.number().optional()}).optional(), subject_type: z.string().optional(), created: timestamp.optional(), created_by: z.number().int().optional(), updated: timestamp.optional(), updated_by: z.number().int().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 }; /** * Get the identified authorized plan * * GET /plans/{id} */ export const getApiPlansId = async (options?: GetApiPlansIdOptions) => { const {baseUrl = "https://api.webscale.com/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/plans/${encodeURIComponent(String(args.id))}${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(plan, await res.json()) };