UNPKG

cloudflare

Version:

The official TypeScript library for the Cloudflare API

192 lines • 7.65 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 { V4PagePaginationArray } from "../../core/pagination.mjs"; import { path } from "../../internal/utils/path.mjs"; export var BaseAccessRules = /* @__PURE__ */ (() => { class BaseAccessRules extends APIResource { /** * Creates a new IP Access rule for an account or zone. The rule will apply to all * zones in the account or zone. * * Note: To create an IP Access rule that applies to a single zone, refer to the * [IP Access rules for a zone](#ip-access-rules-for-a-zone) endpoints. * * @example * ```ts * const accessRule = await client.firewall.accessRules.create( * { * configuration: {}, * mode: 'challenge', * account_id: 'account_id', * }, * ); * ``` */ create(params, options) { const { account_id, zone_id, ...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}/firewall/access_rules/rules`, { body, ...options, })._thenUnwrap((obj) => obj.result); } /** * Fetches IP Access rules of an account or zone. These rules apply to all the * zones in the account or zone. You can filter the results using several optional * parameters. * * @example * ```ts * // Automatically fetches more pages as needed. * for await (const accessRuleListResponse of client.firewall.accessRules.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}/firewall/access_rules/rules`, (V4PagePaginationArray), { query, ...options }); } /** * Deletes an existing IP Access rule defined. * * Note: This operation will affect all zones in the account or zone. * * @example * ```ts * const accessRule = await client.firewall.accessRules.delete( * '023e105f4ecef8ad9ca31a8372d0c353', * { account_id: 'account_id' }, * ); * ``` */ delete(ruleID, 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.delete(path `/${accountOrZone}/${accountOrZoneId}/firewall/access_rules/rules/${ruleID}`, options)._thenUnwrap((obj) => obj.result); } /** * Updates an IP Access rule defined. * * Note: This operation will affect all zones in the account or zone. * * @example * ```ts * const response = await client.firewall.accessRules.edit( * '023e105f4ecef8ad9ca31a8372d0c353', * { * configuration: {}, * mode: 'challenge', * account_id: 'account_id', * }, * ); * ``` */ edit(ruleID, params, options) { const { account_id, zone_id, ...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}/firewall/access_rules/rules/${ruleID}`, { body, ...options, })._thenUnwrap((obj) => obj.result); } /** * Fetches the details of an IP Access rule defined. * * @example * ```ts * const accessRule = await client.firewall.accessRules.get( * '023e105f4ecef8ad9ca31a8372d0c353', * { account_id: 'account_id' }, * ); * ``` */ get(ruleID, 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}/firewall/access_rules/rules/${ruleID}`, options)._thenUnwrap((obj) => obj.result); } } BaseAccessRules._key = Object.freeze([ 'firewall', 'accessRules', ]); return BaseAccessRules; })(); export class AccessRules extends BaseAccessRules { } //# sourceMappingURL=access-rules.mjs.map