UNPKG

cloudflare

Version:

The official TypeScript library for the Cloudflare API

124 lines • 4.78 kB
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import { APIResource } from "../../core/resource.mjs"; import { CloudflareError } from "../../core/error.mjs"; import { path } from "../../internal/utils/path.mjs"; export var BaseRules = /* @__PURE__ */ (() => { class BaseRules extends APIResource { /** * Adds a new rule to an account or zone ruleset. The rule will be added to the end * of the existing list of rules in the ruleset by default. * * @example * ```ts * const rule = await client.rulesets.rules.create( * '2f2feab2026849078ba485f918791bdc', * { account_id: 'account_id' }, * ); * ``` */ create(rulesetID, params, options) { const { account_id, zone_id, dry_run, ...body } = params; if (!account_id && !zone_id) { throw new CloudflareError('You must provide either account_id or zone_id.'); } if (account_id && zone_id) { throw new CloudflareError('You cannot provide both account_id and zone_id.'); } const { accountOrZone, accountOrZoneId } = account_id ? { accountOrZone: 'accounts', accountOrZoneId: account_id, } : { accountOrZone: 'zones', accountOrZoneId: zone_id, }; return this._client.post(path `/${accountOrZone}/${accountOrZoneId}/rulesets/${rulesetID}/rules`, { query: { dry_run }, body, ...options, })._thenUnwrap((obj) => obj.result); } /** * Deletes an existing rule from an account or zone ruleset. * * @example * ```ts * const rule = await client.rulesets.rules.delete( * '3a03d665bac047339bb530ecb439a90d', * { * ruleset_id: '2f2feab2026849078ba485f918791bdc', * account_id: 'account_id', * }, * ); * ``` */ delete(ruleID, params, options) { const { ruleset_id, account_id, zone_id, dry_run } = params; if (!account_id && !zone_id) { throw new CloudflareError('You must provide either account_id or zone_id.'); } if (account_id && zone_id) { throw new CloudflareError('You cannot provide both account_id and zone_id.'); } const { accountOrZone, accountOrZoneId } = account_id ? { accountOrZone: 'accounts', accountOrZoneId: account_id, } : { accountOrZone: 'zones', accountOrZoneId: zone_id, }; return this._client.delete(path `/${accountOrZone}/${accountOrZoneId}/rulesets/${ruleset_id}/rules/${ruleID}`, { query: { dry_run }, ...options, })._thenUnwrap((obj) => obj.result); } /** * Updates an existing rule in an account or zone ruleset. * * @example * ```ts * const response = await client.rulesets.rules.edit( * '3a03d665bac047339bb530ecb439a90d', * { * ruleset_id: '2f2feab2026849078ba485f918791bdc', * account_id: 'account_id', * }, * ); * ``` */ edit(ruleID, params, options) { const { ruleset_id, account_id, zone_id, dry_run, ...body } = params; if (!account_id && !zone_id) { throw new CloudflareError('You must provide either account_id or zone_id.'); } if (account_id && zone_id) { throw new CloudflareError('You cannot provide both account_id and zone_id.'); } const { accountOrZone, accountOrZoneId } = account_id ? { accountOrZone: 'accounts', accountOrZoneId: account_id, } : { accountOrZone: 'zones', accountOrZoneId: zone_id, }; return this._client.patch(path `/${accountOrZone}/${accountOrZoneId}/rulesets/${ruleset_id}/rules/${ruleID}`, { query: { dry_run }, body, ...options, })._thenUnwrap((obj) => obj.result); } } BaseRules._key = Object.freeze([ 'rulesets', 'rules', ]); return BaseRules; })(); export class Rules extends BaseRules { } //# sourceMappingURL=rules.mjs.map