diff --git a/src/etapi/attachments.js b/src/etapi/attachments.ts similarity index 81% rename from src/etapi/attachments.js rename to src/etapi/attachments.ts index 6ea042db1..258b9a186 100644 --- a/src/etapi/attachments.js +++ b/src/etapi/attachments.ts @@ -1,11 +1,13 @@ -const becca = require('../becca/becca'); -const eu = require('./etapi_utils'); -const mappers = require('./mappers'); -const v = require('./validators'); -const utils = require('../services/utils'); +import becca = require('../becca/becca'); +import eu = require('./etapi_utils'); +import mappers = require('./mappers'); +import v = require('./validators'); +import utils = require('../services/utils'); +import { Router } from 'express'; +import { AttachmentRow } from '../becca/entities/rows'; -function register(router) { - const ALLOWED_PROPERTIES_FOR_CREATE_ATTACHMENT = { +function register(router: Router) { + const ALLOWED_PROPERTIES_FOR_CREATE_ATTACHMENT: ValidatorMap = { 'ownerId': [v.notNull, v.isNoteId], 'role': [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) => { - const params = {}; - - eu.validateAndPatch(params, req.body, ALLOWED_PROPERTIES_FOR_CREATE_ATTACHMENT); + const _params: Partial = {}; + eu.validateAndPatch(_params, req.body, ALLOWED_PROPERTIES_FOR_CREATE_ATTACHMENT); + const params = _params as AttachmentRow; try { + if (!params.ownerId) { + throw new Error("Missing owner ID."); + } const note = becca.getNoteOrThrow(params.ownerId); const attachment = note.saveAttachment(params); res.status(201).json(mappers.mapAttachmentToPojo(attachment)); } - catch (e) { + catch (e: any) { throw new eu.EtapiError(500, eu.GENERIC_CODE, e.message); } }); diff --git a/src/etapi/etapi-interface.ts b/src/etapi/etapi-interface.ts new file mode 100644 index 000000000..b53d983dd --- /dev/null +++ b/src/etapi/etapi-interface.ts @@ -0,0 +1,5 @@ +type ValidatorArg = object | undefined | null; + +type ValidatorFunc = (obj: ValidatorArg) => (string | undefined); + +type ValidatorMap = Record; \ No newline at end of file diff --git a/src/etapi/etapi_utils.ts b/src/etapi/etapi_utils.ts index f7678b81f..4880bed86 100644 --- a/src/etapi/etapi_utils.ts +++ b/src/etapi/etapi_utils.ts @@ -120,7 +120,7 @@ function getAndCheckAttribute(attributeId: string) { } } -function validateAndPatch(target: Record, source: Record, allowedProperties: Record boolean)[]>) { +function validateAndPatch(target: any, source: any, allowedProperties: ValidatorMap) { for (const key of Object.keys(source)) { if (!(key in allowedProperties)) { throw new EtapiError(400, "PROPERTY_NOT_ALLOWED", `Property '${key}' is not allowed for this method.`); diff --git a/src/etapi/validators.ts b/src/etapi/validators.ts index 7119a5319..485bd8d33 100644 --- a/src/etapi/validators.ts +++ b/src/etapi/validators.ts @@ -1,19 +1,19 @@ import noteTypeService = require('../services/note_types'); import dateUtils = require('../services/date_utils'); -function mandatory(obj: any | undefined) { +function mandatory(obj: ValidatorArg) { if (obj === undefined) { return `mandatory, but not set`; } } -function notNull(obj: any | null) { +function notNull(obj: ValidatorArg) { if (obj === null) { return `cannot be null`; } } -function isString(obj: object | undefined | null) { +function isString(obj: ValidatorArg) { if (obj === undefined || obj === null) { 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") { return; } @@ -31,7 +31,7 @@ function isLocalDateTime(obj: object | undefined | null) { return dateUtils.validateLocalDateTime(obj); } -function isUtcDateTime(obj: object | undefined | null) { +function isUtcDateTime(obj: ValidatorArg) { if (obj === undefined || obj === null || typeof obj !== "string") { return; } @@ -39,7 +39,7 @@ function isUtcDateTime(obj: object | undefined | null) { return dateUtils.validateUtcDateTime(obj); } -function isBoolean(obj: object | undefined | null) { +function isBoolean(obj: ValidatorArg) { if (obj === undefined || obj === null) { 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) { 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) { 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) { 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) { 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) { return; } diff --git a/src/routes/routes.js b/src/routes/routes.js index 182639b84..a264f3c5b 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -63,7 +63,7 @@ const shareRoutes = require('../share/routes.js'); const etapiAuthRoutes = require('../etapi/auth.js'); const etapiAppInfoRoutes = require('../etapi/app_info'); -const etapiAttachmentRoutes = require('../etapi/attachments.js'); +const etapiAttachmentRoutes = require('../etapi/attachments'); const etapiAttributeRoutes = require('../etapi/attributes'); const etapiBranchRoutes = require('../etapi/branches.js'); const etapiNoteRoutes = require('../etapi/notes.js');