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

Commit

Permalink
refactor: small editorial changes to versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
boywithkeyboard committed Aug 18, 2023
1 parent b43b1f1 commit afb4c21
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cheetah.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export class cheetah extends base<cheetah>() {

const options = typeof r[3][0] !== 'function' ? r[3][0] : null

if (options?.versionRange !== undefined) {
if (options?.gateway !== undefined) {
const result = pathname.match(r[2])

if (!result) {
Expand All @@ -303,7 +303,7 @@ export class cheetah extends base<cheetah>() {

const gateway = isVersionWithinRange(
version as Version,
options.versionRange as VersionRange,
options.gateway as VersionRange,
)

if (!gateway) {
Expand Down
4 changes: 0 additions & 4 deletions context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ export class Context<
return this.#a
}

get gateway(): number {
return this.#a.gateway ?? -1
}

get dev(): boolean {
return this.runtime === 'deno' && Deno.env.get('DEV') === 'true'
}
Expand Down
6 changes: 3 additions & 3 deletions handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function handler<T>() {
transform?: boolean // TODO remove at v2.0
cors?: string
params?: Partial<Record<keyof ExtractParams<Pathname>, ZodType>>
versionRange?: VersionRange
gateway?: VersionRange
})
| Handler<
Pathname,
Expand Down Expand Up @@ -141,7 +141,7 @@ export function bodylessHandler<T>() {
query?: ValidatedQuery
cors?: string
params?: Partial<Record<keyof ExtractParams<Pathname>, ZodType>>
versionRange?: VersionRange
gateway?: VersionRange
}
| BodylessHandler<
Pathname,
Expand All @@ -166,7 +166,7 @@ export type HandlerOrSchema =
transform?: boolean // TODO remove at v2.0
cors?: string
params?: Record<string, ZodType>
versionRange?: VersionRange
gateway?: VersionRange
}
| Handler<unknown>
| BodylessHandler<unknown>
4 changes: 4 additions & 0 deletions request_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export class RequestContext<
this.#e = e
}

get gateway(): number {
return this.#a.gateway ?? -1
}

get ip(): string {
return this.#a.ip
}
Expand Down
32 changes: 16 additions & 16 deletions test/routing/versioning.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Deno.test('versioning', async (t) => {

// unspecified

app.get('/1', (c) => `hello from v${c.gateway}`)
app.get('/1', (c) => `hello from v${c.req.gateway}`)

assertEquals(await get('/v1/1'), 'hello from v1')
assertEquals(await get('/v2/1'), 'hello from v2')
Expand All @@ -30,7 +30,7 @@ Deno.test('versioning', async (t) => {

// exact

app.get('/2', { versionRange: 'v3' }, (c) => `hello from v${c.gateway}`)
app.get('/2', { gateway: 'v3' }, (c) => `hello from v${c.req.gateway}`)

assertEquals(await get('/v1/2') !== 'hello from v1', true)
assertEquals(await get('/v2/2') !== 'hello from v2', true)
Expand All @@ -39,7 +39,7 @@ Deno.test('versioning', async (t) => {

// smaller than

app.get('/3', { versionRange: '< v3' }, (c) => `hello from v${c.gateway}`)
app.get('/3', { gateway: '< v3' }, (c) => `hello from v${c.req.gateway}`)

assertEquals(await get('/v1/3'), 'hello from v1')
assertEquals(await get('/v2/3'), 'hello from v2')
Expand All @@ -48,7 +48,7 @@ Deno.test('versioning', async (t) => {

// smaller than or equal to

app.get('/4', { versionRange: '<= v3' }, (c) => `hello from v${c.gateway}`)
app.get('/4', { gateway: '<= v3' }, (c) => `hello from v${c.req.gateway}`)

assertEquals(await get('/v1/4'), 'hello from v1')
assertEquals(await get('/v2/4'), 'hello from v2')
Expand All @@ -57,7 +57,7 @@ Deno.test('versioning', async (t) => {

// greater than

app.get('/5', { versionRange: '> v3' }, (c) => `hello from v${c.gateway}`)
app.get('/5', { gateway: '> v3' }, (c) => `hello from v${c.req.gateway}`)

assertEquals(await get('/v1/5') !== 'hello from v1', true)
assertEquals(await get('/v2/5') !== 'hello from v2', true)
Expand All @@ -66,7 +66,7 @@ Deno.test('versioning', async (t) => {

// greater than or equal

app.get('/6', { versionRange: '>= v3' }, (c) => `hello from v${c.gateway}`)
app.get('/6', { gateway: '>= v3' }, (c) => `hello from v${c.req.gateway}`)

assertEquals(await get('/v1/6') !== 'hello from v1', true)
assertEquals(await get('/v2/6') !== 'hello from v2', true)
Expand All @@ -77,8 +77,8 @@ Deno.test('versioning', async (t) => {

app.get(
'/7',
{ versionRange: 'v2...v3' },
(c) => `hello from v${c.gateway}`,
{ gateway: 'v2...v3' },
(c) => `hello from v${c.req.gateway}`,
)

assertEquals(await get('/v1/7') !== 'hello from v1', true)
Expand Down Expand Up @@ -114,7 +114,7 @@ Deno.test('versioning', async (t) => {

// unspecified

app.get('/1', (c) => `hello from v${c.gateway}`)
app.get('/1', (c) => `hello from v${c.req.gateway}`)

assertEquals(await get('/v1/1'), 'hello from v1')
assertEquals(await get('/v2/1'), 'hello from v2')
Expand All @@ -124,7 +124,7 @@ Deno.test('versioning', async (t) => {

// exact

app.get('/2', { versionRange: 'v3' }, (c) => `hello from v${c.gateway}`)
app.get('/2', { gateway: 'v3' }, (c) => `hello from v${c.req.gateway}`)

assertEquals(await get('/v1/2') !== 'hello from v1', true)
assertEquals(await get('/v2/2') !== 'hello from v2', true)
Expand All @@ -133,7 +133,7 @@ Deno.test('versioning', async (t) => {

// smaller than

app.get('/3', { versionRange: '< v3' }, (c) => `hello from v${c.gateway}`)
app.get('/3', { gateway: '< v3' }, (c) => `hello from v${c.req.gateway}`)

assertEquals(await get('/v1/3'), 'hello from v1')
assertEquals(await get('/v2/3'), 'hello from v2')
Expand All @@ -142,7 +142,7 @@ Deno.test('versioning', async (t) => {

// smaller than or equal to

app.get('/4', { versionRange: '<= v3' }, (c) => `hello from v${c.gateway}`)
app.get('/4', { gateway: '<= v3' }, (c) => `hello from v${c.req.gateway}`)

assertEquals(await get('/v1/4'), 'hello from v1')
assertEquals(await get('/v2/4'), 'hello from v2')
Expand All @@ -151,7 +151,7 @@ Deno.test('versioning', async (t) => {

// greater than

app.get('/5', { versionRange: '> v3' }, (c) => `hello from v${c.gateway}`)
app.get('/5', { gateway: '> v3' }, (c) => `hello from v${c.req.gateway}`)

assertEquals(await get('/v1/5') !== 'hello from v1', true)
assertEquals(await get('/v2/5') !== 'hello from v2', true)
Expand All @@ -160,7 +160,7 @@ Deno.test('versioning', async (t) => {

// greater than or equal

app.get('/6', { versionRange: '>= v3' }, (c) => `hello from v${c.gateway}`)
app.get('/6', { gateway: '>= v3' }, (c) => `hello from v${c.req.gateway}`)

assertEquals(await get('/v1/6') !== 'hello from v1', true)
assertEquals(await get('/v2/6') !== 'hello from v2', true)
Expand All @@ -171,8 +171,8 @@ Deno.test('versioning', async (t) => {

app.get(
'/7',
{ versionRange: 'v2...v3' },
(c) => `hello from v${c.gateway}`,
{ gateway: 'v2...v3' },
(c) => `hello from v${c.req.gateway}`,
)

assertEquals(await get('/v1/7') !== 'hello from v1', true)
Expand Down

0 comments on commit afb4c21

Please sign in to comment.