mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
server-ts: Convert etapi/attachments
This commit is contained in:
parent
3bd7231ba9
commit
a6de065bf4
@ -1,11 +1,13 @@
|
|||||||
const becca = require('../becca/becca');
|
import becca = require('../becca/becca');
|
||||||
const eu = require('./etapi_utils');
|
import eu = require('./etapi_utils');
|
||||||
const mappers = require('./mappers');
|
import mappers = require('./mappers');
|
||||||
const v = require('./validators');
|
import v = require('./validators');
|
||||||
const utils = require('../services/utils');
|
import utils = require('../services/utils');
|
||||||
|
import { Router } from 'express';
|
||||||
|
import { AttachmentRow } from '../becca/entities/rows';
|
||||||
|
|
||||||
function register(router) {
|
function register(router: Router) {
|
||||||
const ALLOWED_PROPERTIES_FOR_CREATE_ATTACHMENT = {
|
const ALLOWED_PROPERTIES_FOR_CREATE_ATTACHMENT: ValidatorMap = {
|
||||||
'ownerId': [v.notNull, v.isNoteId],
|
'ownerId': [v.notNull, v.isNoteId],
|
||||||
'role': [v.notNull, v.isString],
|
'role': [v.notNull, v.isString],
|
||||||
'mime': [v.notNull, v.isString],
|
'mime': [v.notNull, v.isString],
|
||||||
@ -15,17 +17,20 @@ function register(router) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
eu.route(router, 'post', '/etapi/attachments', (req, res, next) => {
|
eu.route(router, 'post', '/etapi/attachments', (req, res, next) => {
|
||||||
const params = {};
|
const _params: Partial<AttachmentRow> = {};
|
||||||
|
eu.validateAndPatch(_params, req.body, ALLOWED_PROPERTIES_FOR_CREATE_ATTACHMENT);
|
||||||
eu.validateAndPatch(params, req.body, ALLOWED_PROPERTIES_FOR_CREATE_ATTACHMENT);
|
const params = _params as AttachmentRow;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (!params.ownerId) {
|
||||||
|
throw new Error("Missing owner ID.");
|
||||||
|
}
|
||||||
const note = becca.getNoteOrThrow(params.ownerId);
|
const note = becca.getNoteOrThrow(params.ownerId);
|
||||||
const attachment = note.saveAttachment(params);
|
const attachment = note.saveAttachment(params);
|
||||||
|
|
||||||
res.status(201).json(mappers.mapAttachmentToPojo(attachment));
|
res.status(201).json(mappers.mapAttachmentToPojo(attachment));
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e: any) {
|
||||||
throw new eu.EtapiError(500, eu.GENERIC_CODE, e.message);
|
throw new eu.EtapiError(500, eu.GENERIC_CODE, e.message);
|
||||||
}
|
}
|
||||||
});
|
});
|
5
src/etapi/etapi-interface.ts
Normal file
5
src/etapi/etapi-interface.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
type ValidatorArg = object | undefined | null;
|
||||||
|
|
||||||
|
type ValidatorFunc = (obj: ValidatorArg) => (string | undefined);
|
||||||
|
|
||||||
|
type ValidatorMap = Record<string, ValidatorFunc[]>;
|
@ -120,7 +120,7 @@ function getAndCheckAttribute(attributeId: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateAndPatch(target: Record<string, string>, source: Record<string, string>, allowedProperties: Record<string, ((value: string) => boolean)[]>) {
|
function validateAndPatch(target: any, source: any, allowedProperties: ValidatorMap) {
|
||||||
for (const key of Object.keys(source)) {
|
for (const key of Object.keys(source)) {
|
||||||
if (!(key in allowedProperties)) {
|
if (!(key in allowedProperties)) {
|
||||||
throw new EtapiError(400, "PROPERTY_NOT_ALLOWED", `Property '${key}' is not allowed for this method.`);
|
throw new EtapiError(400, "PROPERTY_NOT_ALLOWED", `Property '${key}' is not allowed for this method.`);
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
import noteTypeService = require('../services/note_types');
|
import noteTypeService = require('../services/note_types');
|
||||||
import dateUtils = require('../services/date_utils');
|
import dateUtils = require('../services/date_utils');
|
||||||
|
|
||||||
function mandatory(obj: any | undefined) {
|
function mandatory(obj: ValidatorArg) {
|
||||||
if (obj === undefined) {
|
if (obj === undefined) {
|
||||||
return `mandatory, but not set`;
|
return `mandatory, but not set`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function notNull(obj: any | null) {
|
function notNull(obj: ValidatorArg) {
|
||||||
if (obj === null) {
|
if (obj === null) {
|
||||||
return `cannot be null`;
|
return `cannot be null`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isString(obj: object | undefined | null) {
|
function isString(obj: ValidatorArg) {
|
||||||
if (obj === undefined || obj === null) {
|
if (obj === undefined || obj === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -23,7 +23,7 @@ function isString(obj: object | undefined | null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isLocalDateTime(obj: object | undefined | null) {
|
function isLocalDateTime(obj: ValidatorArg) {
|
||||||
if (obj === undefined || obj === null || typeof obj !== "string") {
|
if (obj === undefined || obj === null || typeof obj !== "string") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -31,7 +31,7 @@ function isLocalDateTime(obj: object | undefined | null) {
|
|||||||
return dateUtils.validateLocalDateTime(obj);
|
return dateUtils.validateLocalDateTime(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isUtcDateTime(obj: object | undefined | null) {
|
function isUtcDateTime(obj: ValidatorArg) {
|
||||||
if (obj === undefined || obj === null || typeof obj !== "string") {
|
if (obj === undefined || obj === null || typeof obj !== "string") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ function isUtcDateTime(obj: object | undefined | null) {
|
|||||||
return dateUtils.validateUtcDateTime(obj);
|
return dateUtils.validateUtcDateTime(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isBoolean(obj: object | undefined | null) {
|
function isBoolean(obj: ValidatorArg) {
|
||||||
if (obj === undefined || obj === null) {
|
if (obj === undefined || obj === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -49,7 +49,7 @@ function isBoolean(obj: object | undefined | null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isInteger(obj: object | undefined | null) {
|
function isInteger(obj: ValidatorArg) {
|
||||||
if (obj === undefined || obj === null) {
|
if (obj === undefined || obj === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -59,7 +59,7 @@ function isInteger(obj: object | undefined | null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isNoteId(obj: object | undefined | null) {
|
function isNoteId(obj: ValidatorArg) {
|
||||||
if (obj === undefined || obj === null) {
|
if (obj === undefined || obj === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -75,7 +75,7 @@ function isNoteId(obj: object | undefined | null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isNoteType(obj: object | undefined | null) {
|
function isNoteType(obj: ValidatorArg) {
|
||||||
if (obj === undefined || obj === null) {
|
if (obj === undefined || obj === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -87,7 +87,7 @@ function isNoteType(obj: object | undefined | null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isAttributeType(obj: object | undefined | null) {
|
function isAttributeType(obj: ValidatorArg) {
|
||||||
if (obj === undefined || obj === null) {
|
if (obj === undefined || obj === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -97,7 +97,7 @@ function isAttributeType(obj: object | undefined | null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isValidEntityId(obj: object | undefined | null) {
|
function isValidEntityId(obj: ValidatorArg) {
|
||||||
if (obj === undefined || obj === null) {
|
if (obj === undefined || obj === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ const shareRoutes = require('../share/routes.js');
|
|||||||
|
|
||||||
const etapiAuthRoutes = require('../etapi/auth.js');
|
const etapiAuthRoutes = require('../etapi/auth.js');
|
||||||
const etapiAppInfoRoutes = require('../etapi/app_info');
|
const etapiAppInfoRoutes = require('../etapi/app_info');
|
||||||
const etapiAttachmentRoutes = require('../etapi/attachments.js');
|
const etapiAttachmentRoutes = require('../etapi/attachments');
|
||||||
const etapiAttributeRoutes = require('../etapi/attributes');
|
const etapiAttributeRoutes = require('../etapi/attributes');
|
||||||
const etapiBranchRoutes = require('../etapi/branches.js');
|
const etapiBranchRoutes = require('../etapi/branches.js');
|
||||||
const etapiNoteRoutes = require('../etapi/notes.js');
|
const etapiNoteRoutes = require('../etapi/notes.js');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user