Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions apps/meteor/app/api/server/router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,44 @@ describe('Router use method', () => {
expect(response2.statusCode).toBe(200);
expect(response2.headers).not.toHaveProperty('x-api-version');
});

it('should parse nested query params into object for GET requests', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

const ajv = new Ajv();
const app = express();

const isTestQueryParams = ajv.compile({
type: 'object',
properties: {
outerProperty: { type: 'object', properties: { innerProperty: { type: 'string' } } },
},
additionalProperties: false,
});

const api = new Router('/api').get(
'/test',
{
response: {
200: isTestQueryParams,
},
query: isTestQueryParams,
},
async function action() {
const { outerProperty } = this.queryParams as any;
return {
statusCode: 200,
body: {
outerProperty,
},
};
},
);

app.use(api.router);

const response1 = await request(app).get('/api/test?outerProperty[innerProperty]=test');

expect(response1.statusCode).toBe(200);
expect(response1.body).toHaveProperty('outerProperty');
expect(response1.body.outerProperty).toHaveProperty('innerProperty', 'test');
});
});
7 changes: 5 additions & 2 deletions apps/meteor/app/api/server/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,12 @@ export class Router<
this.innerRouter[method.toLowerCase() as Lowercase<Method>](`/${subpath}`.replace('//', '/'), ...middlewares, async (c) => {
const { req, res } = c;
req.raw.route = `${c.var.route ?? ''}${subpath}`;

const queryParams = this.parseQueryParams(req);

if (options.query) {
const validatorFn = options.query;
if (typeof options.query === 'function' && !validatorFn(req.query())) {
if (typeof options.query === 'function' && !validatorFn(queryParams)) {
return c.json(
{
success: false,
Expand Down Expand Up @@ -229,7 +232,7 @@ export class Router<
{
requestIp: c.get('remoteAddress'),
urlParams: req.param(),
queryParams: this.parseQueryParams(req),
queryParams,
bodyParams,
request: req.raw.clone(),
path: req.path,
Expand Down
Loading