Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Widen type defs to allow for nonstring version constraints #183

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare namespace Router {
V1 = 'http1',
V2 = 'http2'
}

type HTTPMethod =
| 'ACL'
| 'BIND'
Expand Down Expand Up @@ -57,17 +57,17 @@ declare namespace Router {
store: any
) => void;

interface ConstraintStrategy<V extends HTTPVersion> {
interface ConstraintStrategy<V extends HTTPVersion, ConstraintVersion = string> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might make more sense to call this property ConstraintValue or something like that. I know it says version in all the types below, but these strategies can actually be used for other constraints than version, like host or maybe the Accept header someday. I think the name of that argument in the types should be changed too.

name: string,
mustMatchWhenDerived?: boolean,
storage() : {
get(version: String) : Handler<V> | null,
set(version: String, store: Handler<V>) : void,
del(version: String) : void,
get(version: ConstraintVersion) : Handler<V> | null,
set(version: ConstraintVersion, store: Handler<V>) : void,
del(version: ConstraintVersion) : void,
empty() : void
},
validate(value: unknown): void,
deriveConstraint<Context>(req: Req<V>, ctx?: Context) : String,
validate?: (value: unknown) => asserts value is ConstraintVersion,
deriveConstraint<Context>(req: Req<V>, ctx?: Context) : ConstraintVersion,
}

interface Config<V extends HTTPVersion> {
Expand All @@ -91,7 +91,7 @@ declare namespace Router {
): void;

constraints? : {
[key: string]: ConstraintStrategy<V>
[key: string]: ConstraintStrategy<V, unknown>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it might be a breaking change, do the fastify type tests still pass with this in place?

}
}

Expand Down
59 changes: 59 additions & 0 deletions test/constraint.custom-nonstring.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict'

const assert = require('assert')
const t = require('tap')
const test = t.test
const FindMyWay = require('..')

const customVersioning = {
name: 'version',
// storage factory
storage: function () {
let versions = []
return {
get: (version) => {
assert(typeof version === 'number')
return versions[version] || null
},
set: (version, store) => {
assert(typeof version === 'number')
versions[version] = store
},
del: (version) => {
assert(typeof version === 'number')
versions[version] = null
},
empty: () => {
versions = []
}
}
},
deriveConstraint: (req, ctx) => {
return Number(req.headers['accept-version'])
}
}

test('Custome deriveConstraint can return non-string versions', (t) => {
t.plan(2)

const findMyWay = FindMyWay({ constraints: { version: customVersioning } })

findMyWay.on('GET', '/', { constraints: { version: 42 } }, (req, res, params) => {
t.strictEqual(req.headers['accept-version'], '42')
})

findMyWay.on('GET', '/', { constraints: { version: 43 } }, (req, res, params) => {
t.strictEqual(req.headers['accept-version'], '43')
})

findMyWay.lookup({
method: 'GET',
url: '/',
headers: { 'accept-version': '42' }
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { 'accept-version': '43' }
})
})
40 changes: 39 additions & 1 deletion test/types/router.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expectType } from 'tsd'
import { expectError, expectType } from 'tsd'
import * as Router from '../../'
import { Http2ServerRequest, Http2ServerResponse } from 'http2'
import { IncomingMessage, ServerResponse } from 'http'
Expand Down Expand Up @@ -111,3 +111,41 @@ let http2Res!: Http2ServerResponse;
expectType<string>(router.prettyPrint())

}

// non-string versions
{
let handler: Router.Handler<Router.HTTPVersion.V2>

const versionConstraint: Router.ConstraintStrategy<Router.HTTPVersion.V2, number> = {
name: 'version',
deriveConstraint() { return 42 },
storage() {
return {
get (version: number) { return handler },
set (version: number) {},
del (version: number) {},
empty () {}
}
},
}

Router<Router.HTTPVersion.V2>({
constraints: {
version: versionConstraint
}
})

// NOTE: string is the default version type
expectError<Router.ConstraintStrategy<Router.HTTPVersion.V2>>({
name: 'version',
deriveConstraint(): number { return 42 },
storage() {
return {
get (version: string) { return handler },
set (version: string, handler) {},
del (version: string) {},
empty () {}
}
},
})
}