denoland / deno_ast

Source text parsing, lexing, and AST related functionality for Deno
https://crates.io/crates/deno_ast
MIT License
148 stars 45 forks source link

feat: add as_content_type() and make from_path() public #134

Closed Cre3per closed 1 year ago

Cre3per commented 1 year ago

Closes #133

The mappings in as_content_type() are a suggestion based on from_content_type() and web searches.

Potential issues

from_file_extension() is case-sensitive to file extensions while from_path() is case-insensitive.
If the crate user has non-lower-case extensions, they can transform them once to avoid every library function from having to do the transformation.

Additional changes

Changed return type of as_ts_extension() to 'static. If this change is wrong, I'll also need to remove the 'static from as_content_type().

CLAassistant commented 1 year ago

CLA assistant check
All committers have signed the CLA.

petamoriken commented 1 year ago

The mimemap source code in the h2o server may be helpful.

It may be a good idea to add the following MIME in as_content_type.

And how about also supporting the above MIME in from_content_type, and treating anything that ends with +json as JSON?

FYI: https://github.com/h2o/h2o/blob/master/lib/handler/mimemap/defaults.c.h https://github.com/h2o/h2o/blob/e041bac7f7556415a201bc664d16412c5f6c62de/lib/handler/mimemap.c#L428-L435

Cre3per commented 1 year ago

@petamoriken deno_ast provides functionality for deno itself, which means MediaType only needs to support formats of scripts, configs, and so on. MediaType isn't intended to be a general purpose type. geojson, gltf, and jsonld would be unused, because deno doesn't use these formats.

I had a quick look through media types ending in +json and didn't see any that a web server would set when serving files used by deno. If you know of a file where such a media type is correct, it might be worth opening a separate issue.

petamoriken commented 1 year ago

@Cre3per I suggested this because I thought it would be useful for displaying JSON in LSP and handling JSON in Import Assetions when developing a web application that uses geojson, gltf, and jsonld. I may be misunderstanding, and as you say, it would be better to address this in a separate issue.