From 241ee5e788e89905739d4eb9fa6c63df20952cd9 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Mon, 17 Oct 2022 00:39:58 +0530 Subject: [PATCH] feat: sample collection + bug fixes --- .../RequestPane/RequestBody/StyledWrapper.js | 2 +- .../RequestTabs/RequestTab/index.js | 24 +++- .../src/components/RequestTabs/index.js | 3 +- .../ResponsePane/QueryResult/StyledWrapper.js | 2 +- .../RequestMethod/StyledWrapper.js | 3 + .../CollectionItem/RequestMethod/index.js | 3 +- .../bruno-app/src/components/Welcome/index.js | 17 ++- .../bruno-app/src/providers/Hotkeys/index.js | 7 +- .../ReduxStore/slices/collections/actions.js | 1 + .../ReduxStore/slices/collections/index.js | 2 +- packages/bruno-app/src/styles/globals.css | 8 +- .../bruno-app/src/utils/collections/import.js | 13 ++ .../samples/sample-collection.json | 129 ++++++++++++++++++ .../bruno-app/src/utils/network/browser.js | 8 ++ packages/bruno-electron/src/ipc/network.js | 8 ++ 15 files changed, 217 insertions(+), 13 deletions(-) create mode 100644 packages/bruno-app/src/utils/collections/samples/sample-collection.json diff --git a/packages/bruno-app/src/components/RequestPane/RequestBody/StyledWrapper.js b/packages/bruno-app/src/components/RequestPane/RequestBody/StyledWrapper.js index 7e9c9f11..83ebd814 100644 --- a/packages/bruno-app/src/components/RequestPane/RequestBody/StyledWrapper.js +++ b/packages/bruno-app/src/components/RequestPane/RequestBody/StyledWrapper.js @@ -3,7 +3,7 @@ import styled from 'styled-components'; const Wrapper = styled.div` div.CodeMirror { /* todo: find a better way */ - height: calc(100vh - 240px); + height: calc(100vh - 220px); } `; diff --git a/packages/bruno-app/src/components/RequestTabs/RequestTab/index.js b/packages/bruno-app/src/components/RequestTabs/RequestTab/index.js index 30f9a337..3b4c54b2 100644 --- a/packages/bruno-app/src/components/RequestTabs/RequestTab/index.js +++ b/packages/bruno-app/src/components/RequestTabs/RequestTab/index.js @@ -22,11 +22,31 @@ const RequestTab = ({tab, collection}) => { method = method.toLocaleLowerCase(); switch(method) { case 'get': { - color = 'rgb(5, 150, 105)'; + color = 'var(--color-method-get)'; break; } case 'post': { - color = '#8e44ad'; + color = 'var(--color-method-post)'; + break; + } + case 'put': { + color = 'var(--color-method-put)'; + break; + } + case 'delete': { + color = 'var(--color-method-delete)'; + break; + } + case 'patch': { + color = 'var(--color-method-patch)'; + break; + } + case 'options': { + color = 'var(--color-method-options)'; + break; + } + case 'head': { + color = 'var(--color-method-head)'; break; } } diff --git a/packages/bruno-app/src/components/RequestTabs/index.js b/packages/bruno-app/src/components/RequestTabs/index.js index b18926a4..d6484501 100644 --- a/packages/bruno-app/src/components/RequestTabs/index.js +++ b/packages/bruno-app/src/components/RequestTabs/index.js @@ -78,9 +78,10 @@ const RequestTabs = () => { }); }; + // Todo: Must support ephermal requests return ( - {newRequestModalOpen && setNewRequestModalOpen(false)}/>} + {newRequestModalOpen && setNewRequestModalOpen(false)}/>} {collectionRequestTabs && collectionRequestTabs.length ? ( <> diff --git a/packages/bruno-app/src/components/ResponsePane/QueryResult/StyledWrapper.js b/packages/bruno-app/src/components/ResponsePane/QueryResult/StyledWrapper.js index b7645389..b3d887ec 100644 --- a/packages/bruno-app/src/components/ResponsePane/QueryResult/StyledWrapper.js +++ b/packages/bruno-app/src/components/ResponsePane/QueryResult/StyledWrapper.js @@ -3,7 +3,7 @@ import styled from 'styled-components'; const StyledWrapper = styled.div` div.CodeMirror { /* todo: find a better way */ - height: calc(100vh - 240px); + height: calc(100vh - 220px); } `; diff --git a/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/RequestMethod/StyledWrapper.js b/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/RequestMethod/StyledWrapper.js index c779b67a..c006a56a 100644 --- a/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/RequestMethod/StyledWrapper.js +++ b/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/RequestMethod/StyledWrapper.js @@ -5,6 +5,7 @@ const Wrapper = styled.div` display: flex; align-self: stretch; align-items: center; + min-width: 34px; span { position: relative; @@ -16,6 +17,8 @@ const Wrapper = styled.div` .method-put { color: var(--color-method-put);} .method-delete { color: var(--color-method-delete);} .method-patch { color: var(--color-method-patch);} + .method-options { color: var(--color-method-options);} + .method-head { color: var(--color-method-head);} `; export default Wrapper; diff --git a/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/RequestMethod/index.js b/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/RequestMethod/index.js index c33cf91d..1b2ff6f4 100644 --- a/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/RequestMethod/index.js +++ b/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/RequestMethod/index.js @@ -14,7 +14,8 @@ const RequestMethod = ({item}) => { 'method-post': method === 'post', 'method-put': method === 'put', 'method-delete': method === 'delete', - 'method-patch': method === 'patch' + 'method-patch': method === 'patch', + 'method-head': method === 'head' }); }; diff --git a/packages/bruno-app/src/components/Welcome/index.js b/packages/bruno-app/src/components/Welcome/index.js index 592a12b9..37aa5692 100644 --- a/packages/bruno-app/src/components/Welcome/index.js +++ b/packages/bruno-app/src/components/Welcome/index.js @@ -17,7 +17,7 @@ import { addCollectionToWorkspace } from 'providers/ReduxStore/slices/workspaces import Bruno from 'components/Bruno'; import CreateCollection from 'components/Sidebar/CreateCollection'; import SelectCollection from 'components/Sidebar/Collections/SelectCollection'; -import importCollection from 'utils/collections/import'; +import importCollection, { importSampleCollection } from 'utils/collections/import'; import { isElectron } from 'utils/common/platform'; import StyledWrapper from './StyledWrapper'; @@ -55,6 +55,19 @@ const Welcome = () => { .catch((err) => console.log(err)); }; + const handleImportSampleCollection = () => { + importSampleCollection() + .then((collection) => { + dispatch(collectionImported({collection: collection})); + dispatch(addCollectionToWorkspace(activeWorkspaceUid, collection.uid)); + }) + .then(() => toast.success("Sample Collection loaded successfully")) + .catch((err) => { + toast.error("Load sample collection failed"); + console.log(err); + }); + }; + return ( {createCollectionModalOpen ? ( @@ -89,7 +102,7 @@ const Welcome = () => {
Import Collection
-
+
Load Sample Collection
diff --git a/packages/bruno-app/src/providers/Hotkeys/index.js b/packages/bruno-app/src/providers/Hotkeys/index.js index ca80dbe4..faf071dd 100644 --- a/packages/bruno-app/src/providers/Hotkeys/index.js +++ b/packages/bruno-app/src/providers/Hotkeys/index.js @@ -1,9 +1,11 @@ import React, { useState, useEffect } from 'react'; +import toast from 'react-hot-toast'; import find from 'lodash/find'; import Mousetrap from 'mousetrap'; import { useSelector, useDispatch } from 'react-redux'; import SaveRequest from 'components/RequestPane/SaveRequest'; import EnvironmentSettings from "components/Environments/EnvironmentSettings"; +import NetworkError from "components/ResponsePane/NetworkError"; import NewRequest from "components/Sidebar/NewRequest"; import BrunoSupport from 'components/BrunoSupport'; import { sendRequest, saveRequest } from 'providers/ReduxStore/slices/collections/actions'; @@ -73,7 +75,10 @@ export const HotkeysProvider = props => { if(collection) { const item = findItemInCollection(collection, activeTab.uid); if(item) { - dispatch(sendRequest(item, collection.uid)); + dispatch(sendRequest(item, collection.uid)) + .catch((err) => toast.custom((t) => ( toast.dismiss(t.id)}/>), { + duration: 5000 + })); } } } diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js index ce64adc2..7fb08292 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js @@ -76,6 +76,7 @@ export const openLocalCollectionEvent = (uid, pathname) => (dispatch, getState) export const createCollection = (collectionName) => (dispatch, getState) => { const newCollection = { + version: "1", uid: uuid(), name: collectionName, items: [], diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js index e7d18443..8bb27458 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js @@ -47,7 +47,7 @@ export const collectionsSlice = createSlice({ const { collection } = action.payload; collapseCollection(collection); addDepth(collection.items); - if(!collectionUids.includes(c.uid)) { + if(!collectionUids.includes(collection.uid)) { state.collections.push(collection); } }, diff --git a/packages/bruno-app/src/styles/globals.css b/packages/bruno-app/src/styles/globals.css index 4fc0bed1..5ee75407 100644 --- a/packages/bruno-app/src/styles/globals.css +++ b/packages/bruno-app/src/styles/globals.css @@ -17,9 +17,11 @@ --color-background-danger: #dc3545; --color-method-get: rgb(5, 150, 105); --color-method-post: #8e44ad; - --color-method-put: #8e44ad; - --color-method-delete: #8e44ad; - --color-method-patch: #8e44ad; + --color-method-put: #ca7811; + --color-method-delete: rgb(185, 28, 28); + --color-method-patch: rgb(52 52 52); + --color-method-options: rgb(52 52 52); + --color-method-head: rgb(52 52 52); --color-table-stripe: #f3f3f3; } diff --git a/packages/bruno-app/src/utils/collections/import.js b/packages/bruno-app/src/utils/collections/import.js index 55bd8615..0764d367 100644 --- a/packages/bruno-app/src/utils/collections/import.js +++ b/packages/bruno-app/src/utils/collections/import.js @@ -5,6 +5,7 @@ import toast from 'react-hot-toast'; import { uuid } from 'utils/common'; import { collectionSchema } from '@usebruno/schema'; import { saveCollectionToIdb } from 'utils/idb'; +import sampleCollection from './samples/sample-collection.json'; const readFile = (files) => { return new Promise((resolve, reject) => { @@ -83,4 +84,16 @@ const importCollection = () => { }); }; +export const importSampleCollection = () => { + return new Promise((resolve, reject) => { + validateSchema(sampleCollection) + .then(validateSchema) + .then(updateUidsInCollection) + .then(validateSchema) + .then((collection) => saveCollectionToIdb(window.__idb, collection)) + .then(resolve) + .catch(reject); + }); +}; + export default importCollection; \ No newline at end of file diff --git a/packages/bruno-app/src/utils/collections/samples/sample-collection.json b/packages/bruno-app/src/utils/collections/samples/sample-collection.json new file mode 100644 index 00000000..e7fc42b2 --- /dev/null +++ b/packages/bruno-app/src/utils/collections/samples/sample-collection.json @@ -0,0 +1,129 @@ +{ + "name": "sample-collection", + "uid": "c1PdISj460OeNmFLI8rCY", + "version": "1", + "items": [ + { + "uid": "myeFbgzNlwIhDpYcOVBWS", + "type": "http-request", + "name": "Users", + "request": { + "url": "https://reqres.in/api/users?page=2", + "method": "GET", + "headers": [], + "params": [ + { + "uid": "viHGWSpAQhpiwH9UQxb7W", + "name": "page", + "value": "2", + "enabled": true + } + ], + "body": { + "mode": "json", + "json": "", + "text": null, + "xml": null, + "formUrlEncoded": [], + "multipartForm": [] + } + } + }, + { + "uid": "ytzXIADbhEwLB0BqIQOKD", + "type": "http-request", + "name": "Single User", + "request": { + "url": "https://reqres.in/api/users/2", + "method": "GET", + "headers": [], + "params": [], + "body": { + "mode": "json", + "json": "", + "text": null, + "xml": null, + "formUrlEncoded": [], + "multipartForm": [] + } + } + }, + { + "uid": "DuEyHudhVuxrdplKLAMsU", + "type": "http-request", + "name": "User Not Found", + "request": { + "url": "https://reqres.in/api/users/23", + "method": "GET", + "headers": [], + "params": [], + "body": { + "mode": "json", + "json": "", + "text": null, + "xml": null, + "formUrlEncoded": [], + "multipartForm": [] + } + } + }, + { + "uid": "UZFjsr14q8iZ1Do90q02Q", + "type": "http-request", + "name": "Create", + "request": { + "url": "https://reqres.in/api/users", + "method": "POST", + "headers": [], + "params": [], + "body": { + "mode": "json", + "json": "{\n \"name\": \"morpheus\",\n \"job\": \"leader\"\n}", + "text": null, + "xml": null, + "formUrlEncoded": [], + "multipartForm": [] + } + } + }, + { + "uid": "lYLCTXaerD9etRDLJHbVN", + "type": "http-request", + "name": "Update", + "request": { + "url": "https://reqres.in/api/users/2", + "method": "PUT", + "headers": [], + "params": [], + "body": { + "mode": "json", + "json": "{\n \"name\": \"morpheus\",\n \"job\": \"zion resident\"\n}", + "text": null, + "xml": null, + "formUrlEncoded": [], + "multipartForm": [] + } + } + }, + { + "uid": "qVCKx5xUqxpBf8jeka8Gu", + "type": "http-request", + "name": "Remove", + "request": { + "url": "https://reqres.in/api/users/2", + "method": "DELETE", + "headers": [], + "params": [], + "body": { + "mode": "json", + "json": "{\n \"name\": \"morpheus\",\n \"job\": \"zion resident\"\n}", + "text": null, + "xml": null, + "formUrlEncoded": [], + "multipartForm": [] + } + } + } + ], + "environments": [] +} \ No newline at end of file diff --git a/packages/bruno-app/src/utils/network/browser.js b/packages/bruno-app/src/utils/network/browser.js index 48d38221..f0f0d55d 100644 --- a/packages/bruno-app/src/utils/network/browser.js +++ b/packages/bruno-app/src/utils/network/browser.js @@ -25,6 +25,14 @@ export const sendHttpRequestInBrowser = async (request, options) => { deleteCancelToken(options.cancelTokenUid); } + if(error.response) { + return { + status: error.response.status, + headers: error.response.headers, + data: error.response.data + } + }; + return Promise.reject(error); } }; \ No newline at end of file diff --git a/packages/bruno-electron/src/ipc/network.js b/packages/bruno-electron/src/ipc/network.js index 93876ab4..f290c024 100644 --- a/packages/bruno-electron/src/ipc/network.js +++ b/packages/bruno-electron/src/ipc/network.js @@ -41,6 +41,14 @@ const registerNetworkIpc = () => { deleteCancelToken(options.cancelTokenUid); } + if(error.response) { + return { + status: error.response.status, + headers: error.response.headers, + data: error.response.data + } + }; + return Promise.reject(error); } });