跳至主要内容

Insomnia Plugin Development

Insomnia 支援外掛程式系統擴充功能,使用者可以從 Plugin Hub 瀏覽社群開發的外掛程式。

外掛程式類型

Request Hooks

  • Before Send: 請求傳送前的處理
  • After Response: 回應接收後的處理

Template Tags

  • 動態值生成
  • 環境變數處理
  • 加密解密功能

Themes

  • 自訂介面主題
  • CSS 樣式調整

開發環境設定

專案結構

my-insomnia-plugin/
├── package.json
├── main.js
└── README.md

package.json 設定

{
"name": "insomnia-plugin-my-plugin",
"version": "1.0.0",
"description": "My Insomnia Plugin",
"main": "main.js",
"insomnia": {
"name": "my-plugin",
"description": "Plugin description"
}
}

外掛程式範例

Template Tag 範例

module.exports.templateTags = [{
name: 'timestamp',
displayName: 'Timestamp',
description: 'Generate current timestamp',
args: [
{
displayName: 'Format',
type: 'enum',
options: [
{ displayName: 'Unix', value: 'unix' },
{ displayName: 'ISO', value: 'iso' }
]
}
],
async run(context, format) {
const now = new Date();
return format === 'unix' ?
Math.floor(now.getTime() / 1000) :
now.toISOString();
}
}];

Request Hook 範例

module.exports.requestHooks = [
{
hook: 'beforeSendRequest',
async action(context) {
// 在請求傳送前添加或修改 headers
context.request.setHeader('X-Custom-Header', 'value');
}
}
];

安裝與測試

本地開發

  1. 在 Insomnia 中開啟 Preferences
  2. 選擇 Plugins 頁籤
  3. 點擊 "Reveal Plugins Folder"
  4. 將外掛程式複製到該目錄
  5. 重新啟動 Insomnia

發佈到 npm

# 發佈到 npm registry
npm publish

# 在 Insomnia 中安裝
npm install insomnia-plugin-my-plugin

最佳實踐

錯誤處理

  • 使用 try-catch 包裝非同步操作
  • 提供有意義的錯誤訊息
  • 避免外掛程式崩潰影響 Insomnia

效能考量

  • 避免長時間的同步操作
  • 使用快取機制
  • 減少不必要的 API 呼叫

使用者體驗

  • 提供清楚的參數說明
  • 使用直覺的參數名稱
  • 提供預設值

See Also

官方文件

開發教學

相關資源