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

Support for media type version request #14

Open
bazwilliams opened this issue Jun 11, 2015 · 9 comments
Open

Support for media type version request #14

bazwilliams opened this issue Jun 11, 2015 · 9 comments
Assignees

Comments

@bazwilliams
Copy link

We use vendor specific media types for all our resources and use versions to differentiate between breaking changes to that resource.

For example:

application/vnd.linn.space-configuration+json; version=1

is our current media type for application/vnd.linn.space-configuration+json and type-is correctly returns the type when +json is used. However, the versioning is lost and we have no way to differentiate between a version 2 of this resource when it becomes available.

@dougwilson dougwilson self-assigned this Jun 11, 2015
@dougwilson
Copy link
Contributor

Hi! We are actually actively working on this problem :) This module depends on the negotiator module to do the work, and we have a PR jshttp/negotiator#40 that is trying to get the parameters exposed when you get a negotiation result. Let me know if this seems to fit what you're looking for or not :)

@bazwilliams
Copy link
Author

That looks just the thing. We could ideally provide the following as type params:

[{
    mediaType: 'application/vnd.linn.space-configuration+json',
    version: 1
}, {
    mediaType: 'application/vnd.linn.sapce-configuration+json',
    version: 2
}, {
    mediaType: 'json'
}]

@dougwilson
Copy link
Contributor

We could ideally provide the folliowing as type params

As in, input to this library? If that is that you're suggesting, we already do offer that:

typeis(req, ['application/vnd.linn.space-configuration+json; version=1', 'application/vnd.linn.sapce-configuration+json; version=2', 'json'])

@dougwilson
Copy link
Contributor

Hi @bazwilliams , sorry this went stale, but it looks like I was waiting for clarification to my question. Can you clarify?

@bazwilliams
Copy link
Author

Yes, using type-is to match the entire string including version does work, but doesn't provide much additional benefit over comparing the header directly.

@dougwilson dougwilson reopened this Jul 2, 2015
@dougwilson
Copy link
Contributor

Ok, but you still haven't cleared up to me what you want me to do. Can you elaborate more on what you mean in #14 (comment) ?

@bazwilliams
Copy link
Author

Sorry I wasn't clear.

If you run the following code:

var is=require('type-is');

var req = {
        headers: {
            'content-type': 'application/vnd.linn.test+json; version=1',
            'transfer-encoding': 'chunked'
        }
    };

//Should be false
console.log(is(req,['application/json']));

//Should be application/vnd.linn.test+json
console.log(is(req,['+json']));

//Should be application/vnd.linn.test+json
console.log(is(req,['application/vnd.linn.test+json']));

//Should be application/vnd.linn.test+json, but also want access to the version which matched!
console.log(is(req,['application/vnd.linn.test+json; version=1', 'application/vnd.linn.test+json; version=2']));

The first 3 examples give output that I would expect. But the last fails to match.

Further, it would be useful to be able to find out which version was requested, but particularly important in the last example where our code can receive both version 1 and 2 requests. But unless we parse the header manually, cannot tell what the version of the content-type is.

@dougwilson
Copy link
Contributor

Gotcha, makes sense now. So yes, this library doesn't let you match on the parameters. The only way you can do it currently is to include the content-type library to get the library:

var contentType = require('content-type')
var typeis = require('type-is')

// ... later on

if (typeis(req, 'application/vnd.linn.test+json')) {
  console.log('accepts application/vnd.linn.test+json with version ' + contentType.parse(req).parameters.version)
}

PRs to add things like this are always welcome, though, if the above is not an acceptable solution for you.

@bazwilliams
Copy link
Author

Cheers for the workaround, I'd definitely prefer the library to do this so will fork tomorrow for a future PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants