Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
fix(versioning): respect base path
Browse files Browse the repository at this point in the history
  • Loading branch information
boywithkeyboard committed Aug 30, 2023
1 parent c2f50d7 commit ab5fcc1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
18 changes: 14 additions & 4 deletions cheetah.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,22 @@ export class cheetah extends base<cheetah>() {
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 }
Expand Down
28 changes: 27 additions & 1 deletion test/routing/versioning.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ------------------------------------------------------------------- */
Expand Down

0 comments on commit ab5fcc1

Please sign in to comment.