request-path.mjs
35 lines 1.1 KB
Raw
1 /**
2 * Canonical HTTP path for gateway → canister proxy.
3 * Under app.use('/api/v1', handler), Express sets req.path to the suffix (e.g. /notes);
4 * serverless-http / Netlify may leave req.originalUrl inconsistent. Prefer baseUrl + path.
5 */
6
7 /**
8 * @param {import('express').Request} req
9 * @returns {string} pathname only (no query)
10 */
11 export function effectiveRequestPath(req) {
12 const combined = (req.baseUrl || '') + (req.path || '');
13 const noQuery = combined.split('?')[0];
14 if (noQuery.startsWith('/api/v1')) return noQuery;
15 const raw = (req.originalUrl || req.url || '/').split('?')[0];
16 return raw;
17 }
18
19 /**
20 * Path + query for upstream canister (preserve search from originalUrl).
21 * @param {import('express').Request} req
22 */
23 export function upstreamPathAndQuery(req) {
24 const raw = req.originalUrl || req.url || '/';
25 const q = raw.indexOf('?');
26 const search = q >= 0 ? raw.slice(q) : '';
27 return effectiveRequestPath(req) + search;
28 }
29
30 /**
31 * @param {import('express').Request} req
32 */
33 export function pathPartNoQuery(req) {
34 return effectiveRequestPath(req);
35 }
File History 1 commit