UNPKG

cloudflare

Version:

The official TypeScript library for the Cloudflare API

199 lines • 7.76 kB
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import { APIResource } from "../../core/resource.mjs"; import * as RulesAPI from "./rules.mjs"; import { BaseRules, Rules, } from "./rules.mjs"; import * as VersionsAPI from "./versions.mjs"; import { BaseVersions, Versions, } from "./versions.mjs"; import * as PhasesAPI from "./phases/phases.mjs"; import { BasePhases, Phases, } from "./phases/phases.mjs"; import { CloudflareError } from "../../core/error.mjs"; import { CursorPagination } from "../../core/pagination.mjs"; import { buildHeaders } from "../../internal/headers.mjs"; import { path } from "../../internal/utils/path.mjs"; export var BaseRulesets = /* @__PURE__ */ (() => { class BaseRulesets extends APIResource { /** * Creates a ruleset. * * @example * ```ts * const ruleset = await client.rulesets.create({ * account_id: 'account_id', * }); * ``` */ create(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`, { query: { dry_run }, body, ...options, })._thenUnwrap((obj) => obj.result); } /** * Updates an account or zone ruleset, creating a new version. * * @example * ```ts * const ruleset = await client.rulesets.update( * '2f2feab2026849078ba485f918791bdc', * { account_id: 'account_id' }, * ); * ``` */ update(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.put(path `/${accountOrZone}/${accountOrZoneId}/rulesets/${rulesetID}`, { query: { dry_run }, body, ...options, })._thenUnwrap((obj) => obj.result); } /** * Fetches all rulesets. * * @example * ```ts * // Automatically fetches more pages as needed. * for await (const rulesetListResponse of client.rulesets.list( * { account_id: 'account_id' }, * )) { * // ... * } * ``` */ list(params = {}, options) { const { account_id, zone_id, ...query } = 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.getAPIList(path `/${accountOrZone}/${accountOrZoneId}/rulesets`, (CursorPagination), { query, ...options }); } /** * Deletes all versions of an existing account or zone ruleset. * * @example * ```ts * await client.rulesets.delete( * '2f2feab2026849078ba485f918791bdc', * { account_id: 'account_id' }, * ); * ``` */ delete(rulesetID, params = {}, options) { const { 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/${rulesetID}`, { query: { dry_run }, ...options, headers: buildHeaders([{ Accept: '*/*' }, options?.headers]), }); } /** * Fetches the latest version of an account or zone ruleset. * * @example * ```ts * const ruleset = await client.rulesets.get( * '2f2feab2026849078ba485f918791bdc', * { account_id: 'account_id' }, * ); * ``` */ get(rulesetID, params = {}, options) { const { account_id, zone_id } = 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.get(path `/${accountOrZone}/${accountOrZoneId}/rulesets/${rulesetID}`, options)._thenUnwrap((obj) => obj.result); } } BaseRulesets._key = Object.freeze(['rulesets']); return BaseRulesets; })(); export var Rulesets = /* @__PURE__ */ (() => { class Rulesets extends BaseRulesets { constructor() { super(...arguments); this.phases = new PhasesAPI.Phases(this._client); this.rules = new RulesAPI.Rules(this._client); this.versions = new VersionsAPI.Versions(this._client); } } Rulesets.Phases = Phases; Rulesets.BasePhases = BasePhases; Rulesets.Rules = Rules; Rulesets.BaseRules = BaseRules; Rulesets.Versions = Versions; Rulesets.BaseVersions = BaseVersions; return Rulesets; })(); //# sourceMappingURL=rulesets.mjs.map