Firebase sync

This commit is contained in:
Liyas Thomas
2020-02-24 00:30:22 +05:30
parent 010be95ed5
commit d24c572d7f
8 changed files with 164 additions and 76 deletions

View File

@@ -121,12 +121,19 @@ export default {
let content = event.target.result; let content = event.target.result;
let collections = JSON.parse(content); let collections = JSON.parse(content);
if (collections[0]) { if (collections[0]) {
let [ name, folders, requests ] = Object.keys(collections[0]) let [name, folders, requests] = Object.keys(collections[0]);
if (name === 'name' && folders === 'folders' && requests === 'requests') { if (
name === "name" &&
folders === "folders" &&
requests === "requests"
) {
// Do nothing // Do nothing
} }
} else if (collections.info && collections.info.schema.includes('v2.1.0')) { } else if (
collections = this.parsePostmanCollection(collections) collections.info &&
collections.info.schema.includes("v2.1.0")
) {
collections = this.parsePostmanCollection(collections);
} else { } else {
return this.failedImport(); return this.failedImport();
} }
@@ -141,11 +148,18 @@ export default {
let content = event.target.result; let content = event.target.result;
let collections = JSON.parse(content); let collections = JSON.parse(content);
if (collections[0]) { if (collections[0]) {
let [ name, folders, requests ] = Object.keys(collections[0]) let [name, folders, requests] = Object.keys(collections[0]);
if (name === 'name' && folders === 'folders' && requests === 'requests') { if (
name === "name" &&
folders === "folders" &&
requests === "requests"
) {
// Do nothing // 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); collections = this.parsePostmanCollection(collections);
} else { } else {
return this.failedImport(); return this.failedImport();
@@ -188,28 +202,38 @@ export default {
}); });
}, },
parsePostmanCollection(collection, folders = true) { parsePostmanCollection(collection, folders = true) {
let postwomanCollection = folders ? [{ let postwomanCollection = folders
"name": "", ? [
"folders": [], {
"requests": [] name: "",
}] folders: [],
requests: []
}
]
: { : {
"name": "", name: "",
"requests": [] requests: []
}; };
for(let collectionItem of collection.item) { for (let collectionItem of collection.item) {
if (collectionItem.request) { if (collectionItem.request) {
if (postwomanCollection[0]) { if (postwomanCollection[0]) {
postwomanCollection[0].name = collection.info ? collection.info.name : ""; postwomanCollection[0].name = collection.info
postwomanCollection[0].requests.push(this.parsePostmanRequest(collectionItem)); ? collection.info.name
: "";
postwomanCollection[0].requests.push(
this.parsePostmanRequest(collectionItem)
);
} else { } else {
postwomanCollection.name = collection.name ? collection.name postwomanCollection.name = collection.name ? collection.name : "";
: ""; postwomanCollection.requests.push(
postwomanCollection.requests.push(this.parsePostmanRequest(collectionItem)); this.parsePostmanRequest(collectionItem)
);
} }
} else if (collectionItem.item) { } else if (collectionItem.item) {
if (collectionItem.item[0]) { 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) { parsePostmanRequest(requestObject) {
let pwRequest = { let pwRequest = {
"url": "", url: "",
"path": "", path: "",
"method": "", method: "",
"auth": "", auth: "",
"httpUser": "", httpUser: "",
"httpPassword": "", httpPassword: "",
"passwordFieldType": "password", passwordFieldType: "password",
"bearerToken": "", bearerToken: "",
"headers": [], headers: [],
"params": [], params: [],
"bodyParams": [], bodyParams: [],
"rawParams": "", rawParams: "",
"rawInput": false, rawInput: false,
"contentType": "", contentType: "",
"requestType": "", requestType: "",
"name": "", name: ""
}; };
pwRequest.name = requestObject.name; pwRequest.name = requestObject.name;
let requestObjectUrl = requestObject.request.url.raw.match(/^(.+:\/\/[^\/]+|{[^\/]+})(\/[^\?]+|).*$/); let requestObjectUrl = requestObject.request.url.raw.match(
/^(.+:\/\/[^\/]+|{[^\/]+})(\/[^\?]+|).*$/
);
pwRequest.url = requestObjectUrl[1]; pwRequest.url = requestObjectUrl[1];
pwRequest.path = requestObjectUrl[2] ? requestObjectUrl[2] : ""; pwRequest.path = requestObjectUrl[2] ? requestObjectUrl[2] : "";
pwRequest.method = requestObject.request.method; pwRequest.method = requestObject.request.method;
let itemAuth = requestObject.request.auth ? requestObject.request.auth let itemAuth = requestObject.request.auth
: ""; ? requestObject.request.auth
let authType = itemAuth ? itemAuth.type
: ""; : "";
let authType = itemAuth ? itemAuth.type : "";
if (authType === "basic") { if (authType === "basic") {
pwRequest.auth = "Basic Auth"; pwRequest.auth = "Basic Auth";
pwRequest.httpUser = itemAuth.basic[0].key === "username" pwRequest.httpUser =
? itemAuth.basic[0].value itemAuth.basic[0].key === "username"
: itemAuth.basic[1].value; ? itemAuth.basic[0].value
pwRequest.httpPassword = itemAuth.basic[0].key === "password" : itemAuth.basic[1].value;
? itemAuth.basic[0].value pwRequest.httpPassword =
: itemAuth.basic[1].value; itemAuth.basic[0].key === "password"
? itemAuth.basic[0].value
: itemAuth.basic[1].value;
} else if (authType === "oauth2") { } else if (authType === "oauth2") {
pwRequest.auth = "OAuth 2.0"; pwRequest.auth = "OAuth 2.0";
pwRequest.bearerToken = itemAuth.oauth2[0].key === "accessToken" pwRequest.bearerToken =
? itemAuth.oauth2[0].value itemAuth.oauth2[0].key === "accessToken"
: itemAuth.oauth2[1].value; ? itemAuth.oauth2[0].value
: itemAuth.oauth2[1].value;
} else if (authType === "bearer") { } else if (authType === "bearer") {
pwRequest.auth = "Bearer Token"; pwRequest.auth = "Bearer Token";
pwRequest.bearerToken = itemAuth.bearer[0].value; pwRequest.bearerToken = itemAuth.bearer[0].value;
@@ -280,7 +309,7 @@ export default {
if (requestObject.request.body.mode === "urlencoded") { if (requestObject.request.body.mode === "urlencoded") {
let params = requestObject.request.body.urlencoded; let params = requestObject.request.body.urlencoded;
pwRequest.bodyParams = params ? params : []; pwRequest.bodyParams = params ? params : [];
for(let param of pwRequest.bodyParams) { for (let param of pwRequest.bodyParams) {
delete param.type; delete param.type;
} }
} else if (requestObject.request.body.mode === "raw") { } else if (requestObject.request.body.mode === "raw") {

View File

@@ -43,6 +43,8 @@
</template> </template>
<script> <script>
import { fb } from "../../functions/fb";
export default { export default {
props: { props: {
show: Boolean show: Boolean
@@ -56,6 +58,15 @@ export default {
}; };
}, },
methods: { methods: {
syncEnvironments() {
if (fb.currentUser !== null) {
if (fb.currentSettings[1].value) {
fb.writeEnvironments(
JSON.parse(JSON.stringify(this.$store.state.postwoman.environments))
);
}
}
},
addNewEnvironment() { addNewEnvironment() {
if (!this.$data.name) { if (!this.$data.name) {
this.$toast.info($t("invalid_environment_name")); this.$toast.info($t("invalid_environment_name"));
@@ -69,6 +80,7 @@ export default {
]; ];
this.$store.commit("postwoman/importAddEnvironments", newEnvironment); this.$store.commit("postwoman/importAddEnvironments", newEnvironment);
this.$emit("hide-modal"); this.$emit("hide-modal");
this.syncEnvironments();
}, },
hideModal() { hideModal() {
this.$emit("hide-modal"); this.$emit("hide-modal");

View File

@@ -12,10 +12,6 @@
</div> </div>
</div> </div>
<div class="flex-wrap"> <div class="flex-wrap">
<!-- TODO db syncing
button needs to be switchded to
:disabled=="!fb.currentUser"
-->
<span <span
v-tooltip="{ v-tooltip="{
content: !fb.currentUser content: !fb.currentUser
@@ -23,7 +19,11 @@
: $t('replace_current') : $t('replace_current')
}" }"
> >
<button :disabled="true" class="icon" @click="syncEnvironments"> <button
:disabled="!fb.currentUser"
class="icon"
@click="syncEnvironments"
>
<i class="material-icons">folder_shared</i> <i class="material-icons">folder_shared</i>
<span>{{ $t("import_from_sync") }}</span> <span>{{ $t("import_from_sync") }}</span>
</button> </button>
@@ -154,7 +154,11 @@ export default {
}); });
}, },
syncEnvironments() { syncEnvironments() {
// TODO this.$store.commit(
"postwoman/replaceEnvironments",
fb.currentEnvironments
);
this.fileImported();
}, },
fileImported() { fileImported() {
this.$toast.info(this.$t("file_imported"), { this.$toast.info(this.$t("file_imported"), {

View File

@@ -71,7 +71,7 @@ ul {
<script> <script>
import environment from "./environment"; import environment from "./environment";
// import { fb } from "../functions/fb"; import { fb } from "../../functions/fb";
const updateOnLocalStorage = (propertyName, property) => const updateOnLocalStorage = (propertyName, property) =>
window.localStorage.setItem(propertyName, JSON.stringify(property)); window.localStorage.setItem(propertyName, JSON.stringify(property));
@@ -124,17 +124,24 @@ export default {
this.$data.editingEnvironment = environment; this.$data.editingEnvironment = environment;
this.$data.editingEnvironmentIndex = environmentIndex; this.$data.editingEnvironmentIndex = environmentIndex;
this.displayModalEdit(true); this.displayModalEdit(true);
this.syncEnvironments;
}, },
resetSelectedData() { resetSelectedData() {
this.$data.editingEnvironment = undefined; this.$data.editingEnvironment = undefined;
this.$data.editingEnvironmentIndex = undefined; this.$data.editingEnvironmentIndex = undefined;
}, },
syncEnvironments() { syncEnvironments() {
// TODO if (fb.currentUser !== null) {
}, if (fb.currentSettings[1].value) {
beforeDestroy() { fb.writeEnvironments(
document.removeEventListener("keydown", this._keyListener); JSON.parse(JSON.stringify(this.$store.state.postwoman.environments))
);
}
}
} }
},
beforeDestroy() {
document.removeEventListener("keydown", this._keyListener);
} }
}; };
</script> </script>

View File

@@ -26,6 +26,7 @@ export const fb = {
currentSettings: [], currentSettings: [],
currentHistory: [], currentHistory: [],
currentCollections: [], currentCollections: [],
currentEnvironments: [],
writeFeeds: async (message, label) => { writeFeeds: async (message, label) => {
const dt = { const dt = {
createdOn: new Date(), createdOn: new Date(),
@@ -112,6 +113,21 @@ export const fb = {
.doc("sync") .doc("sync")
.set(cl) .set(cl)
.catch(e => console.error("error updating", cl, e)); .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; 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 { } else {
fb.currentUser = null; fb.currentUser = null;
} }

View File

@@ -256,7 +256,8 @@ export default {
enter_curl: "Enter cURL", enter_curl: "Enter cURL",
empty: "Empty", empty: "Empty",
extensions: "Extensions", 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_info1: "Browser extension that simplifies access to Postwoman",
extensions_info2: "Get Postwoman browser extension!", extensions_info2: "Get Postwoman browser extension!",
installed: "Installed", installed: "Installed",
@@ -267,6 +268,7 @@ export default {
sync: "Sync", sync: "Sync",
syncHistory: "History", syncHistory: "History",
syncCollections: "Collections", syncCollections: "Collections",
syncEnvironments: "Environments",
turn_on: "Turn on", turn_on: "Turn on",
login_first: "Login first", login_first: "Login first",
paste_a_note: "Paste a note", paste_a_note: "Paste a note",

View File

@@ -1086,7 +1086,7 @@
<pw-section class="pink" :label="$t('notes')" ref="sync"> <pw-section class="pink" :label="$t('notes')" ref="sync">
<div v-if="fb.currentUser"> <div v-if="fb.currentUser">
<inputform /> <inputform />
<ballsfeed /> <notes />
</div> </div>
<div v-else> <div v-else>
<ul> <ul>
@@ -1456,7 +1456,7 @@ export default {
saveRequestAs: () => import("../components/collections/saveRequestAs"), saveRequestAs: () => import("../components/collections/saveRequestAs"),
Editor: AceEditor, Editor: AceEditor,
inputform: () => import("../components/firebase/inputform"), inputform: () => import("../components/firebase/inputform"),
ballsfeed: () => import("../components/firebase/feeds"), notes: () => import("../components/firebase/feeds"),
environments: () => import("../components/environments") environments: () => import("../components/environments")
}, },
data() { data() {
@@ -2047,12 +2047,14 @@ export default {
}, },
methods: { methods: {
useSelectedEnvironment(environment) { useSelectedEnvironment(environment) {
let preRequestScriptString = '' let preRequestScriptString = "";
for (let variable of environment.variables) { 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.preRequestScript = preRequestScriptString;
this.showPreRequestScript = true this.showPreRequestScript = true;
}, },
checkCollections() { checkCollections() {
const checkCollectionAvailability = const checkCollectionAvailability =
@@ -2247,7 +2249,7 @@ export default {
}; };
this.$refs.historyComponent.addEntry(entry); this.$refs.historyComponent.addEntry(entry);
if (fb.currentUser !== null) { if (fb.currentUser !== null) {
if (fb.currentSettings[1].value) { if (fb.currentSettings[2].value) {
fb.writeHistory(entry); fb.writeHistory(entry);
} }
} }
@@ -2284,7 +2286,7 @@ export default {
}; };
this.$refs.historyComponent.addEntry(entry); this.$refs.historyComponent.addEntry(entry);
if (fb.currentUser !== null) { if (fb.currentUser !== null) {
if (fb.currentSettings[1].value) { if (fb.currentSettings[2].value) {
fb.writeHistory(entry); fb.writeHistory(entry);
} }
} }

View File

@@ -38,7 +38,7 @@
{{ setting.value ? $t("enabled") : $t("disabled") }} {{ setting.value ? $t("enabled") : $t("disabled") }}
</pw-toggle> </pw-toggle>
</p> </p>
<p v-if="fb.currentSettings.length == 0"> <p v-if="fb.currentSettings.length !== 3">
<button class="" @click="initSettings"> <button class="" @click="initSettings">
<i class="material-icons">sync</i> <i class="material-icons">sync</i>
<span>{{ $t("turn_on") + " " + $t("sync") }}</span> <span>{{ $t("turn_on") + " " + $t("sync") }}</span>
@@ -433,7 +433,8 @@ export default {
text: this.$t("yes"), text: this.$t("yes"),
onClick: (e, toastObject) => { onClick: (e, toastObject) => {
fb.writeSettings("syncHistory", true); fb.writeSettings("syncHistory", true);
fb.writeSettings("syncCollections", false); fb.writeSettings("syncCollections", true);
fb.writeSettings("syncEnvironments", true);
this.$router.push({ path: "/settings" }); this.$router.push({ path: "/settings" });
toastObject.remove(); toastObject.remove();
} }
@@ -462,7 +463,8 @@ export default {
text: this.$t("yes"), text: this.$t("yes"),
onClick: (e, toastObject) => { onClick: (e, toastObject) => {
fb.writeSettings("syncHistory", true); fb.writeSettings("syncHistory", true);
fb.writeSettings("syncCollections", false); fb.writeSettings("syncCollections", true);
fb.writeSettings("syncEnvironments", true);
this.$router.push({ path: "/settings" }); this.$router.push({ path: "/settings" });
toastObject.remove(); toastObject.remove();
} }
@@ -481,7 +483,8 @@ export default {
}, },
initSettings() { initSettings() {
fb.writeSettings("syncHistory", true); fb.writeSettings("syncHistory", true);
fb.writeSettings("syncCollections", false); fb.writeSettings("syncCollections", true);
fb.writeSettings("syncEnvironments", true);
}, },
resetProxy({ target }) { resetProxy({ target }) {
this.settings.PROXY_URL = `https://postwoman.apollotv.xyz/`; this.settings.PROXY_URL = `https://postwoman.apollotv.xyz/`;