w-comor-hapi
Version:
A http communicator in nodejs and browser. Mapping functions from nodejs to other end points, like a simple RPC.
909 lines (275 loc) • 17.9 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>Global - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
<script src="scripts/nav.js" defer></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<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>
<nav >
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#WComorHapiClient">WComorHapiClient</a></li><li><a href="global.html#WComorHapiServer">WComorHapiServer</a></li></ul>
</nav>
<div id="main">
<h1 class="page-title">Global</h1>
<section>
<header>
<h2>
</h2>
</header>
<article>
<div class="container-overview">
<dl class="details">
</dl>
</div>
<h3 class="subsection-title">Methods</h3>
<h4 class="name" id="WComorHapiClient"><span class="type-signature"></span>WComorHapiClient<span class="signature">(opt)</span><span class="type-signature"> → {Promise}</span></h4>
<dl class="details">
<dt class="tag-description">Description:</dt>
<dd class="tag-description"><ul class="dummy"><li><p>建立http API使用者(Node.js與Browser)端物件</p></li></ul></dd>
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="WComorHapiClient.mjs.html">WComorHapiClient.mjs</a>, <a href="WComorHapiClient.mjs.html#line87">line 87</a>
</li></ul></dd>
</dl>
<h5 class="h5-examples">Example</h5>
<pre class="prettyprint"><code>import WComorHapiClient from 'w-comor-hapi/dist/ws-client-node.umd.js'
//opt
let opt = {
url: 'http://localhost:8080/api',
token: '*',
error: function(err) {
console.log('client: error:', err)
},
reconn: function() {
console.log('client: reconn')
},
}
//WComorHapiClient
new WComorHapiClient(opt)
.then(function(wo) {
console.log('client: funcs: ', wo)
function core(ps) {
wo.group.plus(ps)
.then(function(r) {
console.log('client: plus(' + JSON.stringify(ps) + ')=' + r)
})
.catch(function(err) {
console.log('client: plus: catch: ', err)
})
wo.group.div(ps)
.then(function(r) {
console.log('client: div(' + JSON.stringify(ps) + ')=' + r)
})
.catch(function(err) {
console.log('client: div: catch: ', err)
})
wo.add(ps)
.then(function(r) {
console.log(`client: add(${JSON.stringify(ps)})=${r}`)
})
.catch(function(err) {
console.log('client: add: catch: ', err)
})
wo.minu(ps)
.then(function(r) {
console.log(`client: minu(${JSON.stringify(ps)})=${r}`)
})
.catch(function(err) {
console.log('client: minu: catch: ', err)
})
}
let i = 100
setInterval(function() {
i += 1
core({
p1: i,
p2: 10,
})
}, 1000)
})
.catch(function(err) {
console.log('client: catch', err)
})</code></pre>
<h5 class="h5-parameters">Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>opt</code></td>
<td class="type">
<span class="param-type">Object</span>
</td>
<td class="description last"><p>輸入設定參數物件</p>
<h6>Properties</h6>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th>Default</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>url</code></td>
<td class="type">
<span class="param-type">String</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
<code>'http://localhost:8080/api'</code>
</td>
<td class="description last"><p>輸入http API伺服器網址,預設為'http://localhost:8080/api'</p></td>
</tr>
<tr>
<td class="name"><code>token</code></td>
<td class="type">
<span class="param-type">String</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
<code>'*'</code>
</td>
<td class="description last"><p>輸入使用者認證用token,預設為'*'</p></td>
</tr>
<tr>
<td class="name"><code>error</code></td>
<td class="type">
<span class="param-type">function</span>
</td>
<td class="attributes">
</td>
<td class="default">
</td>
<td class="description last"><p>輸入監聽error函數</p></td>
</tr>
<tr>
<td class="name"><code>reconn</code></td>
<td class="type">
<span class="param-type">function</span>
</td>
<td class="attributes">
</td>
<td class="default">
</td>
<td class="description last"><p>輸入監聽reconn函數</p></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<h5 class="h5-returns">Returns:</h5>
<div class="param-desc">
<p>回傳Promise,resolve為映射伺服器端可用函數之物件,各函數輸入皆為單一物件,各函數回傳皆為Promise,用resolve與reject處理回傳結果</p>
</div>
<dl class="param-type">
<dt>
Type
</dt>
<dd>
<span class="param-type">Promise</span>
</dd>
</dl>
<h4 class="name" id="WComorHapiServer"><span class="type-signature"></span>WComorHapiServer<span class="signature">(opt)</span><span class="type-signature"></span></h4>
<dl class="details">
<dt class="tag-description">Description:</dt>
<dd class="tag-description"><ul class="dummy"><li><p>建立http API伺服器</p></li></ul></dd>
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="WComorHapiServer.mjs.html">WComorHapiServer.mjs</a>, <a href="WComorHapiServer.mjs.html#line111">line 111</a>
</li></ul></dd>
</dl>
<h5 class="h5-examples">Example</h5>
<pre class="prettyprint"><code>import WComorHapiServer from 'w-comor-hapi/dist/w-comor-hapi-server.umd.js'
function random(min, max) {
return Math.floor(Math.random() * max) + min
}
let opt = {
port: 8080,
apiName: 'api',
authenticate: function(token) {
//使用token驗證使用者身份
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(true)
}, 1000)
})
},
filterFuncs: function(token, funcs) {
//使用token驗證使用者身份與過濾可用funcs
return new Promise(function(resolve, reject) {
funcs = funcs.filter(function(v) {
return v.indexOf('Hide') < 0
})
resolve(funcs)
})
},
onClientChange: function(conns, opt) {
console.log(`Server[port:${opt.port}] now conns: ${conns.length}`)
},
funcs: {
'group.plus': function({ p1, p2 }) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(p1 * p2)
}, random(100, 3000))
})
},
'group.div': function({ p1, p2 }) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(p1 / p2)
}, random(100, 3000))
})
},
'add': function({ p1, p2 }) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(p1 + p2)
}, random(100, 3000))
})
},
'addHide': function({ p1, p2 }) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(p1 + p2)
}, random(100, 3000))
})
},
'minu': function({ p1, p2 }) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(p1 - p2)
}, random(100, 3000))
})
},
},
routes: [
{
method: 'GET',
path: '/code',
handler: function (req, res) {
//console.log(req)
return 'get code query: ' + JSON.stringify(req.query)
//http://localhost:8080/code?a=1&bb=23.45
// => get code query: {"a":"1","bb":"23.45"}
}
},
],
}
new WComorHapiServer(opt)</code></pre>
<h5 class="h5-parameters">Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>opt</code></td>
<td class="type">
<span class="param-type">Object</span>
</td>
<td class="description last"><p>輸入設定參數物件</p>
<h6>Properties</h6>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th>Default</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>serverHapi</code></td>
<td class="type">
<span class="param-type">Object</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
<code>{}</code>
</td>
<td class="description last"><p>輸入hapi伺服器物件,若提供,本服務將自動加入api至route。使用外部hapi伺服器時,需開啟跨域功能,或是使用nginx反向代理轉入api請求</p></td>
</tr>
<tr>
<td class="name"><code>port</code></td>
<td class="type">
<span class="param-type">Integer</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
<code>8080</code>
</td>
<td class="description last"><p>輸入http API伺服器所在port,預設8080</p></td>
</tr>
<tr>
<td class="name"><code>apiName</code></td>
<td class="type">
<span class="param-type">String</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
<code>'api'</code>
</td>
<td class="description last"><p>輸入http API伺服器網址的api名稱,預設'api'</p></td>
</tr>
<tr>
<td class="name"><code>authenticate</code></td>
<td class="type">
<span class="param-type">function</span>
</td>
<td class="attributes">
</td>
<td class="default">
</td>
<td class="description last"><p>輸入使用者身份認證函數,供伺服器端驗證之用,函數會傳入使用者端連線之token參數,回傳為Promise,resolve(true)為驗證通過,resolve(false)為驗證不通過</p></td>
</tr>
<tr>
<td class="name"><code>funcs</code></td>
<td class="type">
<span class="param-type">Object</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
<code>{}</code>
</td>
<td class="description last"><p>輸入伺服器端供使用者端呼叫之函數物件,各key為函數名稱,對應value為函數本體。各函數之輸入需為單一物件,而各函數回傳皆為Promise,可通過resolve與reject回傳結果,預設{}</p></td>
</tr>
<tr>
<td class="name"><code>routes</code></td>
<td class="type">
<span class="param-type">Array</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
<code>[]</code>
</td>
<td class="description last"><p>輸入伺服器額外掛載routes陣列,預設[]</p></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Sun Mar 24 2024 22:16:24 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>
</body>
</html>