1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/packages/lib/services/rest/utils/requestFields.ts
2020-11-05 16:58:23 +00:00

24 lines
793 B
TypeScript

import { Request } from '../Api';
const BaseItem = require('../../../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) {
const query = request.query;
if (!query || !query.fields) return defaultFieldsByModelType(modelType);
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 : defaultFieldsByModelType(modelType);
}