Last active
February 23, 2023 08:52
-
-
Save adriancuadrado/84408b9b6d53e41858a2f1b9f20090d6 to your computer and use it in GitHub Desktop.
Scripts to import/export Postman's history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async function() { | |
const filename = 'postman-history-export.json'; | |
const download = (function() { | |
const element = document.createElement('a'); | |
return function(filename, contents) { | |
element.setAttribute('href', URL.createObjectURL(new Blob([contents]))); | |
element.setAttribute('download', filename); | |
element.click(); | |
} | |
}()); | |
let transaction = await new Promise( | |
resolve => | |
indexedDB | |
.open('postman-app') | |
.onsuccess = | |
event => | |
resolve( | |
event | |
.target | |
.result | |
.transaction(['history', 'history_responses']) | |
) | |
); | |
function getAll(transaction, objectStore) { | |
return new Promise( | |
resolve => | |
transaction | |
.objectStore(objectStore) | |
.getAll() | |
.onsuccess = | |
event => | |
resolve(event.target.result) | |
); | |
} | |
const data = { | |
requests: await getAll(transaction, 'history'), | |
responses: await getAll(transaction, 'history_responses') | |
}; | |
download(filename, JSON.stringify(data)); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async function() { | |
function selectFile () { | |
let resolveFile = null; | |
const inputElement = document.createElement('input'); | |
inputElement.setAttribute('type', 'file'); | |
inputElement.addEventListener('change', event => resolveFile(event.target.files[0])); | |
inputElement.click(); | |
return new Promise(r => resolveFile = r); | |
} | |
const getFileContents = (function () { | |
let resolveContents = null; | |
const reader = new FileReader(); | |
reader.addEventListener('load', event => resolveContents(event.target.result)); | |
return function (file) { | |
reader.readAsText(file); | |
return new Promise(r => resolveContents = r); | |
} | |
})(); | |
async function importData(data) { | |
let transaction = await new Promise( | |
resolve => | |
indexedDB | |
.open('postman-app') | |
.onsuccess = | |
event => | |
resolve( | |
event | |
.target | |
.result | |
.transaction( | |
[ | |
'history', | |
'history_responses' | |
], 'readwrite' | |
) | |
) | |
); | |
function addAll(objectStore, data) { | |
objectStore.clear().onsuccess = () => data.forEach(e => objectStore.add(e)); | |
} | |
addAll(transaction.objectStore('history'), data.requests); | |
addAll(transaction.objectStore('history_responses'), data.responses); | |
} | |
importData(JSON.parse(await getFileContents(await selectFile()))); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await (async function() { | |
// These are the properties to keep for each request and response. The database returns more properties that are not | |
// interesting. If you remove all items from the `request` and `response` arrays in the `propertiesToKeep` object, the | |
// request and response objects returned will contain all properties from the database instead of just these. | |
let propertiesToKeep = { | |
request: [ | |
'createdAt', | |
'headerData', | |
'method', | |
'data', | |
'response', | |
'url' | |
], | |
response: [ | |
'cookies', | |
'headers', | |
'responseCode', | |
'text' | |
] | |
} | |
let transaction = await new Promise( | |
resolve => | |
indexedDB | |
.open('postman-app') | |
.onsuccess = | |
event => | |
resolve( | |
event | |
.target | |
.result | |
.transaction(['history', 'history_responses']) | |
) | |
); | |
function getAll(transaction, objectStore) { | |
return new Promise( | |
resolve => | |
transaction | |
.objectStore(objectStore) | |
.getAll() | |
.onsuccess = | |
event => | |
resolve(event.target.result) | |
); | |
} | |
let requests = (await getAll(transaction, 'history')) | |
.sort((r1, r2) => r1.createdAt.localeCompare(r2.createdAt)); | |
let responses = await getAll(transaction, 'history_responses'); | |
requests.forEach(req => { | |
let res = responses.find(r => r.history == req.id); | |
req.response = res; | |
if(propertiesToKeep.request.length > 0) { | |
Object.keys(req).filter(k => !propertiesToKeep.request .includes(k)).forEach(k => delete req[k]); | |
} | |
if(propertiesToKeep.response.length > 0){ | |
Object.keys(res).filter(k => !propertiesToKeep.response.includes(k)).forEach(k => delete res[k]); | |
} | |
}); | |
return requests; | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.[] as $req | | |
"curl \\\n" | |
+ "--location \\\n" | |
+ "--request " + $req.method + " \\\n" | |
+ if | |
$req.method != "GET" | |
and $req.method != "HEAD" | |
and $req.method != "DELETE" | |
then "--data-raw '" + $req.data + "' \\\n" | |
else "" | |
end | |
+ if $req.headerData | |
then | |
reduce ( | |
$req.headerData[] | |
| "--header '" + .key + ": " + .value + "' \\\n" | |
) as $line (""; . + $line) | |
else "" | |
end | |
+ "'" + $req.url + "'" + "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks @adriancuadrado - I appreciate the effort