Skip to main content
Module

x/cav/browser/mod.ts

A server framework for Deno
Go to Latest
import * as cav from "https://deno.land/x/cav@0.0.8/browser/mod.ts";

Classes

An error class for describing exceptions during HTTP processing.

Functions

Constructs a new Client tied to a given base URL. The provided set of packers will be used everywhere that data is packed/unpacked when using this client, including web sockets.

Utility function used in the packing functions that determines if an object is a plain object or not. Because this is such a common operation when checking and serializing unknown objects, it's being exported as part of the API.

Packs a value recursively until it's JSON-compatible. Packers can be plugged in to extend the accepted types beyond what Cav supports by default as well as the packers registered with usePackers(). Referential equality will be preserved whenever the same object or symbol value is encountered more than once. If a value isn't recognized by any of the used packers or the default packers, an error is thrown.

Packs a value into a type that is compatible with a Response BodyInit, making it easy to pack values for sending to an external host/client via HTTP. If a provided value is already compatible with BodyInit, it will be returned with an appropriate mime type, skipping the packing process. During packing, this function extends the default supported types to include Blobs and Files. If a Blob is encountered during packing, the resulting body will be a multipart FormData that encodes the shape of the input as well as the blobs that were encountered. Otherwise, a regular JSON string will be returned. Blobs and Files can be placed anywhere on the input value, even if they are nested.

Constructs a Packer. This simply returns the first argument, it's only used for type annotations.

Packs the value into a JSON string. This function is a one-liner:

Unpacks a value packed with pack() into the original value. Referential equality will be restored on the output object. An error will be thrown if a value was packed with an unknown/unused packer.

Unpacks a Request or Response object whose body was packed with packBody(). Any packers used outside of the library defaults during packing need to be provided here as well, or an error may be thrown.

Unpacks the JSON string into the original value. Any packers used during packing (outside of registered packers and the Cav defaults) need to be provided here as well, or an error may be thrown. This function is a one-liner:

Registers the Packers to be used as defaults in addition to the library defaults for the top-level packing functions. Falsy properties are skipped. If any packer keys conflict with the packers that are already in use, an error is thrown. Returns the input packers argument.

Wraps a regular WebSocket with packing functionality and type support.

Interfaces

Client type representing an Rpc endpoint. Uses the Rpc type definition to determine what the expected arguments and response types are.

Initializer arguments for constructing HttpErrors.

A group of functions used to recognize, pack, and unpack objects and special values that are not strings, basic numbers, booleans, or nulls into objects that are JSON compatible.

Cav's WebSocket wrapper interface.

Arguments provided to a SocketHandler. There are four event types: "open", "close", "message", "error". Which properties are available depends on the event type.

Initializer options to use when upgrading a request into a web socket using the upgradeWebSocket function.

Type Aliases

Type alias representing a Packer with any input or output type. Useful for type constraints.

A Proxied function that wraps fetch() with a tailored process for making requests to a Cav server. Each property access on the function itself returns a new Client that extends the URL of the original Client. The periods represent path dividers and the accessed properties are path segments, like this: client("http://localhost/base").nested["pa.th"]() will result in a request to "http://localhost/base/nested/pa.th".

Uses the RpcInit type imported from the server to determine what shape the arguments should be in when making requests to a given Rpc.

A Record of Packers. Packer keys are used to tag packed values on the output JSON, which is required in order to correctly unpack the value on the other side.

Handler type for the various socket event listeners on the SocketInit.

A Packer's unpack() function receives the raw packed JSON value as its first argument and this registration function as the second. Functions registered with WhenDone will be run last-in-first-out (stack order) when the raw JSON has been processed into the final object instances. WhenDone functions are needed whenever the packed data is more complex than simple JSON values, for example when referential equality needs to be maintained or when the pack function returns anything that needs to be re-packed by some other packer. Referenced objects may not be fully initialized when the registered function is called, but its instance will be instantiated so that references can be fixed.