Skip to content

View API ​

A view is an HTML page that the XYZ API serves from a template in workspace.templates. The browser loads the MAPP libraries and runs the view's script. The API does not render the map; it only fills in a few .

Behaviour ​

  • Routes. ${DIR}/view/:template, ${DIR}/view?template=…, ${DIR}/ and / all call the view module.[1] Without a template param the view is default_view. With a DIR, a request for / is redirected to DIR.
  • Core view templates. default_view is _default.html; login_view, register_view and password_reset_view are _login.html; user_admin_view is _user.html.[2] Workspace templates with the same key replace them, and so do CUSTOM_TEMPLATES. See templates.
  • Template lookup. The template is a string template, a src loaded on first use (eg. file:/public/views/_default.html, resolved from XYZ_CWD), or a language template with one HTML string or src per language code. An unknown template key returns 400 with the error message.[3]
  • Access. The view routes pass through the authentication middleware. A PRIVATE instance redirects users without a login to the login view or AUTH_PATH. The view module does not check roles on the template.
  • Substitution. is replaced with the param of that name, or with an empty string when there is no such param.[4] The params are the URL path and query params plus:
ParamValue
The DIR environment variable, the path prefix of the instance
The TITLE environment variable
The AUTH_PATH environment variable of a custom login provider
true if PRIVATE or PUBLIC is set, ie. the instance has an ACL
The language URL param if it is two lower case letters, otherwise en
The template key
The language template of the msg param, used by the login view

Only param values which are strings of A-Z a-z 0-9 _ - are substituted. The values true, false and null are parsed into non-strings, and [a,b] into arrays, so these are removed too.[5] The user is never a param; the browser requests it from /api/user/cookie.

Flow ​

router.js → validateRequestParams.js (query into params, language defaults to en) → validateRequestAuth.js (user, PRIVATE redirect) → view.js → languageTemplates.js → getTemplate.js (cache, src) → substitution → HTML response.

Where to change ​

  • Params and substitution: apps/xyz/mod/view.js.
  • Core view keys: apps/xyz/mod/workspace/templates/_views.js.
  • The core views themselves: public/views/; see core views.

Tests ​

apps/xyz/tests/mod/view.test.mjs loads tests/assets/view.json: the default view returns 200, defaults to en, and a param with a character outside the whitelist is removed. The default view in the browser is tested by apps/ui/tests/default.spec.mjs.

Pitfalls ​

  • A placeholder name must be a letter followed by letters and digits. or NaN does not match, so it stays in the HTML as written.
  • A param value with a dot, space or comma is removed, eg. lat=51.5 or a nested locale=UK,London. Read such values in the browser from URL hooks instead.
  • does not fall back to the user's language: validateRequestParams sets language to en first, so only the URL param counts. The default view applies the user's language in the browser.
  • Views are cached with the workspace. A changed src file is read again only after WORKSPACE_AGE or a cache reset.
  • The public directory is served at both ${DIR}/public and ${DIR}. The core views use /public/…; do the same in custom views.

  1. router.js createRouter and middleware/rootRedirect.js ↩︎

  2. workspace/templates/_views.js ↩︎

  3. utils/languageTemplates.js with the view_template flag ↩︎

  4. view.js ↩︎

  5. middleware/validateRequestParams.js ↩︎

XYZ v5.1.0. Generated from knowledge/ at dd7a6fa27736502733499c1bf485f15659b10338. Concepts marked draft are not reviewed.