UNPKG

cloudflare

Version:

The official TypeScript library for the Cloudflare API

203 lines • 8.49 kB
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import { APIResource } from "../../../core/resource.mjs"; import * as KeysAPI from "./keys.mjs"; import { BaseKeys, Keys, } from "./keys.mjs"; import * as MetadataAPI from "./metadata.mjs"; import { BaseMetadata, Metadata } from "./metadata.mjs"; import * as ValuesAPI from "./values.mjs"; import { BaseValues, Values as ValuesAPIValues, } from "./values.mjs"; import { V4PagePaginationArray, } from "../../../core/pagination.mjs"; import { path } from "../../../internal/utils/path.mjs"; export var BaseNamespaces = /* @__PURE__ */ (() => { class BaseNamespaces extends APIResource { /** * Creates a Workers KV namespace in the specified account with the given title. * Returns `400` if the account already owns a namespace with that title; an * existing namespace must be explicitly deleted before it can be replaced. An * optional jurisdiction restricts where data is durably stored and can only be set * at creation time. * * @example * ```ts * const namespace = await client.kv.namespaces.create({ * account_id: '023e105f4ecef8ad9ca31a8372d0c353', * title: 'My Own Namespace', * }); * ``` */ create(params, options) { const { account_id, ...body } = params; return this._client.post(path `/accounts/${account_id}/storage/kv/namespaces`, { body, ...options, })._thenUnwrap((obj) => obj.result); } /** * Changes the title of the specified Workers KV namespace and returns the updated * namespace. The namespace ID and stored key-value pairs are unchanged. * * @example * ```ts * const namespace = await client.kv.namespaces.update( * '0f2ac74b498b48028cb68387c421e279', * { * account_id: '023e105f4ecef8ad9ca31a8372d0c353', * title: 'My Own Namespace', * }, * ); * ``` */ update(namespaceID, params, options) { const { account_id, ...body } = params; return this._client.put(path `/accounts/${account_id}/storage/kv/namespaces/${namespaceID}`, { body, ...options, })._thenUnwrap((obj) => obj.result); } /** * Lists Workers KV namespaces owned by the specified account. Use `page` and * `per_page` to select a page of results, and `order` and `direction` to control * sorting. * * @example * ```ts * // Automatically fetches more pages as needed. * for await (const namespace of client.kv.namespaces.list({ * account_id: '023e105f4ecef8ad9ca31a8372d0c353', * })) { * // ... * } * ``` */ list(params, options) { const { account_id, ...query } = params; return this._client.getAPIList(path `/accounts/${account_id}/storage/kv/namespaces`, (V4PagePaginationArray), { query, ...options }); } /** * Deletes the specified Workers KV namespace and its stored key-value pairs from * the account. * * @example * ```ts * const namespace = await client.kv.namespaces.delete( * '0f2ac74b498b48028cb68387c421e279', * { account_id: '023e105f4ecef8ad9ca31a8372d0c353' }, * ); * ``` */ delete(namespaceID, params, options) { const { account_id } = params; return this._client.delete(path `/accounts/${account_id}/storage/kv/namespaces/${namespaceID}`, options)._thenUnwrap((obj) => obj.result); } /** * Deletes up to 10,000 key-value pairs from the specified Workers KV namespace. * Send a JSON array of the key names to delete. The result reports the number of * successful deletions and any keys that failed and should be retried. * * @example * ```ts * const response = await client.kv.namespaces.bulkDelete( * '0f2ac74b498b48028cb68387c421e279', * { * account_id: '023e105f4ecef8ad9ca31a8372d0c353', * body: ['My-Key'], * }, * ); * ``` */ bulkDelete(namespaceID, params, options) { const { account_id, body } = params; return this._client.post(path `/accounts/${account_id}/storage/kv/namespaces/${namespaceID}/bulk/delete`, { body: body, ...options, })._thenUnwrap((obj) => obj.result); } /** * Retrieves the text-based values of up to 100 keys from the specified Workers KV * namespace. The result maps each requested key to its value. Set `type` to `json` * to parse JSON values instead of returning strings, and set `withMetadata` to * `true` to include metadata with each value. Binary values are not supported by * this operation. * * @example * ```ts * const response = await client.kv.namespaces.bulkGet( * '0f2ac74b498b48028cb68387c421e279', * { * account_id: '023e105f4ecef8ad9ca31a8372d0c353', * keys: ['My-Key'], * }, * ); * ``` */ bulkGet(namespaceID, params, options) { const { account_id, ...body } = params; return this._client.post(path `/accounts/${account_id}/storage/kv/namespaces/${namespaceID}/bulk/get`, { body, ...options, })._thenUnwrap((obj) => obj.result); } /** * Writes up to 10,000 key-value pairs to the specified Workers KV namespace from a * JSON array, with optional metadata and expiration settings for each pair. * Existing values and expirations are overwritten. If neither `expiration` nor * `expiration_ttl` is specified, the key-value pair will not expire. If both are * set, `expiration_ttl` takes precedence. The entire request must be 100 megabytes * or less. The result reports the number of successful writes and any keys that * failed and should be retried. * * @example * ```ts * const response = await client.kv.namespaces.bulkUpdate( * '0f2ac74b498b48028cb68387c421e279', * { * account_id: '023e105f4ecef8ad9ca31a8372d0c353', * body: [{ key: 'My-Key', value: 'Some string' }], * }, * ); * ``` */ bulkUpdate(namespaceID, params, options) { const { account_id, body } = params; return this._client.put(path `/accounts/${account_id}/storage/kv/namespaces/${namespaceID}/bulk`, { body: body, ...options, })._thenUnwrap((obj) => obj.result); } /** * Returns the Workers KV namespace for the specified account and namespace ID. * * @example * ```ts * const namespace = await client.kv.namespaces.get( * '0f2ac74b498b48028cb68387c421e279', * { account_id: '023e105f4ecef8ad9ca31a8372d0c353' }, * ); * ``` */ get(namespaceID, params, options) { const { account_id } = params; return this._client.get(path `/accounts/${account_id}/storage/kv/namespaces/${namespaceID}`, options)._thenUnwrap((obj) => obj.result); } } BaseNamespaces._key = Object.freeze(['kv', 'namespaces']); return BaseNamespaces; })(); export var Namespaces = /* @__PURE__ */ (() => { class Namespaces extends BaseNamespaces { constructor() { super(...arguments); this.keys = new KeysAPI.Keys(this._client); this.metadata = new MetadataAPI.Metadata(this._client); this.values = new ValuesAPI.Values(this._client); } } Namespaces.Keys = Keys; Namespaces.BaseKeys = BaseKeys; Namespaces.Metadata = Metadata; Namespaces.BaseMetadata = BaseMetadata; Namespaces.Values = ValuesAPIValues; Namespaces.BaseValues = BaseValues; return Namespaces; })(); //# sourceMappingURL=namespaces.mjs.map