@@ -105,64 +105,75 @@ const trpcController = {
105105 throw 'is String' ;
106106 }
107107 } catch ( error ) {
108- return JSON . parse ( str ) ;
108+ if ( error === 'is String' ) {
109+ return JSON . parse ( str ) ;
110+ } else {
111+ return { error } ;
112+ }
109113 }
110114 }
111- const { headers, cookie } = reqResObject . request ; //grab the headers and cookie inputted by user
115+ try {
116+ const { headers, cookie } = reqResObject . request ; //grab the headers and cookie inputted by user
112117
113- const formattedHeaders = { } ; //header object
114- headers . forEach ( ( head ) => {
115- if ( head . active ) {
116- formattedHeaders [ head . key ] = head . value ;
117- }
118- } ) ;
119- if ( cookie ) {
120- //parses cookie data to attach to option object
121- cookie . forEach ( ( cookie ) => {
122- const cookieString = `${ cookie . key } =${ cookie . value } ` ;
123- // attach to formattedHeaders so options object includes this
124- formattedHeaders . cookie = formattedHeaders . cookie
125- ? formattedHeaders . cookie + ';' + cookieString
126- : cookieString ;
118+ const formattedHeaders = { } ; //header object
119+ headers . forEach ( ( head ) => {
120+ if ( head . active ) {
121+ formattedHeaders [ head . key ] = head . value ;
122+ }
127123 } ) ;
128- }
124+ if ( cookie ) {
125+ //parses cookie data to attach to option object
126+ cookie . forEach ( ( cookie ) => {
127+ const cookieString = `${ cookie . key } =${ cookie . value } ` ;
128+ // attach to formattedHeaders so options object includes this
129+ formattedHeaders . cookie = formattedHeaders . cookie
130+ ? formattedHeaders . cookie + ';' + cookieString
131+ : cookieString ;
132+ } ) ;
133+ }
129134
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
132- let url = '' ;
133- const body = { } ;
134- procedures . forEach ( ( procedure , index ) => {
135- if ( procedure . variable ) {
136- body [ index ] = parseString ( procedure . variable ) ;
135+ const outputObj = {
136+ // the main object that will get returned
137+ method,
138+ mode : 'cors' , // no-cors, cors, *same-origin
139+ cache : 'no-cache' , // *default, no-cache, reload, force-cache, only-if-cached
140+ credentials : 'include' , // include, *same-origin, omit
141+ headers : formattedHeaders ,
142+ redirect : 'follow' , // manual, *follow, error
143+ referrer : 'no-referrer' , // no-referrer, *client
144+ } ;
145+ // here we will construct the url and the body using data inside the reqRes obj
146+ // 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
147+ let url = '' ;
148+ const body = { } ;
149+ procedures . forEach ( ( procedure , index ) => {
150+ if ( procedure . variable ) {
151+ body [ index ] = parseString ( procedure . variable ) ;
152+ if ( body [ index ] . error ) {
153+ throw "Invalid variable input: Please check all procedure's input to be in json string format" ;
154+ }
155+ } else {
156+ body [ index ] = { } ;
157+ }
158+ url = url ? url + ',' + procedure . endpoint : procedure . endpoint ;
159+ } ) ;
160+ if ( method === 'POST' ) {
161+ url = reqResObject . url + '/' + url + '?batch=1' ;
162+ outputObj . body = body ;
137163 } else {
138- body [ index ] = { } ;
164+ url =
165+ reqResObject . url +
166+ '/' +
167+ url +
168+ '?batch=1' +
169+ `&input=${ encodeURIComponent ( JSON . stringify ( body ) ) } ` ;
139170 }
140- url = url ? url + ',' + procedure . endpoint : procedure . endpoint ;
141- } ) ;
142- if ( method === 'POST' ) {
143- url = reqResObject . url + '/' + url + '?batch=1' ;
144- outputObj . body = body ;
145- } else {
146- url =
147- reqResObject . url +
148- '/' +
149- url +
150- '?batch=1' +
151- `&input=${ encodeURIComponent ( JSON . stringify ( body ) ) } ` ;
152- }
171+ outputObj . url = url ;
153172
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- } ;
165- return outputObj ;
173+ return outputObj ;
174+ } catch ( error ) {
175+ return { error } ;
176+ }
166177 } ,
167178
168179 openRequest : async function ( event , reqRes ) {
@@ -171,23 +182,35 @@ const trpcController = {
171182 //filter the procedures into either query or mutate in order to make the appropriate http request for each procedure
172183 // all query procedure will be made with a get request
173184 // all mutation procedure will be made with a post request
174- const getReq = procedures . filter (
175- ( procedure ) => procedure . method === 'QUERY'
176- ) ;
177- const postReq = procedures . filter (
178- ( procedure ) => procedure . method === 'MUTATE'
179- ) ;
180-
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
182- const getOption = getReq . length
183- ? this . parseOptionForFetch ( reqRes , 'GET' , getReq )
184- : false ;
185- const postOption = postReq . length
186- ? this . parseOptionForFetch ( reqRes , 'POST' , postReq )
187- : false ;
188-
189- this . makeFetch ( event , reqRes , getOption , postOption ) ; // where we will finally make the request inside of makeFetch
185+ try {
186+ const getReq = procedures . filter (
187+ ( procedure ) => procedure . method === 'QUERY'
188+ ) ;
189+ const postReq = procedures . filter (
190+ ( procedure ) => procedure . method === 'MUTATE'
191+ ) ;
192+
193+ // 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
194+ const getOption = getReq . length
195+ ? this . parseOptionForFetch ( reqRes , 'GET' , getReq )
196+ : false ;
197+ const postOption = postReq . length
198+ ? this . parseOptionForFetch ( reqRes , 'POST' , postReq )
199+ : false ;
200+ if ( getOption . error || postOption . error ) {
201+ throw getOption . error ? getOption . error : postOption . error ;
202+ } else {
203+ this . makeFetch ( event , reqRes , getOption , postOption ) ; // where we will finally make the request inside of makeFetch
204+ }
205+ } catch ( error ) {
206+ //if error we will push the error into event to be display
207+ reqRes . connection = 'error' ;
208+ reqRes . error = error ;
209+ reqRes . response . events . push ( error ) ;
210+ event . sender . send ( 'reqResUpdate' , reqRes ) ; // send updated object back to front end
211+ }
190212 } ,
213+
191214 cookieFormatter ( cookieArray ) {
192215 return cookieArray . map ( ( eachCookie ) => {
193216 const cookieFormat = {
0 commit comments