Repository: directus/directus
File: sdk/src/rest/utils/query-to-params.ts (TypeScript)

Code before:
		// backwards JS compatibility for `fields: "id,name"`
		if (typeof query.fields === 'string') {
			params['fields'] = query.fields;
		}
	}

	if (query.filter && typeof query.filter === 'object' && Object.keys(query.filter).length > 0) {
		params['filter'] = JSON.stringify(query.filter);
	}

	if (query.search && typeof query.search === 'string' && query.search.length > 0) {
		params['search'] = query.search;
	}

	if (query.sort) {
		if (Array.isArray(query.sort) && query.sort.length > 0) {
			params['sort'] = query.sort.join(',');
		}

		if (typeof query.sort === 'string' && query.sort.length > 0) {
			params['sort'] = query.sort;
		}
	}

	if ('limit' in query) {
		if (typeof query.limit === 'number' && query.limit >= -1) {
			params['limit'] = String(query.limit);
		}

		// backwards JS compatibility for string limits
		if (typeof query.limit === 'string' && Number(query.limit) >= -1) {
			params['limit'] = query.limit;
		}
	}

	if ('offset' in query) {
		if (typeof query.offset === 'number' && query.offset >= 0) {
			params['offset'] = String(query.offset);
		}

		// backwards JS compatibility for string offsets
		if (typeof query.offset === 'string' && Number(query.offset) >= 0) {
			params['offset'] = query.offset;
		}
	}

	if ('page' in query) {
		if (typeof query.page === 'number' && query.page >= 1) {
			params['page'] = String(query.page);
		}


Review comment (suggestion):
I wonder if we should be ignoring `-1`? The API errors if an invalid limit is provided which would provide more meaningful feedback than ignoring.

Code after:
 *
 * @param query The nested query object
 *
 * @returns Flat query parameters
 */
export const queryToParams = <Schema = any, Item = Record<string, unknown>>(
	query: ExtendedQuery<Schema, Item>,
): Record<string, string> => {
	const params: Record<string, string> = {};

	if (query.fields) {
		if (isArray(query.fields)) params['fields'] = formatFields(query.fields).join(',');
		// backwards JS compatibility for `fields: "id,name"`
		if (isString(query.fields)) params['fields'] = query.fields;
	}

	if (isObject(query.filter)) params['filter'] = JSON.stringify(query.filter);

	if (isString(query.search)) params['search'] = query.search;

	if (query.sort) {
		if (isArray(query.sort)) params['sort'] = query.sort.join(',');
		if (isString(query.sort)) params['sort'] = query.sort;
	}

	if ('limit' in query) {
		if (isNumber(query.limit) && query.limit >= -1) params['limit'] = String(query.limit);
		// backwards JS compatibility for string limits
		if (isString(query.limit)) params['limit'] = query.limit;
	}

	if ('offset' in query) {
		if (isNumber(query.offset) && query.offset >= 0) params['offset'] = String(query.offset);
		// backwards JS compatibility for string offsets
		if (isString(query.offset)) params['offset'] = query.offset;
	}

	if ('page' in query) {
		if (isNumber(query.page) && query.page >= 1) params['page'] = String(query.page);
		// backwards JS compatibility for string pages
		if (isString(query.page)) params['page'] = query.page;
	}

	if (isObject(query.deep)) params['deep'] = JSON.stringify(query.deep);

	if (isObject(query.alias)) params['alias'] = JSON.stringify(query.alias);

	if (isObject(query.aggregate)) params['aggregate'] = JSON.stringify(query.aggregate);

	if (query.groupBy) {
		if (isArray(query.groupBy)) params['groupBy'] = query.groupBy.join(',');