Search code examples
unit-testinghandlebars.jsintegration-testingmockservermockoon

Mockoon | Error while serving the content


I am using mockoon to mock an api with endpoint localhost:3004/api/v1/users1?filterBy=lastName&value=Brown. Basically I am trying to implement mock api which supports filtering. I am using a data bucket with the following content.

{
  "users":[

    {
  "id": 1,
  "firstName": "Alice",
  "lastName": "Smith",
  "email": "[email protected]",
  "username": "aliceUser",
  "createdAt": "2023-10-02T12:00:00.000Z",
  "phoneNumbers": [
    "123-456-7890",
    "234-567-8901"
    ]
  },
  {
    "id": 2,
    "firstName": "Bob",
    "lastName": "Brown",
    "email": "[email protected]",
    "username": "bobUser",
    "createdAt": "2023-11-01T12:00:00.000Z",
    "phoneNumbers": ["234-567-8901"]
  },
  {
      "id": 3,
      "firstName": "Eve",
      "lastName": "Adams",
      "email": "[email protected]",
      "username": "eve123",
      "createdAt": "2023-11-01T12:00:00.000Z",
      "phoneNumbers": ["234-567-8901"]
    },
    {
      "id": 4,
      "firstName": "Alice",
      "lastName": "Johnson",
      "email": "[email protected]",
      "username": "aliceUser",
      "createdAt": "2023-11-02T14:00:00.000Z",
      "phoneNumbers": ["123-456-7890"]
    }
]
}

I have created the following inline template

{
  "userList": [
    {{#if (queryParamRaw 'filterBy')}}
      {{#each (filter (dataRaw 'userData' 'users') (object (queryParamRaw 'filterBy')=(queryParamRaw 'value')))}}
        {{#unless @first}},{{/unless}}
        {{{stringify this}}}
      {{/each}}
    {{else}}
      {{#each (dataRaw 'userData' 'users')}}
        {{#unless @first}},{{/unless}}
        {{stringify this}}
      {{/each}}
    {{/if}}
  ]
}

But it is throwing an error

Error while serving the content: Parse error on line 4:
...ParamRaw 'filterBy')=(queryParamRaw 'val
-----------------------^
Expecting 'CLOSE_RAW_BLOCK', 'CLOSE', 'CLOSE_UNESCAPED', 'OPEN_SEXPR', 'CLOSE_SEXPR', 'ID', 'OPEN_BLOCK_PARAMS', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'EQUALS'

Can someone tell what is wrong in the template


Solution

  • I don't think Handlebars support dynamic expressions as a named argument's key ({{helper key=value}}), at least I couldn't make it work, no matter what I tried.

    There is probably no other alternative than creating multiple filter expressions in a switch (or ifs), for each user property that could be searched. Less than ideal, I admit.

    If you are ready to get rid of the userList wrapping in the result, Mockoon's CRUD routes offer exactly what you are looking for (searching, filtering, etc.).