Skip to content

Commit

Permalink
GH-24 - Allow /api path prefix
Browse files Browse the repository at this point in the history
* Support prefixing all routes with `/api`

Resolves: GH-24
  • Loading branch information
auniverseaway committed Mar 28, 2024
1 parent df11d30 commit 9554ebc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/utils/daCtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 12 additions & 0 deletions test/utils/daCtx.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions test/utils/mocks/req.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 9554ebc

Please sign in to comment.