Skip to main content

Keywork

Module Overview

Keywork is a modular and opinionated library, providing structured guidence as your web app grows, without locking you into a specific pattern.

Features are categorized into modules that can be imported directly:

import { RequestRouter } from 'keywork/router'

const app = new RequestRouter()

app.get('/', () => 'Hello there! 👋')

export default app
tip

While Keywork has many modules, modern bundlers and transpilers such as ESBuild will intelligently bundle only the Keywork modules you include in your web app.

A full list of Keywork's modules are available in the navigation menu, however the most popular are shown below.

Usage

import * as Keywork from 'keywork'

Options Interfaces

Exports

Errors

Whether you're handling errors in your V8 Worker, Node.JS, or even the browser, Keywork includes error utilities that pair nicely with HTTP requests.

import { KeyworkResourceError, StatusCodes } from 'keywork/errors'

if (isLoggedIn(someUser)) {
throw new KeyworkResourceError("You must be logged in to do that", StatusCodes.UNAUTHORIZED)
}

Explore the Errors Module

Events

Renames and re-exports Keywork#Events

FileUtils

Keywork includes utilities for working with files, such as determining the MIME type while handling an incoming HTTP request.

import { FileUtils } from 'keywork'
import * as FileUtils 'keywork/files'

Explore the File Utilities Module

HTTP

Keywork includes utilities for working with incoming HTTP requests, and extends the native Request class for use with Cloudflare Workers

See each of HTTP's submodules for additional details.

import { CachableResponse, ErrorResponse, isRedirection, ...etc } from 'keywork/http'

Explore the HTTP Module

IDUtils

Renames and re-exports Keywork#IDUtils

Logger

Keywork includes an isomorphic logger available in both browser and worker environments.

import * as mod from 'keywork/logger'
const logger = new Logger('Todo API')

logger.info('Fetching todo', todoID)
logger.error('Unexpected error')

Explore the Logger Module

Middleware

Keywork includes support for middleware as instances of RequestRouter. Middleware can perform any task that of single router such as...

  • Executing any code
  • Make changes to the request and the response of another router
  • Terminate a request
  • Intercept a request to check for authentication
  • Call the next route handler in the stack
  • Automatic response compression
  • Cross-Origin Resource Sharing (CORS)

Explore the Middleware Module

ReactUtils

While optional, Keywork uses React as its primary HTML templating engine.

Explore the React Utilities Module

RouterUtils

Designed with familiarity in mind, the server-side routing API is inspired by Express.js, React Router, and the native Cloudflare Workers platform.

Explore the Router Module

import { RequestRouter } from 'keywork/router'

const app = new RequestRouter()

app.get('/', () => 'Hello there! 👋')

export default app

StringUtils

import {arrayBufferToString, arrayBufferToBase64, ...etc} from 'keywork/strings'

Explore the String Utilities Module

TimerUtils

import { TimerUtils } from 'keywork'
import * as mod from 'keywork/browser/timers'

Explore the Timer Utilities Module

URIUtils

Keywork uses JavaScript's built-in URL Pattern API to create pattern matchers. The syntax is based on path-to-regexp. Wildcards, named capture groups, regular groups, and group modifiers are all supported.

import { URIUtils } from 'keywork'
import * as mod 'keywork/uri'

Explore the URI Module