Skip to content

Commit

Permalink
feat(auto rest): simple text search
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Sep 18, 2024
1 parent a44ba1b commit 555fc05
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ With this configuration, moquerie will automatically create RESTful endpoints fo
- Filter the results with query parameters, for example `GET /resourceType?name=foo`
- Paginate with `__page` (first page is `0`) and `__pageSize` (default `10`) query parameters: `GET /resourceType?__page=1&__pageSize=10`
- Sort with `__sort` query parameter with the syntax `<field>:asc` or `<field>:desc`: `GET /resourceType?__sort=name:asc`
- Search for text with `__search` query parameter: `GET /resourceType?__search=foo`
- `POST /resourceType`: create a new instance
- `GET /resourceType/:id`: get an instance
- `PUT /resourceType/:id`: update an instance
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/rest/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,21 @@ export async function setupRestApi(mq: MoquerieInstance, expressApp: Application
if (req.method === 'GET') {
// Query filters
const predicate = (data: any) => {
// Text search
if (query.__search != null) {
const search = query.__search.toLowerCase()
for (const key in data) {
if (key.match(/^(id|_id|slug)$/)) {
continue
}
const value = get(data, key)
if (typeof value === 'string' && value.toLowerCase().includes(search)) {
return true
}
}
return false
}

for (const key in query) {
if (key.startsWith('__')) {
continue
Expand Down

0 comments on commit 555fc05

Please sign in to comment.