From 15dc0ad9acf91b087a5caac757a497b8a109e2bd Mon Sep 17 00:00:00 2001 From: Jacob Anavisca Date: Sun, 23 Feb 2020 11:38:15 -0500 Subject: [PATCH 1/5] Env management --- components/environments/addEnvironment.vue | 77 +++++++ components/environments/editEnvironment.vue | 206 ++++++++++++++++++ components/environments/environment.vue | 65 ++++++ .../environments/importExportEnvironment.vue | 170 +++++++++++++++ components/environments/index.vue | 133 +++++++++++ lang/en-US.js | 8 + pages/index.vue | 16 +- store/postwoman.js | 68 ++++++ 8 files changed, 742 insertions(+), 1 deletion(-) create mode 100644 components/environments/addEnvironment.vue create mode 100644 components/environments/editEnvironment.vue create mode 100644 components/environments/environment.vue create mode 100644 components/environments/importExportEnvironment.vue create mode 100644 components/environments/index.vue diff --git a/components/environments/addEnvironment.vue b/components/environments/addEnvironment.vue new file mode 100644 index 00000000000..b68768213e9 --- /dev/null +++ b/components/environments/addEnvironment.vue @@ -0,0 +1,77 @@ + + + diff --git a/components/environments/editEnvironment.vue b/components/environments/editEnvironment.vue new file mode 100644 index 00000000000..adb3dc92016 --- /dev/null +++ b/components/environments/editEnvironment.vue @@ -0,0 +1,206 @@ + + + diff --git a/components/environments/environment.vue b/components/environments/environment.vue new file mode 100644 index 00000000000..8fda531985b --- /dev/null +++ b/components/environments/environment.vue @@ -0,0 +1,65 @@ + + + + + diff --git a/components/environments/importExportEnvironment.vue b/components/environments/importExportEnvironment.vue new file mode 100644 index 00000000000..3072f6ad499 --- /dev/null +++ b/components/environments/importExportEnvironment.vue @@ -0,0 +1,170 @@ + + + diff --git a/components/environments/index.vue b/components/environments/index.vue new file mode 100644 index 00000000000..c66e2fd0fcc --- /dev/null +++ b/components/environments/index.vue @@ -0,0 +1,133 @@ + + + + + diff --git a/lang/en-US.js b/lang/en-US.js index 9191cec5fe0..eba422b4bb5 100644 --- a/lang/en-US.js +++ b/lang/en-US.js @@ -45,6 +45,14 @@ export default { preview_html: "Preview HTML", history: "History", collections: "Collections", + environment: "Environment", + new_environment: "New Environment", + my_new_environment: "My New Environment", + edit_environment: "Edit Environment", + env_variable_list: "Variable List", + invalid_environment_name: "Please provide a valid name for the environment", + use_environment: "Use environment", + add_one_variable: "(add at least one variable)", import_curl: "Import cURL", import: "Import", generate_code: "Generate code", diff --git a/pages/index.vue b/pages/index.vue index e74fd46ea72..7096306ad81 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -1075,6 +1075,11 @@
+ + +
+ +
@@ -1451,7 +1456,8 @@ export default { saveRequestAs: () => import("../components/collections/saveRequestAs"), Editor: AceEditor, inputform: () => import("../components/firebase/inputform"), - ballsfeed: () => import("../components/firebase/feeds") + ballsfeed: () => import("../components/firebase/feeds"), + environments: () => import("../components/environments") }, data() { return { @@ -2039,6 +2045,14 @@ export default { } }, methods: { + useSelectedEnvironment(environment) { + let preRequestScriptString = '' + for (let variable of environment.variables) { + preRequestScriptString = preRequestScriptString + `pw.env.set('${variable.key}', '${variable.value}');\n` + } + this.preRequestScript = preRequestScriptString + this.showPreRequestScript = true + }, checkCollections() { const checkCollectionAvailability = this.$store.state.postwoman.collections && diff --git a/store/postwoman.js b/store/postwoman.js index ab79729efc9..945932e3309 100644 --- a/store/postwoman.js +++ b/store/postwoman.js @@ -74,6 +74,13 @@ export const state = () => ({ requests: [] } ], + environments: [ + { + name: "My Env Variables", + variables: [] + } + ], + editingEnvironment: {}, selectedRequest: {}, editingRequest: {} }); @@ -102,6 +109,61 @@ export const mutations = { settings[key] = value; }, + removeVariables({ editingEnvironment }, value) { + editingEnvironment.variables = value + }, + + setEditingEnvironment(state, value ) { + state.editingEnvironment = {...value} + }, + + setVariableKey({ editingEnvironment }, { index, value }) { + editingEnvironment.variables[index].key = value; + }, + + setVariableValue({ editingEnvironment }, { index, value }) { + editingEnvironment.variables[index].value = value; + }, + + removeVariable({ editingEnvironment }, variables) { + editingEnvironment.variables = variables; + }, + + addVariable({ editingEnvironment }, value) { + editingEnvironment.variables.push(value); + }, + + replaceEnvironments(state, environments) { + state.environments = environments; + }, + + importAddEnvironments(state, environments) { + state.environments = [...state.environments, ...environments]; + + let index = 0; + for (let environment of state.environments) { + environment.environmentIndex = index; + index += 1; + } + }, + + removeEnvironment({ environments }, environmentIndex) { + environments.splice(environmentIndex, 1); + }, + + saveEnvironment({ environments }, payload) { + const { environment, environmentIndex } = payload; + const { name } = environment; + const duplicateEnvironment = environments.some(item => { + return item.environmentIndex !== environmentIndex && item.name.toLowerCase() === name.toLowerCase() + }); + if (duplicateEnvironment) { + this.$toast.info("Duplicate environment"); + return; + } + environments[environmentIndex] = environment; + }, + replaceCollections(state, collections) { state.collections = collections; }, @@ -323,3 +385,9 @@ export const mutations = { state.selectedRequest = Object.assign({}, request); } }; + +// export const getters = { +// getEditingEnvironment: state => { +// return state.editingEnvironment +// } +// } From 010be95ed5ffb4bbbbaa11eba614f45b6cdc3419 Mon Sep 17 00:00:00 2001 From: Liyas Thomas Date: Sun, 23 Feb 2020 22:43:12 +0530 Subject: [PATCH 2/5] Lint + few best practices --- components/environments/addEnvironment.vue | 11 ++-- components/environments/editEnvironment.vue | 50 +++++++++++-------- .../environments/importExportEnvironment.vue | 6 +-- components/environments/index.vue | 23 ++++++--- store/postwoman.js | 19 ++++--- 5 files changed, 63 insertions(+), 46 deletions(-) diff --git a/components/environments/addEnvironment.vue b/components/environments/addEnvironment.vue index b68768213e9..23c53d1ac36 100644 --- a/components/environments/addEnvironment.vue +++ b/components/environments/addEnvironment.vue @@ -43,7 +43,6 @@ diff --git a/store/postwoman.js b/store/postwoman.js index 951cf402773..afffa7528c5 100644 --- a/store/postwoman.js +++ b/store/postwoman.js @@ -110,11 +110,11 @@ export const mutations = { }, removeVariables({ editingEnvironment }, value) { - editingEnvironment.variables = value + editingEnvironment.variables = value; }, - setEditingEnvironment(state, value ) { - state.editingEnvironment = {...value} + setEditingEnvironment(state, value) { + state.editingEnvironment = { ...value }; }, setVariableKey({ editingEnvironment }, { index, value }) { @@ -154,9 +154,11 @@ export const mutations = { saveEnvironment({ environments }, payload) { const { environment, environmentIndex } = payload; const { name } = environment; - const duplicateEnvironment = environments.some(item => { - return item.environmentIndex !== environmentIndex && item.name.toLowerCase() === name.toLowerCase() - }); + const duplicateEnvironment = environments.some( + item => + item.environmentIndex !== environmentIndex && + item.name.toLowerCase() === name.toLowerCase() + ); if (duplicateEnvironment) { this.$toast.info("Duplicate environment"); return; @@ -201,7 +203,10 @@ export const mutations = { }, editCollection({ collections }, payload) { - const { collection: { name }, collectionIndex } = payload; + const { + collection: { name }, + collectionIndex + } = payload; const duplicateCollection = collections.some( item => item.name.toLowerCase() === name.toLowerCase() ); From d24c572d7f383847dfeeb3f6dc4e2e5e5083c634 Mon Sep 17 00:00:00 2001 From: Liyas Thomas Date: Mon, 24 Feb 2020 00:30:22 +0530 Subject: [PATCH 3/5] Firebase sync --- .../collections/importExportCollections.vue | 133 +++++++++++------- components/environments/addEnvironment.vue | 12 ++ .../environments/importExportEnvironment.vue | 16 ++- components/environments/index.vue | 17 ++- functions/fb.js | 29 ++++ lang/en-US.js | 4 +- pages/index.vue | 18 +-- pages/settings.vue | 11 +- 8 files changed, 164 insertions(+), 76 deletions(-) diff --git a/components/collections/importExportCollections.vue b/components/collections/importExportCollections.vue index 6d79fb21e74..44ebe6c1361 100644 --- a/components/collections/importExportCollections.vue +++ b/components/collections/importExportCollections.vue @@ -121,12 +121,19 @@ export default { let content = event.target.result; let collections = JSON.parse(content); if (collections[0]) { - let [ name, folders, requests ] = Object.keys(collections[0]) - if (name === 'name' && folders === 'folders' && requests === 'requests') { + let [name, folders, requests] = Object.keys(collections[0]); + if ( + name === "name" && + folders === "folders" && + requests === "requests" + ) { // Do nothing } - } else if (collections.info && collections.info.schema.includes('v2.1.0')) { - collections = this.parsePostmanCollection(collections) + } else if ( + collections.info && + collections.info.schema.includes("v2.1.0") + ) { + collections = this.parsePostmanCollection(collections); } else { return this.failedImport(); } @@ -141,11 +148,18 @@ export default { let content = event.target.result; let collections = JSON.parse(content); if (collections[0]) { - let [ name, folders, requests ] = Object.keys(collections[0]) - if (name === 'name' && folders === 'folders' && requests === 'requests') { + let [name, folders, requests] = Object.keys(collections[0]); + if ( + name === "name" && + folders === "folders" && + requests === "requests" + ) { // Do nothing } - } else if (collections.info && collections.info.schema.includes('v2.1.0')) { + } else if ( + collections.info && + collections.info.schema.includes("v2.1.0") + ) { collections = this.parsePostmanCollection(collections); } else { return this.failedImport(); @@ -188,28 +202,38 @@ export default { }); }, parsePostmanCollection(collection, folders = true) { - let postwomanCollection = folders ? [{ - "name": "", - "folders": [], - "requests": [] - }] + let postwomanCollection = folders + ? [ + { + name: "", + folders: [], + requests: [] + } + ] : { - "name": "", - "requests": [] - }; - for(let collectionItem of collection.item) { + name: "", + requests: [] + }; + for (let collectionItem of collection.item) { if (collectionItem.request) { if (postwomanCollection[0]) { - postwomanCollection[0].name = collection.info ? collection.info.name : ""; - postwomanCollection[0].requests.push(this.parsePostmanRequest(collectionItem)); + postwomanCollection[0].name = collection.info + ? collection.info.name + : ""; + postwomanCollection[0].requests.push( + this.parsePostmanRequest(collectionItem) + ); } else { - postwomanCollection.name = collection.name ? collection.name - : ""; - postwomanCollection.requests.push(this.parsePostmanRequest(collectionItem)); + postwomanCollection.name = collection.name ? collection.name : ""; + postwomanCollection.requests.push( + this.parsePostmanRequest(collectionItem) + ); } } else if (collectionItem.item) { if (collectionItem.item[0]) { - postwomanCollection[0].folders.push(this.parsePostmanCollection(collectionItem, false)); + postwomanCollection[0].folders.push( + this.parsePostmanCollection(collectionItem, false) + ); } } } @@ -217,46 +241,51 @@ export default { }, parsePostmanRequest(requestObject) { let pwRequest = { - "url": "", - "path": "", - "method": "", - "auth": "", - "httpUser": "", - "httpPassword": "", - "passwordFieldType": "password", - "bearerToken": "", - "headers": [], - "params": [], - "bodyParams": [], - "rawParams": "", - "rawInput": false, - "contentType": "", - "requestType": "", - "name": "", + url: "", + path: "", + method: "", + auth: "", + httpUser: "", + httpPassword: "", + passwordFieldType: "password", + bearerToken: "", + headers: [], + params: [], + bodyParams: [], + rawParams: "", + rawInput: false, + contentType: "", + requestType: "", + name: "" }; pwRequest.name = requestObject.name; - let requestObjectUrl = requestObject.request.url.raw.match(/^(.+:\/\/[^\/]+|{[^\/]+})(\/[^\?]+|).*$/); + let requestObjectUrl = requestObject.request.url.raw.match( + /^(.+:\/\/[^\/]+|{[^\/]+})(\/[^\?]+|).*$/ + ); pwRequest.url = requestObjectUrl[1]; pwRequest.path = requestObjectUrl[2] ? requestObjectUrl[2] : ""; pwRequest.method = requestObject.request.method; - let itemAuth = requestObject.request.auth ? requestObject.request.auth - : ""; - let authType = itemAuth ? itemAuth.type + let itemAuth = requestObject.request.auth + ? requestObject.request.auth : ""; + let authType = itemAuth ? itemAuth.type : ""; if (authType === "basic") { pwRequest.auth = "Basic Auth"; - pwRequest.httpUser = itemAuth.basic[0].key === "username" - ? itemAuth.basic[0].value - : itemAuth.basic[1].value; - pwRequest.httpPassword = itemAuth.basic[0].key === "password" - ? itemAuth.basic[0].value - : itemAuth.basic[1].value; + pwRequest.httpUser = + itemAuth.basic[0].key === "username" + ? itemAuth.basic[0].value + : itemAuth.basic[1].value; + pwRequest.httpPassword = + itemAuth.basic[0].key === "password" + ? itemAuth.basic[0].value + : itemAuth.basic[1].value; } else if (authType === "oauth2") { pwRequest.auth = "OAuth 2.0"; - pwRequest.bearerToken = itemAuth.oauth2[0].key === "accessToken" - ? itemAuth.oauth2[0].value - : itemAuth.oauth2[1].value; + pwRequest.bearerToken = + itemAuth.oauth2[0].key === "accessToken" + ? itemAuth.oauth2[0].value + : itemAuth.oauth2[1].value; } else if (authType === "bearer") { pwRequest.auth = "Bearer Token"; pwRequest.bearerToken = itemAuth.bearer[0].value; @@ -280,7 +309,7 @@ export default { if (requestObject.request.body.mode === "urlencoded") { let params = requestObject.request.body.urlencoded; pwRequest.bodyParams = params ? params : []; - for(let param of pwRequest.bodyParams) { + for (let param of pwRequest.bodyParams) { delete param.type; } } else if (requestObject.request.body.mode === "raw") { diff --git a/components/environments/addEnvironment.vue b/components/environments/addEnvironment.vue index 23c53d1ac36..a87873e18ac 100644 --- a/components/environments/addEnvironment.vue +++ b/components/environments/addEnvironment.vue @@ -43,6 +43,8 @@ diff --git a/functions/fb.js b/functions/fb.js index c73a655fcd2..32076b51d34 100644 --- a/functions/fb.js +++ b/functions/fb.js @@ -26,6 +26,7 @@ export const fb = { currentSettings: [], currentHistory: [], currentCollections: [], + currentEnvironments: [], writeFeeds: async (message, label) => { const dt = { createdOn: new Date(), @@ -112,6 +113,21 @@ export const fb = { .doc("sync") .set(cl) .catch(e => console.error("error updating", cl, e)); + }, + writeEnvironments: async environment => { + const ev = { + updatedOn: new Date(), + author: fb.currentUser.uid, + author_name: fb.currentUser.displayName, + author_image: fb.currentUser.photoURL, + environment: environment + }; + usersCollection + .doc(fb.currentUser.uid) + .collection("environments") + .doc("sync") + .set(ev) + .catch(e => console.error("error updating", ev, e)); } }; @@ -186,6 +202,19 @@ firebase.auth().onAuthStateChanged(user => { }); fb.currentCollections = collections[0].collection; }); + + usersCollection + .doc(fb.currentUser.uid) + .collection("environments") + .onSnapshot(environmentsRef => { + const environments = []; + environmentsRef.forEach(doc => { + const environment = doc.data(); + environment.id = doc.id; + environments.push(environment); + }); + fb.currentEnvironments = environments[0].environment; + }); } else { fb.currentUser = null; } diff --git a/lang/en-US.js b/lang/en-US.js index 6986063e332..e541cdf08a1 100644 --- a/lang/en-US.js +++ b/lang/en-US.js @@ -256,7 +256,8 @@ export default { enter_curl: "Enter cURL", empty: "Empty", extensions: "Extensions", - extensions_use_toggle: "Use the browser extension to send requests (if present)", + extensions_use_toggle: + "Use the browser extension to send requests (if present)", extensions_info1: "Browser extension that simplifies access to Postwoman", extensions_info2: "Get Postwoman browser extension!", installed: "Installed", @@ -267,6 +268,7 @@ export default { sync: "Sync", syncHistory: "History", syncCollections: "Collections", + syncEnvironments: "Environments", turn_on: "Turn on", login_first: "Login first", paste_a_note: "Paste a note", diff --git a/pages/index.vue b/pages/index.vue index 95bc8fe09cc..85fd5938f66 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -1086,7 +1086,7 @@
- +
    @@ -1456,7 +1456,7 @@ export default { saveRequestAs: () => import("../components/collections/saveRequestAs"), Editor: AceEditor, inputform: () => import("../components/firebase/inputform"), - ballsfeed: () => import("../components/firebase/feeds"), + notes: () => import("../components/firebase/feeds"), environments: () => import("../components/environments") }, data() { @@ -2047,12 +2047,14 @@ export default { }, methods: { useSelectedEnvironment(environment) { - let preRequestScriptString = '' + let preRequestScriptString = ""; for (let variable of environment.variables) { - preRequestScriptString = preRequestScriptString + `pw.env.set('${variable.key}', '${variable.value}');\n` + preRequestScriptString = + preRequestScriptString + + `pw.env.set('${variable.key}', '${variable.value}');\n`; } - this.preRequestScript = preRequestScriptString - this.showPreRequestScript = true + this.preRequestScript = preRequestScriptString; + this.showPreRequestScript = true; }, checkCollections() { const checkCollectionAvailability = @@ -2247,7 +2249,7 @@ export default { }; this.$refs.historyComponent.addEntry(entry); if (fb.currentUser !== null) { - if (fb.currentSettings[1].value) { + if (fb.currentSettings[2].value) { fb.writeHistory(entry); } } @@ -2284,7 +2286,7 @@ export default { }; this.$refs.historyComponent.addEntry(entry); if (fb.currentUser !== null) { - if (fb.currentSettings[1].value) { + if (fb.currentSettings[2].value) { fb.writeHistory(entry); } } diff --git a/pages/settings.vue b/pages/settings.vue index 345ceea7305..bd8ba090a51 100644 --- a/pages/settings.vue +++ b/pages/settings.vue @@ -38,7 +38,7 @@ {{ setting.value ? $t("enabled") : $t("disabled") }}

    -

    +