An example project using Restt and WorkersKV.
Start by installing the required modules for the project:
$ npm install
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.
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.
You can deploy your edge worker to the edge with Cloudflare Workers by running the following command:
$ npm run deploy
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:
// 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);
-
GET:
https://workers.yourdomain.io/customers/:id
- Fetches a customer by the specified
id
- Fetches a customer by the specified
-
POST:
https://workers.yourdomain.io/customers
- Creates a customer from the specifed
request body
- Creates a customer from the specifed
-
PATCH:
https://workers.yourdomain.io/customers/:id
- Updates a customer by the specified
id
from the specifedrequest body
- Updates a customer by the specified
- GET:
https://workers-cache.yourdomain.io/customers/:id
- Fetches a customer by the specified
id
(cached for 60 seconds)
- Fetches a customer by the specified
Copyright (c) 2019-present, Daniel Larkin