Keywork
Module Overview
Usage
- Node
- Deno
- Browser/ESM
import * as Keywork from 'keywork'
import * as Keywork from 'https://deno.land/x/keywork'
let Keywork = await import('https://esm.sh/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.
- Node
- Browser/ESM
import { KeyworkResourceError, StatusCodes } from 'keywork/errors'
if (isLoggedIn(someUser)) {
throw new KeyworkResourceError("You must be logged in to do that", StatusCodes.UNAUTHORIZED)
}
let { KeyworkResourceError, StatusCodes } = await import('https://esm.sh/keywork/errors')
if (isLoggedIn(someUser)) {
throw new KeyworkResourceError("You must be logged in to do that", StatusCodes.UNAUTHORIZED)
}
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'
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'
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')
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)
ReactUtils
While optional, Keywork uses React as its primary HTML templating engine.
RouterUtils
Designed with familiarity in mind, the server-side routing API is inspired by Express.js, React Router, and the native Cloudflare Workers platform.
- Cloudflare Workers
- Deno
- Browser/ESM
import { RequestRouter } from 'keywork/router'
const app = new RequestRouter()
app.get('/', () => 'Hello there! 👋')
export default app
import { RequestRouter } from 'https://deno.land/x/keywork/modules/router/mod.ts'
import { serve } from 'https://deno.land/[email protected]/http/server.ts'
const app = new RequestRouter()
serve((request) => app.fetch(request))
import { RequestRouter } from 'https://esm.sh/keywork/router'
const app = new RequestRouter()
app.get('/', () => 'Hello there! 👋')
StringUtils
import {arrayBufferToString, arrayBufferToBase64, ...etc} from 'keywork/strings'
TimerUtils
import { TimerUtils } from 'keywork'
import * as mod from 'keywork/browser/timers'
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'
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:
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.