npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cloudflare-kv-wrapper

v1.1.0

Published

A javascript/typescript wrapper for Cloudflare's KV namespaces using REST API

Downloads

89

Readme

@alienkarma/cloudflare-kv-wrapper

Author alienkarma License MIT Code Typescript NPM Version NPM Bundle Size

A javascript/typescript wrapper for Cloudflare's KV namespaces using REST API. This helps access Cloudflare's KV data stores directly without requiring CF workers or Wrangler.

👇 Table of contents:-

📐 Pre-requisites

  • Account ID:
    • Go to https://dash.cloudflare.com/
    • Select an account and a website
    • Scroll to the bottom right side of the page
    • Your account ID will be visible (as shown in the image)
  • Auth Token:
    • Click on "Get your API token" (as shown in the image)
    • Click on "Create Token" and use the template "Edit Cloudflare Workers"
    • Optionally, customize the settings as needed (zone restrictions, TTL, etc)
    • Once done, note down the API token

NOTE: It is better to store the account ID and auth token credentials as part of your .env file and use it from there securely.

CF Account and Auth Token Screenshot

🚀 Get started

Install package via npm

npm install cloudflare-kv-wrapper

Import the appropriate module for use. You can also import their relevant types.

import { init } from "cloudflare-kv-wrapper";

init({
  accountId: "your-account-id",
  authToken: "your-auth-token",
  namespaceId: "your-optional-namespace-id",
});
import { ns } from "cloudflare-kv-wrapper";
import type { NSTypes } from "cloudflare-kv-wrapper";

KV Pair

import { kv } from "cloudflare-kv-wrapper";
import type { KVTypes } from "cloudflare-kv-wrapper";

KV Pair (Multi)

import { kvm } from "cloudflare-kv-wrapper";
import type { KVMTypes } from "cloudflare-kv-wrapper";

And that is it! You can start using KV namespaces and their KV pairs. For usage examples, check out the /example folder.

🦾 API

All functions are async functions and are fully type-supported. Each function's source API link is attached as well for further queries. You can provide the credentials and/or namespace ID beforehand using the init function or provide them as parameters in each function call.

init

Namespaces : create [source]

ns.create();

// Parameters
NSTypes.Create {
  accountId?: string;
  authToken?: string;
  title: string;
}

// Response
NSTypes.CreateResponse {
  errors: Message[];
  messages: Message[];
  success: boolean;
  result: {
    id: string;
    supports_url_encoding: boolean;
    title: string;
  };
}

Namespaces : get [source]

ns.get();

// Parameters
NSTypes.Get {
  accountId?: string;
  authToken?: string;
  namespaceId?: string;
}

// Response
NSTypes.GetResponse {
  errors: Message[];
  messages: Message[];
  success: boolean;
  result: {
    id: string;
    supports_url_encoding: boolean;
    title: string;
  };
}

Namespaces : list [source]

ns.list();

// Parameters
NSTypes.List {
  accountId?: string;
  authToken?: string;
}

// Response
NSTypes.ListResponse {
  errors: Message[];
  messages: Message[];
  success: boolean;
  result: {
    namespaces: {
      id: string;
      title: string;
    }[];
  };
}

Namespaces : remove [source]

ns.remove();

// Parameters
NSTypes.Remove {
  accountId?: string;
  authToken?: string;
  namespaceId?: string;
}

// Response
NSTypes.RemoveResponse {
  errors: Message[];
  messages: Message[];
  success: boolean;
}

Namespaces : rename [source]

ns.rename();

// Parameters
NSTypes.Rename {
  accountId?: string;
  authToken?: string;
  namespaceId?: string;
  title: string;
}

// Response
NSTypes.RenameResponse {
  errors: Message[];
  messages: Message[];
  success: boolean;
  result: {
    id: string;
    supports_url_encoding: boolean;
    title: string;
  };
}

KV Pair : list [source]

kv.list();

// Parameters
KVTypes.List {
  accountId?: string;
  authToken?: string;
  namespaceId?: string;
}

// Response
KVTypes.ListResponse {
  errors: Message[];
  messages: Message[];
  success: boolean;
  result: {
    keys: string[];
  };
}

KV Pair : metadata [source]

kv.metadata();

// Parameters
KVTypes.Metadata {
  accountId?: string;
  authToken?: string;
  namespaceId?: string;
  key: string;
}

// Response
KVTypes.MetadataResponse {
  errors: Message[];
  messages: Message[];
  success: boolean;
  result: {
    expiration: string;
    expiration_ttl: number;
    key: string;
    metadata: {
      [key: string]: string;
    };
  };
}

KV Pair : read [source]

kv.read();

// Parameters
KVTypes.Read {
  accountId?: string;
  authToken?: string;
  namespaceId?: string;
  key: string;
}

// Response
KVTypes.ReadResponse {
  errors: Message[];
  messages: Message[];
  success: boolean;
  result: {
    expiration: string;
    expiration_ttl: number;
    key: string;
    value: string;
  };
}

KV Pair : write [source]

kv.write();

// Parameters
KVTypes.Write {
  accountId?: string;
  authToken?: string;
  namespaceId?: string;
  key: string;
  value: string;
  metadata: {
    [key: string]: string;
  };
  expiration?: string;
  expiration_ttl?: number;
}

// Response
KVTypes.WriteResponse {
  errors: Message[];
  messages: Message[];
  success: boolean;
}

KV Pair : remove [source]

kv.remove();

// Parameters
KVTypes.Remove {
  accountId?: string;
  authToken?: string;
  namespaceId?: string;
  key: string;
}

// Response
KVTypes.RemoveResponse {
  errors: Message[];
  messages: Message[];
  success: boolean;
}

KV Pair (Multi) : write [source]

kvm.write();

// Parameters
KVMTypes.Write {
  accountId?: string;
  authToken?: string;
  namespaceId?: string;
  keys: string[];
}

// Response
KVMTypes.WriteResponse {
  errors: Message[];
  messages: Message[];
  success: boolean;
  result: {
    keys: {
      key: string;
      value: string;
    }[];
  };
}

KV Pair (Multi) : remove [source]

kvm.remove();

// Parameters
KVMTypes.Remove {
  accountId?: string;
  authToken?: string;
  namespaceId?: string;
  keys: string[];
}

// Response
KVMTypes.RemoveResponse {
  errors: Message[];
  messages: Message[];
  success: boolean;
}

If you face any issues, raise a request and I will try my best to solve it. Enjoy! 👍