import {z} from 'zod' export type RatelimitRuleActionType = 1 | 2 | 3; export type WafRuleOperatorType = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 17 | 18; export type WafRuleSeverityType = 0 | 1 | 2; export type WafRuleTransformationType = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21; export type WafRatelimitCounterKeyType = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7; export type WafRateLimitTimeframeType = 1 | 10 | 60 | 300 | 900 | 3600; export type WafRatelimitBlockType = 30 | 60 | 300 | 900 | 1800 | 3600; export type WafChainedRuleConditionItem = {variableTypes?: ({REQUEST_URI?: string | undefined, REQUEST_URI_RAW?: string | undefined, ARGS?: string | undefined, ARGS_COMBINED_SIZE?: string | undefined, ARGS_GET?: string | undefined, ARGS_GET_NAMES?: string | undefined, ARGS_POST?: string | undefined, ARGS_POST_NAMES?: string | undefined, FILES_NAMES?: string | undefined, GEO?: string | undefined, REMOTE_ADDR?: string | undefined, QUERY_STRING?: string | undefined, REQUEST_BASENAME?: string | undefined, REQUEST_BODY?: string | undefined, REQUEST_COOKIES_NAMES?: string | undefined, REQUEST_COOKIES?: string | undefined, REQUEST_FILENAME?: string | undefined, REQUEST_HEADERS_NAMES?: string | undefined, REQUEST_HEADERS?: string | undefined, REQUEST_LINE?: string | undefined, REQUEST_METHOD?: string | undefined, REQUEST_PROTOCOL?: string | undefined, RESPONSE_BODY?: string | undefined, RESPONSE_HEADERS?: string | undefined, RESPONSE_STATUS?: string | undefined, FINGERPRINT?: string | undefined, VERIFIED_BOT_CATEGORY?: string | undefined} | null) | undefined, operatorType?: WafRuleOperatorType | undefined, value?: (string | null) | undefined, isNegated?: (boolean | null) | undefined, isRegexVariable?: (boolean | null) | undefined}; export type CreateWafRateLimitRuleModel = {actionType?: RatelimitRuleActionType | undefined, variableTypes?: ({REQUEST_URI?: string | undefined, REQUEST_URI_RAW?: string | undefined, ARGS?: string | undefined, ARGS_COMBINED_SIZE?: string | undefined, ARGS_GET?: string | undefined, ARGS_GET_NAMES?: string | undefined, ARGS_POST?: string | undefined, ARGS_POST_NAMES?: string | undefined, FILES_NAMES?: string | undefined, GEO?: string | undefined, REMOTE_ADDR?: string | undefined, QUERY_STRING?: string | undefined, REQUEST_BASENAME?: string | undefined, REQUEST_BODY?: string | undefined, REQUEST_COOKIES_NAMES?: string | undefined, REQUEST_COOKIES?: string | undefined, REQUEST_FILENAME?: string | undefined, REQUEST_HEADERS_NAMES?: string | undefined, REQUEST_HEADERS?: string | undefined, REQUEST_LINE?: string | undefined, REQUEST_METHOD?: string | undefined, REQUEST_PROTOCOL?: string | undefined, RESPONSE_BODY?: string | undefined, RESPONSE_HEADERS?: string | undefined, RESPONSE_STATUS?: string | undefined, FINGERPRINT?: string | undefined, VERIFIED_BOT_CATEGORY?: string | undefined} | null) | undefined, operatorType?: WafRuleOperatorType | undefined, severityType?: WafRuleSeverityType | undefined, transformationTypes?: (Array | null) | undefined, value?: (string | null) | undefined, isNegated?: (boolean | null) | undefined, isRegexVariable?: (boolean | null) | undefined, requestCount?: number | undefined, counterKeyType?: WafRatelimitCounterKeyType | undefined, timeframe?: WafRateLimitTimeframeType | undefined, blockTime?: WafRatelimitBlockType | undefined, chainedRuleConditions?: (Array | null) | undefined}; export type UpdateWafRateLimitRequest = {ruleName?: (string | null) | undefined, ruleDescription?: (string | null) | undefined, ruleConfiguration?: CreateWafRateLimitRuleModel | undefined}; export type PatchApiShieldRateLimitIdArgs = {id: number, body: UpdateWafRateLimitRequest}; export type PatchApiShieldRateLimitIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const ratelimitRuleActionType = z.union([z.literal(1), z.literal(2), z.literal(3)]); export const wafRuleOperatorType = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8), z.literal(9), z.literal(12), z.literal(13), z.literal(14), z.literal(15), z.literal(17), z.literal(18)]); export const wafRuleSeverityType = z.union([z.literal(0), z.literal(1), z.literal(2)]); export const wafRuleTransformationType = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8), z.literal(9), z.literal(10), z.literal(11), z.literal(12), z.literal(13), z.literal(14), z.literal(15), z.literal(16), z.literal(17), z.literal(18), z.literal(19), z.literal(20), z.literal(21)]); export const wafRatelimitCounterKeyType = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7)]); export const wafRateLimitTimeframeType = z.union([z.literal(1), z.literal(10), z.literal(60), z.literal(300), z.literal(900), z.literal(3600)]); export const wafRatelimitBlockType = z.union([z.literal(30), z.literal(60), z.literal(300), z.literal(900), z.literal(1800), z.literal(3600)]); export const wafChainedRuleConditionItem = z.object({variableTypes: z.object({REQUEST_URI: z.string().optional(), REQUEST_URI_RAW: z.string().optional(), ARGS: z.string().optional(), ARGS_COMBINED_SIZE: z.string().optional(), ARGS_GET: z.string().optional(), ARGS_GET_NAMES: z.string().optional(), ARGS_POST: z.string().optional(), ARGS_POST_NAMES: z.string().optional(), FILES_NAMES: z.string().optional(), GEO: z.string().optional(), REMOTE_ADDR: z.string().optional(), QUERY_STRING: z.string().optional(), REQUEST_BASENAME: z.string().optional(), REQUEST_BODY: z.string().optional(), REQUEST_COOKIES_NAMES: z.string().optional(), REQUEST_COOKIES: z.string().optional(), REQUEST_FILENAME: z.string().optional(), REQUEST_HEADERS_NAMES: z.string().optional(), REQUEST_HEADERS: z.string().optional(), REQUEST_LINE: z.string().optional(), REQUEST_METHOD: z.string().optional(), REQUEST_PROTOCOL: z.string().optional(), RESPONSE_BODY: z.string().optional(), RESPONSE_HEADERS: z.string().optional(), RESPONSE_STATUS: z.string().optional(), FINGERPRINT: z.string().optional(), VERIFIED_BOT_CATEGORY: z.string().optional()}).nullable().optional(), operatorType: wafRuleOperatorType.optional(), value: z.string().nullable().optional(), isNegated: z.boolean().nullable().optional(), isRegexVariable: z.boolean().nullable().optional()}); export const createWafRateLimitRuleModel = z.object({actionType: ratelimitRuleActionType.optional(), variableTypes: z.object({REQUEST_URI: z.string().optional(), REQUEST_URI_RAW: z.string().optional(), ARGS: z.string().optional(), ARGS_COMBINED_SIZE: z.string().optional(), ARGS_GET: z.string().optional(), ARGS_GET_NAMES: z.string().optional(), ARGS_POST: z.string().optional(), ARGS_POST_NAMES: z.string().optional(), FILES_NAMES: z.string().optional(), GEO: z.string().optional(), REMOTE_ADDR: z.string().optional(), QUERY_STRING: z.string().optional(), REQUEST_BASENAME: z.string().optional(), REQUEST_BODY: z.string().optional(), REQUEST_COOKIES_NAMES: z.string().optional(), REQUEST_COOKIES: z.string().optional(), REQUEST_FILENAME: z.string().optional(), REQUEST_HEADERS_NAMES: z.string().optional(), REQUEST_HEADERS: z.string().optional(), REQUEST_LINE: z.string().optional(), REQUEST_METHOD: z.string().optional(), REQUEST_PROTOCOL: z.string().optional(), RESPONSE_BODY: z.string().optional(), RESPONSE_HEADERS: z.string().optional(), RESPONSE_STATUS: z.string().optional(), FINGERPRINT: z.string().optional(), VERIFIED_BOT_CATEGORY: z.string().optional()}).nullable().optional(), operatorType: wafRuleOperatorType.optional(), severityType: wafRuleSeverityType.optional(), transformationTypes: z.array(wafRuleTransformationType).nullable().optional(), value: z.string().nullable().optional(), isNegated: z.boolean().nullable().optional(), isRegexVariable: z.boolean().nullable().optional(), requestCount: z.number().int().optional(), counterKeyType: wafRatelimitCounterKeyType.optional(), timeframe: wafRateLimitTimeframeType.optional(), blockTime: wafRatelimitBlockType.optional(), chainedRuleConditions: z.array(wafChainedRuleConditionItem).nullable().optional()}); 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 rateLimitRule = z.object({id: z.number().int().optional(), shieldZoneId: z.number().int().optional(), userId: z.string().nullable().optional(), ruleName: z.string().nullable().optional(), ruleDescription: z.string().nullable().optional(), ruleJson: z.string().nullable().optional(), ruleConfiguration: createWafRateLimitRuleModel.optional(), errorResponse: 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 a Rate Limit configuration on your Shield Zone * * PATCH /shield/rate-limit/{id} */ export const patchApiShieldRateLimitId = async (args: PatchApiShieldRateLimitIdArgs, options?: PatchApiShieldRateLimitIdOptions) => { 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/rate-limit/${encodeURIComponent(String(args.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(rateLimitRule, await res.json()) };