Skip to content

Commit

Permalink
fix(api): support filtering with comma-separated values in rest handler
Browse files Browse the repository at this point in the history
Fixes #1573
  • Loading branch information
ymc9 committed Jul 28, 2024
1 parent d0fd350 commit a4f2ea7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/server/src/api/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,16 @@ class RequestHandler extends APIHandlerBase {
}
return { isEmpty: value === 'true' ? true : false };
default:
return op ? { [op]: coerced } : { equals: coerced };
if (op === undefined) {
// regular filter, split value by comma
const values = value
.split(',')
.filter((i) => i)
.map((v) => this.coerce(fieldInfo.type, v));
return values.length > 1 ? { in: values } : { equals: values[0] };
} else {
return { [op]: coerced };
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/server/tests/api/rest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,16 @@ describe('REST server tests', () => {
expect(r.body.data).toHaveLength(1);
expect(r.body.data[0]).toMatchObject({ id: 'user2' });

// multi-id filter
r = await handler({
method: 'get',
path: '/user',
query: { ['filter[id]']: 'user1,user2' },
prisma,
});
expect(r.status).toBe(200);
expect(r.body.data).toHaveLength(2);

// String filter
r = await handler({
method: 'get',
Expand Down

0 comments on commit a4f2ea7

Please sign in to comment.