-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add MAX_ITEMS_PER_QUERY #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
c57d99a
599b765
a8ca2ef
a509ea0
ae2a540
97ce4fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,25 @@ | ||
| import type { Accountability, Aggregate, Filter, Query } from '@wbce-d9/types'; | ||
| import { parseFilter, parseJSON } from '@wbce-d9/utils'; | ||
| import { flatten, get, isPlainObject, merge, set } from 'lodash-es'; | ||
| import env from '../env.js'; | ||
| import logger from '../logger.js'; | ||
| import { Meta } from '../types/index.js'; | ||
|
|
||
| export function sanitizeQuery(rawQuery: Record<string, any>, accountability?: Accountability | null): Query { | ||
| const query: Query = {}; | ||
|
|
||
| if (rawQuery['limit'] !== undefined) { | ||
| const limit = sanitizeLimit(rawQuery['limit']); | ||
| let limit = sanitizeLimit(rawQuery['limit']); | ||
|
|
||
| if (typeof limit === 'number') { | ||
| const maxItemsPerQuery = Number(env['MAX_ITEMS_PER_QUERY']); | ||
|
||
|
|
||
| if (maxItemsPerQuery !== -1) { | ||
|
||
| if (limit === -1) { | ||
| limit = maxItemsPerQuery; | ||
| } | ||
| } | ||
|
|
||
| query.limit = limit; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,10 @@ export function validateQuery(query: Query): Query { | |
| validateAlias(query.alias); | ||
| } | ||
|
|
||
| if (query.limit) { | ||
| validateLimit(query.limit); | ||
| } | ||
|
|
||
| validateRelationalDepth(query); | ||
|
|
||
| if (error) { | ||
|
|
@@ -232,3 +236,13 @@ function validateRelationalDepth(query: Query) { | |
| } | ||
| } | ||
| } | ||
|
|
||
| function validateLimit(limit: any) { | ||
| const maxItemsPerQuery = Number(env['MAX_ITEMS_PER_QUERY']) || 1000; | ||
|
||
|
|
||
| if (limit !== -1 && maxItemsPerQuery !== -1) { | ||
|
||
| if (limit > maxItemsPerQuery) { | ||
| throw new InvalidQueryException(`"Limit" can't exceed ${maxItemsPerQuery}`); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not present in the rest of the code ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It already existed; it just wasn’t present here like all environment variables. Here’s an example of a call:
const maxRelationalDepth = Number(env['MAX_RELATIONAL_DEPTH']) > 2 ? Number(env['MAX_RELATIONAL_DEPTH']) : 2;