-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
52 lines (46 loc) · 1.41 KB
/
main.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
const tempy = require('tempy')
const etl = require('etl')
const mime = require('mime-types')
const child = require('child_process');
const CONTENT_TYPE_BLACKLIST = ["text/", "application/json"]
module.exports.templateTags = [{
name: 'open_sesame',
displayName: 'Open',
description: 'Saves response body to a temp file and opens it using system defaults',
args: [
{
displayName: 'Request',
type: 'model',
model: 'Request',
},
]
}]
module.exports.responseHooks = [
context => {
const contentType = context.response.getHeader('Content-Type')
if (context.response.getStatusCode() !== 200 || CONTENT_TYPE_BLACKLIST.some((it) => contentType.startsWith(it))) {
return
}
const extension = mime.extension(contentType)
if (!extension) {
throw new Error(`Invalid content type ${extension}`)
}
const tempFile = tempy.file({extension: extension})
context.response.getBodyStream()
.pipe(etl.toFile(tempFile))
.promise()
.then(() => child.exec(getCommandLine() + ' ' + tempFile))
}
]
function getCommandLine() {
switch (process.platform) {
case 'darwin' :
return 'open';
case 'win32' :
return 'start';
case 'win64' :
return 'start';
default :
return 'xdg-open';
}
}