Vite to bundle insomnia plugin

This commit is contained in:
Gregory Schier
2023-11-05 22:13:22 -08:00
parent abc6d0ff1e
commit a74c8a94db
12 changed files with 52 additions and 1180 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,100 +0,0 @@
{
"_type": "export",
"__export_format": 4,
"__export_date": "2023-11-01T23:41:02.844Z",
"__export_source": "insomnia.desktop.app:v8.3.0",
"resources": [
{
"_id": "req_c8ae891b0fe549a4a530a75da59b6e34",
"parentId": "wrk_ea69a78d6a0540f583d2ec80666a1724",
"modified": 1698767088880,
"created": 1698767077168,
"url": "https://schier.co",
"name": "My Request",
"description": "",
"method": "GET",
"body": {},
"parameters": [],
"headers": [{ "name": "User-Agent", "value": "insomnia/8.3.0" }],
"authentication": {},
"metaSortKey": -1698767077168,
"isPrivate": false,
"settingStoreCookies": true,
"settingSendCookies": true,
"settingDisableRenderRequestBody": false,
"settingEncodeUrl": true,
"settingRebuildPath": true,
"settingFollowRedirects": "global",
"_type": "request"
},
{
"_id": "wrk_ea69a78d6a0540f583d2ec80666a1724",
"parentId": null,
"modified": 1698767073768,
"created": 1698767068649,
"name": "Hello World",
"description": "",
"scope": "collection",
"_type": "workspace"
},
{
"_id": "env_90b3abd7ed857fd535396167018da33932100672",
"parentId": "wrk_ea69a78d6a0540f583d2ec80666a1724",
"modified": 1698881852559,
"created": 1698767068650,
"name": "Base Environment",
"data": { "base": true },
"dataPropertyOrder": { "&": ["base"] },
"color": null,
"isPrivate": false,
"metaSortKey": 1698767068650,
"_type": "environment"
},
{
"_id": "jar_90b3abd7ed857fd535396167018da33932100672",
"parentId": "wrk_ea69a78d6a0540f583d2ec80666a1724",
"modified": 1698767090390,
"created": 1698767068651,
"name": "Default Jar",
"cookies": [
{
"key": "_gorilla_csrf",
"value": "MTY5ODc2NzA5MHxJa1Z1U0RCVVMzcDJhbEJFWkd0Q09WVkllbXBMVlhSd1VtaGFkVlpsVVhobVNVNDVTV2hDWmpFd1JtTTlJZ289fPkab2rsnQwWmJi-pCbg5Wz4O_6csc29ZcYOdB0tOLtD",
"expires": "2023-11-07T15:44:50.000Z",
"maxAge": 604800,
"domain": "schier.co",
"path": "/",
"httpOnly": true,
"hostOnly": true,
"creation": "2023-10-31T15:44:50.390Z",
"lastAccessed": "2023-10-31T15:44:50.390Z",
"sameSite": "lax",
"id": "672286917061701"
}
],
"_type": "cookie_jar"
},
{
"_id": "env_d04deba50c2f44b0b9bd01c53efebff4",
"parentId": "env_90b3abd7ed857fd535396167018da33932100672",
"modified": 1698882026143,
"created": 1698881855600,
"name": "Sub Environment",
"data": {
"string": "string",
"bool": true,
"number": 123,
"object": { "foo": "bar" },
"array": [1, 2, 3]
},
"dataPropertyOrder": {
"&": ["string", "bool", "number", "object", "array"],
"&~|object": ["foo"]
},
"color": null,
"isPrivate": false,
"metaSortKey": 1698881855600,
"_type": "environment"
}
]
}

View File

@@ -1,7 +0,0 @@
export function parseVariables(data) {
return Object.entries(data).map(([name, value]) => ({
enabled: true,
name,
value: `${value}`,
}));
}

View File

@@ -17,3 +17,7 @@ export function isEnvironment(obj) {
export function isJSObject(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}
export function isJSString(obj) {
return Object.prototype.toString.call(obj) === '[object String]';
}

View File

@@ -0,0 +1,18 @@
import { isJSString } from './types.js';
export function parseVariables(data) {
return Object.entries(data).map(([name, value]) => ({
enabled: true,
name,
value: `${value}`,
}));
}
/**
* Convert Insomnia syntax to Yaak syntax
* @param {string} variable - Text to convert
*/
export function convertSyntax(variable) {
if (!isJSString(variable)) return variable;
return variable.replaceAll(/{{\s*(_\.)?([^}]+)\s*}}/g, '${[$2]}');
}

View File

@@ -1,3 +1,5 @@
import { convertSyntax } from '../helpers/variables.js';
/**
* Import an Insomnia request object.
* @param {Object} r - The request object to import.
@@ -6,20 +8,32 @@
*/
export function importRequest(r, workspaceId, sortPriority = 0) {
console.log('IMPORTING REQUEST', r._id, r.name, JSON.stringify(r, null, 2));
let bodyType = null;
let body = null;
if (r.body?.mimeType === 'application/graphql') {
bodyType = 'graphql';
body = r.body.text;
body = convertSyntax(r.body.text);
} else if (r.body?.mimeType === 'application/json') {
bodyType = 'application/json';
body = convertSyntax(r.body.text);
}
let authenticationType = null;
let authentication = {};
if (r.authentication.type === 'bearer') {
authenticationType = 'bearer';
authentication = {
token: r.authentication.token,
token: convertSyntax(r.authentication.token),
};
} else if (r.authentication.type === 'basic') {
authenticationType = 'basic';
authentication = {
username: convertSyntax(r.authentication.username),
password: convertSyntax(r.authentication.password),
};
}
return {
id: r._id,
createdAt: new Date(r.created ?? Date.now()).toISOString().replace('Z', ''),
@@ -29,7 +43,7 @@ export function importRequest(r, workspaceId, sortPriority = 0) {
model: 'http_request',
sortPriority,
name: r.name,
url: r.url,
url: convertSyntax(r.url),
body,
bodyType,
authentication,

View File

@@ -0,0 +1,12 @@
import { resolve } from 'path';
import { defineConfig } from 'vite';
export default defineConfig({
build: {
lib: {
entry: resolve(__dirname, 'src/index.js'),
fileName: 'index',
formats: ['es'],
},
},
});

View File

@@ -97,7 +97,7 @@ fn run_plugin(
.resolve_resource("plugins")
.expect("failed to resolve plugin directory resource")
.join(plugin_name);
let plugin_index_file = plugin_dir.join("index.js");
let plugin_index_file = plugin_dir.join("dist/index.js");
println!("Plugin dir={:?} file={:?}", plugin_dir, plugin_index_file);