Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@whi/essence

Package Overview
Dependencies
Maintainers
2
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@whi/essence

A Javascript implementation for packing/unpacking contextual responses

latest
Source
npmnpm
Version
0.1.3
Version published
Maintainers
2
Created
Source

Essence

A Javascript implementation for packing/unpacking contextual responses. This could be considered as a tool for the data translation layer.

Overview

The main feature of this tool is packing or unpacking messages.

Features

  • Parse JSON encoded Essence package
  • Create Essence package
  • Create Essence package from value
  • Create Essence package from a Javascript error

Install

npm i @whi/essence

Specification

See docs/specification.md

Basic Usage

Unpacking

Handling a success package

const { Translator } = require("@whi/essence");
const Interpreter = new Translator();

let pack = Interpreter.parse({
    "type": "success",
    "payload": data,
});

let payload = pack.value();
// payload == data

Handling an failure package

const { Translator } = require("@whi/essence");
const Interpreter = new Translator();

let pack = Interpreter.parse({
    "type": "failure",
    "payload": {
        "kind": "UserError",
        "error": "MissingInputError",
        "message": "You forgot the resource ID",
    },
});

let payload = pack.value();
// payload instanceof Error

Packing

Creating a success package

const { Translator } = require("@whi/essence");
const Interpreter = new Translator();

let pack = Interpreter.create( true );

JSON.stringify( pack, null, 4 );
// {
//     "type": "success",
//     "payload": true
// }

Creating a failure package

const { Translator } = require("@whi/essence");
const Interpreter = new Translator();

let pack = Interpreter.createFromError( "UserError", new TypeError("You broke it...") );

JSON.stringify( pack, null, 4 );
// {
//     "type": "failure",
//     "payload": {
//         "kind": "UserError",
//         "error": "TypeError",
//         "message": "You broke it",
//         "stack": [
//             "TypeError: You broke it",
//             "    at ...",
//             ...
//         ]
//     }
// }

API Reference

See docs/API.md

Contributing

See CONTRIBUTING.md

FAQs

Package last updated on 28 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts