Created
March 13, 2024 13:34
-
-
Save iRomul/06a2080e4dd8c0f2dd7b8a4a6e67c871 to your computer and use it in GitHub Desktop.
Example of custom Insomnia plugin
This file contains 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
const fs = require('fs'); | |
function selectFileArg(args) { | |
const variant = args[0]; | |
console.log("selectFileArg::variant", variant); | |
const pos = Number.parseInt(variant.value.replaceAll(/[^\d]/gi, "")) | |
return args[pos]; | |
} | |
function selectFile(args) { | |
return selectFileArg(args).value; | |
} | |
function isFilePresent(args) { | |
const filePath = selectFile(args); | |
return filePath?.length > 0 ?? false; | |
} | |
function w(value) { | |
return { | |
value | |
} | |
} | |
module.exports.templateTags = [ | |
{ | |
name: 'fileTag', | |
displayName: 'File Tag', | |
description: 'Quick select one of file', | |
disablePreview(args) { | |
console.log("disablePreview", args); | |
return !isFilePresent(args); | |
}, | |
args: [ | |
{ | |
displayName: 'Variant', | |
description: 'Which one?', | |
defaultValue: 'file1', | |
type: 'enum', | |
options: [ | |
{ | |
displayName: 'File 1', | |
value: 'file1' | |
}, | |
{ | |
displayName: 'File 2', | |
value: 'file2' | |
}, | |
{ | |
displayName: 'File 3', | |
value: 'file3' | |
}, | |
{ | |
displayName: 'File 4', | |
value: 'file4' | |
}, | |
{ | |
displayName: 'File 5', | |
value: 'file5' | |
} | |
] | |
}, | |
{ | |
displayName: 'Filename 1', | |
type: 'file' | |
}, | |
{ | |
displayName: 'Filename 2', | |
type: 'file' | |
}, | |
{ | |
displayName: 'Filename 3', | |
type: 'file' | |
}, | |
{ | |
displayName: 'Filename 4', | |
type: 'file' | |
}, | |
{ | |
displayName: 'Filename 5', | |
type: 'file' | |
} | |
], | |
async run(_context, arg1, arg2, arg3, arg4, arg5) { | |
const file = selectFileArg([w(arg1), w(arg2), w(arg3), w(arg4), w(arg5)]); | |
console.log("run::arg#", arg1, arg2, arg3); | |
if (!file) { | |
throw Error("No file selected"); | |
} | |
return fs.readFileSync(file.value, 'utf8'); | |
} | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment