1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-30 10:36:35 +02:00
joplin/packages/lib/services/rest/utils/requestFields.ts

29 lines
921 B
TypeScript
Raw Normal View History

2020-11-05 18:58:23 +02:00
import { Request } from '../Api';
import BaseItem from '../../../models/BaseItem';
2020-11-05 18:58:23 +02:00
function defaultFieldsByModelType(modelType: number): string[] {
2020-11-05 18:58:23 +02:00
const ModelClass = BaseItem.getClassByItemType(modelType);
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;
}
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;
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(',')
.map((f: string) => f.trim())
.filter((f: string) => !!f);
return fields.length ? fields : getDefaults();
2020-11-05 18:58:23 +02:00
}