@@ -5,16 +5,19 @@ const { ipcMain } = require('electron');
55const trpcController = {
66 makeFetch : async function ( event , reqRes , get , post ) {
77 try {
8+ // wrap it in a try catch block so if anything goes wrong we can just send the error to the front end instead of having no response
89 reqRes . timeSent = Date . now ( ) ;
10+ //these will contain data about the response that we will get back from our get/post request
911 const cookies = [ ] ;
1012 const getHeaders = { } ;
1113 const postHeaders = { } ;
1214 const events = [ ] ;
1315
14- const reqArr = [ get , post ] ;
16+ const reqArr = [ get , post ] ; // our array of request, put into an array in order to use promise.all easier
1517 const resArr = await Promise . all (
1618 reqArr . map ( ( req ) => {
1719 if ( req ) {
20+ // only make a request if it exists
1821 return fetch ( req . url , {
1922 // credentials: req.credentials,
2023 // mode: req.mode,
@@ -26,12 +29,14 @@ const trpcController = {
2629 ...( req . body && { body : JSON . stringify ( req . body ) } ) ,
2730 } ) ;
2831 } else {
29- return Promise . resolve ( false ) ;
32+ return Promise . resolve ( false ) ; //if it doesnt exist we can simply return false
3033 }
3134 } )
3235 ) ;
3336 reqRes . timeReceived = Date . now ( ) ;
3437 resArr . forEach ( ( res , index ) => {
38+ // here we will combine cookies recieved from both request into one array, however we will seperate out the header by post/get request
39+ // was sleep-deprived should have also seperate cookie into get/post cookie
3540 if ( res ) {
3641 const headersResponse = res . headers . raw ( ) ;
3742 if ( headersResponse [ 'set-cookie' ] ) {
@@ -54,27 +59,31 @@ const trpcController = {
5459 } ) ;
5560
5661 const resData = await Promise . all (
62+ //if there was a response we will parse it if not we will just return it (which is just the value of false)
5763 resArr . map ( ( res ) => {
5864 return res ? res . json ( ) : res ;
5965 } )
6066 ) ;
61- console . dir ( resData , { depth : null } ) ;
67+ //attach meta data about the response onto the reqRes object to send back to front end
6268 resData . forEach ( ( res ) => events . push ( res ) ) ;
6369 reqRes . response . cookies = cookies ;
6470 reqRes . response . events = events ;
6571 reqRes . response . headers = [ getHeaders , postHeaders ] ;
6672 reqRes . connection = 'closed' ;
6773 reqRes . connectionType = 'plain' ;
68- event . sender . send ( 'reqResUpdate' , reqRes ) ;
74+ event . sender . send ( 'reqResUpdate' , reqRes ) ; //send object back to front end
6975 } catch ( error ) {
76+ //if error we will push the error into event to be display
7077 reqRes . connection = 'error' ;
7178 reqRes . error = error ;
7279 reqRes . response . events . push ( error ) ;
73- event . sender . send ( 'reqResUpdate' , reqRes ) ;
80+ event . sender . send ( 'reqResUpdate' , reqRes ) ; // send updated object back to front end
7481 }
7582 } ,
7683 parseOptionForFetch ( reqResObject , method , procedures ) {
84+ // this method using the data attach to the response property to construct an object that we could use to easily make the neccessary request
7785 function parseString ( str ) {
86+ //this function is use to parse user inputted argument from json into javascript
7887 if ( str === 'true' ) {
7988 return true ;
8089 }
@@ -99,34 +108,27 @@ const trpcController = {
99108 return JSON . parse ( str ) ;
100109 }
101110 }
102- const { headers, cookie } = reqResObject . request ;
111+ const { headers, cookie } = reqResObject . request ; //grab the headers and cookie inputted by user
103112
104- const formattedHeaders = { } ;
113+ const formattedHeaders = { } ; //header object
105114 headers . forEach ( ( head ) => {
106115 if ( head . active ) {
107116 formattedHeaders [ head . key ] = head . value ;
108117 }
109118 } ) ;
110119 if ( cookie ) {
120+ //parses cookie data to attach to option object
111121 cookie . forEach ( ( cookie ) => {
112122 const cookieString = `${ cookie . key } =${ cookie . value } ` ;
113123 // attach to formattedHeaders so options object includes this
114-
115124 formattedHeaders . cookie = formattedHeaders . cookie
116125 ? formattedHeaders . cookie + ';' + cookieString
117126 : cookieString ;
118127 } ) ;
119128 }
120129
121- const outputObj = {
122- method,
123- mode : 'cors' , // no-cors, cors, *same-origin
124- cache : 'no-cache' , // *default, no-cache, reload, force-cache, only-if-cached
125- credentials : 'include' , // include, *same-origin, omit
126- headers : formattedHeaders ,
127- redirect : 'follow' , // manual, *follow, error
128- referrer : 'no-referrer' , // no-referrer, *client
129- } ;
130+ // here we will construct the url and the body using data inside the reqRes obj
131+ // because a user could batch procedures together/ we need to account for the request object to contain data for both a get request and a post request
130132 let url = '' ;
131133 const body = { } ;
132134 procedures . forEach ( ( procedure , index ) => {
@@ -148,34 +150,43 @@ const trpcController = {
148150 '?batch=1' +
149151 `&input=${ encodeURIComponent ( JSON . stringify ( body ) ) } ` ;
150152 }
151- outputObj . url = url ;
153+
154+ const outputObj = {
155+ // the main object that will get returned
156+ url,
157+ method,
158+ mode : 'cors' , // no-cors, cors, *same-origin
159+ cache : 'no-cache' , // *default, no-cache, reload, force-cache, only-if-cached
160+ credentials : 'include' , // include, *same-origin, omit
161+ headers : formattedHeaders ,
162+ redirect : 'follow' , // manual, *follow, error
163+ referrer : 'no-referrer' , // no-referrer, *client
164+ } ;
152165 return outputObj ;
153166 } ,
154167
155168 openRequest : async function ( event , reqRes ) {
156- // const reqRes = Object.assign({}, reqResOriginal);
157- // reqRes.response = Object.assign({}, reqRes.response);
158- const procedures = reqRes . request . procedures ;
169+ const procedures = reqRes . request . procedures ; // grabbing all of the procedures out of the reqRes object
170+
171+ //filter the procedures into either query or mutate in order to make the appropriate http request for each procedure
172+ // all query procedure will be made with a get request
173+ // all mutation procedure will be made with a post request
159174 const getReq = procedures . filter (
160175 ( procedure ) => procedure . method === 'QUERY'
161176 ) ;
162177 const postReq = procedures . filter (
163178 ( procedure ) => procedure . method === 'MUTATE'
164179 ) ;
165180
181+ // parsing data from the reqRes object to construct either a get/post option object that contains everything we need to make our get/post http request
166182 const getOption = getReq . length
167183 ? this . parseOptionForFetch ( reqRes , 'GET' , getReq )
168184 : false ;
169185 const postOption = postReq . length
170186 ? this . parseOptionForFetch ( reqRes , 'POST' , postReq )
171187 : false ;
172188
173- const updatedReqRes = await this . makeFetch (
174- event ,
175- reqRes ,
176- getOption ,
177- postOption
178- ) ;
189+ this . makeFetch ( event , reqRes , getOption , postOption ) ; // where we will finally make the request inside of makeFetch
179190 } ,
180191 cookieFormatter ( cookieArray ) {
181192 return cookieArray . map ( ( eachCookie ) => {
@@ -201,3 +212,4 @@ module.exports = () => {
201212 trpcController . openRequest ( event , reqResObj ) ;
202213 } ) ;
203214} ;
215+
0 commit comments