cloudflare-client
Version:
Universal HTTP client for Cloudflare API
123 lines (122 loc) • 4.59 kB
JavaScript
/* SPDX-FileCopyrightText: 2022-present Kriasoft */
/* SPDX-License-Identifier: MIT */
import { baseUrl, createFetch, HttpMethod } from "./fetch.js";
// #endregion
const { GET, PUT, POST, DELETE } = HttpMethod;
/**
* Cloudflare KV Storage client
*/
export function kv(options) {
const { accountId, ...credentials } = options;
const url = `${baseUrl}/accounts/${accountId}/storage/kv/namespaces`;
if (!accountId)
throw new TypeError(`options.accountId`);
if ("accessToken" in credentials) {
if (!credentials.accessToken)
throw new TypeError(`options.accessToken`);
}
else {
if (!credentials.authKey)
throw new TypeError(`options.authKey`);
if (!credentials.authEmail)
throw new TypeError(`options.authEmail`);
}
return {
/**
* List Namespaces
* @see https://api.cloudflare.com/#workers-kv-namespace-list-namespaces
*/
find: createFetch((params) => ({
method: GET,
url,
searchParams: params,
credentials,
})).query(),
/**
* Create a Namespace
* @see https://api.cloudflare.com/#workers-kv-namespace-properties
*/
create: createFetch((name) => ({
method: POST,
url,
body: JSON.stringify({ title: name }),
credentials,
})).response(),
/**
* Rename a Namespace
* @see https://api.cloudflare.com/#workers-kv-namespace-rename-a-namespace
*/
update: createFetch((id, title) => ({
method: PUT,
url: `${url}/${id}`,
body: JSON.stringify({ title }),
credentials,
})).response(),
/**
* Remove a Namespace
* @see https://api.cloudflare.com/#workers-kv-namespace-remove-a-namespace
*/
delete: createFetch((id) => ({
method: DELETE,
url: `${url}/${id}`,
credentials,
})).response(),
namespace(id) {
const namespaceUrl = `${url}/${id}`;
const namespaceId = id;
return {
keys: createFetch((params) => ({
method: GET,
url: `${namespaceUrl}/keys`,
searchParams: {
prefix: params?.prefix,
limit: params?.first,
cursor: params?.after,
},
credentials,
})).query(),
/**
* Read key-value pair
* @see https://api.cloudflare.com/#workers-kv-namespace-read-key-value-pair
*/
get: createFetch((key, options) => ({
method: GET,
url: `${url}/${namespaceId}/values/${key}`,
credentials,
type: options?.decode === false
? "text"
: options?.decode
? "binary"
: "json",
notFoundResponse: undefined,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
})).response(),
/**
* Write key-value pair
* @see https://api.cloudflare.com/#workers-kv-namespace-write-key-value-pair
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
set: createFetch((key, value, options) => ({
method: PUT,
url: `${namespaceUrl}/values/${key}`,
searchParams: {
expiration: options?.expires,
expiration_ttl: options?.expiresTtl,
},
body: options?.encode === false ||
(typeof options?.encode === undefined && typeof value === "string")
? value
: JSON.stringify(value),
contentType: options?.contentType ?? "text/plain; charset=utf-8",
credentials,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
})).response(),
delete: createFetch((id) => ({
method: DELETE,
url: `${url}/${namespaceId}/values/${id}`,
credentials,
})).response(),
};
},
};
}