-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.js
106 lines (89 loc) · 3.29 KB
/
plugin.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const fs = require('fs')
const axios = require('axios')
const { v4 } = require('uuid')
const storeKey = key => `insomnia-plugin-sync-${key}`
const getOrSetEndpoint = async context => {
console.log(context);
if (await context.store.hasItem(storeKey('endpoint'))) {
return context.store.getItem(storeKey('endpoint'))
} else {
const endpoint = await context.app.prompt('set endpoint')
await context.store.setItem(storeKey('endpoint'), endpoint)
return endpoint
}
}
const depositWorkspace = async content => {
if (!fs.existsSync(__dirname+'/deposit')) fs.mkdirSync(__dirname+'/deposit')
fs.writeFileSync(__dirname+'/deposit/'+Date.now()+'_export.json', JSON.stringify(content))
}
const getWorkspace = async (context, models) => {
return await context.data.export.insomnia({
includePrivate: false,
format: 'json',
workspace: models.workspace,
})
}
const workspaceEndpoint = (endpoint, name) => {
const url = new URL(endpoint);
url.searchParams.append('workspace', name);
return url.href;
}
module.exports.workspaceActions = [
{
label: 'Sync upload',
icon: 'fa-upload',
action: async (context, models) => {
console.log('models', models);
if (!confirm('Ready to upload?')) return
const revision = v4()
const endpoint = await getOrSetEndpoint(context)
const workspace = await getWorkspace(context, models)
await context.store.setItem(storeKey('revision'), revision)
const data = { workspace, revision }
await depositWorkspace(data)
await axios.post(workspaceEndpoint(endpoint, models.workspace.name), data, {
headers: {'Content-Type': 'application/json'}
}).then((res) => {
console.log('insomnia-plugin-sync', 'upload', res)
}).catch((err) => {
console.error('insomnia-plugin-sync', 'upload', err)
})
}
},
{
label: 'Sync download',
icon: 'fa-download',
action: async (context, models) => {
if (!confirm('Ready to download?')) return
const endpoint = await getOrSetEndpoint(context)
const revision = await context.store.getItem(storeKey('revision'))
const workspace = await getWorkspace(context, models)
await depositWorkspace({ workspace, revision })
await axios.get(workspaceEndpoint(endpoint, models.workspace.name)).then((res) => {
const data = res.data
if (typeof data !== 'object' || typeof data.workspace === 'undefined') {
context.app.alert('Invalid API data type. See console...')
console.warn('sync upload', res.data)
return
}
context.store.setItem(storeKey('revision'), data.revision)
context.data.import.raw(data.workspace).then((res) => {
console.log('insomnia-plugin-sync', 'download')
}).catch((err) => {
console.error('insomnia-plugin-sync', 'download', err)
})
})
},
},
{
label: 'Sync check',
icon: 'fa-check',
action: async (context, models) => {
const endpoint = await getOrSetEndpoint(context)
const revision = await context.store.getItem(storeKey('revision'))
await axios.get(workspaceEndpoint(endpoint, models.workspace.name)).then((res) => {
context.app.alert('Sync plugin', revision === res.data.revision ? 'Up to date' : 'Update ready')
})
},
}
]