-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmigrate-insomnia-from-auth-to-header.js
34 lines (31 loc) · 1.24 KB
/
migrate-insomnia-from-auth-to-header.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//Simple insmonia workspace migration tool which moves authentication from "Auth" section to "Headers" section
const fs = require("fs");
const fileName = "/Users/jdk/work/libra/usy_librag01_configuration/usy_librag01_configuration-server/test/insomnia/insomnia-workspace.json";
const PLUGIN_RE = /^({% plus4uToken [^,]*, [^,]*, [^,]*, [^,]*(?:, ([^,]+))?) %}$/
let wsStr = fs.readFileSync(fileName);
let ws = JSON.parse(wsStr);
ws.resources.filter(res => res._type === "request" && res.authentication && res.authentication.type === "bearer").forEach(req => {
if (!req.headers) {
req.headers = [];
}
req.headers = req.headers.filter(header => header.name != "Authorization");
req.headers.push({
name: "Authorization",
value: `Bearer ${req.authentication.token}`
});
delete req.authentication;
});
ws.resources.filter(res => res._type === "environment").forEach(env => {
Object.keys(env.data).forEach(key => {
let match = env.data[key].match(PLUGIN_RE);
if(match) {
if(match[2]){
env.data[key] = match[1]+", true %}";
}else{
env.data[key] = match[1]+", 'https://oidc.plus4u.net/uu-oidcg01-main/0-0', true %}";
}
}
});
});
wsStr = JSON.stringify(ws, null, 2);
fs.writeFileSync(fileName, wsStr);