Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Improve documentation around using KVStore from code #117

Open
joladev opened this issue Sep 4, 2019 · 1 comment
Open

Improve documentation around using KVStore from code #117

joladev opened this issue Sep 4, 2019 · 1 comment

Comments

@joladev
Copy link

joladev commented Sep 4, 2019

There are examples of how to set up bindings using the CLI but nothing for running it programmatically.

Through trial and error I've managed the following

// @ts-ignore
import Cloudworker, { KeyValueStore } from '@dollarshaveclub/cloudworker';
...
  const keyStore = new KeyValueStore();
  store.put('key', 'value');
  const cw = new Cloudworker(script, {
    bindings: {
      STORE: store,
    },
  });

Note that TypeScript errors on that import with has no exported member 'KeyValueStore'. Not sure how to fix that, maybe I'm importing it wrong?

Anyway, if this looks right I can make a PR with a simple example of using the KV Store to the README.

@iadityak
Copy link

iadityak commented Feb 5, 2020

You can make the import using require for CommonJS library, no need to go ES6 style.

const Cloudworker = require('@dollarshaveclub/cloudworker')


const simpleScript = `addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request))
  })
  
  async function handleRequest(request) {
    const response = await fetch(request)
    console.log(await STORE.get("test"))
    return response
  }`

  const store = new Cloudworker.KeyValueStore();
  store.put("test", "helloWorld");
  
  const req = new Cloudworker.Request('https://www.google.com/')
  const cw = new Cloudworker(simpleScript,  {
    bindings: {
      STORE: store,
    },
  })
  cw.dispatch(req).then((res) => {
    console.log("Response Status: ", res.status)
    res.text().then((body) =>{
      console.log("Response Body: ", body) // 
    })
  })

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants