文件
mynode/packages/lib/services/rest/utils/defaultAction.ts
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

54 行
1.9 KiB
TypeScript

import { Request, RequestMethod } from '../Api';
import defaultSaveOptions from './defaultSaveOptions';
import { ErrorMethodNotAllowed, ErrorNotFound } from './errors';
import paginatedResults from './paginatedResults';
import readonlyProperties from './readonlyProperties';
import requestFields from './requestFields';
import BaseItem from '../../../models/BaseItem';
import { LoadOptions } from '../../../models/utils/types';
import { WhereQuery } from '../../../models/utils/paginatedFeed';
export default async function(modelType: number, request: Request, id: string = null, link: string = null, defaultFields: string[] = null, whereQuery: WhereQuery = null) {
if (link) throw new ErrorNotFound(); // Default action doesn't support links at all for now
const ModelClass = BaseItem.getClassByItemType(modelType);
const getOneModel = async (options: LoadOptions = null) => {
const model = await ModelClass.load(id, options || {});
if (!model) throw new ErrorNotFound();
return model;
};
if (request.method === 'GET') {
if (id) {
return getOneModel({
fields: requestFields(request, modelType, defaultFields),
});
} else {
return paginatedResults(modelType, request, whereQuery, defaultFields);
}
}
if (request.method === 'PUT' && id) {
const model = await getOneModel();
const newModel = { ...model, ...request.bodyJson(readonlyProperties('PUT')) };
return ModelClass.save(newModel, { userSideValidation: true });
}
if (request.method === 'DELETE' && id) {
const model = await getOneModel();
await ModelClass.delete(model.id, { source: 'API: DELETE method' });
return;
}
if (request.method === RequestMethod.POST) {
const props = readonlyProperties('POST');
const idIdx = props.indexOf('id');
if (idIdx >= 0) props.splice(idIdx, 1);
const model = request.bodyJson(props);
return ModelClass.save(model, defaultSaveOptions('POST', model.id));
}
throw new ErrorMethodNotAllowed();
}