balazsbotond / urlcat

A URL builder library for JavaScript.
https://urlcat.org
MIT License
1.82k stars 57 forks source link

Restrict the allowed types of path parameters #9

Closed balazsbotond closed 3 years ago

balazsbotond commented 4 years ago

Only the following should be allowed as path parameter values:

The following primitive types, in my opinion, should not be allowed:

Complex types (arrays and object) should not be allowed at all.

The error thrown should be a TypeError and its message should contain the parameter name and a list of allowed types.

harshilparmar commented 4 years ago

Can I try this ?

balazsbotond commented 4 years ago

@harshilparmar sure, it's yours! :)

harshilparmar commented 4 years ago

`function path(template: string, params: ParamMap) { const remainingParams = { ...params }; const renderedPath = template.replace(/:\w+/g, p => { const key = p.slice(1); if (!params.hasOwnProperty(key)) { return p; } delete remainingParams[key]; const allowedTypes = ["boolean","string","number"]; if(allowedTypes.includes(typeof params[key])){ return encodeURIComponent(params[key]); }else{ throw new TypeError("${typeof params[key]} is not Valid,Params must be number,string or boolean") } });

return { renderedPath, remainingParams }; }` @balazsbotond is this correct way? Please correct me If I am wrong πŸ™‡β€β™‚οΈ

balazsbotond commented 4 years ago

@harshilparmar thank you, I believe this is correct πŸ‘ I like the names you used and the overall logic of your code.

Just a couple of minor, mostly stylistic suggestions:

Could you please create a pull request with your chages? This would make it easier to review the code.

Also, could you please include tests in your PR - I think they need to be added for the subst and urlcat functions.

Thanks again - I'm very glad you're interested in the project and took the time to contribute code!

harshilparmar commented 4 years ago

@balazsbotond Thanks for suggestions brother!!! I will make pr with this changes.I would love to contribute and learn from you. Thanks for 1st and last points.For 2ed and 3rd point I changed code accordingly only to show you,HAHAHAH But it was right.Thanks brother πŸ’―

harshilparmar commented 4 years ago

@balazsbotond Would you like to show specific type of objects??

https://stackoverflow.com/questions/7893776/the-most-accurate-way-to-check-js-objects-type https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/

balazsbotond commented 4 years ago

@harshilparmar I agree that in general, just the string "object" is not very informative - and in most cases it would be a good idea to also return the name of the type.

But almost all objects passed to urlcat will be object literals - for those these solutions would still return "object" or "Object" - so in this specific case, I don't think it's worth adding more code to return the exact type.

harshilparmar commented 4 years ago

@harshilparmar I agree that in general, just the string "object" is not very informative - and in most cases it would be a good idea to also return the name of the type.

But almost all objects passed to urlcat will be object literals - for those these solutions would still return "object" or "Object" - so in this specific case, I don't think it's worth adding more code to return the exact type.

@balazsbotond ohh, for that we don't need to add thatπŸ’―

harshilparmar commented 4 years ago

@balazsbotond Can you help me to figure out how to write test case for TypeError? I have made something like this it('Don\'t allow null as parameter', () => { const actual = subst(':p', { p: null }); expect(actual).to.throw(new TypeError('Path parameter p cannot be of type object.Allowed types are: boolean, string, number.')); });

But it's not passing test !!

balazsbotond commented 4 years ago

@harshilparmar Null and undefined values are removed in urlcatImpl before calling path (by design - though I'm planning to change this in the future) so that's why your code won't throw an exception in that case. I could have explained this better in my original comment on this issue, sorry about that.

So the solution is simply not writing a test case for null and undefined.

balazsbotond commented 3 years ago

Categorized this as a bug instead of an enhancement because it fixes unexpected behavior.

Closing.

balazsbotond commented 3 years ago

@all-contributors please add @harshilparmar for code

allcontributors[bot] commented 3 years ago

@balazsbotond

I've put up a pull request to add @harshilparmar! :tada: