mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-30 19:19:03 +01:00 
			
		
		
		
	server-ts: Convert etapi/attachments
This commit is contained in:
		
							parent
							
								
									3bd7231ba9
								
							
						
					
					
						commit
						a6de065bf4
					
				| @ -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<AttachmentRow> = {}; | ||||
|         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); | ||||
|         } | ||||
|     }); | ||||
							
								
								
									
										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)) { | ||||
|         if (!(key in allowedProperties)) { | ||||
|             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 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; | ||||
|     } | ||||
|  | ||||
| @ -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'); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Elian Doran
						Elian Doran