import electron from "electron" ; const { net } = electron ; export type ResponseType = { headers : object ; statusCode : number ; statusMessage : string ; body : string ; } ; export const postRequest = ( options , data ? ) : Promise < ResponseType > => { return new Promise ( ( resolve , reject ) => { const ops = { ... options } ; const headers = ops . headers || { } ; headers [ "Content-Type" ] = "application/x-www-form-urlencoded" ; const request = net . request ( { ... ops , headers , method : "POST" } ) ; request . on ( "response" , response => { const datas = [ ] ; response . on ( "data" , chunk => { datas . push ( chunk ) ; } ) ; response . on ( "end" , ( ) => { const body = Buffer . concat ( datas ) ; const resp = { headers : response . headers , statusCode : response . statusCode , statusMessage : response . statusMessage , body : body . toString ( ) , } ; if ( response . statusCode >= 400 ) { reject ( new Error ( `${ resp . statusCode } - ${ resp . statusMessage } : ${ resp . body }` ) ) ; return ; } resolve ( resp ) ; } ) ; response . on ( "error" , ( error ) => { reject ( error ) ; } ) ; } ) ; if ( data ) { request . write ( data , "utf8" ) ; } request . end ( ) ; } ) ; } ;