0.7.3 • Published 4 months ago

worktop v0.7.3

Weekly downloads
15
License
MIT
Repository
github
Last release
4 months ago

Features

  • Super lightweight
  • First-class TypeScript support
  • Custom Middleware Support
  • Well-organized submodules for à la carte functionality*
  • Includes Router with support for pattern definitions
  • Familiar Request-Response handler API
  • Supports async/await handlers
  • Fully treeshakable

*More to come!

Install

$ npm install --save worktop

Usage

Check out /examples for a list of working demos!

import { Router } from 'worktop';
import * as Cache from 'worktop/cache';
import { uid as toUID } from 'worktop/utils';
import { read, write } from 'worktop/kv';
import type { KV } from 'worktop/kv';

declare var DATA: KV.Namespace;

interface Message {
  id: string;
  text: string;
  // ...
}

// Initialize
const API = new Router();


API.add('GET', '/messages/:id', async (req, res) => {
  // Pre-parsed `req.params` object
  const key = `messages::${req.params.id}`;

  // Assumes JSON (can override)
  const message = await read<Message>(DATA, key);

  // Alter response headers directly
  res.setHeader('Cache-Control', 'public, max-age=60');

  // Smart `res.send()` helper
  // ~> automatically stringifies JSON objects
  // ~> auto-sets `Content-Type` & `Content-Length` headers
  res.send(200, message);
});


API.add('POST', '/messages', async (req, res) => {
  try {
    // Smart `req.body` helper
    // ~> parses JSON header as JSON
    // ~> parses form-like header as FormData, ...etc
    var input = await req.body<Message>();
  } catch (err) {
    return res.send(400, 'Error parsing request body');
  }

  if (!input || !input.text.trim()) {
    return res.send(422, { text: 'required' });
  }

  const value: Message = {
    id: toUID(16),
    text: input.text.trim(),
    // ...
  };

  // Assumes JSON (can override)
  const key = `messages::${value.id}`;
  const success = await write<Message>(DATA, key, value);
  //    ^ boolean

  // Alias for `event.waitUntil`
  // ~> queues background task (does NOT delay response)
  req.extend(
    fetch('https://.../logs', {
      method: 'POST',
      headers: { 'content-type': 'application/json '},
      body: JSON.stringify({ success, value })
    })
  );

  if (success) res.send(201, value);
  else res.send(500, 'Error creating record');
});


API.add('GET', '/alive', (req, res) => {
  res.end('OK'); // Node.js-like `res.end`
});


// Attach "fetch" event handler
// ~> use `Cache` for request-matching, when permitted
// ~> store Response in `Cache`, when permitted
Cache.listen(API.run);

API

Module: worktop

View worktop API documentation

The main module – concerned with routing. This is core of most applications. Exports the Router class.

Module: worktop/kv

View worktop/kv API documentation

The worktop/kv submodule contains all classes and utilities related to Workers KV.

Module: worktop/cache

View worktop/cache API documentation

The worktop/cache submodule contains all utilities related to Cloudflare's Cache.

Module: worktop/durable

View worktop/durable API documentation

The worktop/durable submodule includes native types for Durable Objects as well as an Actor abstract class that provides a blueprint for authoring a Durable Object that handles WebSocket connections.

Note: Durable Objects can only be used with the Module Worker format. You must integrate the Router with the worktop/modules submodule.

Module: worktop/modules

View worktop/modules API documentation

The worktop/modules submodule includes two utilities related to the Module Workers format. Most notably, it exports a listen method to conform existing Router applications from Service Worker to Module Worker format.

Module: worktop/request

View worktop/request API documentation

The worktop/request submodule contains the ServerRequest class, which provides an interface similar to the request instance(s) found in most other Node.js frameworks.

Note: This module is used internally and will (very likely) never be imported by your application.

Module: worktop/response

View worktop/response API documentation

The worktop/response submodule contains the ServerResponse class, which provides an interface similar to the IncomingMessage (aka, "response") object that Node.js provides.

Note: This module is used internally and will (very likely) never be imported by your application.

Module: worktop/base64

View worktop/base64 API documentation

The worktop/base64 submodule contains a few utilities related to the Base 64 encoding.

Module: worktop/cookie

View worktop/cookie API documentation

The worktop/cookie submodule contains parse and stringify utilities for dealing with cookie header(s).

Module: worktop/cors

View worktop/cors API documentation

The worktop/cors submodule offers utilities for dealing with Cross-Origin Resource Sharing (CORS) headers.

Module: worktop/crypto

View worktop/crypto API documentation

The worktop/crypto submodule is a collection of cryptographic functionalities.

Module: worktop/utils

View worktop/utils API documentation

The worktop/utils submodule is a collection of standalone, general-purpose utilities that you may find useful. These may include – but are not limited to – hashing functions and unique identifier generators.

Module: worktop/ws

View worktop/ws API documentation

The worktop/ws submodule contains the WebSocket and WebSocketPair class definitions, as well as two middleware handlers for validating and/or setting up a SocketHandler for the WebSocket connection.

License

MIT © Luke Edwards

0.8.0-next.17

4 months ago

0.8.0-next.18

4 months ago

0.8.0-next.16

5 months ago

0.8.0-next.15

1 year ago

0.8.0-next.13

2 years ago

0.8.0-next.14

2 years ago

0.8.0-next.12

2 years ago

0.8.0-next.10

2 years ago

0.8.0-next.9

2 years ago

0.8.0-next.11

2 years ago

0.8.0-next.8

2 years ago

0.8.0-next.6

3 years ago

0.8.0-next.7

2 years ago

0.8.0-next.4

3 years ago

0.8.0-next.5

3 years ago

0.8.0-next.2

3 years ago

0.8.0-next.3

3 years ago

0.8.0-next.0

3 years ago

0.8.0-next.1

3 years ago

0.7.3

3 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.5.0

3 years ago

0.7.0

3 years ago

0.6.1

3 years ago

0.5.2

3 years ago

0.6.0

3 years ago

0.5.1

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

4 years ago

0.0.0

4 years ago