EAPI for Edge API, or Extremelly Awesome Programation Interface, you decide
A template for kick starting a TypeScript Cloudflare worker project with all the bells and whistles.
- Inspired by
worker-typescript-template
& Express - Includes:
- A Middleware setup to help with code reuse accross enpoints
- A number of type definitions to help you code with confidence
- A number of compatible middlwares & utility that can be found at
p-j/eapi
- A test setup with Jest & all the polyfills & mock you need
tiny-request-router
for routing your requests, you can replace it with any other router you like though, it's only baked in for convenience
This template is meant to be used with Wrangler. If you are not already familiar with the tool, I recommend that you install the tool and configure it to work with your Cloudflare account. Documentation can be found here.
To use this template, click here to create a github repository from it.
Alternatively you can use Wrangler to generate a new project:
# This currently doesn't work, waiting on https://github.com/cloudflare/wrangler/pull/1653 or an alternative fix
wrangler generate my-ts-project p-j/worker-eapi-template --branch=main
cd my-ts-project
yarn
src/index.js
setup the fetchEventHandler with the appropriate router instance and default middlewares
src/router.js
define the routes, the middlewares and handlers for your application
src/middlwares
contains contains a demo middleware, you can find other 'ready to use' middlewares at p-j/eapi
src/handlers
contains a demo handler
__tests__/handlers.ts
contains a functional test for the demo
This template comes with jest tests which simply test that the request handler will perform as expected.
This template uses prettier
to format the project. To invoke, run yarn format
.
For information on how to preview and publish your worker, please see the Wrangler docs.
If you run into issues with this specific project, please feel free to file an issue here. If the problem is with Wrangler, please file an issue here.
The service-worker-mock
used by the tests is not a perfect representation of the Cloudflare Workers runtime. It is a general approximation. We recommend that you test end to end with wrangler dev
in addition to a staging environment to test things before deploying.
Also, the KV_MOCK
is currently fairly naive and could use a more thorough implementation.