I'm migrating a .NET API to use the NestJS framework and I ran into an unfortunate problem. Case-sensitive query parameters!
This is migrate "in place" type work of a well used public API. Putting aside any philosophical feelings about whether params should be case-sensitive in the first place, I can't release breaking changes.
I found few articles on the Net. And this one which deals with a similar issue. However, the suggested solutions are either laborious, or require altering all my DTO objects to have constructors.
I was thinking of something like this:
- Middleware to convert all request params to a single case (upper, lower, whatever).
- Custom decorator that takes a string. The string is the name of the freshly re-cased parameter.
- That's it.
The calling code doesn't need to be rewritten. The controllers don't need to touched. The only substantive change required would be new decorators on interesting parameters.
Some example/pseudo code might better illustrate what I'm thinking. Here is Express middleware I used in a previous project:
app.use(async (request, response, next): Promise<void> => {
request.query = new Proxy(request.query, {
get: (target, name) =>
target[Object.keys(target).find(key => key.toLowerCase() === name.toLowerCase())]
})
return next();
});
And then the request DTO would look something like this (Hat tip to Moshi, the best JSON serialization library of all time):
export class GetRequest {
@NewDecoratorOfSomeKind('camelcasedname')
@IsOptional()
@IsString()
camelCasedName?: string;
// This would also work, which is neat. Useful?
@NewDecoratorOfSomeKind('some_name_you_dont_like')
@IsOptional()
@IsString()
aNameYouDoLike?: string;
}
So, before I spend most of a weekend figuring out whether this is even feasible, I seek the wisdom of the Stack Overflow. Cut holes into my plan. Is this even possible? Is it a pattern people would/should/could embrace? Is there something out there already that does this? Is there a better way to get the same result?