-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
media_types API is not good #4292
Comments
It was a port of https://github.com/jshttp/mime-types which is the defacto standard. The interfaces as they stand align well to sort of functions needed when creating a web server, as things like charset, translating both directions, etc. The API serve the minimal purpose of performing those functions, returning what is just needed and aligning to the behaviour a server would need. While your suggestion is understandable it would be a breaking change with already built servers (as well as make it difficult for others to adapt code based on the Could there be improvements to the README to make it clearer what the APIs do? |
If the API is locked down and README is all that can be fixed, then the most important thing to clarify in the docs is that while |
Can we add another module which implements this better API as @jakajancar suggests and leave a legacy module?
^-- I didn't realize.. That is terrible. |
One could argue that |
Looking at oak, I realised that this API serves a useful purpose. The purpose of the API is to normalise content-types, when users take shortcuts. See how this API is used in oak: So I would prefer to not to change the API, it isn't broken. Clearing up documentation of its purpose, or changing the name of the API to normalizeContentType would be better. The reason it resolves "extensions" is so that a user can set the content type to a response to something like "json" but end up with a full valid media type as output. It was never designed to do paths, as that feature is lookup(). |
@kitsonk lookup() takes a path but contentType() takes something like a content-type which is then passed to lookup(). I ran into this problem earlier today while editing In the tests it seems like contentType() is supposed to take a path too: ` assertEquals(contentType("file.json"), "application/json; charset=utf-8"); but it's very surprising that the answer is different when you put in I think normalizeContentType makes sense - but I don't think it should also operate on filenames. |
@ry I would be fine with that. I don't think it would break too much upstream. I think it was in the original library because people would just be lazy and pass the basename of a file as the content type, which is silly. It should be either an extension or a content type which needs to be normalized. |
I'm a little late to this, but given this:
Wouldn't it make sense for this to live in EDIT: oh, sorry - |
It is now available on oakserver/media_types and https://deno.land/x/media_types/ |
The media_types API employs too much magic leading to unexpected behavior:
Also, the naming, while understandable for someone who knows the implementation (e.g. how the database is keyed), is a bit confusing for a newcomer:
lookup()
? Aren't they all lookups of some type or another?lookup()
returns a (non-full?) "content type", which you pass tocontentType()
and it returns a "full" content type" ?So many exports/functions:
lookup(path)
contentType(type)
extension(type)
charset(type)
extensions
types
A (imo) much easier to understand API would be to just expose (maybe a subset of) the database record type:
The text was updated successfully, but these errors were encountered: