From d24c572d7f383847dfeeb3f6dc4e2e5e5083c634 Mon Sep 17 00:00:00 2001 From: Liyas Thomas Date: Mon, 24 Feb 2020 00:30:22 +0530 Subject: [PATCH] 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 6d79fb21e..44ebe6c13 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 23c53d1ac..a87873e18 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 c73a655fc..32076b51d 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 6986063e3..e541cdf08 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 95bc8fe09..85fd5938f 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 345ceea73..bd8ba090a 100644 --- a/pages/settings.vue +++ b/pages/settings.vue @@ -38,7 +38,7 @@ {{ setting.value ? $t("enabled") : $t("disabled") }}

    -

    +