Merge pull request #12 from syumai/remove-interface

remove some interface types
This commit is contained in:
syumai 2022-08-03 00:25:54 +09:00 committed by GitHub
commit fd68e38ca7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 39 deletions

View File

@ -22,7 +22,7 @@ func handleErr(w http.ResponseWriter, msg string, err error) {
} }
type server struct { type server struct {
bucket workers.R2Bucket bucket *workers.R2Bucket
} }
func newServer() (*server, error) { func newServer() (*server, error) {

29
kv.go
View File

@ -9,30 +9,19 @@ import (
// KVNamespace represents interface of Cloudflare Worker's KV namespace instance. // KVNamespace represents interface of Cloudflare Worker's KV namespace instance.
// - https://developers.cloudflare.com/workers/runtime-apis/kv/ // - https://developers.cloudflare.com/workers/runtime-apis/kv/
// - https://github.com/cloudflare/workers-types/blob/3012f263fb1239825e5f0061b267c8650d01b717/index.d.ts#L850 // - https://github.com/cloudflare/workers-types/blob/3012f263fb1239825e5f0061b267c8650d01b717/index.d.ts#L850
type KVNamespace interface { type KVNamespace struct {
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 {
instance js.Value instance js.Value
} }
var _ KVNamespace = &kvNamespace{}
// NewKVNamespace returns KVNamespace for given variable name. // NewKVNamespace returns KVNamespace for given variable name.
// - variable name must be defined in wrangler.toml as kv_namespace's binding. // - 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. // - 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) inst := js.Global().Get(varName)
if inst.IsUndefined() { if inst.IsUndefined() {
return nil, fmt.Errorf("%s is undefined", varName) 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. // 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. // GetString gets string value by the specified key.
// - if a network error happens, returns error. // - 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")) p := kv.instance.Call("get", key, opts.toJS("text"))
v, err := awaitPromise(p) v, err := awaitPromise(p)
if err != nil { if err != nil {
@ -66,7 +55,7 @@ func (kv *kvNamespace) GetString(key string, opts *KVNamespaceGetOptions) (strin
// GetReader gets stream value by the specified key. // GetReader gets stream value by the specified key.
// - if a network error happens, returns error. // - 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")) p := kv.instance.Call("get", key, opts.toJS("stream"))
v, err := awaitPromise(p) v, err := awaitPromise(p)
if err != nil { if err != nil {
@ -160,7 +149,7 @@ func toKVNamespaceListResult(v js.Value) (*KVNamespaceListResult, error) {
} }
// List lists keys stored into the KV namespace. // 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()) p := kv.instance.Call("list", opts.toJS())
v, err := awaitPromise(p) v, err := awaitPromise(p)
if err != nil { if err != nil {
@ -193,7 +182,7 @@ func (opts *KVNamespacePutOptions) toJS() js.Value {
// PutString puts string value into KV with key. // PutString puts string value into KV with key.
// - if a network error happens, returns error. // - 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()) p := kv.instance.Call("put", key, value, opts.toJS())
_, err := awaitPromise(p) _, err := awaitPromise(p)
if err != nil { 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. // PutReader puts stream value into KV with key.
// - This method copies all bytes into memory for implementation restriction. // - This method copies all bytes into memory for implementation restriction.
// - if a network error happens, returns error. // - 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 // fetch body cannot be ReadableStream. see: https://github.com/whatwg/fetch/issues/1438
b, err := io.ReadAll(value) b, err := io.ReadAll(value)
if err != nil { 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. // Delete deletes key-value pair specified by the key.
// - if a network error happens, returns error. // - 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) p := kv.instance.Call("delete", key)
_, err := awaitPromise(p) _, err := awaitPromise(p)
if err != nil { if err != nil {

View File

@ -9,37 +9,27 @@ import (
// R2Bucket represents interface of Cloudflare Worker's R2 Bucket instance. // R2Bucket represents interface of Cloudflare Worker's R2 Bucket instance.
// - https://developers.cloudflare.com/r2/runtime-apis/#bucket-method-definitions // - https://developers.cloudflare.com/r2/runtime-apis/#bucket-method-definitions
// - https://github.com/cloudflare/workers-types/blob/3012f263fb1239825e5f0061b267c8650d01b717/index.d.ts#L1006 // - https://github.com/cloudflare/workers-types/blob/3012f263fb1239825e5f0061b267c8650d01b717/index.d.ts#L1006
type R2Bucket interface { type R2Bucket struct {
Head(key string) (*R2Object, error)
Get(key string) (*R2Object, error)
Put(key string, value io.ReadCloser, opts *R2PutOptions) (*R2Object, error)
Delete(key string) error
List() (*R2Objects, error)
}
type r2Bucket struct {
instance js.Value instance js.Value
} }
var _ R2Bucket = &r2Bucket{}
// NewR2Bucket returns R2Bucket for given variable name. // NewR2Bucket returns R2Bucket for given variable name.
// - variable name must be defined in wrangler.toml. // - variable name must be defined in wrangler.toml.
// - see example: https://github.com/syumai/workers/tree/main/examples/r2-image-viewer // - see example: https://github.com/syumai/workers/tree/main/examples/r2-image-viewer
// - if the given variable name doesn't exist on global object, returns error. // - if the given variable name doesn't exist on global object, returns error.
func NewR2Bucket(varName string) (R2Bucket, error) { func NewR2Bucket(varName string) (*R2Bucket, error) {
inst := js.Global().Get(varName) inst := js.Global().Get(varName)
if inst.IsUndefined() { if inst.IsUndefined() {
return nil, fmt.Errorf("%s is undefined", varName) return nil, fmt.Errorf("%s is undefined", varName)
} }
return &r2Bucket{instance: inst}, nil return &R2Bucket{instance: inst}, nil
} }
// Head returns the result of `head` call to R2Bucket. // Head returns the result of `head` call to R2Bucket.
// - Body field of *R2Object is always nil for Head call. // - Body field of *R2Object is always nil for Head call.
// - if the object for given key doesn't exist, returns nil. // - if the object for given key doesn't exist, returns nil.
// - if a network error happens, returns error. // - if a network error happens, returns error.
func (r *r2Bucket) Head(key string) (*R2Object, error) { func (r *R2Bucket) Head(key string) (*R2Object, error) {
p := r.instance.Call("head", key) p := r.instance.Call("head", key)
v, err := awaitPromise(p) v, err := awaitPromise(p)
if err != nil { if err != nil {
@ -54,7 +44,7 @@ func (r *r2Bucket) Head(key string) (*R2Object, error) {
// Get returns the result of `get` call to R2Bucket. // Get returns the result of `get` call to R2Bucket.
// - if the object for given key doesn't exist, returns nil. // - if the object for given key doesn't exist, returns nil.
// - if a network error happens, returns error. // - if a network error happens, returns error.
func (r *r2Bucket) Get(key string) (*R2Object, error) { func (r *R2Bucket) Get(key string) (*R2Object, error) {
p := r.instance.Call("get", key) p := r.instance.Call("get", key)
v, err := awaitPromise(p) v, err := awaitPromise(p)
if err != nil { if err != nil {
@ -102,7 +92,7 @@ func (opts *R2PutOptions) toJS() js.Value {
// * This method copies all bytes into memory for implementation restriction. // * This method copies all bytes into memory for implementation restriction.
// * Body field of *R2Object is always nil for Put call. // * Body field of *R2Object is always nil for Put call.
// * if a network error happens, returns error. // * if a network error happens, returns error.
func (r *r2Bucket) Put(key string, value io.ReadCloser, opts *R2PutOptions) (*R2Object, error) { func (r *R2Bucket) Put(key string, value io.ReadCloser, opts *R2PutOptions) (*R2Object, error) {
// fetch body cannot be ReadableStream. see: https://github.com/whatwg/fetch/issues/1438 // fetch body cannot be ReadableStream. see: https://github.com/whatwg/fetch/issues/1438
b, err := io.ReadAll(value) b, err := io.ReadAll(value)
if err != nil { if err != nil {
@ -121,7 +111,7 @@ func (r *r2Bucket) Put(key string, value io.ReadCloser, opts *R2PutOptions) (*R2
// Delete returns the result of `delete` call to R2Bucket. // Delete returns the result of `delete` call to R2Bucket.
// * if a network error happens, returns error. // * if a network error happens, returns error.
func (r *r2Bucket) Delete(key string) error { func (r *R2Bucket) Delete(key string) error {
p := r.instance.Call("delete", key) p := r.instance.Call("delete", key)
if _, err := awaitPromise(p); err != nil { if _, err := awaitPromise(p); err != nil {
return err return err
@ -131,7 +121,7 @@ func (r *r2Bucket) Delete(key string) error {
// List returns the result of `list` call to R2Bucket. // List returns the result of `list` call to R2Bucket.
// * if a network error happens, returns error. // * if a network error happens, returns error.
func (r *r2Bucket) List() (*R2Objects, error) { func (r *R2Bucket) List() (*R2Objects, error) {
p := r.instance.Call("list") p := r.instance.Call("list")
v, err := awaitPromise(p) v, err := awaitPromise(p)
if err != nil { if err != nil {