@@ -201,80 +201,120 @@ function createPluginFromGithub(req) {
201201 } ) ;
202202}
203203
204- router . post ( '/' , ( req , res ) => {
204+ function createEmptyPlugin ( req , metadata ) {
205205 const { s3, Bucket } = S3 . state ( )
206206
207207 const user = format ( req . user . email )
208208
209- UserManager . createUserIfNotExists ( req )
210- . then ( ( ) => {
211- UserManager . getUser ( req )
212- . then ( data => {
213- const pluginId = crypto . randomUUID ( )
214- const plugins = [
215- ...( data . plugins || [ ] ) ,
216- {
217- filename : req . body . plugin ,
218- type : req . body . type ,
219- pluginId : pluginId
209+ return new Promise ( resolve => {
210+ UserManager . createUserIfNotExists ( req )
211+ . then ( ( ) => {
212+ UserManager . getUser ( req )
213+ . then ( data => {
214+ const pluginId = crypto . randomUUID ( )
215+ const plugins = [
216+ ...( data . plugins || [ ] ) ,
217+ {
218+ filename : metadata . name ,
219+ type : metadata . type ,
220+ pluginId : pluginId
221+ }
222+ ]
223+ const params = {
224+ Bucket,
225+ Key : `${ user } .json` ,
226+ Body : JSON . stringify ( {
227+ ...data ,
228+ plugins
229+ } )
220230 }
221- ]
222- const params = {
223- Bucket,
224- Key : `${ user } .json` ,
225- Body : JSON . stringify ( {
226- ...data ,
227- plugins
228- } )
229- }
230231
231- s3 . send ( new PutObjectCommand ( params ) )
232- . then ( ( ) => res
233- . status ( 201 )
234- . json ( {
235- plugins
236- } ) )
237- . catch ( err => {
238- console . log ( err )
239- res
240- . status ( err . $metadata . httpStatusCode )
241- . json ( {
242- error : err . Code ,
243- status : err . $metadata . httpStatusCode
232+ s3 . send ( new PutObjectCommand ( params ) )
233+ . then ( ( ) => {
234+ resolve ( {
235+ status : 201 ,
236+ body : {
237+ plugins
238+ }
244239 } )
245- } )
246- } )
247- } )
248- . catch ( err => {
249- res
250- . status ( 400 )
251- . json ( {
252- error : err . message
240+ } )
241+ . catch ( err => {
242+ resolve ( {
243+ status : err . $metadata . httpStatusCode ,
244+ body : {
245+ error : err . Code ,
246+ status : err . $metadata . httpStatusCode
247+ }
248+ } )
249+ } )
250+ } )
251+ } )
252+ . catch ( err => {
253+ resolve ( {
254+ status : 400 ,
255+ body : {
256+ error : err . message
257+ }
253258 } )
254- } )
255- } )
259+ } )
260+ } )
261+ }
256262
257- router . put ( '/:id' , ( req , res ) => {
263+ function updatePluginContent ( id , body ) {
258264 const { s3, Bucket } = S3 . state ( ) ;
259265
260266 const params = {
261267 Bucket,
262- Key : `${ req . params . id } .zip` ,
263- Body : req . body
268+ Key : `${ id } .zip` ,
269+ Body : body
264270 }
265271
266- s3 . send ( new PutObjectCommand ( params ) )
267- . then ( ( ) => res
268- . status ( 204 )
269- . json ( null ) )
272+ return s3 . send ( new PutObjectCommand ( params ) )
273+ . then ( ( ) => ( {
274+ status : 204 ,
275+ body : null
276+ } ) )
270277 . catch ( err => {
271- res
272- . status ( err . $metadata . httpStatusCode )
273- . json ( {
278+ return {
279+ status : err . $metadata . httpStatusCode ,
280+ body : {
274281 error : err . Code ,
275282 status : err . $metadata . httpStatusCode
276- } )
283+ }
284+ }
277285 } )
286+ }
287+
288+ router . post ( '/' , async ( req , res ) => {
289+
290+ if ( ! req . body ||
291+ Object . keys ( req . body ) . length === 0 ||
292+ ( ! req . body . metadata && ! ( req . body . plugin && req . body . type ) ) ) {
293+ return res . status ( 400 ) . json ( { error : 'missing body' } ) ;
294+ }
295+
296+ const out = await createEmptyPlugin ( req , req . body . metadata || {
297+ name : req . body . plugin ,
298+ type : req . body . type
299+ } ) ;
300+
301+ if ( out . status !== 201 || ! req . body . files ) {
302+ return res . status ( out . status ) . json ( out . body ) ;
303+ } else {
304+ const templatesFiles = await FileSystem . templatesFilesToJSON ( req . body . metadata . type ) ;
305+ const zip = await FileSystem . createZipFromJSONFiles ( req . body . files , templatesFiles ) ;
306+ updatePluginContent ( out . body . plugins [ out . body . plugins . length - 1 ] . pluginId , zip )
307+ . then ( out => {
308+ res . status ( out . status ) . json ( out . body ) ;
309+ } ) ;
310+ }
311+ } )
312+
313+ router . put ( '/:id' , ( req , res ) => {
314+ updatePluginContent ( req . params . id , req . body )
315+ . then ( out => {
316+ res . status ( out . status ) . json ( out . body ) ;
317+ } ) ;
278318} )
279319
280320router . delete ( '/:id' , async ( req , res ) => {
0 commit comments