Skip to content

Commit 14fe850

Browse files
committed
able to group query/mutate procedure together and able to make a request individually, needs to refactor to use promise.all
1 parent d4a2f41 commit 14fe850

File tree

3 files changed

+133
-90
lines changed

3 files changed

+133
-90
lines changed

main_process/main_httpController.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ const httpController = {
295295
const { method, headers, body } = args.options;
296296
const response = await fetch(headers.url, { method, headers, body });
297297
const headersResponse = response.headers.raw();
298-
event.sender.send('console', 'WHATS UP');
299298
if (headersResponse['content-type'][0].includes('stream')) {
300299
return {
301300
headers: headersResponse,

src/client/components/main/TRPC-composer/TRPCComposer.tsx

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -254,36 +254,79 @@ export default function TRPCComposer(props) {
254254
// return getPath(obj[properties.shift()], properties.join('.'), args);
255255
// }
256256

257-
const cache = [
258-
{
259-
method: 'MUTATE',
260-
endpoint: 'update',
261-
variable: '{\n "userId": "1",\n "name": "nguyen"\n }',
262-
},
263-
{ method: 'QUERY', endpoint: 'sayHi', variable: '' },
264-
{
265-
method: 'MUTATE',
266-
endpoint: 'user.update',
267-
variable: '{\n "userId": "1",\n "name": "justin"\n }',
268-
},
269-
{ method: 'QUERY', endpoint: 'message', variable: 'hello world' },
270-
];
271-
const cookiCache = [
272-
{ id: 'cookie0', active: true, key: 'cookie1', value: 'cookie1Val' },
273-
{ id: 'cookie1', active: true, key: 'cookie2', value: 'cookie2Val' },
274-
];
275-
const headerCache = [
276-
{ id: 351217.45631817693, active: true, key: 'auth1', value: 'auth2' },
277-
{ id: 87384.65282544694, active: true, key: 'auth3', value: 'auth4' },
278-
];
279257
const sendRequest = async () => {
280258
const id = uuid();
259+
281260
// const headers = newRequest.newRequestHeaders.headersArr.filter(
282261
// (x) => x.active
283262
// );
284263

285-
const cookie = cookiesArr.filter((x) => x.active);
264+
// const cookie = cookiesArr.filter((x) => x.active);
286265

266+
// const reqRes = {
267+
// id,
268+
// createdAt: new Date(),
269+
// protocol,
270+
// trpc: true,
271+
// url: 'http://localhost:3000/trpc',
272+
// graphQL,
273+
// gRPC,
274+
// webrtc,
275+
// timeSent: null,
276+
// timeReceived: null,
277+
// connection: 'uninitialized',
278+
// connectionType: null,
279+
// checkSelected: false,
280+
// request: {
281+
// method,
282+
// headers,
283+
// procedures,
284+
// bodyType,
285+
// rawType,
286+
// network,
287+
// restUrl,
288+
// wsUrl,
289+
// gqlUrl,
290+
// cookie,
291+
// },
292+
// response: {
293+
// cookies: [],
294+
// headers: {},
295+
// stream: null,
296+
// events: [],
297+
// },
298+
// checked: false,
299+
// minimized: false,
300+
// tab: currentTab,
301+
// };
302+
303+
const cache = [
304+
{
305+
method: 'MUTATE',
306+
endpoint: 'update',
307+
variable: '{\n "userId": "1",\n "name": "nguyen"\n }',
308+
},
309+
{ method: 'QUERY', endpoint: 'sayHi', variable: '' },
310+
{
311+
method: 'MUTATE',
312+
endpoint: 'user.update',
313+
variable: '{\n "userId": "1",\n "name": "justin"\n }',
314+
},
315+
{
316+
method: 'QUERY',
317+
endpoint: 'secretData',
318+
variable: '{\n "userId": "1",\n "name": "justin"\n }',
319+
},
320+
];
321+
322+
const cookiCache = [
323+
{ id: 'cookie0', active: true, key: 'cookie1', value: 'cookie1Val' },
324+
{ id: 'cookie1', active: true, key: 'cookie2', value: 'cookie2Val' },
325+
];
326+
const headerCache = [
327+
{ id: 351217.45631817693, active: true, key: 'auth1', value: 'auth2' },
328+
{ id: 87384.65282544694, active: true, key: 'auth3', value: 'auth4' },
329+
];
287330
const reqRes = {
288331
id,
289332
createdAt: new Date(),
@@ -308,7 +351,7 @@ export default function TRPCComposer(props) {
308351
restUrl,
309352
wsUrl,
310353
gqlUrl,
311-
cookies: cookiCache,
354+
cookie: cookiCache,
312355
},
313356
response: {
314357
cookies: [],
@@ -320,14 +363,13 @@ export default function TRPCComposer(props) {
320363
minimized: false,
321364
tab: currentTab,
322365
};
323-
324366
// add request to history
325367
historyController.addHistoryToIndexedDb(reqRes);
326368
reqResItemAdded(reqRes);
327369

328370
//reset for next request
329-
composerFieldsReset();
330-
trpcController.sendRequest(reqRes);
371+
// composerFieldsReset();
372+
const updatedReqRes = await trpcController.sendRequest(reqRes);
331373

332374
// const links = [];
333375
// const batchConfigureObject = {};

src/client/controllers/trpcController.js

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,23 @@
11
const trpcController = {
2-
makeFetch: async (args, event, reqResObj) => {
3-
try {
4-
const { method, headers, body } = args.options;
5-
const response = await fetch(headers.url, { method, headers, body });
6-
const headersResponse = response.headers.raw();
7-
event.sender.send('console', 'WHATS UP');
8-
if (headersResponse['content-type'][0].includes('stream')) {
9-
return {
10-
headers: headersResponse,
11-
body: { error: 'This Is An SSE endpoint' },
12-
};
13-
}
14-
15-
headersResponse[':status'] = response.status;
16-
const receivedCookie = headersResponse['set-cookie'];
17-
headersResponse.cookies = receivedCookie;
18-
19-
const contentType = response.headers.get('content-type');
20-
const contents = /json/.test(contentType)
21-
? await response.json()
22-
: await response.text();
23-
24-
return {
25-
headers: headersResponse,
26-
body: contents,
27-
};
28-
} catch (error) {
29-
reqResObj.connection = 'error';
30-
reqResObj.error = error;
31-
reqResObj.response.events.push(error);
32-
event.sender.send('reqResUpdate', reqResObj);
33-
throw error;
34-
}
2+
makeFetch: async (reqRes, get, post) => {
3+
const { cache, headers, method, redirect, referrer, url } = get;
4+
console.log(get, post);
5+
const getRes = await fetch(url, {
6+
cache,
7+
headers,
8+
method,
9+
redirect,
10+
referrer,
11+
});
12+
const getData = await getRes.json();
13+
console.log(getData);
14+
const resHeader = {};
15+
getRes.headers.forEach((value, key) => {
16+
resHeader[key] = value;
17+
});
18+
console.log(resHeader);
3519
},
36-
parseOptionForFetch(reqResObject, method, postProcedure) {
20+
parseOptionForFetch(reqResObject, method, procedures) {
3721
function parseString(str) {
3822
if (str === 'true') {
3923
return true;
@@ -49,7 +33,7 @@ const trpcController = {
4933

5034
try {
5135
const parsedJson = JSON.parse(str.replace(/\s/g, ''));
52-
console.log(parsedJson);
36+
5337
if (typeof parsedJson === 'object' && parsedJson !== null) {
5438
return parsedJson;
5539
} else {
@@ -59,18 +43,16 @@ const trpcController = {
5943
return JSON.parse(str);
6044
}
6145
}
62-
const { headers, cookies } = reqResObject.request;
46+
const { headers, cookie } = reqResObject.request;
6347

64-
const formattedHeaders = {
65-
url: reqResObject.url,
66-
};
48+
const formattedHeaders = {};
6749
headers.forEach((head) => {
6850
if (head.active) {
6951
formattedHeaders[head.key] = head.value;
7052
}
7153
});
72-
if (cookies) {
73-
cookies.forEach((cookie) => {
54+
if (cookie) {
55+
cookie.forEach((cookie) => {
7456
const cookieString = `${cookie.key}=${cookie.value}`;
7557
// attach to formattedHeaders so options object includes this
7658

@@ -89,42 +71,62 @@ const trpcController = {
8971
redirect: 'follow', // manual, *follow, error
9072
referrer: 'no-referrer', // no-referrer, *client
9173
};
92-
let endPoint = '';
74+
let url = '';
75+
const body = {};
76+
procedures.forEach((procedure, index) => {
77+
if (procedure.variable) {
78+
body[index] = parseString(procedure.variable);
79+
} else {
80+
body[index] = {};
81+
}
82+
url = url ? url + ',' + procedure.endpoint : procedure.endpoint;
83+
});
9384
if (method === 'POST') {
94-
const body = {};
95-
96-
postProcedure.forEach((procedure, index) => {
97-
if (procedure.variable) {
98-
body[index] = parseString(procedure.variable);
99-
} else {
100-
body[index] = {};
101-
}
102-
endPoint = endPoint
103-
? endPoint + ',' + procedure.endpoint
104-
: procedure.endpoint;
105-
});
85+
url = reqResObject.url + '/' + url + '?batch=1';
10686
outputObj.body = body;
10787
} else {
108-
const input = '';
88+
url =
89+
reqResObject.url +
90+
'/' +
91+
url +
92+
'?batch=1' +
93+
`&input=${encodeURIComponent(JSON.stringify(body))}`;
10994
}
110-
outputObj.endPoint = endPoint;
95+
outputObj.url = url;
11196
return outputObj;
11297
},
11398

114-
sendRequest: function (reqRes) {
115-
console.log(reqRes);
99+
sendRequest: async function (reqRes) {
116100
const procedures = reqRes.request.procedures;
117101
const getReq = procedures.filter(
118102
(procedure) => procedure.method === 'QUERY'
119103
);
120104
const postReq = procedures.filter(
121105
(procedure) => procedure.method === 'MUTATE'
122106
);
123-
console.log(getReq);
124-
const getOption = this.parseOptionForFetch(reqRes, 'GET', getReq);
125-
const postOption = this.parseOptionForFetch(reqRes, 'POST', postReq);
126107

127-
console.log(getOption, postOption);
108+
const getOption = getReq.length
109+
? this.parseOptionForFetch(reqRes, 'GET', getReq)
110+
: false;
111+
const postOption = postReq.length
112+
? this.parseOptionForFetch(reqRes, 'POST', postReq)
113+
: false;
114+
115+
const updatedReqRes = await this.makeFetch(reqRes, getOption, postOption);
116+
// const data = await fetch(reqRes.url + '/' + getOption.endPoint);
117+
// const res = await data.json();
118+
// const resHeader = {};
119+
// data.headers.forEach((value, key) => {
120+
// resHeader[key] = value;
121+
// });
122+
// console.log(header);
123+
124+
// const postData = await fetch(reqRes.url + '/' + postOption.endPoint, {
125+
// method: 'POST',
126+
// body: JSON.stringify(postOption.body),
127+
// });
128+
// const postRes = await postData.json();
129+
// console.log(res, postRes);
128130
},
129131
};
130132

0 commit comments

Comments
 (0)