-
Notifications
You must be signed in to change notification settings - Fork 141
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ declare namespace Router { | |
V1 = 'http1', | ||
V2 = 'http2' | ||
} | ||
|
||
type HTTPMethod = | ||
| 'ACL' | ||
| 'BIND' | ||
|
@@ -57,17 +57,17 @@ declare namespace Router { | |
store: any | ||
) => void; | ||
|
||
interface ConstraintStrategy<V extends HTTPVersion> { | ||
interface ConstraintStrategy<V extends HTTPVersion, ConstraintVersion = string> { | ||
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> { | ||
|
@@ -91,7 +91,7 @@ declare namespace Router { | |
): void; | ||
|
||
constraints? : { | ||
[key: string]: ConstraintStrategy<V> | ||
[key: string]: ConstraintStrategy<V, unknown> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 saysversion
in all the types below, but these strategies can actually be used for other constraints than version, likehost
or maybe theAccept
header someday. I think the name of that argument in the types should be changed too.