2020-11-05 18:58:23 +02:00
|
|
|
import { Request } from '../Api';
|
2021-01-22 19:41:11 +02:00
|
|
|
import BaseItem from '../../../models/BaseItem';
|
2020-11-05 18:58:23 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function defaultFieldsByModelType(modelType: number): string[] {
|
2020-11-05 18:58:23 +02:00
|
|
|
const ModelClass = BaseItem.getClassByItemType(modelType);
|
2024-03-02 16:25:27 +02:00
|
|
|
const possibleFields = ['id', 'parent_id', 'title', 'deleted_time'];
|
2020-11-05 18:58:23 +02:00
|
|
|
const output = [];
|
|
|
|
for (const f of possibleFields) {
|
|
|
|
if (ModelClass.hasField(f)) output.push(f);
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:53:24 +02:00
|
|
|
export default function(request: Request, modelType: number, defaultFields: string[] = null) {
|
|
|
|
const getDefaults = () => {
|
|
|
|
if (defaultFields) return defaultFields;
|
|
|
|
return defaultFieldsByModelType(modelType);
|
|
|
|
};
|
|
|
|
|
2020-11-05 18:58:23 +02:00
|
|
|
const query = request.query;
|
2021-08-30 19:53:24 +02:00
|
|
|
if (!query || !query.fields) return getDefaults();
|
2020-11-05 18:58:23 +02:00
|
|
|
if (Array.isArray(query.fields)) return query.fields.slice();
|
|
|
|
const fields = query.fields
|
|
|
|
.split(',')
|
2020-11-12 21:13:28 +02:00
|
|
|
.map((f: string) => f.trim())
|
|
|
|
.filter((f: string) => !!f);
|
2021-08-30 19:53:24 +02:00
|
|
|
return fields.length ? fields : getDefaults();
|
2020-11-05 18:58:23 +02:00
|
|
|
}
|