cf-atlas
v0.0.7
Published
API Service builder library for Cloudflare Workers.
Downloads
9
Readme
Atlas (for Cloudflare)
Atlas (for Cloudflare)* is a Typescript library for speeding up API-layer
development on Cloudflare workers. It allows endpoints to be defined compactly
using a Route
s which define HTTP endpoints for a given path and method. The
Router
will handle decoding the HTTP request body and encoding the
application response, using the Route.codec
, so the application only has to
provide a properly typed Route.handler
function.
Note (*): Not affiliated with Cloudflare.
Installation
npm install cf-atlas
Example
In this example, the application's getUserHandler
function can immediately
start working on fetching the user (no decoding UserId
), and when done, it
can return the user directly (no encoding), or an error.
interface UserId { Username: string; }
interface User { /* TODO */ }
async function getUserHandler(
ctx: ServerContext<AppCtxT, Env>, input: UserId,
): Promise<[User, Err]> { /* TODO */ }
let router = new Router<AppCtxT, Env>(NewAppCtx).
registerRoute<UserId, User>({
codec: codec.JSONCodec(),
method: "GET",
pattern: "/users",
middleware: [],
handler: getUserHandler,
})
export default { fetch: cffetch(router) };