@@ -89,7 +89,6 @@ export default function TRPCComposer(props) {
8989 const [ procedures , proceduresDipatch ] = useReducer ( reducer , [
9090 PROCEDURE_DEFAULT ,
9191 ] ) ;
92- // const [procedures, setProcedures] = useState([{ PROCEDURE_DEFAULT }]);
9392
9493 const {
9594 currentTab,
@@ -109,120 +108,11 @@ export default function TRPCComposer(props) {
109108
110109 /** reqRes slice from redux store, contains request and response data */
111110 const newRequest = useSelector ( ( state : RootState ) => state . newRequest ) ;
112- // const headers = newRequest.newRequestHeaders.headersArr.filter(
113- // (x) => x.active
114- // );
115111
116112 let subscription : any ;
117113 const addProcedures = ( ) => {
118114 proceduresDipatch ( { type : 'ADD' } ) ;
119115 } ;
120- // const sendRequest = () => {
121- // let isWebsocket = false;
122- // const links = [];
123- // const clientURL: string = requestFields.url; //grabbing url
124- // const request = requestBody.bodyContent;
125- // const httpRegex =
126- // /^http:\/\/([a-zA-Z0-9-]+\.[a-zA-Z]{2,}|localhost)(:[0-9]+)?(\/.*)?$/; // trpc doesn't accept https requests to my knowledge otherwise https?
127- // const wsRegex =
128- // /^(ws|wss):\/\/(([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}|localhost)(:[0-9]+)?(\/.*)?$/;
129-
130- // //checks if URL is to WebSocket or standard HTTP
131- // if (wsRegex.test(clientURL)) {
132- // // setup links array with ws
133- // isWebsocket = true;
134-
135- // //instantiates a WebSocket
136- // const wsClient = createWSClient({ url: clientURL });
137- // links.push(wsLink({ client: wsClient }));
138-
139- // //grabs the WebSocket from tRPC's wsClient
140- // const ws = wsClient.getConnection();
141- // const persistentData: Array<any> = [];
142-
143- // //current WebSocket is listening for anytime an event is sent back to client
144- // ws.onmessage = (event) => {
145- // persistentData.push(JSON.parse(event.data));
146- // const newCurrentResponse: any = {
147- // checkSelected: false,
148- // checked: false,
149- // connection: 'closed',
150- // connectionType: 'plain',
151- // createdAt: new Date(),
152- // gRPC: false,
153- // graphQL: false,
154- // host: clientURL,
155- // id: uuid(),
156- // minimized: false,
157- // path: '/',
158- // protoPath: undefined,
159- // protocol: 'ws://',
160- // request: { ...requestStuff },
161- // tab: undefined,
162- // timeReceived: 1676146914257,
163- // timeSent: 1676146914244,
164- // url: clientURL,
165- // webrtc: false,
166- // response: {
167- // events: [],
168- // },
169- // };
170- // newCurrentResponse.response.events.push([...persistentData]);
171- // dispatch(responseDataSaved(newCurrentResponse));
172- // };
173- // } else if (httpRegex.test(clientURL)) {
174- // // setup links array with http
175- // links.push(httpBatchLink({ url: clientURL }));
176- // } else {
177- // console.log('error in url');
178- // }
179-
180- // const client = createTRPCProxyClient({ links: links });
181-
182- // //if the request is to a WebSocket server + is a subscription, execute request
183- // if (isWebsocket) {
184- // //replacing user's client name to what app is expecting
185- // const editedRequest = request.replace(/^[^.]*./, 'client.');
186- // subscription = eval(editedRequest);
187- // } else {
188- // //if request is not from Websocket server + is query/mutation, execute request
189- // //this handles batch queries + mutations
190- // const reqArray = request.split('\n').map((el) => {
191- // el = el.replace(/^[^.]*./, 'client.');
192- // return el;
193- // });
194-
195- // Promise.all(reqArray.map((el) => eval(el))).then((res: any) => {
196- // const newCurrentResponse: any = {
197- // checkSelected: false,
198- // checked: false,
199- // connection: 'closed',
200- // connectionType: 'plain',
201- // createdAt: new Date(),
202- // gRPC: false,
203- // graphQL: false,
204- // host: clientURL,
205- // id: uuid(),
206- // minimized: false,
207- // path: '/',
208- // protoPath: undefined,
209- // protocol: 'http://',
210- // request: { ...requestStuff },
211- // tab: undefined,
212- // timeReceived: null,
213- // timeSent: null,
214- // url: clientURL,
215- // webrtc: false,
216- // response: {
217- // events: [res],
218- // },
219- // };
220-
221- // //dispatch response to it's slice, to update the state
222- // dispatch(responseDataSarved(newCurrentResponse));
223- // });
224- // }
225- // };
226116
227117 function parseString ( str ) {
228118 if ( str === 'true' ) {
@@ -248,6 +138,35 @@ export default function TRPCComposer(props) {
248138 }
249139 }
250140
141+ const dispatchTRPCResponse = ( tRPCResponse ) => {
142+
143+ const newCurrentResponse : any = {
144+ checkSelected : false ,
145+ checked : false ,
146+ connection : 'closed' ,
147+ connectionType : 'plain' ,
148+ createdAt : new Date ( ) ,
149+ gRPC : false ,
150+ graphQL : false ,
151+ host : requestFields . url ,
152+ id : uuid ( ) ,
153+ minimized : false ,
154+ path : '/' ,
155+ protoPath : undefined ,
156+ protocol : 'http://' ,
157+ request : { ...newRequest } ,
158+ tab : undefined ,
159+ timeReceived : null ,
160+ timeSent : null ,
161+ url : requestFields . url ,
162+ webrtc : false ,
163+ response : {
164+ events : [ tRPCResponse ] ,
165+ } ,
166+ } ;
167+ dispatch ( responseDataSaved ( newCurrentResponse ) ) ;
168+ }
169+
251170 const sendRequest = async ( ) => {
252171 const links = [ ] ;
253172 const batchConfigureObject = { } ;
@@ -262,87 +181,41 @@ export default function TRPCComposer(props) {
262181 batchConfigureObject . headers = headers ;
263182 }
264183 links . push ( httpBatchLink ( batchConfigureObject ) ) ;
265- // const clientURL: string = requestFields.url; //grabbing url
266-
184+
267185 const client = createTRPCProxyClient ( { links } ) ;
186+
187+ // processes the request variables, sends the request to the tRPC endpoint, and handles any errors
268188 Promise . all (
189+
269190 procedures . map ( ( procedure ) => {
270191 let endpoint = procedure . endpoint ;
271192 const method = procedure . method . toLowerCase ( ) ;
193+ let tempArg = '' ;
194+
272195 if ( procedure . variable ) {
273- console . log ( 'SHOULD NOT HI' ) ;
274196 let arg = parseString ( procedure . variable . replace ( / \s / g, '' ) ) ;
275- const tempArg = procedure . variable . replace ( / \s / g, '' ) ;
276- const e = `client.${ endpoint } .${ method } (${ tempArg } )` ;
277- return eval ( e ) ;
278- } else {
279- return eval ( `client.${ endpoint } .${ method } ()` ) ;
197+ tempArg = procedure . variable . replace ( / \s / g, '' ) ;
280198 }
199+ const e = `client.${ endpoint } .${ method } (${ tempArg } )` ;
200+
201+ new Promise ( ( resolve , reject ) => {
202+ try {
203+ const result = eval ( e ) ;
204+ resolve ( result ) ;
205+ } catch ( error ) {
206+ reject ( error ) ;
207+ }
208+ } )
209+ . then ( res => {
210+ dispatchTRPCResponse ( res ) ;
211+ } )
212+ . catch ( error => {
213+ dispatchTRPCResponse ( error ) ;
214+ } )
281215 } )
282- ) . then ( ( res ) => {
283- // const fakeRes = {
284- // id: uuid(),
285- // createdAt: new Date(),
286- // protocol: 'http://',
287- // url: 'google.com',
288- // timeSent: null,
289- // timeReceived: null,
290- // connection: 'uninitialized',
291- // connectionType: null,
292- // checkSelected: false,
293- // request: {
294- // method: 'Get',
295- // },
296- // response: {
297- // headers: {},
298- // events: [],
299- // },
300- // checked: false,
301- // minimized: false,
302- // tab: currentTab,
303- // };
304- const newCurrentResponse : any = {
305- checkSelected : false ,
306- checked : false ,
307- connection : 'closed' ,
308- connectionType : 'plain' ,
309- createdAt : new Date ( ) ,
310- gRPC : false ,
311- graphQL : false ,
312- host : requestFields . url ,
313- id : uuid ( ) ,
314- minimized : false ,
315- path : '/' ,
316- protoPath : undefined ,
317- protocol : 'http://' ,
318- request : { ...newRequest } ,
319- tab : undefined ,
320- timeReceived : null ,
321- timeSent : null ,
322- url : requestFields . url ,
323- webrtc : false ,
324- response : {
325- events : [ res ] ,
326- } ,
327- } ;
328-
329- //dispatch response to it's slice, to update the state
330- // reqResItemAdded(fakeRes);
331- dispatch ( responseDataSaved ( newCurrentResponse ) ) ;
332- } ) ;
333-
334- // const arg = JSON.parse(currProc.variable.replace(/\s/g, ''));
335-
336- // const res = await client[endpoint][method](arg);
337- // console.log(res);
338- // console.log((procedures.variable = procedures.variable.replace(/\s/g, '')));
216+ )
217+ }
339218
340- // const res = await client.update.mutate({
341- // userId: '1',
342- // name: 'nguyen',
343- // });
344- // console.log(res);
345- } ;
346219 return (
347220 < Box
348221 className = "is-flex is-flex-direction-column is-justify-content-space-between"
@@ -376,12 +249,7 @@ export default function TRPCComposer(props) {
376249 Add Procedure
377250 </ button >
378251 < SendRequestButton onClick = { sendRequest } />
379- { /* {requestFields.method === 'SUBSCRIPTION' && ( ////for subscription
380- <SendRequestButton
381- onClick={() => subscription.unsubscribe()}
382- buttonText="Close Subscription"
383- ></SendRequestButton>
384- )} */ }
252+
385253 </ div >
386254 </ Box >
387255 ) ;
0 commit comments