0.0.3 • Published 6 years ago

abt-payload v0.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

ABT PayloadJS Library

This library provides a set of helper methods to handle, manipulate and abide to the RPC Payload Contract.

Installation

Installation is only available through the private repository. Ensure you have the proper access rights before using this library.

yarn add ssh://git@gitlab.com:realogy/abt/libraries/abt-payload.git

Usage

The library exposes higher level objects that allow to manipulate the following key contract items.

Please refer to the RPC Payload documentation for more information. Documentation

Response

The Response structure is the most basic object allowing to process a response.

Import

import {Response} from "abt-payload"

constructor Response(string responseData)

A constructor that will initialize a new Response.

method ToJSON()

Returns the response as JSON string.

method Validators...

A set of validator methods allowing to easily parse the response's result code.

  • Ok() - Returns true if the response was ok
  • NotFound() - Returns true if the response was notfound
  • Exception() - Returns any of the known exception codes.

method Item()

Returns the Item of the response

method Deflate()

Returns the response raw content.

Example

import {Response} from "abt-payload"

const response = new Response(data);

if(response.Ok()) {
    const item = response.Item();
}

Item

The Item object handles all Item related operations.

Import

import Item from "abt-payload"

constructor Item(string itemData)

A constructor that will initialize a new Item.

method Type()

Returns the RPC contract type as string.

method MustBe(string type)

Returns true if the Type of the Item is of the given type.

method Statement()

Returns the Statement object of the item.

method Values()

Returns the Values list of the item's Statement.

method Attributes()

Returns the Attributes list of the item's Statement.

method Items()

Returns the Items array of the item's Statement.

method ToStatementInputPayload()

Formats the Item's current Statement as proper structured RPC Payload Statement.

method ToItemInputPayload()

Formats the Item's current content as proper structured RPC Payload Item including the Statement.

method Deflate(bool valuesOnRoot)

This method converts a Item data payload to a flatten JSON object.

If set valuesOnRoot is set to true this method will move all values found in the list Values to the root of the output JSON object. The default is true.

Example

import Item from "abt-payload"

const itemDataJson = {
    "type": "Property",
    "statement": {
        "id": "123"
    } 
}

const item = new Item(itemDataJson);
const propertyId = item.MustBe("Property").id;

Items

The Items object handles all Items related operations. It is a container that helps to work with many Item's and exposes the same methods as described in Item.


List

The List object handles all List related operations. There are two common lists Values and Attributes which are exposed through the Item.

Import

import {List} from "abt-payload"

constructor List(string listData, string ref)

A constructor that will initialize a new List.

method Items()

Returns the RPC contract array of ListItem's.

method Flatten()

Flattens the RPC contract items into a flat JSONObject.

Example {name: Foo, value: Bar} becomes {Foo: Bar}.

method Unflatten()

Does the reverse of Flatten.

Example {Foo: Bar} becomes {name: Foo, value: Bar}

method Deflate()

Internal method that deflated dot value notation to JSON Objects.

method Enflate()

Internal method that enflates JSON objects to dot value notation.

Real World Usage

Here is an example how to process an Item for consumption by React Components.

/**
 * @param mustBeType
 * @param stateName
 * @returns {function(*, *): {item: *, error: (*|boolean)}}
 */
export const toItemData = (mustBeType = false, stateName = "item") => (
  source,
  data
) => {
  const response = new Response(data);
  return {
    error:
      (!response.Ok() ? response.Exception() : false) || data.error || false,
    [stateName]: response
      .Item()
      .MustBe(mustBeType)
      .Deflate()
  };
};
0.0.3

6 years ago