From 17ecb84fbe0aa72d7a3a2cec4e5aaa94aa42c683 Mon Sep 17 00:00:00 2001 From: Samuel Kopp <62482066+boywithkeyboard@users.noreply.github.com> Date: Fri, 18 Aug 2023 23:13:34 +0200 Subject: [PATCH] fix outstanding issues --- cheetah.ts | 25 +++++++++++++++---------- test/routing/versioning.test.ts | 6 ++++-- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/cheetah.ts b/cheetah.ts index 873b7a4..7e1bbc0 100644 --- a/cheetah.ts +++ b/cheetah.ts @@ -250,15 +250,17 @@ export class cheetah extends base() { const regex = /^v[1-9][0-9]?$|^100$/ if (this.#versioning.type === 'uri') { - pathname = pathname.replace('/', '') + const arr = pathname.replace('/', '').split('/') - if (regex.test(pathname.split('/')[0])) { - pathname.split('/').shift() + if (regex.test(arr[0])) { + const version = arr[0] - return { version: pathname.split('/')[0], pathname: '/' + pathname } + arr.shift() + + return { version, pathname: '/' + arr.join('/') } } - return { version: this.#versioning.current, pathname: '/' + pathname } + return { version: this.#versioning.current, pathname } } const header = headers.get(this.#versioning.header) @@ -281,18 +283,18 @@ export class cheetah extends base() { this.#preflight && request.method === 'HEAD' && r[0] === 'GET' ) { if (this.#versioning) { - const options = typeof r[3][0] !== 'function' ? r[3][0] : null - const { pathname, version } = this.#parseVersion(request.headers, p) if ( parseInt(version.replace('v', '')) > parseInt(this.#versioning.current.replace('v', '')) ) { - return null + break } - if (options !== null && options.versionRange !== undefined) { + const options = typeof r[3][0] !== 'function' ? r[3][0] : null + + if (options?.versionRange !== undefined) { const result = pathname.match(r[2]) if (!result) { @@ -305,7 +307,7 @@ export class cheetah extends base() { ) if (!gateway) { - return null + break } return { @@ -376,6 +378,7 @@ export class cheetah extends base() { runtime: this.#runtime, oauth: this.#oauth, versioning: this.#versioning, + gateway: -1, } if (this.#extensions.size > 0) { @@ -444,6 +447,8 @@ export class cheetah extends base() { __app.request.pathname, ) + __app.gateway = route?.gateway ?? -1 + if (!route) { if (!this.#notFound) { throw new Exception('Not Found', undefined, 404) diff --git a/test/routing/versioning.test.ts b/test/routing/versioning.test.ts index c166626..4b6e2ed 100644 --- a/test/routing/versioning.test.ts +++ b/test/routing/versioning.test.ts @@ -25,7 +25,8 @@ Deno.test('versioning', async (t) => { assertEquals(await get('/v1/1'), 'hello from v1') assertEquals(await get('/v2/1'), 'hello from v2') assertEquals(await get('/v3/1'), 'hello from v3') - assertEquals(await get('/v4/1') !== 'hello from v4', true) + assertEquals(await get('/v4/1'), 'hello from v4') + assertEquals(await get('/v5/1') !== 'hello from v5', true) // exact @@ -118,7 +119,8 @@ Deno.test('versioning', async (t) => { assertEquals(await get('/v1/1'), 'hello from v1') assertEquals(await get('/v2/1'), 'hello from v2') assertEquals(await get('/v3/1'), 'hello from v3') - assertEquals(await get('/v4/1') !== 'hello from v4', true) + assertEquals(await get('/v4/1'), 'hello from v4') + assertEquals(await get('/v5/1') !== 'hello from v5', true) // exact