Waibu

Waibu class definition.

This class provides methods and properties for managing web applications, routes, and configurations within the Bajo framework. You should NOT use this class to create routes etc, instead of that, use the following webapps to extend your plugin:

  • waibuMpa - Provide everything to serve multi-page web applications
  • waibuStatic - Provide everything to serve static & virtual files
  • waibuApi - Provide everything to serve REST API endpoints

Only use this class if you want to create your own webapp or if you really need to use the fastify context root directly.

Constructor

new Waibu()

Members

config :Waibu.TConfig

Configuration object. To override the default configuration, you can create data/config/waibu.json

escapeChars :Waibu.TEscapeChars

hookTypes :Array.<string>

Type:
  • Array.<string>
Default Value
  • ['onRequest', 'onResponse', 'preParsing', 'preValidation', 'preHandler', 'preSerialization', 'onSend', 'onTimeout', 'onError']

qs :Object

Query string parser and stringifier. It is a wrapper of query-string package.

Type:
  • Object
Properties
NameTypeDescription
qs.parsefunction

Query string parser

qs.parseUrlfunction

Query string url parser

qs.stringifyfunction

Query string stringifier

qs.stringifyUrlfunction

Query string url stringifier

Methods

(async) _handleAppHook() → {Promise.<void>}

Handle app hooks. It will run hooks for each web app and main app.

Returns:
Type: 
Promise.<void>

(async) _handleError() → {Promise.<void>}

Route error handler.

Returns:
Type: 
Promise.<void>

(async) _handleFavicon() → {Promise.<void>}

Create route for '/favicon.:ext'

Location of favicon file can be found in:

  1. main plugin's file; if not found, then
  2. site attachment; if not found, then
  3. static dir of theme; if not found, then
  4. default plugin's file
Returns:
Type: 
Promise.<void>

(async) _handleForward() → {Promise.<void>}

Handle forward. It will decorate reply object with forwardTo method.

Returns:
Type: 
Promise.<void>

(async) _handleHome() → {Promise.<void>}

Create route for '/' to redirect to home path.

Returns:
Type: 
Promise.<void>

(async) _handleNotFound() → {Promise.<void>}

Route not found handler.

Returns:
Type: 
Promise.<void>

(async) _handleRedirect(options) → {Promise.<void>}

Handle redirect. It will decorate reply object with redirectTo method.

Parameters:
NameTypeDescription
optionsobject
Returns:
Type: 
Promise.<void>

(async) _handleRobotsTxt() → {Promise.<void>}

Create route for '/robots.txt'.

Location of robots.txt file can be found in:

  1. main plugin's file; if not found, then
  2. site attachment; if not found, then
  3. default plugin's file
Returns:
Type: 
Promise.<void>

_printRoutes() → {void}

Print all registered routes.

Returns:
Type: 
void

(async) _runWebApps() → {Promise.<void>}

Run web applications.

Returns:
Type: 
Promise.<void>

base64JsonDecode(data) → {Object}

Decode base64 encoded json string

Parameters:
NameTypeDescription
datastring

Base64 encoded JSON string

Returns:

Decoded JSON object

Type: 
Object

base64JsonEncode(data) → {string}

Encode JSON object to base64 string

Parameters:
NameTypeDescription
dataObject

JSON object to encode

Returns:

Base64 encoded JSON string

Type: 
string

escape(text) → {string}

Escape text

Parameters:
NameTypeDescription
textstring
Returns:
Type: 
string

(async) exit()

Exit handler

(async) fetch(url, optsopt, extraopt) → {Object}

Fetch something from url. A wrapper of bajo-extra's fetchUrl which support bajo's ns based url.

Parameters:
NameTypeAttributesDefaultDescription
urlstring

Also support ns based url

optsObject<optional>
{}

node's fetch options

extraObject<optional>
{}

See bajo-extra

Returns:
Type: 
Object

findRoute(name) → {Object}

Find route by route name

Parameters:
NameTypeDescription
namestring

ns based route name

Returns:

Route object

Type: 
Object

getHostname(req) → {string}

Get hostname from fastify's request object

Parameters:
NameTypeDescription
reqObject
Returns:
Type: 
string

getIp(req) → {string}

Get visitor IP from fastify's request object

Parameters:
NameTypeDescription
reqObject

request object

Returns:
Type: 
string

getOrigin(req) → {string}

Get origin from fastify's request object

Parameters:
NameTypeDescription
reqObject
Returns:
Type: 
string

getPluginByPrefix(prefix, nsOnly) → {Object}

Get plugin by prefix

Parameters:
NameTypeDescription
prefixstring
nsOnlyboolean

Set true to return plugin's namespace only

Returns:
Type: 
Object

getPluginPrefix(name, webAppopt) → {string}

Get plugin's prefix by name

Parameters:
NameTypeAttributesDefaultDescription
namestring

Plugin's name

webAppstring<optional>
waibuMpa

Web app to use

Returns:
Type: 
string

getRoutes(groupedopt, liteopt) → {Array}

Get all available routes

Parameters:
NameTypeAttributesDefaultDescription
groupedboolean<optional>
false

Returns as groups of urls and methods

lite*<optional>
false

Retuns only urls and methods

Returns:
Type: 
Array

