import {z} from 'zod' export type ShieldPlanType = 0 | 1 | 2 | 3; export type WAFExecutionMode = 0 | 1; export type PullZoneWafConfigVariableModel = {name: string | null, valueEncoded: string | null}; export type WAFPayloadLimitAction = 0 | 1 | 2; export type DDoSShieldSensitivity = 0 | 1 | 2 | 3 | 4 | 5; export type ShieldZoneRequest = {shieldZoneId?: number | undefined, premiumPlan?: (boolean | null) | undefined, planType?: ShieldPlanType | undefined, learningMode?: (boolean | null) | undefined, learningModeUntil?: (string | null) | undefined, wafEnabled?: (boolean | null) | undefined, wafExecutionMode?: WAFExecutionMode | undefined, wafDisabledRules?: (Array | null) | undefined, wafLogOnlyRules?: (Array | null) | undefined, wafCustomRuleOrder?: (Array | null) | undefined, wafRequestHeaderLoggingEnabled?: (boolean | null) | undefined, requestBodyLoggingEnabled?: (boolean | null) | undefined, wafRequestIgnoredHeaders?: (Array | null) | undefined, wafRealtimeThreatIntelligenceEnabled?: (boolean | null) | undefined, wafProfileId?: (number | null) | undefined, wafEngineConfig?: (Array | null) | undefined, wafRequestBodyLimitAction?: WAFPayloadLimitAction | undefined, wafResponseBodyLimitAction?: WAFPayloadLimitAction | undefined, dDoSShieldSensitivity?: DDoSShieldSensitivity | undefined, dDoSChallengeWindow?: (number | null) | undefined, whitelabelResponsePages?: (boolean | null) | undefined}; export type UpdateShieldZoneRequest = {shieldZone?: ShieldZoneRequest | undefined, shieldZoneId: number}; export type PatchApiShieldShieldZoneArgs = {body: UpdateShieldZoneRequest}; export type PatchApiShieldShieldZoneOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const shieldPlanType = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]); export const wAFExecutionMode = z.union([z.literal(0), z.literal(1)]); export const pullZoneWafConfigVariableModel = z.object({name: z.string().nullable(), valueEncoded: z.string().nullable()}); export const wAFPayloadLimitAction = z.union([z.literal(0), z.literal(1), z.literal(2)]); export const dDoSShieldSensitivity = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5)]); export const shieldZoneRequest = z.object({shieldZoneId: z.number().int().optional(), premiumPlan: z.boolean().nullable().optional(), planType: shieldPlanType.optional(), learningMode: z.boolean().nullable().optional(), learningModeUntil: z.string().nullable().optional(), wafEnabled: z.boolean().nullable().optional(), wafExecutionMode: wAFExecutionMode.optional(), wafDisabledRules: z.array(z.string()).nullable().optional(), wafLogOnlyRules: z.array(z.string()).nullable().optional(), wafCustomRuleOrder: z.array(z.number().int()).nullable().optional(), wafRequestHeaderLoggingEnabled: z.boolean().nullable().optional(), requestBodyLoggingEnabled: z.boolean().nullable().optional(), wafRequestIgnoredHeaders: z.array(z.string()).nullable().optional(), wafRealtimeThreatIntelligenceEnabled: z.boolean().nullable().optional(), wafProfileId: z.number().int().nullable().optional(), wafEngineConfig: z.array(pullZoneWafConfigVariableModel).nullable().optional(), wafRequestBodyLimitAction: wAFPayloadLimitAction.optional(), wafResponseBodyLimitAction: wAFPayloadLimitAction.optional(), dDoSShieldSensitivity: dDoSShieldSensitivity.optional(), dDoSChallengeWindow: z.number().int().nullable().optional(), whitelabelResponsePages: z.boolean().nullable().optional()}); export const updateShieldZoneRequest = z.object({shieldZone: shieldZoneRequest.optional(), shieldZoneId: z.number().int()}); export const httpStatusCode = z.union([z.literal(100), z.literal(101), z.literal(102), z.literal(103), z.literal(200), z.literal(201), z.literal(202), z.literal(203), z.literal(204), z.literal(205), z.literal(206), z.literal(207), z.literal(208), z.literal(226), z.literal(300), z.literal(301), z.literal(302), z.literal(303), z.literal(304), z.literal(305), z.literal(306), z.literal(307), z.literal(308), z.literal(400), z.literal(401), z.literal(402), z.literal(403), z.literal(404), z.literal(405), z.literal(406), z.literal(407), z.literal(408), z.literal(409), z.literal(410), z.literal(411), z.literal(412), z.literal(413), z.literal(414), z.literal(415), z.literal(416), z.literal(417), z.literal(421), z.literal(422), z.literal(423), z.literal(424), z.literal(426), z.literal(428), z.literal(429), z.literal(431), z.literal(451), z.literal(500), z.literal(501), z.literal(502), z.literal(503), z.literal(504), z.literal(505), z.literal(506), z.literal(507), z.literal(508), z.literal(510), z.literal(511)]); export const genericRequestResponse = z.object({statusCode: httpStatusCode.optional(), success: z.boolean().optional(), message: z.string().nullable().optional(), errorKey: z.string().nullable().optional()}); export const updateShieldZoneResponse = z.object({data: updateShieldZoneRequest.optional(), error: genericRequestResponse.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 your Shield Zone configuration * * PATCH /shield/shield-zone */ export const patchApiShieldShieldZone = async (args: PatchApiShieldShieldZoneArgs, options?: PatchApiShieldShieldZoneOptions) => { const {baseUrl = "https://api.bunny.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/shield/shield-zone${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(updateShieldZoneResponse, await res.json()) };