XYZ API
XYZ is the Node.js API in apps/xyz (@geolytix/xyz-app). It serves the workspace, runs SQL query templates, authenticates users, and serves the MAPP views and bundles. MAPP and MAPP.UI are its browser clients; this page is the server side of that boundary.
Entry points
- router.js
createRouter(middleWare)returns an Express router; the middleware array runs before each endpoint module.[1] - server.js creates the router with the default chain
rootRedirect,validateRequestParams,validateRequestAuth, mounts it, and listens onPORTunless it runs on Vercel. It exports the app as default and the router asxyzRouter. - A host app extends XYZ instead of forking it. apps/auth creates its own router with
createRouter()and adds custom routes; apps/saml imports the default app and adds the${DIR}/saml/*routes to it. - The package exports only
./router,./serverand./mod/*. Importingmod/utils/processEnv.jsassigns the frozenglobalThis.xyzEnv. Modules read configuration fromxyzEnv. Exceptions readprocess.envdirectly:processEnv.jsitself,telemetry.jsfor the standardOTEL_*and platform variables, and_workspace.jsforVITEST. See Server and the other environment concepts.
Request flow
Router-level handlers run in this order for every request:
corsadds CORS headers for origins inCORS_ORIGINS; without it, it does nothing.[2]- The rate limiter (
RATE_LIMIT,RATE_LIMIT_WINDOW), then the cookie parser. ${DIR}/apiresponses getCache-Control: private, no-cache.- Static files:
${DIR}/publicand${DIR}frompublic/. requestSpantraces the rest; then body parser, middleware chain and endpoint.
The default chain:
rootRedirectredirects/toDIR.validateRequestParamsmerges path and query params, rejects keys outside[A-Za-z0-9_-], deletes auserparam, parsestrue/false/nulland[a,b]arrays, defaultslanguagetoen, and stores the result onreq._params.[3]validateRequestAuthcopiesreq._paramstoreq.paramsand setsreq.params.userfrom a token or cookie. An auth error returns 401. APRIVATEinstance redirects anonymous requests toAUTH_PATHor/api/user/login.[4]
The workspace, query, user, provider, sign and view modules also copy req._params, so they work behind a chain without validateRequestAuth.
Routes and modules
| Route | Module | Notes |
|---|---|---|
api/workspace{/:key} | workspace/_workspace.js | locales, locale, layer; admin only: scopes, test, compose, templates. roles returns 410. |
api/query{/:template} | query.js | GET and POST. Runs a SQL template against a DBS_* connection. |
api/user{/:method}{/:key} | user/_user.js | Account methods, eg. admin, cookie, key, token, update. |
api/user/login, logout, register, verify{/:key} | user/ | Mounted before api/user and without the middleware chain. |
api/provider{/:provider} | provider/_provider.js | cloudfront, file, s3. |
api/sign{/:signer} | sign/_sign.js | cloudfront, cloudinary, file, s3, plus one file signer per SIGN_* variable. |
view{/:template}, ${DIR}/, / | view.js | Serves a view template; default_view without a template. |
All routes are prefixed with DIR. Each dispatcher only calls a method which is an own member of its method map, and rejects any other key.
Modules behind the routes
- workspace/ loads the workspace from
WORKSPACEwith cache.js, caches it forWORKSPACE_AGE, and resolvessrcreferences through provider/getSrc.js. composeObj.js merges templates and applies roles for the requesting user;getLocale,getLayerandgetTemplatebuild on it. See Templates and workspace environment. - workspace/templates/ holds the built-in templates. _queries.js lists the SQL templates and flags (
layer,admin,value_only). Layer formats usewkt,geojson,mvtandcluster; locations uselocation_get,location_new,location_updateandlocation_delete; filters usedistinct_valuesandfield_minmax. _views.js maps view keys topublic/views/*.html. - query.js composes the template for the user, resolves the layer for layer templates, builds the SQL filter with sqlFilter.js, substitutes
${param}as whitelisted strings and%{param}as$nvalues, and sends the query to dbs.js. See Layer data source and Databases. - user/ holds the ACL (acl.js), token and cookie authentication (auth.js) and the account methods. See JWT claims.
- view.js substitutes
in the view with whitelisted string params anddir,authPath,loginandtitlefromxyzEnv.
How XYZ serves MAPP
- The
default_viewsetsdata-dir=""on the head and loadspublic/js/lib/mapp.jsandui.js; MAPP readsmapp.hostfrom it. - The view script requests
api/workspace/localesandapi/workspace/locale?layers=true. MAPP requestsapi/workspace/layerfor a location's layer andapi/queryfor features and locations. MAPP.UI adds filter templates,api/user/*andapi/sign/cloudinary. - The client only sends template and layer keys. SQL,
dbsand role restrictions stay in the workspace on the server. - The bundles are build outputs; see Monorepo. For pages on another origin see the @geolytix/mapp package.
Where to change
- A new endpoint: a route in
router.js, a module undermod/, and a test in router.test.mjs. - A new built-in query: a template file and an entry in
_queries.js. A deployment adds its own inworkspace.templatesinstead. - Param or auth rules: the middleware modules. A host which builds its own chain must keep
validateRequestParamsbeforevalidateRequestAuth. - A new environment variable:
processEnv.jsand an environment concept. - Tests: apps/xyz/tests mirrors
mod/; runpnpm test:xyz, see TESTING.md.