waibu-rest-api 2.14.0 → 2.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/WaibuRestApi.html +1 -1
- package/docs/data/search.json +1 -1
- package/docs/global.html +1 -1
- package/docs/index.html +1 -1
- package/docs/index.js.html +572 -82
- package/docs/lib_config.js.html +93 -0
- package/package.json +1 -1
- package/wiki/CHANGES.md +4 -0
- /package/extend/waibu/{boot.js → webapp/boot.js} +0 -0
package/docs/index.js.html
CHANGED
|
@@ -1,102 +1,63 @@
|
|
|
1
1
|
<!DOCTYPE html><html lang="en" style="font-size:16px"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Source: index.js</title><!--[if lt IE 9]>
|
|
2
2
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
3
|
-
<![endif]--><script src="scripts/third-party/hljs.js" defer="defer"></script><script src="scripts/third-party/hljs-line-num.js" defer="defer"></script><script src="scripts/third-party/popper.js" defer="defer"></script><script src="scripts/third-party/tippy.js" defer="defer"></script><script src="scripts/third-party/tocbot.min.js"></script><script>var baseURL="/",locationPathname="";baseURL=(locationPathname=document.location.pathname).substr(0,locationPathname.lastIndexOf("/")+1)</script><link rel="stylesheet" href="styles/clean-jsdoc-theme.min.css"><svg aria-hidden="true" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="display:none"><defs><symbol id="copy-icon" viewbox="0 0 488.3 488.3"><g><path d="M314.25,85.4h-227c-21.3,0-38.6,17.3-38.6,38.6v325.7c0,21.3,17.3,38.6,38.6,38.6h227c21.3,0,38.6-17.3,38.6-38.6V124 C352.75,102.7,335.45,85.4,314.25,85.4z M325.75,449.6c0,6.4-5.2,11.6-11.6,11.6h-227c-6.4,0-11.6-5.2-11.6-11.6V124 c0-6.4,5.2-11.6,11.6-11.6h227c6.4,0,11.6,5.2,11.6,11.6V449.6z"/><path d="M401.05,0h-227c-21.3,0-38.6,17.3-38.6,38.6c0,7.5,6,13.5,13.5,13.5s13.5-6,13.5-13.5c0-6.4,5.2-11.6,11.6-11.6h227 c6.4,0,11.6,5.2,11.6,11.6v325.7c0,6.4-5.2,11.6-11.6,11.6c-7.5,0-13.5,6-13.5,13.5s6,13.5,13.5,13.5c21.3,0,38.6-17.3,38.6-38.6 V38.6C439.65,17.3,422.35,0,401.05,0z"/></g></symbol><symbol id="search-icon" viewBox="0 0 512 512"><g><g><path d="M225.474,0C101.151,0,0,101.151,0,225.474c0,124.33,101.151,225.474,225.474,225.474 c124.33,0,225.474-101.144,225.474-225.474C450.948,101.151,349.804,0,225.474,0z M225.474,409.323 c-101.373,0-183.848-82.475-183.848-183.848S124.101,41.626,225.474,41.626s183.848,82.475,183.848,183.848 S326.847,409.323,225.474,409.323z"/></g></g><g><g><path d="M505.902,476.472L386.574,357.144c-8.131-8.131-21.299-8.131-29.43,0c-8.131,8.124-8.131,21.306,0,29.43l119.328,119.328 c4.065,4.065,9.387,6.098,14.715,6.098c5.321,0,10.649-2.033,14.715-6.098C514.033,497.778,514.033,484.596,505.902,476.472z"/></g></g></symbol><symbol id="font-size-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M11.246 15H4.754l-2 5H.6L7 4h2l6.4 16h-2.154l-2-5zm-.8-2L8 6.885 5.554 13h4.892zM21 12.535V12h2v8h-2v-.535a4 4 0 1 1 0-6.93zM19 18a2 2 0 1 0 0-4 2 2 0 0 0 0 4z"/></symbol><symbol id="add-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 11V5h2v6h6v2h-6v6h-2v-6H5v-2z"/></symbol><symbol id="minus-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M5 11h14v2H5z"/></symbol><symbol id="dark-theme-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M10 7a7 7 0 0 0 12 4.9v.1c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2h.1A6.979 6.979 0 0 0 10 7zm-6 5a8 8 0 0 0 15.062 3.762A9 9 0 0 1 8.238 4.938 7.999 7.999 0 0 0 4 12z"/></symbol><symbol id="light-theme-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 18a6 6 0 1 1 0-12 6 6 0 0 1 0 12zm0-2a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM11 1h2v3h-2V1zm0 19h2v3h-2v-3zM3.515 4.929l1.414-1.414L7.05 5.636 5.636 7.05 3.515 4.93zM16.95 18.364l1.414-1.414 2.121 2.121-1.414 1.414-2.121-2.121zm2.121-14.85l1.414 1.415-2.121 2.121-1.414-1.414 2.121-2.121zM5.636 16.95l1.414 1.414-2.121 2.121-1.414-1.414 2.121-2.121zM23 11v2h-3v-2h3zM4 11v2H1v-2h3z"/></symbol><symbol id="reset-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M18.537 19.567A9.961 9.961 0 0 1 12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10c0 2.136-.67 4.116-1.81 5.74L17 12h3a8 8 0 1 0-2.46 5.772l.997 1.795z"/></symbol><symbol id="down-icon" viewBox="0 0 16 16"><path fill-rule="evenodd" clip-rule="evenodd" d="M12.7803 6.21967C13.0732 6.51256 13.0732 6.98744 12.7803 7.28033L8.53033 11.5303C8.23744 11.8232 7.76256 11.8232 7.46967 11.5303L3.21967 7.28033C2.92678 6.98744 2.92678 6.51256 3.21967 6.21967C3.51256 5.92678 3.98744 5.92678 4.28033 6.21967L8 9.93934L11.7197 6.21967C12.0126 5.92678 12.4874 5.92678 12.7803 6.21967Z"></path></symbol><symbol id="codepen-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M16.5 13.202L13 15.535v3.596L19.197 15 16.5 13.202zM14.697 12L12 10.202 9.303 12 12 13.798 14.697 12zM20 10.869L18.303 12 20 13.131V10.87zM19.197 9L13 4.869v3.596l3.5 2.333L19.197 9zM7.5 10.798L11 8.465V4.869L4.803 9 7.5 10.798zM4.803 15L11 19.131v-3.596l-3.5-2.333L4.803 15zM4 13.131L5.697 12 4 10.869v2.262zM2 9a1 1 0 0 1 .445-.832l9-6a1 1 0 0 1 1.11 0l9 6A1 1 0 0 1 22 9v6a1 1 0 0 1-.445.832l-9 6a1 1 0 0 1-1.11 0l-9-6A1 1 0 0 1 2 15V9z"/></symbol><symbol id="close-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 10.586l4.95-4.95 1.414 1.414-4.95 4.95 4.95 4.95-1.414 1.414-4.95-4.95-4.95 4.95-1.414-1.414 4.95-4.95-4.95-4.95L7.05 5.636z"/></symbol><symbol id="menu-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M3 4h18v2H3V4zm0 7h18v2H3v-2zm0 7h18v2H3v-2z"/></symbol></defs></svg></head><body data-theme="light"><div class="sidebar-container"><div class="sidebar" id="sidebar"><a href="/" class="sidebar-title sidebar-title-anchor">WaibuRestApi API</a><div class="sidebar-items-container"><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-classes"><div>Classes</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="WaibuRestApi.html">WaibuRestApi</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-global"><div>Global</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="global.html#factory">factory</a></div></div></div></div></div><div class="navbar-container" id="VuAckcnZhf"><nav class="navbar"><div class="navbar-left-items"><div class="navbar-item"><a id="" href="https://www.npmjs.com/package/waibu-rest-api" target="">NPM</a></div><div class="navbar-item"><a id="" href="https://github.com/ardhi/waibu-rest-api" target="">Github</a></div><div class="navbar-item"><a id="" href="https://waibu.bajo.app/" target="">Waibu</a></div><div class="navbar-item"><a id="" href="https://bajo.app/" target="">Bajo</a></div></div><div class="navbar-right-items"><div class="navbar-right-item"><button class="icon-button search-button" aria-label="open-search"><svg><use xlink:href="#search-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button theme-toggle" aria-label="toggle-theme"><svg><use class="theme-svg-use" xlink:href="#dark-theme-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button font-size" aria-label="change-font-size"><svg><use xlink:href="#font-size-icon"></use></svg></button></div></div><nav></nav></nav></div><div class="toc-container"><div class="toc-content"><span class="bold">On this page</span><div id="eed4d2a0bfd64539bb9df78095dec881"></div></div></div><div class="body-wrapper"><div class="main-content"><div class="main-wrapper"><section id="source-page" class="source-page"><header><h1 id="title" class="has-anchor">index.js</h1></header><article><pre class="prettyprint source lang-js"><code>import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import docSchemaParams from './lib/doc-schema/params.js'
|
|
9
|
-
import docSchemaQs from './lib/doc-schema/qs.js'
|
|
3
|
+
<![endif]--><script src="scripts/third-party/hljs.js" defer="defer"></script><script src="scripts/third-party/hljs-line-num.js" defer="defer"></script><script src="scripts/third-party/popper.js" defer="defer"></script><script src="scripts/third-party/tippy.js" defer="defer"></script><script src="scripts/third-party/tocbot.min.js"></script><script>var baseURL="/",locationPathname="";baseURL=(locationPathname=document.location.pathname).substr(0,locationPathname.lastIndexOf("/")+1)</script><link rel="stylesheet" href="styles/clean-jsdoc-theme.min.css"><svg aria-hidden="true" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="display:none"><defs><symbol id="copy-icon" viewbox="0 0 488.3 488.3"><g><path d="M314.25,85.4h-227c-21.3,0-38.6,17.3-38.6,38.6v325.7c0,21.3,17.3,38.6,38.6,38.6h227c21.3,0,38.6-17.3,38.6-38.6V124 C352.75,102.7,335.45,85.4,314.25,85.4z M325.75,449.6c0,6.4-5.2,11.6-11.6,11.6h-227c-6.4,0-11.6-5.2-11.6-11.6V124 c0-6.4,5.2-11.6,11.6-11.6h227c6.4,0,11.6,5.2,11.6,11.6V449.6z"/><path d="M401.05,0h-227c-21.3,0-38.6,17.3-38.6,38.6c0,7.5,6,13.5,13.5,13.5s13.5-6,13.5-13.5c0-6.4,5.2-11.6,11.6-11.6h227 c6.4,0,11.6,5.2,11.6,11.6v325.7c0,6.4-5.2,11.6-11.6,11.6c-7.5,0-13.5,6-13.5,13.5s6,13.5,13.5,13.5c21.3,0,38.6-17.3,38.6-38.6 V38.6C439.65,17.3,422.35,0,401.05,0z"/></g></symbol><symbol id="search-icon" viewBox="0 0 512 512"><g><g><path d="M225.474,0C101.151,0,0,101.151,0,225.474c0,124.33,101.151,225.474,225.474,225.474 c124.33,0,225.474-101.144,225.474-225.474C450.948,101.151,349.804,0,225.474,0z M225.474,409.323 c-101.373,0-183.848-82.475-183.848-183.848S124.101,41.626,225.474,41.626s183.848,82.475,183.848,183.848 S326.847,409.323,225.474,409.323z"/></g></g><g><g><path d="M505.902,476.472L386.574,357.144c-8.131-8.131-21.299-8.131-29.43,0c-8.131,8.124-8.131,21.306,0,29.43l119.328,119.328 c4.065,4.065,9.387,6.098,14.715,6.098c5.321,0,10.649-2.033,14.715-6.098C514.033,497.778,514.033,484.596,505.902,476.472z"/></g></g></symbol><symbol id="font-size-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M11.246 15H4.754l-2 5H.6L7 4h2l6.4 16h-2.154l-2-5zm-.8-2L8 6.885 5.554 13h4.892zM21 12.535V12h2v8h-2v-.535a4 4 0 1 1 0-6.93zM19 18a2 2 0 1 0 0-4 2 2 0 0 0 0 4z"/></symbol><symbol id="add-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 11V5h2v6h6v2h-6v6h-2v-6H5v-2z"/></symbol><symbol id="minus-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M5 11h14v2H5z"/></symbol><symbol id="dark-theme-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M10 7a7 7 0 0 0 12 4.9v.1c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2h.1A6.979 6.979 0 0 0 10 7zm-6 5a8 8 0 0 0 15.062 3.762A9 9 0 0 1 8.238 4.938 7.999 7.999 0 0 0 4 12z"/></symbol><symbol id="light-theme-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 18a6 6 0 1 1 0-12 6 6 0 0 1 0 12zm0-2a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM11 1h2v3h-2V1zm0 19h2v3h-2v-3zM3.515 4.929l1.414-1.414L7.05 5.636 5.636 7.05 3.515 4.93zM16.95 18.364l1.414-1.414 2.121 2.121-1.414 1.414-2.121-2.121zm2.121-14.85l1.414 1.415-2.121 2.121-1.414-1.414 2.121-2.121zM5.636 16.95l1.414 1.414-2.121 2.121-1.414-1.414 2.121-2.121zM23 11v2h-3v-2h3zM4 11v2H1v-2h3z"/></symbol><symbol id="reset-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M18.537 19.567A9.961 9.961 0 0 1 12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10c0 2.136-.67 4.116-1.81 5.74L17 12h3a8 8 0 1 0-2.46 5.772l.997 1.795z"/></symbol><symbol id="down-icon" viewBox="0 0 16 16"><path fill-rule="evenodd" clip-rule="evenodd" d="M12.7803 6.21967C13.0732 6.51256 13.0732 6.98744 12.7803 7.28033L8.53033 11.5303C8.23744 11.8232 7.76256 11.8232 7.46967 11.5303L3.21967 7.28033C2.92678 6.98744 2.92678 6.51256 3.21967 6.21967C3.51256 5.92678 3.98744 5.92678 4.28033 6.21967L8 9.93934L11.7197 6.21967C12.0126 5.92678 12.4874 5.92678 12.7803 6.21967Z"></path></symbol><symbol id="codepen-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M16.5 13.202L13 15.535v3.596L19.197 15 16.5 13.202zM14.697 12L12 10.202 9.303 12 12 13.798 14.697 12zM20 10.869L18.303 12 20 13.131V10.87zM19.197 9L13 4.869v3.596l3.5 2.333L19.197 9zM7.5 10.798L11 8.465V4.869L4.803 9 7.5 10.798zM4.803 15L11 19.131v-3.596l-3.5-2.333L4.803 15zM4 13.131L5.697 12 4 10.869v2.262zM2 9a1 1 0 0 1 .445-.832l9-6a1 1 0 0 1 1.11 0l9 6A1 1 0 0 1 22 9v6a1 1 0 0 1-.445.832l-9 6a1 1 0 0 1-1.11 0l-9-6A1 1 0 0 1 2 15V9z"/></symbol><symbol id="close-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 10.586l4.95-4.95 1.414 1.414-4.95 4.95 4.95 4.95-1.414 1.414-4.95-4.95-4.95 4.95-1.414-1.414 4.95-4.95-4.95-4.95L7.05 5.636z"/></symbol><symbol id="menu-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M3 4h18v2H3V4zm0 7h18v2H3v-2zm0 7h18v2H3v-2z"/></symbol></defs></svg></head><body data-theme="light"><div class="sidebar-container"><div class="sidebar" id="sidebar"><a href="/" class="sidebar-title sidebar-title-anchor">WaibuRestApi API</a><div class="sidebar-items-container"><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-classes"><div>Classes</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="WaibuRestApi.html">WaibuRestApi</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-global"><div>Global</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="global.html#TConfig">TConfig</a></div><div class="sidebar-section-children"><a href="global.html#factory">factory</a></div></div></div></div></div><div class="navbar-container" id="VuAckcnZhf"><nav class="navbar"><div class="navbar-left-items"><div class="navbar-item"><a id="" href="https://www.npmjs.com/package/waibu-rest-api" target="">NPM</a></div><div class="navbar-item"><a id="" href="https://github.com/ardhi/waibu-rest-api" target="">Github</a></div><div class="navbar-item"><a id="" href="https://waibu.bajo.app/" target="">Waibu</a></div><div class="navbar-item"><a id="" href="https://bajo.app/" target="">Bajo</a></div></div><div class="navbar-right-items"><div class="navbar-right-item"><button class="icon-button search-button" aria-label="open-search"><svg><use xlink:href="#search-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button theme-toggle" aria-label="toggle-theme"><svg><use class="theme-svg-use" xlink:href="#dark-theme-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button font-size" aria-label="change-font-size"><svg><use xlink:href="#font-size-icon"></use></svg></button></div></div><nav></nav></nav></div><div class="toc-container"><div class="toc-content"><span class="bold">On this page</span><div id="eed4d2a0bfd64539bb9df78095dec881"></div></div></div><div class="body-wrapper"><div class="main-content"><div class="main-wrapper"><section id="source-page" class="source-page"><header><h1 id="title" class="has-anchor">index.js</h1></header><article><pre class="prettyprint source lang-js"><code>import path from 'path'
|
|
4
|
+
import config from './lib/config.js'
|
|
5
|
+
import {
|
|
6
|
+
handleResponseXml, extractHeaders, buildPropsReqsForSchema, buildResponseForSchema
|
|
7
|
+
} from './lib/helper.js'
|
|
10
8
|
|
|
11
9
|
/**
|
|
12
|
-
* Plugin factory
|
|
10
|
+
* Plugin factory.
|
|
11
|
+
*
|
|
12
|
+
* **Never** call this function directly!!! It's only-meant to be called by the {@link https://ardhi.github.io/bajo|Bajo framework} during plugin initialization.
|
|
13
13
|
*
|
|
14
14
|
* @param {string} pkgName - NPM package name
|
|
15
|
-
* @returns {class
|
|
15
|
+
* @returns {WaibuRestApi} WaibuRestApi class
|
|
16
16
|
*/
|
|
17
17
|
async function factory (pkgName) {
|
|
18
18
|
const me = this
|
|
19
|
+
const { defaultsDeep } = this.app.lib.aneka
|
|
20
|
+
const { importModule, runHook } = this.app.bajo
|
|
21
|
+
const { getPluginPrefix } = this.app.waibu
|
|
22
|
+
const { methodMap, getParams } = this.app.waibuDb
|
|
23
|
+
const { pick, isFunction, merge, cloneDeep, each, get } = this.app.lib._
|
|
19
24
|
|
|
20
25
|
/**
|
|
21
|
-
* WaibuRestApi class
|
|
26
|
+
* WaibuRestApi class defniton. This class is used to create a REST API services for Waibu.
|
|
22
27
|
*
|
|
23
28
|
* @class
|
|
24
29
|
*/
|
|
25
30
|
class WaibuRestApi extends this.app.baseClass.Base {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Constructor
|
|
33
|
+
*/
|
|
29
34
|
constructor () {
|
|
30
35
|
super(pkgName, me.app)
|
|
31
36
|
this.routePathHandlers = ['restapi']
|
|
32
|
-
this.config =
|
|
33
|
-
waibu: {
|
|
34
|
-
prefix: 'api'
|
|
35
|
-
},
|
|
36
|
-
mountMainAsRoot: true,
|
|
37
|
-
exposeHead: true,
|
|
38
|
-
enablePatch: false,
|
|
39
|
-
bodyLimit: 1048576,
|
|
40
|
-
intl: {
|
|
41
|
-
detectors: ['qs', 'header']
|
|
42
|
-
},
|
|
43
|
-
format: {
|
|
44
|
-
supported: ['json', 'xml'],
|
|
45
|
-
asExt: false,
|
|
46
|
-
xml: {
|
|
47
|
-
bodyParser: {
|
|
48
|
-
contentTypes: ['text/xml', 'application/xml', 'application/rss+xml'],
|
|
49
|
-
validate: false
|
|
50
|
-
},
|
|
51
|
-
response: {
|
|
52
|
-
wrapper: 'doc',
|
|
53
|
-
declaration: true,
|
|
54
|
-
valueAsAttributes: true
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
responseKey: {
|
|
59
|
-
data: 'data',
|
|
60
|
-
oldData: 'oldData',
|
|
61
|
-
page: 'page',
|
|
62
|
-
count: 'count',
|
|
63
|
-
pages: 'pages',
|
|
64
|
-
success: 'success',
|
|
65
|
-
statusCode: 'code',
|
|
66
|
-
error: 'error',
|
|
67
|
-
cached: 'cached',
|
|
68
|
-
message: 'message',
|
|
69
|
-
details: 'details'
|
|
70
|
-
},
|
|
71
|
-
mapSlash: '~',
|
|
72
|
-
mapDot: ',',
|
|
73
|
-
multipart: {
|
|
74
|
-
},
|
|
75
|
-
cors: {},
|
|
76
|
-
helmet: {},
|
|
77
|
-
compress: false,
|
|
78
|
-
rateLimit: false,
|
|
79
|
-
disabled: [],
|
|
80
|
-
rerouted: {}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
this.selfBind([
|
|
84
|
-
'docSchemaDescription',
|
|
85
|
-
'docSchemaForFind',
|
|
86
|
-
'docSchemaGeneral',
|
|
87
|
-
'docSchemaLib',
|
|
88
|
-
'docSchemaModel',
|
|
89
|
-
'docSchemaParams',
|
|
90
|
-
'docSchemaQs'
|
|
91
|
-
])
|
|
37
|
+
this.config = config
|
|
92
38
|
}
|
|
93
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Initialize the plugin.
|
|
42
|
+
*
|
|
43
|
+
* @async
|
|
44
|
+
* @method
|
|
45
|
+
*/
|
|
94
46
|
init = async () => {
|
|
95
47
|
const { uniq } = this.app.lib._
|
|
96
48
|
this.config.format.supported.push('json')
|
|
97
49
|
this.config.format.supported = uniq(this.config.format.supported)
|
|
98
50
|
}
|
|
99
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Generate the route path for a given name.
|
|
54
|
+
*
|
|
55
|
+
* @param {string} name - The name of the route
|
|
56
|
+
* @param {Object} [options={}] - Options for route path generation
|
|
57
|
+
* @param {string|boolean} [options.defFormat='.json'] - Default format for the route
|
|
58
|
+
* @param {boolean} [options.uriEncoded=true] - Whether to URI encode the path
|
|
59
|
+
* @returns {string} The generated route path
|
|
60
|
+
*/
|
|
100
61
|
routePath = (name, { defFormat = '.json', uriEncoded = true } = {}) => {
|
|
101
62
|
const { get, trimStart } = this.app.lib._
|
|
102
63
|
const { fullPath, ns } = this.app.bajo.breakNsPath(name)
|
|
@@ -107,13 +68,24 @@ async function factory (pkgName) {
|
|
|
107
68
|
return '/' + trimStart(path, '/')
|
|
108
69
|
}
|
|
109
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Transform the result before sending the response.
|
|
73
|
+
*
|
|
74
|
+
* @param {Object} param0 - The parameters for transforming the result
|
|
75
|
+
* @param {Object} param0.data - The data to be transformed
|
|
76
|
+
* @param {Object} param0.req - The request object
|
|
77
|
+
* @param {Object} param0.reply - The reply object
|
|
78
|
+
* @param {Object} [param0.options={}] - Additional options for transformation
|
|
79
|
+
* @returns {Object} The transformed result
|
|
80
|
+
*/
|
|
110
81
|
transformResult = ({ data, req, reply, options = {} }) => {
|
|
111
82
|
const reformat = ({ data, req, reply, options = {} }) => {
|
|
112
83
|
const { forOwn, get } = this.app.lib._
|
|
113
84
|
const newData = {}
|
|
114
85
|
forOwn(data, (v, k) => {
|
|
115
|
-
let key = get(this.config, `responseKey.${k}
|
|
86
|
+
let key = get(this.config, `responseKey.${k}`)
|
|
116
87
|
if (options.forFind && k === 'data') key = get(this.config, 'responseKey.data')
|
|
88
|
+
if (!key) return undefined
|
|
117
89
|
newData[key] = v
|
|
118
90
|
})
|
|
119
91
|
return newData
|
|
@@ -146,6 +118,7 @@ async function factory (pkgName) {
|
|
|
146
118
|
const { each, keys, omit, get } = this.app.lib._
|
|
147
119
|
const cfg = this.config
|
|
148
120
|
const cfgWdb = this.app.waibuDb.config
|
|
121
|
+
if (!options.showWarnings) delete data.warnings
|
|
149
122
|
if (reply) {
|
|
150
123
|
reply.code(req.method.toUpperCase() === 'POST' ? 201 : 200)
|
|
151
124
|
if (cfgWdb.dbModel.dataOnly) {
|
|
@@ -164,17 +137,534 @@ async function factory (pkgName) {
|
|
|
164
137
|
return returnSuccess({ data, req, reply, options })
|
|
165
138
|
}
|
|
166
139
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
140
|
+
/**
|
|
141
|
+
* Generate the schema description for a given method.
|
|
142
|
+
*
|
|
143
|
+
* @method
|
|
144
|
+
* @param {string} method - The method name
|
|
145
|
+
* @returns {string} The schema description
|
|
146
|
+
*/
|
|
147
|
+
docSchemaDescription = (method) => {
|
|
148
|
+
const desc = {
|
|
149
|
+
create: 'postRecordWithBody',
|
|
150
|
+
find: 'findRecordsWithFilter',
|
|
151
|
+
get: 'getRecordById',
|
|
152
|
+
update: 'updateRecordByIdWithBody',
|
|
153
|
+
remove: 'removeRecordById'
|
|
154
|
+
}
|
|
155
|
+
return this.t(desc[method])
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Generate the schema for the "find" operation.
|
|
160
|
+
*
|
|
161
|
+
* @async
|
|
162
|
+
* @method
|
|
163
|
+
* @param {Object} items - The items schema
|
|
164
|
+
* @returns {Object} The schema for the "find" operation
|
|
165
|
+
*/
|
|
166
|
+
docSchemaForFind = async (items) => {
|
|
167
|
+
return {
|
|
168
|
+
data: {
|
|
169
|
+
type: 'array',
|
|
170
|
+
items
|
|
171
|
+
},
|
|
172
|
+
limit: { type: 'integer' },
|
|
173
|
+
page: { type: 'integer' },
|
|
174
|
+
pages: { type: 'integer' },
|
|
175
|
+
count: { type: 'integer' },
|
|
176
|
+
success: { type: 'boolean', default: true },
|
|
177
|
+
statusCode: { type: 'integer', default: 200 },
|
|
178
|
+
warnings: { type: 'array' }
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Generate the general schema for the API responses.
|
|
184
|
+
*
|
|
185
|
+
* @async
|
|
186
|
+
* @method
|
|
187
|
+
* @returns {Object} The general schema for API responses
|
|
188
|
+
*/
|
|
189
|
+
docSchemaGeneral = async () => {
|
|
190
|
+
const cfg = this.config
|
|
191
|
+
const cfgWdb = this.app.waibuDb.config
|
|
192
|
+
const def = {
|
|
193
|
+
type: 'object',
|
|
194
|
+
properties: {
|
|
195
|
+
error: { type: 'string' },
|
|
196
|
+
message: { type: 'string' }
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
for (const type of ['4xx', '5xx']) {
|
|
200
|
+
const item = cloneDeep(def)
|
|
201
|
+
if (type === '4xx') {
|
|
202
|
+
merge(item, {
|
|
203
|
+
properties: {
|
|
204
|
+
details: {
|
|
205
|
+
type: 'array',
|
|
206
|
+
items: {
|
|
207
|
+
type: 'object',
|
|
208
|
+
properties: {
|
|
209
|
+
field: { type: 'string' },
|
|
210
|
+
error: { type: 'string' }
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
success: { type: 'boolean', default: false },
|
|
215
|
+
statusCode: { type: 'integer', default: 400 }
|
|
216
|
+
}
|
|
217
|
+
})
|
|
218
|
+
} else {
|
|
219
|
+
merge(item, {
|
|
220
|
+
properties: {
|
|
221
|
+
success: { type: 'boolean', default: false },
|
|
222
|
+
statusCode: { type: 'integer', default: 500 }
|
|
223
|
+
}
|
|
224
|
+
})
|
|
225
|
+
}
|
|
226
|
+
if (cfgWdb.dbModel.dataOnly) item.properties = { error: item.properties.message }
|
|
227
|
+
const props = {}
|
|
228
|
+
each(item.properties, (v, k) => {
|
|
229
|
+
const key = get(cfg, `responseKey.${k}`, k)
|
|
230
|
+
props[key] = item.properties[k]
|
|
231
|
+
})
|
|
232
|
+
item.properties = props
|
|
233
|
+
await this.docSchemaLib(type + 'Resp', item)
|
|
234
|
+
}
|
|
235
|
+
await this.docSchemaParams('qsFilter',
|
|
236
|
+
'query||NQL/Mongo Query. Leave empty to disable query',
|
|
237
|
+
'limit|integer|Number of records per page. Must be >= 1|' + this.app.dobo.config.default.filter.limit,
|
|
238
|
+
'page|integer|Desired page number. Must be >= 1|1',
|
|
239
|
+
'sort||Order of records, format: &lt;field&gt;:&lt;dir&gt;[,&lt;field&gt;:&lt;dir&gt;,[...]]',
|
|
240
|
+
'fields||Comma delimited fields to show. Leave empty to show all fields',
|
|
241
|
+
true
|
|
242
|
+
)
|
|
243
|
+
await this.docSchemaParams('paramsId', 'id||Record ID')
|
|
244
|
+
await this.docSchemaParams('qsFields',
|
|
245
|
+
'fields||Comma delimited fields to show. Leave empty to show all fields',
|
|
246
|
+
true
|
|
247
|
+
)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Add a schema to the library.
|
|
252
|
+
*
|
|
253
|
+
* @async
|
|
254
|
+
* @method
|
|
255
|
+
* @param {string} name - The name of the schema
|
|
256
|
+
* @param {Object} obj - The schema object
|
|
257
|
+
*/
|
|
258
|
+
docSchemaLib = async (name, obj) => {
|
|
259
|
+
const { merge } = this.app.lib._
|
|
260
|
+
if (this.webAppCtx.getSchema(name)) return
|
|
261
|
+
const value = merge({}, obj, { $id: name })
|
|
262
|
+
this.webAppCtx.addSchema(value)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Generate the schema for a model method.
|
|
267
|
+
*
|
|
268
|
+
* @async
|
|
269
|
+
* @method
|
|
270
|
+
* @param {Object} param0 - The parameters for generating the schema
|
|
271
|
+
* @param {string} param0.model - The model name
|
|
272
|
+
* @param {string} param0.method - The method name
|
|
273
|
+
* @param {Object} [param0.options={}] - Additional options for schema generation
|
|
274
|
+
* @returns {Object} The generated schema
|
|
275
|
+
*/
|
|
276
|
+
docSchemaModel = async ({ model, method, options = {} }) => {
|
|
277
|
+
const mdl = this.app.dobo.getModel(model)
|
|
278
|
+
const { omit } = this.app.lib._
|
|
279
|
+
const out = {
|
|
280
|
+
description: options.description ?? this.docSchemaDescription(method),
|
|
281
|
+
tags: [this.alias.toUpperCase(), ...(options.alias ?? [])]
|
|
282
|
+
}
|
|
283
|
+
if (['find'].includes(method)) {
|
|
284
|
+
out.querystring = { $ref: 'qsFilter#' }
|
|
285
|
+
}
|
|
286
|
+
if (['get', 'update', 'replace', 'remove'].includes(method)) {
|
|
287
|
+
out.querystring = { $ref: 'qsFields#' }
|
|
288
|
+
if (!options.noId) out.params = { $ref: 'paramsId#' }
|
|
289
|
+
}
|
|
290
|
+
if (['update'].includes(method)) {
|
|
291
|
+
const { properties } = await buildPropsReqsForSchema.call(this, { model: mdl, method, options })
|
|
292
|
+
const name = 'model' + mdl.name + 'Update'
|
|
293
|
+
delete properties._ref
|
|
294
|
+
await this.docSchemaLib(name, {
|
|
295
|
+
type: 'object',
|
|
296
|
+
properties: omit(properties, ['id'])
|
|
297
|
+
})
|
|
298
|
+
out.body = { $ref: name + '#' }
|
|
299
|
+
}
|
|
300
|
+
if (['replace'].includes(method)) {
|
|
301
|
+
const { properties, required } = await buildPropsReqsForSchema.call(this, { model: mdl, method, options })
|
|
302
|
+
const name = 'model' + model.name + 'Replace'
|
|
303
|
+
delete properties._ref
|
|
304
|
+
await this.docSchemaLib(name, {
|
|
305
|
+
type: 'object',
|
|
306
|
+
properties: omit(properties, ['id']),
|
|
307
|
+
required
|
|
308
|
+
})
|
|
309
|
+
out.body = { $ref: name + '#' }
|
|
310
|
+
}
|
|
311
|
+
if (['create'].includes(method)) {
|
|
312
|
+
const { properties, required } = await buildPropsReqsForSchema.call(this, { model: mdl, method, options })
|
|
313
|
+
const name = 'model' + model.name + 'Create'
|
|
314
|
+
delete properties._ref
|
|
315
|
+
await this.docSchemaLib(name, {
|
|
316
|
+
type: 'object',
|
|
317
|
+
properties,
|
|
318
|
+
required
|
|
319
|
+
})
|
|
320
|
+
out.body = { $ref: name + '#' }
|
|
321
|
+
out.querystring = { $ref: 'qsFields#' }
|
|
322
|
+
}
|
|
323
|
+
out.response = await buildResponseForSchema.call(this, { model: mdl, method, options })
|
|
324
|
+
return out
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Generate the schema for query parameters.
|
|
329
|
+
*
|
|
330
|
+
* @async
|
|
331
|
+
* @method
|
|
332
|
+
* @param {string} paramName - The name of the parameter
|
|
333
|
+
* @param {...string} args - The arguments for the parameter schema
|
|
334
|
+
* @returns {void}
|
|
335
|
+
*/
|
|
336
|
+
docSchemaParams = async (paramName, ...args) => {
|
|
337
|
+
const { each, isEmpty, keys, last, isBoolean } = this.app.lib._
|
|
338
|
+
const cfgWeb = this.app.waibu.getConfig()
|
|
339
|
+
let transform = false
|
|
340
|
+
if (isBoolean(last(args))) {
|
|
341
|
+
transform = args.pop()
|
|
342
|
+
}
|
|
343
|
+
const item = {
|
|
344
|
+
type: 'object',
|
|
345
|
+
properties: {}
|
|
346
|
+
}
|
|
347
|
+
each(args, a => {
|
|
348
|
+
let [name, type, description, def] = a.split('|')
|
|
349
|
+
if (isEmpty(type)) type = 'string'
|
|
350
|
+
item.properties[name] = { type, description: this.t(description), default: def }
|
|
351
|
+
})
|
|
352
|
+
if (transform) {
|
|
353
|
+
each(keys(cfgWeb.qsKey), k => {
|
|
354
|
+
const v = cfgWeb.qsKey[k]
|
|
355
|
+
if (k === v || !item.properties[k]) return undefined
|
|
356
|
+
item.properties[v] = item.properties[k]
|
|
357
|
+
delete item.properties[k]
|
|
358
|
+
})
|
|
359
|
+
}
|
|
360
|
+
if (!isEmpty(args)) await this.docSchemaLib(paramName, item)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Generate the schema for query string parameters.
|
|
365
|
+
*
|
|
366
|
+
* @async
|
|
367
|
+
* @method
|
|
368
|
+
* @param {string} paramName - The name of the parameter
|
|
369
|
+
* @param {...string} args - The arguments for the parameter schema
|
|
370
|
+
* @returns {void}
|
|
371
|
+
*/
|
|
372
|
+
docSchemaQs = async (paramName, ...args) => {
|
|
373
|
+
const { each, isEmpty } = this.app.lib._
|
|
374
|
+
const item = {
|
|
375
|
+
type: 'object',
|
|
376
|
+
properties: {}
|
|
377
|
+
}
|
|
378
|
+
each(args, a => {
|
|
379
|
+
let [name, type, description] = a.split(':')
|
|
380
|
+
if (isEmpty(type)) type = 'string'
|
|
381
|
+
item.properties[name] = { type, description: this.t(description) }
|
|
382
|
+
})
|
|
383
|
+
if (!isEmpty(args)) await this.docSchemaLib(paramName, item)
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Handle not found errors.
|
|
388
|
+
*
|
|
389
|
+
* @async
|
|
390
|
+
* @method
|
|
391
|
+
* @param {Object} [err={}] - The error object
|
|
392
|
+
* @param {Object} req - The request object
|
|
393
|
+
* @param {Object} reply - The reply object
|
|
394
|
+
* @returns {Object} The transformed result
|
|
395
|
+
*/
|
|
396
|
+
_handleNotFound = async (err = {}, req, reply) => {
|
|
397
|
+
const msg = this.t('routeNotFound%s%s', req.url, req.method)
|
|
398
|
+
const data = this.webAppCtx.httpErrors.createError(404, msg)
|
|
399
|
+
return this.transformResult({ data, req, reply })
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Handle general errors.
|
|
404
|
+
*
|
|
405
|
+
* @async
|
|
406
|
+
* @method
|
|
407
|
+
* @param {Object} err - The error object
|
|
408
|
+
* @param {Object} req - The request object
|
|
409
|
+
* @param {Object} reply - The reply object
|
|
410
|
+
* @returns {Object} The transformed result
|
|
411
|
+
*/
|
|
412
|
+
_handleError = async (err, req, reply) => {
|
|
413
|
+
err.statusCode = err.statusCode ?? 500
|
|
414
|
+
const msg = err.message
|
|
415
|
+
if (msg === '_notFound' || err.statusCode === 404) {
|
|
416
|
+
return this._handleNotFound(err, req, reply)
|
|
417
|
+
}
|
|
418
|
+
const data = this.webAppCtx.httpErrors.createError(err.statusCode, msg)
|
|
419
|
+
data.statusCode = err.statusCode
|
|
420
|
+
data.details = err.details
|
|
421
|
+
return this.transformResult({ data, req, reply })
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Handle the response.
|
|
426
|
+
*
|
|
427
|
+
* @async
|
|
428
|
+
* @method
|
|
429
|
+
* @returns {void}
|
|
430
|
+
*/
|
|
431
|
+
_handleResponse = async () => {
|
|
432
|
+
const me = this
|
|
433
|
+
|
|
434
|
+
this.webAppCtx.addHook('onSend', async function (req, reply, payload) {
|
|
435
|
+
let type = req.params.format
|
|
436
|
+
if (!type) {
|
|
437
|
+
const types = req.accepts().types()
|
|
438
|
+
if (me.config.format.supported.includes('xml') && types[0] === 'application/xml') type = 'xml'
|
|
439
|
+
else type = 'json'
|
|
440
|
+
}
|
|
441
|
+
let result = payload
|
|
442
|
+
switch (type) {
|
|
443
|
+
case 'xml': result = await handleResponseXml.call(me, req, reply, payload); break
|
|
444
|
+
}
|
|
445
|
+
return result
|
|
446
|
+
})
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Serve a route.
|
|
451
|
+
*
|
|
452
|
+
* @async
|
|
453
|
+
* @method
|
|
454
|
+
* @param {Object} options - The options for serving the route
|
|
455
|
+
* @param {Object} options.mod - The module object
|
|
456
|
+
* @param {string} options.ns - The namespace
|
|
457
|
+
* @param {string} [options.parent] - The parent namespace
|
|
458
|
+
* @returns {Object} The served route
|
|
459
|
+
*/
|
|
460
|
+
_serveRoute = async (options) => {
|
|
461
|
+
let { mod, ns, parent } = options
|
|
462
|
+
const { mergeRouteHooks } = await importModule('waibu:/lib/webapp.js', { asDefaultImport: false })
|
|
463
|
+
const cfgWdb = this.app.waibuDb.config
|
|
464
|
+
mod.config = mod.config ?? {}
|
|
465
|
+
mod.config.prefix = getPluginPrefix(ns, 'waibuRestApi')
|
|
466
|
+
mod.config.pathSrc = mod.url
|
|
467
|
+
mod.config.webApp = parent ?? ns
|
|
468
|
+
mod.config.ns = ns
|
|
469
|
+
mod.config.subNs = 'restapi'
|
|
470
|
+
mod.config.xSite = mod.xSite
|
|
471
|
+
mod.config.title = mod.title
|
|
472
|
+
delete mod.title
|
|
473
|
+
if (mod.method === 'PUT' && cfgWdb.dbModel.patchEnabled) mod.method = [mod.method, 'PATCH']
|
|
474
|
+
mod = defaultsDeep(pick(this.config, ['exposeHeadRoute', 'bodyLimit']), mod)
|
|
475
|
+
await mergeRouteHooks.call(this, mod, false) // Don't include handler, hence 'false'
|
|
476
|
+
return mod
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Build a route based on the model configuration.
|
|
481
|
+
*
|
|
482
|
+
* @async
|
|
483
|
+
* @method
|
|
484
|
+
* @param {Object} options - The options for building the route
|
|
485
|
+
* @param {string} options.file - The file path of the model configuration
|
|
486
|
+
* @param {string} options.dir - The directory of the model configuration
|
|
487
|
+
* @param {string} options.pathPrefix - The path prefix for the route
|
|
488
|
+
* @param {string} options.ns - The namespace
|
|
489
|
+
* @param {string} [options.parent] - The parent namespace
|
|
490
|
+
* @returns {Object} The built route
|
|
491
|
+
*/
|
|
492
|
+
_routeByModelBuilder = async (options) => {
|
|
493
|
+
const { file, dir, pathPrefix, ns, parent } = options
|
|
494
|
+
const { readConfig } = this.app.bajo
|
|
495
|
+
const { pascalCase } = this.app.lib.aneka
|
|
496
|
+
const { methodMap, getParams } = this.app.waibuDb
|
|
497
|
+
const { isFunction, camelCase, omit, merge, keys, filter } = this.app.lib._
|
|
498
|
+
const builder = await readConfig(file, { ns, ignoreError: true })
|
|
499
|
+
builder.disabled = builder.disabled ?? []
|
|
500
|
+
let url = path.dirname(file).replace(`${dir}/extend/${pathPrefix}`, '').replaceAll('@', ':')
|
|
501
|
+
if (isFunction(builder.url)) url = await builder.url.call(this)
|
|
502
|
+
const methods = keys(methodMap)
|
|
503
|
+
if (this.config.enablePatch) methods.push('replace')
|
|
504
|
+
const me = this
|
|
505
|
+
const mods = []
|
|
506
|
+
let model = {}
|
|
507
|
+
if (!builder.model.startsWith(':')) model = this.app.dobo.getModel(builder.model)
|
|
508
|
+
for (const method of methods) {
|
|
509
|
+
let disabled = (model.disabled ?? []).includes(method) || builder.disabled.includes(method)
|
|
510
|
+
if (method === 'replace' && ((model.disabled ?? []).includes('update') || (builder.disabled ?? []).includes('update'))) disabled = true
|
|
511
|
+
if (disabled) continue
|
|
512
|
+
const mod = omit(builder, ['model', 'url', 'method', 'handler', 'hidden', ...methods])
|
|
513
|
+
const customMod = builder[method] ?? {}
|
|
514
|
+
customMod.schema = customMod.schema ?? false
|
|
515
|
+
if (customMod.schema === true && !builder.model.startsWith(':')) customMod.schema = await this.docSchemaModel({ model: builder.model, method, options: { hidden: builder.hidden } })
|
|
516
|
+
let mapmethod = methodMap[method]
|
|
517
|
+
if (this.config.enablePatch) {
|
|
518
|
+
if (method === 'replace') mapmethod = 'PUT'
|
|
519
|
+
else if (method === 'update') mapmethod = 'PATCH'
|
|
520
|
+
}
|
|
521
|
+
merge(mod, customMod, {
|
|
522
|
+
url: ['get', 'update', 'replace', 'remove'].includes(method) ? `${url}/:id` : url,
|
|
523
|
+
method: mapmethod,
|
|
524
|
+
handler: async function (req, reply) {
|
|
525
|
+
const helper = me.app.waibuDb[`${method === 'replace' ? 'update' : method}Record`]
|
|
526
|
+
const { fields } = getParams(req)
|
|
527
|
+
const options = { fields, dynHooks: builder.hooks ?? [] }
|
|
528
|
+
extractHeaders.call(me, req, options)
|
|
529
|
+
options.hidden = builder.hidden ?? []
|
|
530
|
+
options.queryHandler = builder.queryHandler
|
|
531
|
+
if (method === 'replace') options.partial = false
|
|
532
|
+
if (['get', 'remove', 'update'].includes(method)) options.throwNotFound = true
|
|
533
|
+
const model = builder.model.startsWith(':') ? pascalCase(req.params[builder.model.slice(1)]) : builder.model
|
|
534
|
+
const data = await helper({ model, req, reply, options, transaction: builder.transaction })
|
|
535
|
+
data.success = true
|
|
536
|
+
data.statusCode = method === 'create' ? 201 : 200
|
|
537
|
+
if (method === 'find') options.forFind = true
|
|
538
|
+
return me.transformResult({ data, req, reply, options })
|
|
539
|
+
}
|
|
540
|
+
})
|
|
541
|
+
if (mod.schema === false) delete mod.schema
|
|
542
|
+
mod.config = mod.config ?? {}
|
|
543
|
+
mod.config.pathSrc = url
|
|
544
|
+
mods.push(await me._serveRoute({ mod, ns, parent }))
|
|
545
|
+
}
|
|
546
|
+
// stats
|
|
547
|
+
const supported = ['aggregate', 'histogram']
|
|
548
|
+
builder.stat = filter(builder.stat ?? supported, item => supported.includes(item))
|
|
549
|
+
const method = 'GET'
|
|
550
|
+
const disabled = (model.disabled ?? []).includes(method) || builder.disabled.includes(method)
|
|
551
|
+
if (disabled) return mods
|
|
552
|
+
const mod = {
|
|
553
|
+
schema: false,
|
|
554
|
+
method,
|
|
555
|
+
url: `${url}/stat/:stat`,
|
|
556
|
+
handler: async function (req, reply) {
|
|
557
|
+
if (!supported.includes(req.params.stat)) throw me.error('_notFound')
|
|
558
|
+
const helper = me.app.waibuDb[camelCase(`create ${req.params.stat}`)]
|
|
559
|
+
if (!helper) throw me.error('_notFound')
|
|
560
|
+
const options = { queryHandler: builder.queryHandler }
|
|
561
|
+
const model = builder.model.startsWith(':') ? pascalCase(req.params[builder.model.slice(1)]) : builder.model
|
|
562
|
+
const data = await helper({ model, req, reply, options })
|
|
563
|
+
data.success = true
|
|
564
|
+
data.statusCode = method === 'create' ? 201 : 200
|
|
565
|
+
options.forFind = true
|
|
566
|
+
return me.transformResult({ data, req, reply, options })
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
mod.config = builder.config ?? {}
|
|
570
|
+
mod.config.pathSrc = url
|
|
571
|
+
mods.push(await me._serveRoute({ mod, ns, parent }))
|
|
572
|
+
return mods
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* Build a route based on the HTTP verb.
|
|
577
|
+
*
|
|
578
|
+
* @async
|
|
579
|
+
* @method
|
|
580
|
+
* @param {Object} options - The options for building the route
|
|
581
|
+
* @param {string} options.file - The file path of the route configuration
|
|
582
|
+
* @param {Object} options.appCtx - The application context
|
|
583
|
+
* @param {string} options.dir - The directory of the route configuration
|
|
584
|
+
* @param {string} options.pathPrefix - The path prefix for the route
|
|
585
|
+
* @param {string} options.ns - The namespace
|
|
586
|
+
* @param {string} options.alias - The alias for the route
|
|
587
|
+
* @param {string} [options.parent] - The parent namespace
|
|
588
|
+
* @returns {Object} The built route
|
|
589
|
+
*/
|
|
590
|
+
_routeByVerb = async (options) => {
|
|
591
|
+
const { file, appCtx, dir, pathPrefix, ns, alias, parent } = options
|
|
592
|
+
const me = this
|
|
593
|
+
const url = path.dirname(file).replace(`${dir}/extend/${pathPrefix}`, '')
|
|
594
|
+
.replaceAll('@@', '*').replaceAll('@', ':')
|
|
595
|
+
const action = path.basename(file, '.js')
|
|
596
|
+
let method = methodMap[action]
|
|
597
|
+
if (this.enablePatch) {
|
|
598
|
+
if (action === 'update') method = 'PATCH'
|
|
599
|
+
else if (action === 'replace') method = 'PUT'
|
|
600
|
+
}
|
|
601
|
+
let mod = await importModule(file)
|
|
602
|
+
if (isFunction(mod)) {
|
|
603
|
+
if (mod.length <= 1) mod = await mod.call(this)
|
|
604
|
+
else mod = { handler: mod }
|
|
605
|
+
}
|
|
606
|
+
mod.url = mod.url ?? url
|
|
607
|
+
if (isFunction(mod.url)) mod.url = await mod.url.call(this)
|
|
608
|
+
mod.method = method
|
|
609
|
+
const defSchema = {
|
|
610
|
+
description: this.docSchemaDescription(action),
|
|
611
|
+
tags: [alias.toUpperCase()],
|
|
612
|
+
response: {}
|
|
613
|
+
}
|
|
614
|
+
if (['create', 'update', 'replace'].includes(action)) {
|
|
615
|
+
defSchema.response['4xx'] = {
|
|
616
|
+
description: this.t('docErrorResponse'),
|
|
617
|
+
$ref: '4xxResp#'
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
defSchema.response['5xx'] = {
|
|
621
|
+
description: this.t('generalErrorResponse'),
|
|
622
|
+
$ref: '5xxResp#'
|
|
623
|
+
}
|
|
624
|
+
if (action === 'find') defSchema.querystring = { $ref: 'qsFilter#' }
|
|
625
|
+
if (isFunction(mod.schema)) mod.schema = await mod.schema.call(this, appCtx)
|
|
626
|
+
mod.schema = merge({}, defSchema, mod.schema ?? {})
|
|
627
|
+
const oldHandler = mod.handler
|
|
628
|
+
mod.handler = async function (req, reply) {
|
|
629
|
+
const { fields } = getParams(req)
|
|
630
|
+
const options = { fields, dynHooks: mod.hooks ?? [] }
|
|
631
|
+
extractHeaders.call(me, req, options)
|
|
632
|
+
if (method === 'find') options.forFind = true
|
|
633
|
+
if (method === 'replace') options.partial = false
|
|
634
|
+
const data = await oldHandler.call(me.app[ns], req, reply, options, this)
|
|
635
|
+
if (!data) return
|
|
636
|
+
data.success = true
|
|
637
|
+
data.statusCode = method === 'create' ? 201 : 200
|
|
638
|
+
return me.transformResult({ data, req, reply, options })
|
|
639
|
+
}
|
|
640
|
+
if (mod.schema === false) delete mod.schema
|
|
641
|
+
mod.config = mod.config ?? {}
|
|
642
|
+
mod.config.pathSrc = url
|
|
643
|
+
return me._serveRoute({ mod, ns, parent })
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* Handle sub-applications by collecting and registering their routes.
|
|
648
|
+
*
|
|
649
|
+
* @async
|
|
650
|
+
* @method
|
|
651
|
+
*/
|
|
652
|
+
_handleSubApp = async () => {
|
|
653
|
+
const { collectWebApps } = await importModule('waibu:/lib/helper.js', { asDefaultImport: false })
|
|
654
|
+
const mods = await collectWebApps.call(this.app[this.ns], 'boot.js', 'waibuRestApi')
|
|
655
|
+
for (const m of mods) {
|
|
656
|
+
this.log.debug('bootSubApp%s', m.ns)
|
|
657
|
+
await this.webAppCtx.register(async (subCtx) => {
|
|
658
|
+
this.app[m.ns].instance = subCtx
|
|
659
|
+
await runHook(`${this.ns}.${m.alias}:afterCreateContext`, subCtx)
|
|
660
|
+
await m.handler.call(this.app[m.ns], subCtx, m.prefix)
|
|
661
|
+
}, { prefix: m.prefix })
|
|
662
|
+
}
|
|
663
|
+
}
|
|
174
664
|
}
|
|
175
665
|
|
|
176
666
|
return WaibuRestApi
|
|
177
667
|
}
|
|
178
668
|
|
|
179
669
|
export default factory
|
|
180
|
-
</code></pre></article></section></div></div></div><div class="search-container" id="PkfLWpAbet" style="display:none"><div class="wrapper" id="iCxFxjkHbP"><button class="icon-button search-close-button" id="VjLlGakifb" aria-label="close search"><svg><use xlink:href="#close-icon"></use></svg></button><div class="search-box-c"><svg><use xlink:href="#search-icon"></use></svg> <input type="text" id="vpcKVYIppa" class="search-input" placeholder="Search..." autofocus></div><div class="search-result-c" id="fWwVHRuDuN"><span class="search-result-c-text">Type anything to view search result</span></div></div></div><div class="mobile-menu-icon-container"><button class="icon-button" id="mobile-menu" data-isopen="false" aria-label="menu"><svg><use xlink:href="#menu-icon"></use></svg></button></div><div id="mobile-sidebar" class="mobile-sidebar-container"><div class="mobile-sidebar-wrapper"><a href="/" class="sidebar-title sidebar-title-anchor">WaibuRestApi API</a><div class="mobile-nav-links"><div class="navbar-item"><a id="" href="https://www.npmjs.com/package/waibu-rest-api" target="">NPM</a></div><div class="navbar-item"><a id="" href="https://github.com/ardhi/waibu-rest-api" target="">Github</a></div><div class="navbar-item"><a id="" href="https://waibu.bajo.app/" target="">Waibu</a></div><div class="navbar-item"><a id="" href="https://bajo.app/" target="">Bajo</a></div></div><div class="mobile-sidebar-items-c"><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-classes"><div>Classes</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="WaibuRestApi.html">WaibuRestApi</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-global"><div>Global</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="global.html#factory">factory</a></div></div></div><div class="mobile-navbar-actions"><div class="navbar-right-item"><button class="icon-button search-button" aria-label="open-search"><svg><use xlink:href="#search-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button theme-toggle" aria-label="toggle-theme"><svg><use class="theme-svg-use" xlink:href="#dark-theme-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button font-size" aria-label="change-font-size"><svg><use xlink:href="#font-size-icon"></use></svg></button></div></div></div></div><script type="text/javascript" src="scripts/core.min.js"></script><script src="scripts/search.min.js" defer="defer"></script><script src="scripts/third-party/fuse.js" defer="defer"></script><script type="text/javascript">var tocbotInstance=tocbot.init({tocSelector:"#eed4d2a0bfd64539bb9df78095dec881",contentSelector:".main-content",headingSelector:"h1, h2, h3",hasInnerContainers:!0,scrollContainer:".main-content",headingsOffset:130,onClick:bringLinkToView})</script></body></html>
|
|
670
|
+
</code></pre></article></section></div></div></div><div class="search-container" id="PkfLWpAbet" style="display:none"><div class="wrapper" id="iCxFxjkHbP"><button class="icon-button search-close-button" id="VjLlGakifb" aria-label="close search"><svg><use xlink:href="#close-icon"></use></svg></button><div class="search-box-c"><svg><use xlink:href="#search-icon"></use></svg> <input type="text" id="vpcKVYIppa" class="search-input" placeholder="Search..." autofocus></div><div class="search-result-c" id="fWwVHRuDuN"><span class="search-result-c-text">Type anything to view search result</span></div></div></div><div class="mobile-menu-icon-container"><button class="icon-button" id="mobile-menu" data-isopen="false" aria-label="menu"><svg><use xlink:href="#menu-icon"></use></svg></button></div><div id="mobile-sidebar" class="mobile-sidebar-container"><div class="mobile-sidebar-wrapper"><a href="/" class="sidebar-title sidebar-title-anchor">WaibuRestApi API</a><div class="mobile-nav-links"><div class="navbar-item"><a id="" href="https://www.npmjs.com/package/waibu-rest-api" target="">NPM</a></div><div class="navbar-item"><a id="" href="https://github.com/ardhi/waibu-rest-api" target="">Github</a></div><div class="navbar-item"><a id="" href="https://waibu.bajo.app/" target="">Waibu</a></div><div class="navbar-item"><a id="" href="https://bajo.app/" target="">Bajo</a></div></div><div class="mobile-sidebar-items-c"><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-classes"><div>Classes</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="WaibuRestApi.html">WaibuRestApi</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-global"><div>Global</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="global.html#TConfig">TConfig</a></div><div class="sidebar-section-children"><a href="global.html#factory">factory</a></div></div></div><div class="mobile-navbar-actions"><div class="navbar-right-item"><button class="icon-button search-button" aria-label="open-search"><svg><use xlink:href="#search-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button theme-toggle" aria-label="toggle-theme"><svg><use class="theme-svg-use" xlink:href="#dark-theme-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button font-size" aria-label="change-font-size"><svg><use xlink:href="#font-size-icon"></use></svg></button></div></div></div></div><script type="text/javascript" src="scripts/core.min.js"></script><script src="scripts/search.min.js" defer="defer"></script><script src="scripts/third-party/fuse.js" defer="defer"></script><script type="text/javascript">var tocbotInstance=tocbot.init({tocSelector:"#eed4d2a0bfd64539bb9df78095dec881",contentSelector:".main-content",headingSelector:"h1, h2, h3",hasInnerContainers:!0,scrollContainer:".main-content",headingsOffset:130,onClick:bringLinkToView})</script></body></html>
|