mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-21 09:38:01 +02:00
29 lines
905 B
TypeScript
29 lines
905 B
TypeScript
import { Request } from '../Api';
|
|
import BaseItem from '../../../models/BaseItem';
|
|
|
|
function defaultFieldsByModelType(modelType: number): string[] {
|
|
const ModelClass = BaseItem.getClassByItemType(modelType);
|
|
const possibleFields = ['id', 'parent_id', 'title'];
|
|
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);
|
|
};
|
|
|
|
const query = request.query;
|
|
if (!query || !query.fields) return getDefaults();
|
|
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();
|
|
}
|