feat: add and update request headers

This commit is contained in:
Anoop M D 2022-03-19 18:33:16 +05:30
parent 5cbf163e26
commit cb96a175df
6 changed files with 125 additions and 39 deletions

View File

@ -4,7 +4,7 @@ import QueryParams from 'components/RequestPane/QueryParams';
import RequestHeaders from 'components/RequestPane/RequestHeaders'; import RequestHeaders from 'components/RequestPane/RequestHeaders';
import StyledWrapper from './StyledWrapper'; import StyledWrapper from './StyledWrapper';
const HttpRequestPane = ({leftPaneWidth}) => { const HttpRequestPane = ({item, collection, leftPaneWidth}) => {
return ( return (
<StyledWrapper className="h-full"> <StyledWrapper className="h-full">
<Tabs className='react-tabs mt-1 flex flex-grow flex-col h-full' forceRenderTabPanel> <Tabs className='react-tabs mt-1 flex flex-grow flex-col h-full' forceRenderTabPanel>
@ -21,7 +21,7 @@ const HttpRequestPane = ({leftPaneWidth}) => {
<div>Body</div> <div>Body</div>
</TabPanel> </TabPanel>
<TabPanel> <TabPanel>
<RequestHeaders /> <RequestHeaders item={item} collection={collection}/>
</TabPanel> </TabPanel>
<TabPanel> <TabPanel>
<div>Auth</div> <div>Auth</div>

View File

@ -1,36 +1,55 @@
import React, { useState } from 'react'; import React from 'react';
import { nanoid } from 'nanoid'; import get from 'lodash/get';
import cloneDeep from 'lodash/cloneDeep';
import { IconTrash } from '@tabler/icons'; import { IconTrash } from '@tabler/icons';
import { useDispatch } from 'react-redux';
import { requestChanged } from 'providers/ReduxStore/slices/tabs';
import { addRequestHeader, updateRequestHeader } from 'providers/ReduxStore/slices/collections';
import StyledWrapper from './StyledWrapper'; import StyledWrapper from './StyledWrapper';
const initialState = [{ const RequestHeaders = ({item, collection}) => {
uid: nanoid(), const dispatch = useDispatch();
enabled: true const headers = item.draft ? get(item, 'draft.request.headers') : get(item, 'request.headers');
}];
const RequestHeaders = () => {
const [headers, setHeaders] = useState(initialState);
const addHeader = () => { const addHeader = () => {
let newHeader = { dispatch(requestChanged({
uid: nanoid(), itemUid: item.uid,
key: '', collectionUid: collection.uid
value: '', }));
description: '', dispatch(addRequestHeader({
enabled: true itemUid: item.uid,
}; collectionUid: collection.uid,
}));
let newHeaders = [...headers, newHeader];
setHeaders(newHeaders);
}; };
const handleHeaderValueChange = (e, index, menu) => { const handleHeaderValueChange = (e, _header, type) => {
// todo: yet to implement const header = cloneDeep(_header);
switch(type) {
case 'name' : {
header.name = e.target.value;
break;
}
case 'value' : {
header.value = e.target.value;
break;
}
case 'description' : {
header.description = e.target.value;
break;
}
}
dispatch(requestChanged({
itemUid: item.uid,
collectionUid: collection.uid
}));
dispatch(updateRequestHeader({
header: header,
itemUid: item.uid,
collectionUid: collection.uid,
}));
}; };
const handleRemoveHeader = (index) => { const handleRemoveHeader = (index) => {
headers.splice(index, 1);
setHeaders([...headers]);
}; };
return ( return (
@ -51,28 +70,28 @@ const RequestHeaders = () => {
<td> <td>
<input <input
type="text" type="text"
name="key"
autoComplete="off" autoComplete="off"
defaultValue={headers[index].key} value={header.name}
onChange={(e) => handleHeaderValueChange(e, index, 'key')} className="mousetrap"
onChange={(e) => handleHeaderValueChange(e, header, 'name')}
/> />
</td> </td>
<td> <td>
<input <input
type="text" type="text"
name="value"
autoComplete="off" autoComplete="off"
defaultValue={headers[index].value} value={header.value}
onChange={(e) => handleHeaderValueChange(e, index, 'value')} className="mousetrap"
onChange={(e) => handleHeaderValueChange(e, header, 'value')}
/> />
</td> </td>
<td> <td>
<input <input
type="text" type="text"
name="description"
autoComplete="off" autoComplete="off"
defaultValue={headers[index].description} value={header.description}
onChange={(e) => handleHeaderValueChange(e, index, 'description')} className="mousetrap"
onChange={(e) => handleHeaderValueChange(e, header, 'description')}
/> />
</td> </td>
<td> <td>
@ -80,9 +99,9 @@ const RequestHeaders = () => {
<input <input
type="checkbox" type="checkbox"
className="mr-3" className="mr-3"
defaultChecked={header.enabled} checked={header.enabled}
name="enabled" className="mousetrap"
onChange={(e) => handleHeaderValueChange(e, index, 'enabled')} onChange={(e) => handleHeaderValueChange(e, header, 'enabled')}
/> />
<button onClick={() => handleRemoveHeader(index)}> <button onClick={() => handleRemoveHeader(index)}>
<IconTrash strokeWidth={1.5} size={20}/> <IconTrash strokeWidth={1.5} size={20}/>

View File

@ -145,6 +145,8 @@ const RequestTabPanel = () => {
{item.type === 'http-request' ? ( {item.type === 'http-request' ? (
<HttpRequestPane <HttpRequestPane
item={item}
collection={collection}
leftPaneWidth={leftPaneWidth} leftPaneWidth={leftPaneWidth}
/> />
) : null} ) : null}

View File

@ -1,4 +1,5 @@
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
import find from 'lodash/find';
import each from 'lodash/each'; import each from 'lodash/each';
import cloneDeep from 'lodash/cloneDeep'; import cloneDeep from 'lodash/cloneDeep';
import { createSlice } from '@reduxjs/toolkit' import { createSlice } from '@reduxjs/toolkit'
@ -129,6 +130,47 @@ export const collectionsSlice = createSlice({
item.draft.request.url = action.payload.url; item.draft.request.url = action.payload.url;
} }
} }
},
addRequestHeader: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if(collection) {
const item = findItemInCollection(collection, action.payload.itemUid);
if(item) {
if(!item.draft) {
item.draft = cloneItem(item);
}
item.draft.request.headers = item.draft.request.headers || [];
item.draft.request.headers.push({
uid: nanoid(),
name: '',
value: '',
description: '',
enabled: true
});
}
}
},
updateRequestHeader: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if(collection) {
const item = findItemInCollection(collection, action.payload.itemUid);
if(item) {
if(!item.draft) {
item.draft = cloneItem(item);
}
const header = find(item.draft.request.headers, (h) => h.uid === action.payload.header.uid);
if(header) {
header.name = action.payload.header.name;
header.value = action.payload.header.value;
header.description = action.payload.header.description;
header.enabled = action.payload.header.enabled;
}
}
}
} }
} }
}); });
@ -145,6 +187,8 @@ export const {
collectionClicked, collectionClicked,
collectionFolderClicked, collectionFolderClicked,
requestUrlChanged, requestUrlChanged,
addRequestHeader,
updateRequestHeader
} = collectionsSlice.actions; } = collectionsSlice.actions;
export const loadCollectionsFromIdb = () => (dispatch) => { export const loadCollectionsFromIdb = () => (dispatch) => {

View File

@ -1,5 +1,6 @@
import each from 'lodash/each'; import each from 'lodash/each';
import find from 'lodash/find'; import find from 'lodash/find';
import map from 'lodash/map';
import filter from 'lodash/filter'; import filter from 'lodash/filter';
import cloneDeep from 'lodash/cloneDeep'; import cloneDeep from 'lodash/cloneDeep';
@ -54,6 +55,18 @@ export const cloneItem = (item) => {
}; };
export const transformCollectionToSaveToIdb = (collection, options = {}) => { export const transformCollectionToSaveToIdb = (collection, options = {}) => {
const copyHeaders = (headers) => {
return map(headers, (header) => {
return {
uid: header.uid,
name: header.name,
value: header.value,
description: header.description,
enabled: header.enabled
}
});
};
const copyItems = (sourceItems, destItems) => { const copyItems = (sourceItems, destItems) => {
each(sourceItems, (si) => { each(sourceItems, (si) => {
const di = { const di = {
@ -69,7 +82,7 @@ export const transformCollectionToSaveToIdb = (collection, options = {}) => {
di.request = { di.request = {
url: si.draft.request.url, url: si.draft.request.url,
method: si.draft.request.method, method: si.draft.request.method,
headers: si.draft.request.headers, headers: copyHeaders(si.draft.request.headers),
body: si.draft.request.body body: si.draft.request.body
}; };
} }
@ -78,7 +91,7 @@ export const transformCollectionToSaveToIdb = (collection, options = {}) => {
di.request = { di.request = {
url: si.request.url, url: si.request.url,
method: si.request.method, method: si.request.method,
headers: si.request.headers, headers: copyHeaders(si.request.headers),
body: si.request.body body: si.request.body
} }
}; };

View File

@ -1,3 +1,4 @@
import each from 'lodash/each';
import { rawRequest, gql } from 'graphql-request'; import { rawRequest, gql } from 'graphql-request';
const sendNetworkRequest = async (item) => { const sendNetworkRequest = async (item) => {
@ -26,10 +27,17 @@ const sendHttpRequest = async (request) => {
const { ipcRenderer } = window.require("electron"); const { ipcRenderer } = window.require("electron");
console.log(request); console.log(request);
const headers = {};
each(request.headers, (h) => {
if(h.enabled) {
headers[h.name] = h.value;
}
});
let options = { let options = {
method: request.method, method: request.method,
url: request.url, url: request.url,
headers: headers
}; };
ipcRenderer ipcRenderer