onemsdk
Version:
ONEm developer library for NodeJS
638 lines (537 loc) • 21.6 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
index.js - Documentation
</title>
<link href="https://www.braintreepayments.com/images/favicon-ccda0b14.png" rel="icon" type="image/png">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
<link type="text/css" rel="stylesheet" href="styles/collapse.css">
<!-- start Mixpanel -->
<script type="text/javascript">(function(e,a){if(!a.__SV){var b=window;try{var c,l,i,j=b.location,g=j.hash;c=function(a,b){return(l=a.match(RegExp(b+"=([^&]*)")))?l[1]:null};g&&c(g,"state")&&(i=JSON.parse(decodeURIComponent(c(g,"state"))),"mpeditor"===i.action&&(b.sessionStorage.setItem("_mpcehash",g),history.replaceState(i.desiredHash||"",e.title,j.pathname+j.search)))}catch(m){}var k,h;window.mixpanel=a;a._i=[];a.init=function(b,c,f){function e(b,a){var c=a.split(".");2==c.length&&(b=b[c[0]],a=c[1]);b[a]=function(){b.push([a].concat(Array.prototype.slice.call(arguments,
0)))}}var d=a;"undefined"!==typeof f?d=a[f]=[]:f="mixpanel";d.people=d.people||[];d.toString=function(b){var a="mixpanel";"mixpanel"!==f&&(a+="."+f);b||(a+=" (stub)");return a};d.people.toString=function(){return d.toString(1)+".people (stub)"};k="disable time_event track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config reset people.set people.set_once people.increment people.append people.union people.track_charge people.clear_charges people.delete_user".split(" ");
for(h=0;h<k.length;h++)e(d,k[h]);a._i.push([b,c,f])};a.__SV=1.2;b=e.createElement("script");b.type="text/javascript";b.async=!0;b.src="undefined"!==typeof MIXPANEL_CUSTOM_LIB_URL?MIXPANEL_CUSTOM_LIB_URL:"file:"===e.location.protocol&&"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js".match(/^\/\//)?"https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js":"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js";c=e.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c)}})(document,window.mixpanel||[]);
mixpanel.init("1919205b2da72e4da3b9b6639b444d59");</script>
<!-- end Mixpanel -->
</head>
<body>
<svg style="display: none;">
<defs>
<symbol id="linkIcon" fill="#706d77" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M0 0h24v24H0z" fill="none"/>
<path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/>
</symbol>
</defs>
</svg>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<div class="top-nav-wrapper">
<ul>
<li >
<a href="index.html">
<svg fill="#6D6D6D" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/>
<path d="M0 0h24v24H0z" fill="none"/>
</svg>
</a>
</li>
</ul>
</div>
<nav>
<h3 class="reference-title">
ONEm Node.js SDK
</h3>
<h3>Classes</h3><ul><li id="ATag-nav"><a href="global.html#ATag">ATag</a><ul class='methods'><li data-type="method" id="ATag-getAttributes-nav"><a href="global.html#ATag#.getAttributes">getAttributes</a></li></ul></li><li id="ATagAttrs-nav"><a href="ATagAttrs.html">ATagAttrs</a></li><li id="BrTag-nav"><a href="global.html#BrTag">BrTag</a></li><li id="FooterTag-nav"><a href="global.html#FooterTag">FooterTag</a></li><li id="Form-nav"><a href="global.html#Form">Form</a><ul class='methods'><li data-type="method" id="Form-fromTag-nav"><a href="global.html#Form#.fromTag">fromTag</a></li></ul></li><li id="FormItemContent-nav"><a href="global.html#FormItemContent">FormItemContent</a><ul class='methods'><li data-type="method" id="FormItemContent-fromTag-nav"><a href="global.html#FormItemContent#.fromTag">fromTag</a></li></ul></li><li id="FormItemMenu-nav"><a href="global.html#FormItemMenu">FormItemMenu</a><ul class='methods'><li data-type="method" id="FormItemMenu-fromTag-nav"><a href="global.html#FormItemMenu#.fromTag">fromTag</a></li></ul></li><li id="FormItemMenuItem-nav"><a href="global.html#FormItemMenuItem">FormItemMenuItem</a><ul class='methods'><li data-type="method" id="FormItemMenuItem-fromTag-nav"><a href="global.html#FormItemMenuItem#.fromTag">fromTag</a></li></ul></li><li id="FormItemMenuMeta-nav"><a href="global.html#FormItemMenuMeta">FormItemMenuMeta</a></li><li id="FormMeta-nav"><a href="global.html#FormMeta">FormMeta</a></li><li id="FormTag-nav"><a href="global.html#FormTag">FormTag</a></li><li id="FormTagAttrs-nav"><a href="FormTagAttrs.html">FormTagAttrs</a></li><li id="HeaderTag-nav"><a href="global.html#HeaderTag">HeaderTag</a></li><li id="InputTag-nav"><a href="global.html#InputTag">InputTag</a><ul class='methods'><li data-type="method" id="InputTag-getAttributes-nav"><a href="global.html#InputTag#.getAttributes">getAttributes</a></li></ul></li><li id="InputTagAttrs-nav"><a href="InputTagAttrs.html">InputTagAttrs</a></li><li id="LiTag-nav"><a href="global.html#LiTag">LiTag</a></li><li id="LiTagAttrs-nav"><a href="LiTagAttrs.html">LiTagAttrs</a></li><li id="Menu-nav"><a href="global.html#Menu">Menu</a><ul class='methods'><li data-type="method" id="Menu-fromTag-nav"><a href="global.html#Menu#.fromTag">fromTag</a></li></ul></li><li id="MenuItem-nav"><a href="global.html#MenuItem">MenuItem</a><ul class='methods'><li data-type="method" id="MenuItem-fromTag-nav"><a href="global.html#MenuItem#.fromTag">fromTag</a></li></ul></li><li id="MenuMeta-nav"><a href="global.html#MenuMeta">MenuMeta</a></li><li id="PTag-nav"><a href="global.html#PTag">PTag</a></li><li id="Response-nav"><a href="global.html#Response">Response</a><ul class='methods'><li data-type="method" id="Response-fromTag-nav"><a href="global.html#Response#.fromTag">fromTag</a></li></ul></li><li id="SectionTag-nav"><a href="global.html#SectionTag">SectionTag</a></li><li id="SectionTagAttrs-nav"><a href="SectionTagAttrs.html">SectionTagAttrs</a></li><li id="Tag-nav"><a href="global.html#Tag">Tag</a><ul class='methods'><li data-type="method" id="Tag-fromNode-nav"><a href="global.html#Tag#.fromNode">fromNode</a></li><li data-type="method" id="Tag-getAttributes-nav"><a href="global.html#Tag#.getAttributes">getAttributes</a></li></ul></li><li id="UlTag-nav"><a href="global.html#UlTag">UlTag</a></li></ul><h3 id="global-nav">Global</h3><ul><li><a href="global.html#loadHtml">loadHtml</a></li><li><a href="global.html#loadTemplate">loadTemplate</a></li><li><a href="global.html#tagClsMap">tagClsMap</a></li></ul>
</nav>
<div id="main">
<h1 class="page-title">
index.js
</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>const snakecase = require('snakecase-keys');
const tags = require('./tag');
const UlTag = tags.UlTag,
SectionTag = tags.SectionTag,
FormTag = tags.FormTag,
LiTag = tags.LiTag,
ATag = tags.ATag,
HeaderTag = tags.HeaderTag,
FooterTag = tags.FooterTag,
InputTag = tags.InputTag;
/**
* @typedef {object} Form
* @property {('form')} type
* @property {string|undefined} header header value
* @property {string|undefined} footer footer value
* @property {FormMeta} meta
* @property {('get'|'post'|'put'|'delete')} method
* @property {string} path
* @property {FormItemContent | FormItemMenu} body form body object
*/
/**
* @typedef {object} FormMeta
* @property {boolean|undefined} completionStatusShow
* @property {boolean|undefined} completionStatusInHeader
* @property {boolean|undefined} confirmationNeeded
*/
/**
* @typedef {object} FormItemContent
* @property {('string'|'date'|'datetime')} type
* @property {string} name
* @property {string} description
* @property {string|undefined} header
* @property {string|undefined} footer
*/
/**
* @typedef {Object} FormItemMenu
* @property {('form-menu')} type
* @property {Array<FormItemMenuItem>} body
* @property {string} name
* @property {string|undefined} header
* @property {string|undefined} footer
* @property {FormItemMenuMeta|undefined} meta
*/
/**
* @typedef {Object} FormItemMenuMeta
* @property {boolean} autoSelect
* @property {boolean} multiSelect
* @property {boolean} numbered
*/
/**
* @typedef {object} FormItemMenuItem
* @property {('option'|'content')} type
* @property {string} description
* @property {string|undefined} value
*/
/**
* @typedef {Object} Menu
* @property {('menu')} type
* @property {Array<MenuItem>} body
* @property {string|undefined} header
* @property {string|undefined} footer
* @property {MenuMeta|undefined} meta
*/
/**
* @typedef {Object} MenuMeta
* @property {boolean} autoSelect
*/
/**
* @typedef {object} MenuItem
* @property {('option'|'content')} type indicating menu option or plain content
* @property {string} description
* @property {string|undefined} path For menu options only. Path to be used for HTTP callback (added to base path configured in app's settings in developer portal)
* @property {('get'|'post'|'put'|'delete'|undefined)} method=get For menu options only. HTTP method that should be used when redirecting after successful menu option submission
*/
/**
* @typedef {Object} Response
* @property {('form'|'menu')} contentType
* @property {Form | Menu} content
*/
/**
* Instantiates a new Form
* @param {Array<FormItemContent|FormItemMenu>} body
* @param {('GET'|'POST'|'PUT'|'DELETE')} method='POST'
* @param {string} path
* @param {string|undefined} header
* @param {string|undefined} footer
* @param {FormMeta|undefined} meta
* @constructor
*/
function Form(body, method, path, header, footer, meta) {
if (!body || !path) {
throw Error('(body, path) are mandatory');
}
this.type = 'form';
this.body = body;
this.method = method || 'POST';
this.path = path;
this.header = header || null;
this.footer = footer || null;
this.meta = meta || null;
}
/**
* Creates a Form from a FormTag
* @param {FormTag} formTag
* @returns {Form}
*/
Form.fromTag = function (formTag) {
let body = [];
formTag.children.forEach(function (sectionTag) {
let childType = FormItemContent;
for (let i = 0; i < sectionTag.children.length; i++) {
if (sectionTag.children[i] instanceof UlTag) {
childType = FormItemMenu;
break;
}
}
body.push(childType.fromTag(sectionTag));
});
return new Form(
body,
formTag.attrs.method,
formTag.attrs.action,
formTag.attrs.header,
formTag.attrs.footer,
new FormMeta(
formTag.attrs.completionStatusShow,
formTag.attrs.completionStatusInHeader,
formTag.attrs.confirmationNeeded
)
);
};
/**
* Instantiates a new FormMeta
* @param {boolean|undefined} completionStatusShow
* @param {boolean|undefined} completionStatusInHeader
* @param {boolean|undefined} confirmationNeeded
* @constructor
*/
function FormMeta(completionStatusShow, completionStatusInHeader, confirmationNeeded) {
if (typeof completionStatusShow === 'boolean') {
this.completionStatusShow = completionStatusShow;
} else {
this.completionStatusShow = completionStatusShow !== undefined;
}
if (typeof completionStatusInHeader === 'boolean') {
this.completionStatusInHeader = completionStatusInHeader;
} else {
this.completionStatusInHeader = completionStatusInHeader !== undefined;
}
if (typeof confirmationNeeded === 'boolean') {
this.confirmationNeeded = confirmationNeeded;
} else {
this.confirmationNeeded = confirmationNeeded !== undefined;
}
}
/**
* Instantiates a new FormItemContent
* @param {('string'|'date'|'datetime')} type
* @param {string} name
* @param {string} description
* @param {string|undefined} header
* @param {string|undefined} footer
* @constructor
*/
function FormItemContent(type, name, description, header, footer) {
this.type = type;
this.name = name;
this.description = description;
this.header = header || null;
this.footer = footer || null;
}
/**
* Creates a FormItemContent from a SectionTag
* @param {SectionTag} sectionTag
* @returns {FormItemContent}
*/
FormItemContent.fromTag = function (sectionTag) {
let type,
header,
footer;
sectionTag.children.forEach(function (child) {
if (child instanceof InputTag) {
type = child.attrs.type;
}
});
if (!type) {
throw Error('When <section> plays the role of a form item content, ' +
'it must contain a <input/>')
}
// Translate the InputTag type to FormItemContent type
switch (type) {
case 'text':
type = 'string';
break;
case 'date':
case 'datetime':
// These are the same
break;
default:
throw Error(`<input/> type "#{type}" is not supported`);
}
if (sectionTag.children[0] instanceof HeaderTag) {
header = sectionTag.children[0].toString();
}
if (sectionTag.children[sectionTag.children.length - 1] instanceof FooterTag) {
footer = sectionTag.children[sectionTag.children.length - 1].toString();
}
return new FormItemContent(
type,
sectionTag.attrs.name,
sectionTag.toString(true, true),
header || sectionTag.attrs.header,
footer || sectionTag.attrs.footer,
);
};
/**
* Instantiates a new FormItemMenu
* @param {Array<FormItemMenuItem>} body
* @param {string} name
* @param {string|undefined} header
* @param {string|undefined} footer
* @param {FormItemMenuMeta|undefined} meta
* @constructor
*/
function FormItemMenu(body, name, header, footer, meta) {
this.type = 'form-menu';
this.body = body;
this.name = name;
this.header = header || null;
this.footer = footer || null;
this.meta = meta || null;
}
/**
* Creates a FormItemMenu from a SectionTag
* @param {SectionTag} sectionTag
* @returns {FormItemMenu}
*/
FormItemMenu.fromTag = function (sectionTag) {
let body = [],
header,
footer;
sectionTag.children.forEach(function (child) {
if (child instanceof UlTag) {
child.children.forEach(function (liTag) {
body.push(FormItemMenuItem.fromTag(liTag));
})
} else if (child instanceof HeaderTag) {
header = child.toString();
} else if (child instanceof FooterTag) {
footer = child.toString();
} else {
body.push(FormItemMenuItem.fromTag(child));
}
});
// Discard all the menu items evaluated to false (eg: those with no description)
body = body.filter(function (menuItem) {
return menuItem;
});
return new FormItemMenu(
body,
sectionTag.attrs.name,
header || sectionTag.attrs.header,
footer || sectionTag.attrs.footer,
new FormItemMenuMeta(
sectionTag.attrs.autoSelect,
sectionTag.attrs.multiSelect,
sectionTag.attrs.numbered,
)
);
};
/**
* Instantiates a new FormItemMenuMeta
* @param {boolean} autoSelect
* @param {boolean} multiSelect
* @param {boolean} numbered
* @constructor
*/
function FormItemMenuMeta(autoSelect, multiSelect, numbered) {
this.autoSelect = autoSelect;
this.multiSelect = multiSelect;
this.numbered = numbered;
}
/**
* Instantiates a new FormItemMenuItem
* @param {('option'|'content')} type
* @param {string} description
* @param {string|undefined} value
* @constructor
*/
function FormItemMenuItem(type, description, value) {
this.type = type;
this.description = description;
this.value = value || null;
}
/**
* Creates a FormItemMenuItem from a SectionTag's child
* @param tag
* @returns {FormItemMenuItem}
*/
FormItemMenuItem.fromTag = function (tag) {
let description,
type = 'content',
value;
if (typeof tag === 'string') {
description = tag;
} else {
description = tag.toString();
}
if (!description) {
// Ignore the menu items without text
return undefined;
}
if (tag instanceof LiTag && tag.attrs.value) {
type = 'option';
value = tag.attrs.value;
}
return new FormItemMenuItem(type, description, value);
};
/**
* Instantiates a new Menu
* @param {Array<MenuItem>} body
* @param {string|undefined} header
* @param {string|undefined} footer
* @param {MenuMeta} meta
* @constructor
*/
function Menu(body, header, footer, meta) {
this.type = "menu";
this.body = body;
this.header = header || null;
this.footer = footer || null;
this.meta = meta || null;
}
/**
* Creates a Menu from a SectionTag
* @param {SectionTag} sectionTag
* @returns {Menu}
*/
Menu.fromTag = function (sectionTag) {
let body = [],
header,
footer;
sectionTag.children.forEach(function (child) {
if (child instanceof UlTag) {
child.children.forEach(function (liTag) {
body.push(MenuItem.fromTag(liTag));
});
} else if (child instanceof HeaderTag) {
header = child.toString();
} else if (child instanceof FooterTag) {
footer = child.toString();
} else {
body.push(MenuItem.fromTag(child));
}
});
// Discard all the menu items evaluated to false (eg: those with no description)
body = body.filter(function (menuItem) {
return menuItem;
});
return new Menu(
body,
header || sectionTag.attrs.header,
footer || sectionTag.attrs.footer,
new MenuMeta(sectionTag.attrs.autoSelect)
);
};
/**
* Instantiates a new MenuMeta
* @param {boolean} autoSelect
* @constructor
*/
function MenuMeta(autoSelect) {
this.autoSelect = autoSelect;
}
/**
* Instantiates a new MenuItem
* @param {('option'|'content')} type
* @param {string} description
* @param {('GET'|'POST'|'PUT'|'DELETE'|undefined)} method
* @param {string|undefined} path
* @constructor
*/
function MenuItem(type, description, method, path) {
this.type = type;
this.description = description;
this.method = method || null;
this.path = path || null;
}
/**
* Creates a MenuItem from a SectionTag's child
* @param {LiTag|BrTag|PTag|LabelTag|InputTag|string} tag
* @returns {MenuItem}
*/
MenuItem.fromTag = function (tag) {
let description,
method,
path,
type = 'content';
if (typeof tag === 'string') {
description = tag;
} else {
description = tag.toString();
}
if (!description) {
// Ignore the menu items without text
return undefined;
}
if (tag instanceof LiTag && tag.children[0] instanceof ATag) {
const aTag = tag.children[0];
method = aTag.attrs.method;
path = aTag.attrs.href;
type = 'option';
}
return new MenuItem(type, description, method, path);
};
/**
* Instantiates a Response object
* @param {Form|Menu} content
* @constructor
*/
function Response(content) {
if (!content) {
throw Error('content is mandatory');
}
let contentType;
if (content instanceof Form) {
contentType = 'form';
} else if (content instanceof Menu) {
contentType = 'menu';
} else {
throw Error(`Cannot create Response from ${content.constructor}`)
}
this.contentType = contentType;
this.content = content;
}
/**
* Creates a Response from a FormTag or SectionTag
* @param {FormTag|SectionTag} tag
* @returns {Response}
*/
Response.fromTag = function (tag) {
if (tag instanceof FormTag) {
return new Response(Form.fromTag(tag));
} else if (tag instanceof SectionTag) {
return new Response(Menu.fromTag(tag));
} else {
throw Error(`Cannot create response from ${tag.tagName} tag`)
}
};
Response.prototype.toJSON = function () {
return snakecase(this);
};
exports.Form = Form;
exports.Response = Response;
exports.Menu = Menu;
exports.MenuItem = MenuItem;
exports.FormItemMenu = FormItemMenu;
exports.FormItemMenuItem = FormItemMenuItem;
exports.FormItemContent = FormItemContent;
exports.FormMeta = FormMeta;
exports.MenuMeta = MenuMeta;
exports.FormItemMenuMeta = FormItemMenuMeta;
exports.parser = require('./parser');
exports.tags = require('./tag');
exports.config = require('./config');
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a>
</footer>
<script src="scripts/linenumber.js"></script>
<script src="scripts/pagelocation.js"></script>
</body>
</html>