Skip to content

Repository files navigation

EAPI

EAPI for Edge API, or Extremelly Awesome Programming Interface, you decide 😎

lernalernacodecovBuild StatusLicenseFOSSA Status

What are EAPI packages?

It's a collection of common building blocks you can use to build scalable and composable API at the Edge using Cloudflare Workers and TypeScript.

While EAPI packages are meant to work together with p-j/worker-eapi-template, you can also use them as standalone functions as demonstrated below.

PackageDescriptionVersionChangelog
@p-j/eapi-middleware-cacheConfigure browser & CDN cacheversionchangelog
@p-j/eapi-middleware-corsValidate Origin & Apply CORS Headersversionchangelog
@p-j/eapi-middleware-errorhandlerCatch exceptions, forward them or print themversionchangelog
@p-j/eapi-middleware-redirectRedirect, Rewrite or Proxy Requestsversionchangelog
@p-j/eapi-middleware-headersAdd/Remove headers on Request & Responseversionchangelog
@p-j/eapi-util-applymiddlewaresA utility to combine multiple middlewaresversionchangelog
@p-j/eapi-util-fetcheventhandlerApply global middlewares & Match Routesversionchangelog
@p-j/eapi-typesCommon TypeScript typings for EAPI projectsversionchangelog

Usage

Middlewares

eapi-middleware-* packages are providing Middleware Factory, that, given a number of options, will return actual Middlewares functions. The middlewares functions can then be applied to the request handler middlware(requestHandler) and returns an enhanced request handler. The type definitions can help you better understand how things work together.

That's the recommended way of using this middlewares as it was built with this integration in mind. This example is already setup in @p-j/worker-eapi-template

// src/router.tsimport{Router}from'tiny-request-router'import{withCache}from'@p-j/eapi-middleware-cache'import{withErrorHandler}from'@p-j/eapi-middleware-errorhandler'import{applyMiddlewares}from'@p-j/eapi-util-applymiddlewares'constTTL_30MINUTES=60*30functionrequestHandler({ event, request, params }: RequestContext): Response{returnnewResponse('Hello World!')}/** * Route definitions */exportconstrouter=newRouter()router.all('/',applyMiddlewares(requestHandler,withErrorHandler({enableDebug: true}),withCache({cacheControl: `public, max-age=${TTL_30MINUTES}`,cdnTtl: TTL_30MINUTES,}),),)// The router is then used to match request in the src/index.ts

With EAPI toolkit

Combining some of the EAPI tools while not embracing the whole template, you can build an extensible setup that allow you to easily apply a variaty of middlewares.

import{withCache}from'@p-j/eapi-middleware-cache'import{applyMiddlewares}from'@p-j/eapi-util-applymiddlewares'import{withAwesomeMiddleware}from'./withAwesomeMiddleware'functionrequestHandler({ event, request, params }: RequestContext): Response{returnnewResponse('Hello World!')}addEventListener('fetch',(event)=>{constrequestContext={ event,request: event.request,params: {}}// apply all the middlewares on top of the original handlerconsthandler=applyMiddlewares(requestHandler,withCache({cacheControl: 'public, max-age=3600',cdnTtl: 3600,}),withAwesomeMiddleware(),)// use the enhanced handler to respondevent.respondWith(handler(requestContext))})

Standalone

While this middleware is intended to be used with @p-j/worker-eapi-template, you can also make use of it without any other EAPI dependency.

import{withCache}from'@p-j/eapi-middleware-cache'functionrequestHandler({ event, request, params }: RequestContext): Response{returnnewResponse('Hello World!')}// withCache is a Middleware Factory, here we get back a "pre configured" middlewareconstcacheForOneHour=withCache({cacheControl: 'public; max-age=3600'})// apply the middleware to the request handlerconstfinalRequestHandler=cacheForOneHour(requestHandler)addEventListener('fetch',(event)=>{// The only constraints is that the RequestHandler needs to take in a RequestContextconstrequestContext={ event,request: event.request,params: {}}// use the enhanced request handler to build the responseevent.respondWith(finaleRequestHandler(requestContext))})

API

Types

Contributing

Creating your own Middleware

As you create your own middlewares to extend your application a few things should be considered:

Making use of factory functions

By convention and to facilitate evolution without breaking changes, middleware are usually returned by a factory function, even if at the beginning they don't take in any options.

You may want to consider doing the same for your own middlewares, especially if you plan on opensourcing them.

Making use of the provided types

While nothing prevents you from using all the eapi-* packages in a Javascript project, using TypeScript will a whole lot to the table with very limited friction. You can check @p-j/worker-eapi-template as an example or inspiration for your own configuration.

@p-j/eapi-types contains types that are useful for building compatible functions. You may want to depend on it to ensure compatibility.

Making them discoverable

Feel free to use the eapi-* naming if your middleware is compatible, and the eapi tag on github & npm to ensure discoverability.

Contributing to this repository

Pull Request are welcome to add more middlewares, utility or request handler to this collection.

License

FOSSA Status

About

Edge API, building APIs on the Edge with Cloudflare Workers. Building blocks to work with the EAPI Worker Template.

Topics

Resources

Code of conduct

Stars

14 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages