-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (100 loc) · 3.64 KB
/
index.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
107
108
109
110
111
112
113
114
115
116
117
const sortJson = require('sort-json')
const sortAscendingKey = 'sort-ascending'
const sortDepthKey = 'sort-depth'
const ignoreCaseKey = 'ignore-case'
const sortAscendingDefault = true
const sortDepthDefault = 5
const ignoreCaseDefault = true
const setConfigValue = async (context, key, value) => {
await context.store.setItem(key, value)
console.warn(`Set sort-json ${key} to ${value}`)
}
const setUpDefaults = async (context) => {
if(!await context.store.hasItem(sortAscendingKey)) {
await setConfigValue(context, sortAscendingKey, sortAscendingDefault)
}
//for now, always set the sortDepth to default in case people want to change it easily
await setConfigValue(context, sortDepthKey, sortDepthDefault)
if(!await context.store.hasItem(ignoreCaseKey)) {
await setConfigValue(context, ignoreCaseKey, ignoreCaseDefault)
}
}
const getConfigBoolean = async (context, key) => {
return await context.store.getItem(key) == 'true'
}
const getConfigInt = async (context, key) => {
return parseInt(await context.store.getItem(key))
}
module.exports.responseHooks = [
async (context) => {
//stopgap until insomnia can give us a load hook with context
await setUpDefaults(context)
const body = await context.response.getBody()
let data = {}
try {
data = JSON.parse(Buffer.from(body).toString())
} catch (e) {
return await context.response.setBody(body) //if the JSON fails to parse, pass response body through anyway
}
//sort-ascending means reverse = false
const options = {
reverse: !await getConfigBoolean(context, sortAscendingKey),
depth: await getConfigInt(context, sortDepthKey),
ignoreCase: await getConfigBoolean(context, ignoreCaseKey)
}
const sortedData = Buffer.from(JSON.stringify(sortJson(data, options)))
return await context.response.setBody(sortedData)
}
]
module.exports.workspaceActions = [
{
label: "sort-json ascending (default)",
icon: "fa-chevron-up",
action: async (context, _) => {
await setConfigValue(context, sortAscendingKey, true)
}
},
{
label: "sort-json descending",
icon:"fa-chevron-down",
action: async(context, _) => {
await setConfigValue(context, sortAscendingKey, false)
}
},
// including below for when context.app.prompt is fixed for workspaceActions
// {
// label: "sort-json depth",
// icon: "fa-chevron-right",
// action: async (context, data) => {
// const result = await context.app.prompt('Configure Sort Depth', {
// label: 'sort-json sort depth',
// defaultValue: '1',
// submitName: 'Set',
// cancelable: true
// })
// }
// },
{
label: "sort-json ignore-case (default)",
icon:"fa-font",
action: async(context, _) => {
await setConfigValue(context, ignoreCaseKey, true)
}
},
{
label: "sort-json NO-ignore-case",
icon:"fa-font",
action: async(context, _) => {
await setConfigValue(context, ignoreCaseKey, false)
}
},
{
label: "sort-json reset to defaults",
icon:"fa-cogs",
action: async(context, _) => {
await setConfigValue(context, sortAscendingKey, sortAscendingDefault)
await setConfigValue(context, sortDepthKey, sortDepthDefault)
await setConfigValue(context, ignoreCaseKey, ignoreCaseDefault)
}
},
]