Skip to content

resttjs/workerskv-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


An example project using Restt and WorkersKV.

Overview

Usage

Getting started

Start by installing the required modules for the project:

$ npm install

Configuration

Once you've installed the required packages for this project you'll need to add your Cloudflare credentials to restt.config.json as described in the Restt-CLI documentation.

You'll also want to configure the cloudflare.routes to match the domains you are using.

Serving for development

You can serve the edge worker locally for development by running the following command:

$ npm run serve

Restt-CLI will output the origins where your services are being served.

Deploying to the edge

You can deploy your edge worker to the edge with Cloudflare Workers by running the following command:

$ npm run deploy

Testing your services

There are two different services: Customers (which includes a fetch, create and update) and Cached Customers (which includes fetch).

You can check out the full services specification below.

Testing each of the resources on the services is easiest using either Postman or Fetch.

An example script using Fetch:

test-create.js
// Define the development domain
const developmentDomain = 'http://localhost:3000/workers.yourdomain.io';

// Define the production domain
const productionDomain = 'https:///workers.yourdomain.io';

// Perfom a test for customer creation
const createCustomer = (domain) => {

  // Attempt to create a customer
  try {

    // Create a customer
    const response = await fetch(`${domain}/customers`, {
      method: 'POST',
      body: JSON.stringify({
        email: '[email protected]',
        name: 'Charlie Brown'
      })
    });

    // Log out the response as JSON
    console.log(await response.json());

  } catch(error) {

    // Log out the error
    console.error(error);
  }
}

// Test for development
createCustomer(developmentDomain);

// Test for production
createCustomer(productionDomain);

Services

Customers

  • GET: https://workers.yourdomain.io/customers/:id

    • Fetches a customer by the specified id
  • POST: https://workers.yourdomain.io/customers

    • Creates a customer from the specifed request body
  • PATCH: https://workers.yourdomain.io/customers/:id

    • Updates a customer by the specified id from the specifed request body

Customers (Cached)

  • GET: https://workers-cache.yourdomain.io/customers/:id
    • Fetches a customer by the specified id (cached for 60 seconds)

License

MIT

Copyright (c) 2019-present, Daniel Larkin

About

An example project using Restt and WorkersKV

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published