mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
remove KV interface type
This commit is contained in:
parent
981de5e8f3
commit
e3163f30c6
29
kv.go
29
kv.go
@ -9,30 +9,19 @@ import (
|
||||
// KVNamespace represents interface of Cloudflare Worker's KV namespace instance.
|
||||
// - https://developers.cloudflare.com/workers/runtime-apis/kv/
|
||||
// - https://github.com/cloudflare/workers-types/blob/3012f263fb1239825e5f0061b267c8650d01b717/index.d.ts#L850
|
||||
type KVNamespace interface {
|
||||
GetString(key string, opts *KVNamespaceGetOptions) (string, error)
|
||||
GetReader(key string, opts *KVNamespaceGetOptions) (io.Reader, error)
|
||||
List(opts *KVNamespaceListOptions) (*KVNamespaceListResult, error)
|
||||
PutString(key string, value string, opts *KVNamespacePutOptions) error
|
||||
PutReader(key string, value io.Reader, opts *KVNamespacePutOptions) error
|
||||
Delete(key string) error
|
||||
}
|
||||
|
||||
type kvNamespace struct {
|
||||
type KVNamespace struct {
|
||||
instance js.Value
|
||||
}
|
||||
|
||||
var _ KVNamespace = &kvNamespace{}
|
||||
|
||||
// NewKVNamespace returns KVNamespace for given variable name.
|
||||
// - variable name must be defined in wrangler.toml as kv_namespace's binding.
|
||||
// - if the given variable name doesn't exist on global object, returns error.
|
||||
func NewKVNamespace(varName string) (KVNamespace, error) {
|
||||
func NewKVNamespace(varName string) (*KVNamespace, error) {
|
||||
inst := js.Global().Get(varName)
|
||||
if inst.IsUndefined() {
|
||||
return nil, fmt.Errorf("%s is undefined", varName)
|
||||
}
|
||||
return &kvNamespace{instance: inst}, nil
|
||||
return &KVNamespace{instance: inst}, nil
|
||||
}
|
||||
|
||||
// KVNamespaceGetOptions represents Cloudflare KV namespace get options.
|
||||
@ -55,7 +44,7 @@ func (opts *KVNamespaceGetOptions) toJS(type_ string) js.Value {
|
||||
|
||||
// GetString gets string value by the specified key.
|
||||
// - if a network error happens, returns error.
|
||||
func (kv *kvNamespace) GetString(key string, opts *KVNamespaceGetOptions) (string, error) {
|
||||
func (kv *KVNamespace) GetString(key string, opts *KVNamespaceGetOptions) (string, error) {
|
||||
p := kv.instance.Call("get", key, opts.toJS("text"))
|
||||
v, err := awaitPromise(p)
|
||||
if err != nil {
|
||||
@ -66,7 +55,7 @@ func (kv *kvNamespace) GetString(key string, opts *KVNamespaceGetOptions) (strin
|
||||
|
||||
// GetReader gets stream value by the specified key.
|
||||
// - if a network error happens, returns error.
|
||||
func (kv *kvNamespace) GetReader(key string, opts *KVNamespaceGetOptions) (io.Reader, error) {
|
||||
func (kv *KVNamespace) GetReader(key string, opts *KVNamespaceGetOptions) (io.Reader, error) {
|
||||
p := kv.instance.Call("get", key, opts.toJS("stream"))
|
||||
v, err := awaitPromise(p)
|
||||
if err != nil {
|
||||
@ -160,7 +149,7 @@ func toKVNamespaceListResult(v js.Value) (*KVNamespaceListResult, error) {
|
||||
}
|
||||
|
||||
// List lists keys stored into the KV namespace.
|
||||
func (kv *kvNamespace) List(opts *KVNamespaceListOptions) (*KVNamespaceListResult, error) {
|
||||
func (kv *KVNamespace) List(opts *KVNamespaceListOptions) (*KVNamespaceListResult, error) {
|
||||
p := kv.instance.Call("list", opts.toJS())
|
||||
v, err := awaitPromise(p)
|
||||
if err != nil {
|
||||
@ -193,7 +182,7 @@ func (opts *KVNamespacePutOptions) toJS() js.Value {
|
||||
|
||||
// PutString puts string value into KV with key.
|
||||
// - if a network error happens, returns error.
|
||||
func (kv *kvNamespace) PutString(key string, value string, opts *KVNamespacePutOptions) error {
|
||||
func (kv *KVNamespace) PutString(key string, value string, opts *KVNamespacePutOptions) error {
|
||||
p := kv.instance.Call("put", key, value, opts.toJS())
|
||||
_, err := awaitPromise(p)
|
||||
if err != nil {
|
||||
@ -205,7 +194,7 @@ func (kv *kvNamespace) PutString(key string, value string, opts *KVNamespacePutO
|
||||
// PutReader puts stream value into KV with key.
|
||||
// - This method copies all bytes into memory for implementation restriction.
|
||||
// - if a network error happens, returns error.
|
||||
func (kv *kvNamespace) PutReader(key string, value io.Reader, opts *KVNamespacePutOptions) error {
|
||||
func (kv *KVNamespace) PutReader(key string, value io.Reader, opts *KVNamespacePutOptions) error {
|
||||
// fetch body cannot be ReadableStream. see: https://github.com/whatwg/fetch/issues/1438
|
||||
b, err := io.ReadAll(value)
|
||||
if err != nil {
|
||||
@ -223,7 +212,7 @@ func (kv *kvNamespace) PutReader(key string, value io.Reader, opts *KVNamespaceP
|
||||
|
||||
// Delete deletes key-value pair specified by the key.
|
||||
// - if a network error happens, returns error.
|
||||
func (kv *kvNamespace) Delete(key string) error {
|
||||
func (kv *KVNamespace) Delete(key string) error {
|
||||
p := kv.instance.Call("delete", key)
|
||||
_, err := awaitPromise(p)
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user