contiamo / restful-react

A consistent, declarative way of interacting with RESTful backends, featuring code-generation from Swagger and OpenAPI specs 🔥
MIT License
1.87k stars 109 forks source link

Is there a way to enforce encoding for path parameters when using generated hooks? #289

Open brotheroftux opened 4 years ago

brotheroftux commented 4 years ago

Path parameters in the generated code are getting simply interpolated into the resulting path string, which is used in an actual request without any URI encoding (https://github.com/contiamo/restful-react/blob/454db37e029fdd3b8fceb98a6dc977720a91cb0b/src/scripts/import-open-api.ts#L301). Is there a way to somehow enforce encoding for path parameters without having to either write a custom generator or wrap every parameter value in encodeURIComponent at call site?

Thanks in advance.

fabien0102 commented 4 years ago

Seams reasonable to wrap everything in the generator, do you want to open a PR for this?

brotheroftux commented 4 years ago

Sure, if by wrapping everything in generator you mean just adding encodeURIComponent to the built-in generator's code for every path parameter. I'm not quite sure if this feature needs to be configurable — there might be a use case where a developer would want to provide, say, their own encoding function — so maybe we might want to add this option for the config.

fabien0102 commented 4 years ago

I think it's a reasonable behavior, I don't really see any drawback of using encodeURI everytime. If somebody have a need for more config over there, let's iterate, but for now I'm in favor of keep it simple 😁

brotheroftux commented 4 years ago

Right, I'll open a PR later today then.

brotheroftux commented 4 years ago

@fabien0102 We've got a case where we need the encoding to be RFC 3986 compliant (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent), so I guess I'll be adding configuration support for custom encode functions after all.