getUploadedFiles(reqId, fileUrlopt, returnDir) → {Object|Array}

Get uploaded files by request ID

Parameters:
NameTypeAttributesDefaultDescription
reqIdstring

Request ID

fileUrlboolean<optional>
false

If true, files returned as file url format (file:///...)

returnDir*

If true, also return its directory

Returns:
  • Returns object if returnDir is true, array of files otherwise
Type: 
Object | Array

(async) init()

Initialize plugin

isIntlPath(ns) → {boolean}

Is namespace's path contains language detector token?

Parameters:
NameTypeDescription
nsstring

Plugin name

Returns:
Type: 
boolean

parseFilter(req) → {Object}

Parse filter found from Fastify's request based on keys set in config object

Parameters:
NameTypeDescription
reqObject

Request object

Returns:
Type: 
Object

routeDir(ns, baseNsopt) → {string}

Get route directory by plugin's name

Parameters:
NameTypeAttributesDescription
ns*

Namespace

baseNs*<optional>

Base namespace. If not provided, defaults to scope's ns

Returns:
Type: 
string

routePath(name, optionsopt) → {string}

Get route path by route's name:

  • If it is a mailto: or tel: url, it returns as is
  • If it starts with :/, name will be prefixed with its ns automatically
  • If it is a ns based name, it will be parsed first
Parameters:
NameTypeAttributesDefaultDescription
namestring
optionsObject<optional>
{}

Options object

Properties
NameTypeAttributesDefaultDescription
nsstring<optional>
waibu

Base namespace

guessHostboolean<optional>

If true, guest host if host is not set

queryObject<optional>
{}

Query string's object. If provided, it will be added to returned value

paramsObject<optional>
{}

Parameter object. If provided, it will be merged to returned value

Returns:
Type: 
string

(async) start()

Start plugin

unescape(text) → {string}

Unescape text using TEscapeChars rules

Parameters:
NameTypeDescription
textstring

Text to unescape

Returns:
Type: 
string

unescapeBlock(content, start, end, startReplacer, endReplacer) → {string}

Recursively unescape block of texts

Parameters:
NameTypeDescription
contentstring

Source content

startstring

Block's start

endstring

Block's end

startReplacerstring

Token to use as block's start replacer

endReplacerstring

Token to use as block's end replacer

Returns:
Type: 
string

Type Definitions

TConfig

Type:
  • Object
Properties
NameTypeAttributesDefaultDescription
homeObject

Home configuration

serverObject

Server configuration

Properties
NameTypeAttributesDefaultDescription
hoststring<optional>
'127.0.0.1'

Server host

portnumber<optional>
17845

Server port

factoryObject

Factory configuration. @see https://fastify.dev/docs/latest/Reference/Server/#factory

intlObject

Internationalization configuration

Properties
NameTypeAttributesDefaultDescription
detectorsArray.<string><optional>
['qs']

Language detectors

logObject

Logging configuration

Properties
NameTypeAttributesDefaultDescription
disableArray.<string><optional>
[]

Disabled loggers. Available values: 'request' & 'response'

deferboolean<optional>
false

Defer logging

prefixVirtualstring

Prefix for virtual routes

faviconboolean | string<optional>
true

Favicon configuration

robotsTxtboolean<optional>
true

Set to 'false' to disable robots.txt route

qsKeyObject

Query string key configuration

Properties
NameTypeAttributesDefaultDescription
bboxstring<optional>
'bbox'

Bounding box key

bboxLatFieldstring<optional>
'bboxLatField'

Bounding box latitude field key

bboxLngFieldstring<optional>
'bboxLngField'

Bounding box longitude field key

querystring<optional>
'query'

Query key

searchstring<optional>
'search'

Search key

skipstring<optional>
'skip'

Skip key

pagestring<optional>
'page'

Page key

limitstring<optional>
'limit'

Limit key

sortstring<optional>
'sort'

Sort key

fieldsstring<optional>
'fields'

Fields key

langstring<optional>
'lang'

Language key

paramsCharMapObject<optional>
{}

Parameters character map

routeObject<optional>
{}

Route configuration

Properties
NameTypeAttributesDefaultDescription
printboolean<optional>
true

Print route information

disabledArray.<string><optional>
[]

Disabled routes

pageTitleFormatstring<optional>
'%s : %s'

Page title format

siteInfoObject<optional>
{}

Site information

Properties
NameTypeAttributesDefaultDescription
titlestring<optional>
'My Website'

Site title

orgNamestring<optional>
'My Organization'

Organization name

corsObject<optional>
{}

CORS configuration

compressObject<optional>
{}

Compression configuration

helmetObject<optional>
{}

Helmet configuration

rateLimitObject<optional>
{}

Rate limit configuration

multipartObject<optional>
{}

Multipart configuration

exposeErrorboolean<optional>
false

Expose error configuration

underPressureboolean<optional>
false

Under pressure configuration

forwardOptsObject<optional>
{}

Forward options configuration

Properties
NameTypeAttributesDefaultDescription
disableRequestLoggingboolean<optional>
true

Disable request logging for forward requests

undiciObject<optional>
{}

Undici configuration for forward requests

TEscapeChars

Type:
  • Object
Properties
NameTypeDefaultDescription
<string&lt;
>string&gt;
"string&quot;
'string&apos;