inversify-express-utils icon indicating copy to clipboard operation
inversify-express-utils copied to clipboard

@QueryParams use constructor

Open mbayou opened this issue 8 years ago • 1 comments

I want a complexe query param parse by constructor of my object Filter.

Expected Behavior

I use a complexe query params for filter with must and should value :

params=param1,param2;param3

From this query param, I want to build an object with two arrays

  • Must : [param1, param2]
  • Should : [param3]

To do this, I create objects Filter with construct who parse the string "param1,param2;param3" in the two previous arrays.

Current Behavior

When I try with this code :

@Get("/accidents")
    public async findAccidents(@QueryParam("area") areaFilter: Filter, req: Express.Request, res: Express.Response, next: Express.NextFunction)

The constructor of Filter :

    constructor(query: string) {
        const splittedElements: string[] = query.split(SearchFilter.MAIN_SPLITTER_CHAR);
        Utils.checkArguments(splittedElements.length <= SearchFilter.MAXIMUM_MAIN_PART, "SearchQuery does not have enough main elements");

        this.mustValues = this.mustValues.concat(splittedElements[0].split(SearchFilter.EXPRESSION_SPLITTER_CHAR));
        if (splittedElements.length === SearchFilter.MAXIMUM_MAIN_PART) {
            this.shouldValues = this.shouldValues.concat(splittedElements[1].split(SearchFilter.EXPRESSION_SPLITTER_CHAR));
        }
    }

areaFilter is just a string and I can't use the method of Filter.

Context

I can use a trick to build my filter directly in my controller method but It's not the philosophy I want to use. I think it will be better if the @QueryBuilder can give me the Object as expect.

Your Environment

  • Version used: 3.5.2 (inversely-express-utils)
  • Node JS 7.7.1
  • OSX 10.12.5

mbayou avatar May 30 '17 09:05 mbayou

I think that the problem is that the @QueryParam("paramName") decorator can only ever be a string because it is looking up the "paramName" in the query field of the incoming request. The solution should probably be to throw an error if the type is anything but a string.

We could discuss adding middleware to this if it is a common use case.

dcavanagh avatar Jul 10 '17 18:07 dcavanagh