diff --git a/cheetah.ts b/cheetah.ts index 27c125b..7708975 100644 --- a/cheetah.ts +++ b/cheetah.ts @@ -262,12 +262,22 @@ export class cheetah extends base() { if (this.#versioning.type === 'uri') { const arr = pathname.replace('/', '').split('/') - if (regex.test(arr[0])) { - const version = arr[0] + if (this.#base) { + if (regex.test(arr[1])) { + const version = arr[1] - arr.shift() + arr.splice(1, 1) - return { version, pathname: '/' + arr.join('/') } + return { version, pathname: '/' + arr.join('/') } + } + } else { + if (regex.test(arr[0])) { + const version = arr[0] + + arr.shift() + + return { version, pathname: '/' + arr.join('/') } + } } return { version: this.#versioning.current, pathname } diff --git a/test/routing/versioning.test.ts b/test/routing/versioning.test.ts index 26e21e1..74090ad 100644 --- a/test/routing/versioning.test.ts +++ b/test/routing/versioning.test.ts @@ -4,7 +4,7 @@ import { assertEquals } from '../deps.ts' Deno.test('versioning', async (t) => { /* uri ---------------------------------------------------------------------- */ - await t.step('uri', async () => { + await t.step('uri', async (t) => { const app = new cheetah({ versioning: { current: 'v4', // latest @@ -85,6 +85,32 @@ Deno.test('versioning', async (t) => { assertEquals(await get('/v2/7'), 'hello from v2') assertEquals(await get('/v3/7'), 'hello from v3') assertEquals(await get('/v4/7') !== 'hello from v4', true) + + // with base path + const app2 = new cheetah({ + base: '/api', + versioning: { + current: 'v4', // latest + type: 'uri', + }, + }) + + async function get2(path: string) { + const res = await app2.fetch(new Request(`http://localhost${path}`)) + + return await res.text() + } + + app2.get( + '/7', + { gateway: 'v2...v3' }, + (c) => `hello from v${c.req.gateway}`, + ) + + assertEquals(await get2('/api/v1/7') !== 'hello from v1', true) + assertEquals(await get2('/api/v2/7'), 'hello from v2') + assertEquals(await get2('/api/v3/7'), 'hello from v3') + assertEquals(await get2('/api/v4/7') !== 'hello from v4', true) }) /* header ------------------------------------------------------------------- */