w-web-api
Version:
A web service for APIs.
898 lines (295 loc) • 17.7 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>WWebApi - 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>Classes</h3><ul><li><a href="WWebApi.html">WWebApi</a></li></ul>
</nav>
<div id="main">
<h1 class="page-title">WWebApi</h1>
<section>
<header>
<h2>
WWebApi
</h2>
</header>
<article>
<div class="container-overview">
<h4 class="name" id="WWebApi"><span class="type-signature"></span>new WWebApi<span class="signature">(WOrm, url, db, getUserByToken, verifyClientUser, verifyAppUser, opt<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Object}</span></h4>
<dl class="details">
<dt class="tag-description">Description:</dt>
<dd class="tag-description"><ul class="dummy"><li><p>API伺服器</p></li></ul></dd>
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="WWebApi.mjs.html">WWebApi.mjs</a>, <a href="WWebApi.mjs.html#line131">line 131</a>
</li></ul></dd>
</dl>
<h5 class="h5-examples">Example</h5>
<pre class="prettyprint"><code>import WOrm from 'w-orm-lmdb/src/WOrmLmdb.mjs'
import WWebApi from './server/WWebApi.mjs'
import getSettings from './g.getSettings.mjs'
//st
let st = getSettings()
let url = st.dbUrl
let db = st.dbName
let opt = {
useCheckUser: false,
getUserById: null,
useExcludeWhenNotAdmin: false,
serverPort: 11005,
subfolder: '', //mapi
urlRedirect: 'https://www.google.com/', //本機測試時得先編譯, 再瀏覽: http://localhost:11005/
webName: {
'eng': 'API Service',
'cht': 'API管理系統',
},
webDescription: {
'eng': 'A web service package as methods to send requests to and receive responses from an API.',
'cht': 'A web service package as methods to send requests to and receive responses from an API.',
},
webLogo: 'data:image/svg+xml;base64,...',
}
let getUserByToken = async (token) => {
// return {} //測試無法登入
if (token === '{token-for-application}') { //提供外部應用系統作為存取使用者
return {
id: 'id-for-application',
name: 'application',
email: 'application@example.com',
isAdmin: 'y',
}
}
if (token === 'sys') { //開發階段w-ui-loginout自動給予browser使用者(且位於localhost)的token為sys
return {
id: 'id-for-admin',
name: '測試者',
email: 'admin@example.com',
isAdmin: 'y',
}
}
console.log('invalid token', token)
console.log('於生產環境時得加入SSO等驗證token機制')
return {}
}
let verifyClientUser = (user, from) => {
console.log('verifyClientUser/user', user)
// return false //測試無法登入
console.log('於生產環境時得加入限制瀏覽器使用者身份機制')
return user.isAdmin === 'y' //測試僅系統管理者使用
}
let verifyAppUser = (user, from) => {
console.log('verifyAppUser/user', user)
// return false //測試無法登入
console.log('於生產環境時得加入限制應用程式使用者身份機制')
return user.isAdmin === 'y' //測試僅系統管理者使用
}
//WWebApi
let instWWebApi = WWebApi(WOrm, url, db, getUserByToken, verifyClientUser, verifyAppUser, opt)
instWWebApi.on('error', (err) => {
console.log(err)
})</code></pre>
<h5 class="h5-parameters">Parameters:</h5>
<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>WOrm</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>輸入資料庫ORM函數</p></td>
</tr>
<tr>
<td class="name"><code>url</code></td>
<td class="type">
<span class="param-type">String</span>
</td>
<td class="attributes">
</td>
<td class="default">
</td>
<td class="description last"><p>輸入資料庫連線字串,例如w-orm-lmdb為'./db',或w-orm-mongodb為'mongodb://username:password@$127.0.0.1:27017'</p></td>
</tr>
<tr>
<td class="name"><code>db</code></td>
<td class="type">
<span class="param-type">String</span>
</td>
<td class="attributes">
</td>
<td class="default">
</td>
<td class="description last"><p>輸入資料庫名稱字串</p></td>
</tr>
<tr>
<td class="name"><code>getUserByToken</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,通過此函數處理後並回傳使用者資訊物件,並至少須提供'id'、'email'、'name'、'isAdmin'欄位,且'isAdmin'限輸入'y'或'n',且輸入'y'時會複寫權限系統該使用者之'isAdmin'欄位值</p></td>
</tr>
<tr>
<td class="name"><code>verifyClientUser</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>輸入驗證瀏覽使用者身份之處理函數,函數會傳入使用者資訊物件,通過此函數識別後回傳布林值,允許使用者回傳true,反之回傳false</p></td>
</tr>
<tr>
<td class="name"><code>verifyAppUser</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>輸入驗證應用程序使用者身份之處理函數,函數會傳入使用者資訊物件,通過此函數識別後回傳布林值,允許使用者回傳true,反之回傳false</p></td>
</tr>
<tr>
<td class="name"><code>opt</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>輸入設定物件,預設{}</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>serverPort</code></td>
<td class="type">
<span class="param-type">Integer</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
<code>11005</code>
</td>
<td class="description last"><p>輸入伺服器通訊port,預設11005</p></td>
</tr>
<tr>
<td class="name"><code>useCheckUser</code></td>
<td class="type">
<span class="param-type">Boolean</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
<code>false</code>
</td>
<td class="description last"><p>輸入是否檢查使用者資訊布林值,預設false</p></td>
</tr>
<tr>
<td class="name"><code>getUserById</code></td>
<td class="type">
<span class="param-type">function</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
<code>null</code>
</td>
<td class="description last"><p>輸入當useCheckUser=true時依照使用者ID取得使用者資訊物件函數,預設null</p></td>
</tr>
<tr>
<td class="name"><code>useExcludeWhenNotAdmin</code></td>
<td class="type">
<span class="param-type">Boolean</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
<code>false</code>
</td>
<td class="description last"><p>輸入使用ORM的select方法時是否自動刪除數據內isActive欄位之布林值,預設false</p></td>
</tr>
<tr>
<td class="name"><code>webName</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>輸入站台名稱物件,至少包含語系eng與cht鍵的名稱,預設{}</p></td>
</tr>
<tr>
<td class="name"><code>webDescription</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>輸入站台描述物件,至少包含語系eng與cht鍵的名稱,預設{}</p></td>
</tr>
<tr>
<td class="name"><code>webLogo</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>輸入站台logo字串,採base64格式,預設''</p></td>
</tr>
<tr>
<td class="name"><code>subfolder</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>輸入站台所在子目錄字串,提供站台位於內網採反向代理進行服務時,故需支援位於子目錄情形,預設''</p></td>
</tr>
<tr>
<td class="name"><code>urlRedirect</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>輸入錯誤時自動轉址字串,提供站台例如無法登入或驗證失敗時須自動轉址,預設''</p></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<h5 class="h5-returns">Returns:</h5>
<div class="param-desc">
<p>回傳物件,其內server為hapi伺服器實體,wsrv為w-converhp的伺服器事件物件,wsds為w-serv-webdata的伺服器事件物件,可監聽error事件</p>
</div>
<dl class="param-type">
<dt>
Type
</dt>
<dd>
<span class="param-type">Object</span>
</dd>
</dl>
</div>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Sun Jul 20 2025 22:09:28 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>