@@ -20,66 +20,67 @@ const {
2020} = require ( '../helpers/placeholder' ) ;
2121
2222/**
23- * Validate and do something
24- * @param {Request } req
25- * @param {Response } res
23+ * Get the valid version
2624 * @param {Object } body
2725 */
28- const validateAndDoSomething = async ( req , res , body ) => {
26+ const validate = async body => {
2927 if ( ! await validateImage ( body . background ) )
30- return res . status ( statusCode . BAD_REQUEST ) . send ( `Invalid value for certificate background: Image not found!` ) ;
28+ throw new Error ( `Invalid value for certificate background: Image not found!` ) ;
3129
3230 const imgLocation = await getImageLocation ( body . background ) ;
3331 if ( ! imgLocation )
34- return res . status ( statusCode . BAD_REQUEST ) . send ( `Invalid value for background: Image not accessible!` ) ;
32+ throw new Error ( `Invalid value for background: Image not accessible!` ) ;
3533
3634 body . background = imgLocation ;
3735
3836 if ( body . fields == null )
39- return res . status ( statusCode . BAD_REQUEST ) . send ( `Invalid value for fields!` ) ;
37+ throw new Error ( `Invalid value for fields!` ) ;
4038
4139 for ( const field of body . fields ) {
4240 if ( body . fields . filter ( f => f . name === field . name ) . length > 1 )
43- return res . status ( statusCode . BAD_REQUEST ) . send ( `Duplicate fields named '${ field . name } ' received!` ) ;
41+ throw new Error ( `Duplicate fields named '${ field . name } ' received!` ) ;
42+
43+ if ( field . type == null )
44+ field . type = 'String' ;
4445
4546 if ( [ 'Number' , 'Boolean' , 'String' , 'Image' , 'Date' ] . indexOf ( field . type ) < 0 )
46- return res . status ( statusCode . BAD_REQUEST ) . send ( `Invalid type for field '${ field . name } ': Only Number, Boolean, String, Image, and Date allowed.` ) ;
47+ throw new Error ( `Invalid type for field '${ field . name } ': Only Number, Boolean, String, Image, and Date allowed.` ) ;
4748
4849 if ( [ 'TITLE' , 'template' , 'uid' , '_id' ] . indexOf ( field . name ) >= 0 )
49- return res . status ( statusCode . NOT_ACCEPTABLE ) . send ( `Invalid name for field '${ field . name } ': Name not allowed for fields.` ) ;
50+ throw new Error ( `Invalid name for field '${ field . name } ': Name not allowed for fields.` ) ;
5051
5152 if ( ( field . fixed || field . placeholder ) && field . value == null )
52- return res . status ( statusCode . BAD_REQUEST ) . send ( `Fixed field '${ field . name } ' cannot have an empty value.` ) ;
53+ throw new Error ( `Fixed field '${ field . name } ' cannot have an empty value.` ) ;
5354
5455 if ( field . placeholder )
5556 if ( ! isValidPlaceholder ( field ) )
56- return res . status ( statusCode . BAD_REQUEST ) . send ( `Field '${ field . name } ' is an invalid placeholder!` ) ;
57+ throw new Error ( `Field '${ field . name } ' is an invalid placeholder!` ) ;
5758 else
5859 continue ;
5960
6061 if ( field . type === 'Image' ) {
6162 if ( field . image == null )
62- return res . status ( statusCode . BAD_REQUEST ) . send ( `Invalid value for field '${ field . name } ': An expected size must be defined.` ) ;
63+ throw new Error ( `Invalid value for field '${ field . name } ': An expected size must be defined.` ) ;
6364
6465 const { value, defaultValue } = field ?? { } ;
6566 if ( value != null && ! await validateImage ( value ) )
66- return res . status ( statusCode . BAD_REQUEST ) . send ( `Invalid value for field '${ field . name } ': Image not found!` ) ;
67+ throw new Error ( `Invalid value for field '${ field . name } ': Image not found!` ) ;
6768
6869 if ( value != null ) {
6970 const imgLocation = await getImageLocation ( value ) ;
7071 if ( ! imgLocation )
71- return res . status ( statusCode . BAD_REQUEST ) . send ( `Invalid value for field '${ field . name } ': Image not accessible!` ) ;
72+ throw new Error ( `Invalid value for field '${ field . name } ': Image not accessible!` ) ;
7273
7374 field . value = imgLocation ;
7475 }
7576
7677 if ( defaultValue != null && ! await validateImage ( defaultValue ) )
77- return res . status ( statusCode . BAD_REQUEST ) . send ( `Invalid default value for field '${ field . name } ': Image not found!` ) ;
78+ throw new Error ( `Invalid default value for field '${ field . name } ': Image not found!` ) ;
7879
7980 if ( defaultValue != null ) {
8081 const imgLocation = await getImageLocation ( defaultValue ) ;
8182 if ( ! imgLocation )
82- return res . status ( statusCode . BAD_REQUEST ) . send ( `Invalid default value for field '${ field . name } ': Image not accessible!` ) ;
83+ throw new Error ( `Invalid default value for field '${ field . name } ': Image not accessible!` ) ;
8384
8485 field . defaultValue = imgLocation ;
8586 }
@@ -93,7 +94,7 @@ const validateAndDoSomething = async (req, res, body) => {
9394 } = field . textFormat ;
9495
9596 if ( style != null && style ?. type == 'gradient' && ( style . gradient == null || style . gradient . stops ?. length < 2 ) )
96- return res . status ( statusCode . BAD_REQUEST ) . send ( `Invalid style for field '${ field . name } ': Invalid gradient configuration!` ) ;
97+ throw new Error ( `Invalid style for field '${ field . name } ': Invalid gradient configuration!` ) ;
9798 }
9899
99100 if ( field . type === 'Date' ) {
@@ -106,7 +107,7 @@ const validateAndDoSomething = async (req, res, body) => {
106107 try {
107108 new Date ( Date . parse ( value ) ) . toISOString ( ) ;
108109 } catch ( e ) {
109- return res . status ( statusCode . BAD_REQUEST ) . send ( `Invalid value for field '${ field . name } ': Invalid date! Use the UTC/ISO format.` ) ;
110+ throw new Error ( `Invalid value for field '${ field . name } ': Invalid date! Use the UTC/ISO format.` ) ;
110111 }
111112 }
112113 }
@@ -118,14 +119,28 @@ const validateAndDoSomething = async (req, res, body) => {
118119 try {
119120 new Date ( Date . parse ( defaultValue ) ) . toISOString ( ) ;
120121 } catch ( e ) {
121- return res . status ( statusCode . BAD_REQUEST ) . send ( `Invalid value for field '${ field . name } ': Invalid date! Use the UTC/ISO format.` ) ;
122+ throw new Error ( `Invalid value for field '${ field . name } ': Invalid date! Use the UTC/ISO format.` ) ;
122123 }
123124 }
124125 }
125126 }
126127 }
128+ } ;
127129
128- return true ;
130+ /**
131+ * Validate and do something
132+ * @param {Request } req
133+ * @param {Response } res
134+ * @param {Object } body
135+ */
136+ const validateAndDoSomething = async ( req , res , body ) => {
137+ try {
138+ await validate ( body ) ;
139+ return true ;
140+ } catch ( e ) {
141+ res . status ( statusCode . BAD_REQUEST ) . send ( e . message ) ;
142+ return false ;
143+ }
129144} ;
130145
131146/**
@@ -512,6 +527,7 @@ const patch = async (req, res) => {
512527 */
513528const exportTemplate = async ( req , res ) => {
514529 const { name } = req . params ;
530+ const plain = 'plain' in req . query ;
515531 const template = await Template . findOne ( { name } ) ;
516532
517533 if ( template == null )
@@ -527,7 +543,8 @@ const exportTemplate = async (req, res) => {
527543 if ( exportedObj [ prop ] ) delete exportedObj [ prop ] ;
528544
529545 try {
530- exportedObj . background = await imgToBase64 ( exportedObj . background ) ;
546+ if ( ! plain )
547+ exportedObj . background = await imgToBase64 ( exportedObj . background ) ;
531548 } catch ( e ) {
532549 const msg = `Failed to export template '${ name } ': Unable to export background (${ e . message } )!` ;
533550 console . error ( msg ) ;
@@ -536,7 +553,7 @@ const exportTemplate = async (req, res) => {
536553 }
537554
538555 for ( const field of exportedObj . fields ) {
539- if ( field . type !== 'Image' )
556+ if ( field . type !== 'Image' || plain )
540557 continue ;
541558
542559 try {
0 commit comments