import { commands } from '@config/commands' ; import { PluginMessage } from '@typings/pluginEvent' ; import { urlExportRequestBody , urlExportSettings } from '@typings/urlExportData' ; const responeHandler = ( request ) : string => { if ( request . status === 401 ) { return '🚨 401: Check your access token' ; } if ( request . status === 404 ) { return '🚨 404: Check your server url, auth type and the access token' ; } if ( request . status > 399 ) { return `🚨 ${ request . status }: An error occured, please check the console for details.` ; } return '🎉 Design tokens pushed to server!' ; } ; const urlExport = ( parent , exportSettings , requestBody ) => { if ( exportSettings . url === '' ) { parent . postMessage ( { pluginMessage : { command : commands . closePlugin , payload : { notification : '🚨 No server url was provided, push aborted!' } } as PluginMessage } , '*' ) ; } const request = new XMLHttpRequest ( ) ; request . open ( 'POST' , exportSettings . url ) ; if ( exportSettings . acceptHeader !== '' ) { request . setRequestHeader ( 'Accept' , 'application/vnd.github.everest-preview+json' ) ; } if ( exportSettings . accessToken !== '' && exportSettings . authType !== '' ) { request . setRequestHeader ( 'Authorization' , `${ exportSettings . authType } ${ exportSettings . accessToken }` ) ; } request . onerror = ( event ) => { parent . postMessage ( { pluginMessage : { command : commands . closePlugin , payload : { notification : '🚨 An error occured while sending the tokens: check your settings & your server.' } } as PluginMessage } , '*' ) ; } ; request . onload = ( progressEvent ) => { parent . postMessage ( { pluginMessage : { command : commands . closePlugin , payload : { notification : responeHandler ( progressEvent . target as XMLHttpRequest ) } } as PluginMessage } , '*' ) ; } ; request . send ( JSON . stringify ( requestBody , null , 2 ) ) ; } ; export { urlExport } ;