From 9554ebc4d1b33bc8fd2af8093f8f0d149b994e15 Mon Sep 17 00:00:00 2001 From: Chris Millar Date: Thu, 28 Mar 2024 14:32:55 -0700 Subject: [PATCH] GH-24 - Allow /api path prefix * Support prefixing all routes with `/api` Resolves: GH-24 --- src/utils/daCtx.js | 4 +++- test/utils/daCtx.test.js | 12 ++++++++++++ test/utils/mocks/req.js | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/utils/daCtx.js b/src/utils/daCtx.js index 46b3df6..9bb9f4a 100644 --- a/src/utils/daCtx.js +++ b/src/utils/daCtx.js @@ -18,7 +18,9 @@ import { getUsers, isAuthorized } from './auth.js'; * @returns {DaCtx} The Dark Alley Context. */ export default async function getDaCtx(req, env) { - const { pathname } = new URL(req.url); + let { pathname } = new URL(req.url); + // Remove proxied api route + if (pathname.startsWith('/api')) pathname = pathname.replace('/api', ''); const users = await getUsers(req, env); diff --git a/test/utils/daCtx.test.js b/test/utils/daCtx.test.js index 90b3605..84ec099 100644 --- a/test/utils/daCtx.test.js +++ b/test/utils/daCtx.test.js @@ -12,6 +12,18 @@ const getDaCtx = await esmock( ); describe('Dark Alley context', () => { + describe('API context', async () => { + let daCtx; + + before(async () => { + daCtx = await getDaCtx(reqs.api, env); + }); + + it('should remove api from path name', () => { + assert.strictEqual(daCtx.api, 'source'); + }); + }); + describe('Org context', async () => { let daCtx; diff --git a/test/utils/mocks/req.js b/test/utils/mocks/req.js index 229cb6f..cb67781 100644 --- a/test/utils/mocks/req.js +++ b/test/utils/mocks/req.js @@ -45,6 +45,7 @@ const optsWithForceFail = { }; const reqs = { + api: new Request('https://da.live/api/source/cq/', optsWithEmptyHead), org: new Request('https://da.live/source/cq/', optsWithEmptyHead), site: new Request('https://da.live/source/cq/Geometrixx', optsWithAuth), folder: new Request('https://da.live/source/cq/Geometrixx/NFT/', optsWithExpAuth),