Postman collection parsing
This commit is contained in:
@@ -120,20 +120,40 @@ export default {
|
|||||||
reader.onload = event => {
|
reader.onload = event => {
|
||||||
let content = event.target.result;
|
let content = event.target.result;
|
||||||
let collections = JSON.parse(content);
|
let collections = JSON.parse(content);
|
||||||
this.$store.commit("postwoman/replaceCollections", collections);
|
if (collections[0]) {
|
||||||
|
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 {
|
||||||
|
return this.failedImport();
|
||||||
|
}
|
||||||
|
this.$store.commit("postwoman/importCollections", collections);
|
||||||
|
this.fileImported();
|
||||||
};
|
};
|
||||||
reader.readAsText(this.$refs.inputChooseFileToReplaceWith.files[0]);
|
reader.readAsText(this.$refs.inputChooseFileToReplaceWith.files[0]);
|
||||||
this.fileImported();
|
|
||||||
},
|
},
|
||||||
importFromJSON() {
|
importFromJSON() {
|
||||||
let reader = new FileReader();
|
let reader = new FileReader();
|
||||||
reader.onload = event => {
|
reader.onload = event => {
|
||||||
let content = event.target.result;
|
let content = event.target.result;
|
||||||
let collections = JSON.parse(content);
|
let collections = JSON.parse(content);
|
||||||
|
if (collections[0]) {
|
||||||
|
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 {
|
||||||
|
return this.failedImport();
|
||||||
|
}
|
||||||
this.$store.commit("postwoman/importCollections", collections);
|
this.$store.commit("postwoman/importCollections", collections);
|
||||||
|
this.fileImported();
|
||||||
};
|
};
|
||||||
reader.readAsText(this.$refs.inputChooseFileToImportFrom.files[0]);
|
reader.readAsText(this.$refs.inputChooseFileToImportFrom.files[0]);
|
||||||
this.fileImported();
|
|
||||||
},
|
},
|
||||||
exportJSON() {
|
exportJSON() {
|
||||||
let text = this.collectionJson;
|
let text = this.collectionJson;
|
||||||
@@ -161,6 +181,114 @@ export default {
|
|||||||
this.$toast.info(this.$t("file_imported"), {
|
this.$toast.info(this.$t("file_imported"), {
|
||||||
icon: "folder_shared"
|
icon: "folder_shared"
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
failedImport() {
|
||||||
|
this.$toast.error(this.$t("import_failed"), {
|
||||||
|
icon: "error"
|
||||||
|
});
|
||||||
|
},
|
||||||
|
parsePostmanCollection(collection, folders = true) {
|
||||||
|
let postwomanCollection = folders ? [{
|
||||||
|
"name": "",
|
||||||
|
"folders": [],
|
||||||
|
"requests": []
|
||||||
|
}]
|
||||||
|
: {
|
||||||
|
"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));
|
||||||
|
} else {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return postwomanCollection;
|
||||||
|
},
|
||||||
|
parsePostmanRequest(requestObject) {
|
||||||
|
let pwRequest = {
|
||||||
|
"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(/^(.+:\/\/[^\/]+|{[^\/]+})(\/[^\?]+|).*$/);
|
||||||
|
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
|
||||||
|
: "";
|
||||||
|
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;
|
||||||
|
} else if (authType === "oauth2") {
|
||||||
|
pwRequest.auth = "OAuth 2.0";
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
let requestObjectHeaders = requestObject.request.header;
|
||||||
|
if (requestObjectHeaders) {
|
||||||
|
pwRequest.headers = requestObjectHeaders;
|
||||||
|
for (let header of pwRequest.headers) {
|
||||||
|
delete header.name;
|
||||||
|
delete header.type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let requestObjectParams = requestObject.request.url.query;
|
||||||
|
if (requestObjectParams) {
|
||||||
|
pwRequest.params = requestObjectParams;
|
||||||
|
for (let param of pwRequest.params) {
|
||||||
|
delete param.disabled;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (requestObject.request.body) {
|
||||||
|
if (requestObject.request.body.mode === "urlencoded") {
|
||||||
|
let params = requestObject.request.body.urlencoded;
|
||||||
|
pwRequest.bodyParams = params ? params : [];
|
||||||
|
for(let param of pwRequest.bodyParams) {
|
||||||
|
delete param.type;
|
||||||
|
}
|
||||||
|
} else if (requestObject.request.body.mode === "raw") {
|
||||||
|
pwRequest.rawInput = true;
|
||||||
|
pwRequest.rawParams = requestObject.request.body.raw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pwRequest;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -222,6 +222,7 @@ export default {
|
|||||||
payload: "Payload",
|
payload: "Payload",
|
||||||
choose_file: "Choose a file",
|
choose_file: "Choose a file",
|
||||||
file_imported: "File imported",
|
file_imported: "File imported",
|
||||||
|
import_failed: "Import failed",
|
||||||
f12_details: "(F12 for details)",
|
f12_details: "(F12 for details)",
|
||||||
we_use_cookies: "We use cookies",
|
we_use_cookies: "We use cookies",
|
||||||
copied_to_clipboard: "Copied to clipboard",
|
copied_to_clipboard: "Copied to clipboard",
|
||||||
|
|||||||
Reference in New Issue
Block a user