Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/cav/pack.ts

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

Functions

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.

Interfaces

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.

Type Aliases

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

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.

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 instance. 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.