Recursive Insomnia import!

This commit is contained in:
Gregory Schier
2023-11-05 13:33:23 -08:00
parent 33d1a84ecd
commit f7a4ea9735
21 changed files with 1354 additions and 159 deletions

View File

@@ -39,7 +39,7 @@ tauri = { version = "1.3", features = [
"shell-open",
"system-tray",
"updater",
"window-start-dragging",
"window-start-dragging",
"dialog-open",
] }
tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }

View File

@@ -0,0 +1,4 @@
export function greet() {
// Call Rust-provided fn!
sayHello('Plugin');
}

View File

@@ -0,0 +1,7 @@
import { greet } from './greet.js';
export function hello() {
greet();
console.log('Try JSON parse', JSON.parse(`{ "hello": 123 }`).hello);
console.log('Try RegExp', '123'.match(/[\d]+/));
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,100 @@
{
"_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

@@ -0,0 +1,19 @@
export function isWorkspace(obj) {
return isJSObject(obj) && obj._type === 'workspace';
}
export function isRequestGroup(obj) {
return isJSObject(obj) && obj._type === 'request_group';
}
export function isRequest(obj) {
return isJSObject(obj) && obj._type === 'request';
}
export function isEnvironment(obj) {
return isJSObject(obj) && obj._type === 'environment';
}
export function isJSObject(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}

View File

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

View File

@@ -0,0 +1,21 @@
/**
* Import an Insomnia environment object.
* @param {Object} e - The environment object to import.
* @param workspaceId - Workspace to import into.
*/
export function importEnvironment(e, workspaceId) {
console.log('IMPORTING Environment', e._id, e.name, JSON.stringify(e, null, 2));
return {
id: e._id,
createdAt: new Date(e.created ?? Date.now()).toISOString().replace('Z', ''),
updatedAt: new Date(e.updated ?? Date.now()).toISOString().replace('Z', ''),
workspaceId,
model: 'environment',
name: e.name,
variables: Object.entries(e.data).map(([name, value]) => ({
enabled: true,
name,
value: `${value}`,
})),
};
}

View File

@@ -0,0 +1,17 @@
/**
* Import an Insomnia folder object.
* @param {Object} f - The environment object to import.
* @param workspaceId - Workspace to import into.
*/
export function importFolder(f, workspaceId) {
console.log('IMPORTING FOLDER', f._id, f.name, JSON.stringify(f, null, 2));
return {
id: f._id,
createdAt: new Date(f.created ?? Date.now()).toISOString().replace('Z', ''),
updatedAt: new Date(f.updated ?? Date.now()).toISOString().replace('Z', ''),
folderId: f.parentId === workspaceId ? null : f.parentId,
workspaceId,
model: 'folder',
name: f.name,
};
}

View File

@@ -0,0 +1,30 @@
/**
* Import an Insomnia request object.
* @param {Object} r - The request object to import.
* @param workspaceId - The workspace ID to use for the request.
* @param {number} sortPriority - The sort priority to use for the request.
*/
export function importRequest(r, workspaceId, sortPriority = 0) {
console.log('IMPORTING REQUEST', r._id, r.name, JSON.stringify(r, null, 2));
return {
id: r._id,
createdAt: new Date(r.created ?? Date.now()).toISOString().replace('Z', ''),
updatedAt: new Date(r.updated ?? Date.now()).toISOString().replace('Z', ''),
workspaceId,
folderId: r.parentId === workspaceId ? null : r.parentId,
model: 'http_request',
sortPriority,
name: r.name,
url: r.url,
body: null, // TODO: Import body
bodyType: null,
authentication: {}, // TODO: Import authentication
authenticationType: null,
method: r.method,
headers: (r.headers ?? []).map(({ name, value, disabled }) => ({
enabled: !disabled,
name,
value,
})),
};
}

View File

@@ -0,0 +1,15 @@
/**
* Import an Insomnia workspace object.
* @param {Object} w - The workspace object to import.
*/
export function importWorkspace(w, variables) {
console.log('IMPORTING Workspace', w._id, w.name, JSON.stringify(w, null, 2));
return {
id: w._id,
createdAt: new Date(w.created ?? Date.now()).toISOString().replace('Z', ''),
updatedAt: new Date(w.updated ?? Date.now()).toISOString().replace('Z', ''),
model: 'workspace',
name: w.name,
variables,
};
}

View File

@@ -0,0 +1,78 @@
import { importEnvironment } from './importers/environment.js';
import { importRequest } from './importers/request.js';
import { importWorkspace } from './importers/workspace.js';
import {
isEnvironment,
isJSObject,
isRequest,
isRequestGroup,
isWorkspace,
} from './helpers/types.js';
import { parseVariables } from './helpers/variables.js';
import { importFolder } from './importers/folder.js';
export function pluginHookImport(contents) {
const parsed = JSON.parse(contents);
if (!isJSObject(parsed)) {
return;
}
const { _type, __export_format } = parsed;
if (_type !== 'export' || __export_format !== 4 || !Array.isArray(parsed.resources)) {
return;
}
const resources = {
workspaces: [],
requests: [],
environments: [],
folders: [],
};
// Import workspaces
const workspacesToImport = parsed.resources.filter(isWorkspace);
for (const workspaceToImport of workspacesToImport) {
console.log('IMPORTING WORKSPACE', workspaceToImport.name);
const baseEnvironment = parsed.resources.find(
(r) => isEnvironment(r) && r.parentId === workspaceToImport._id,
);
console.log('FOUND BASE ENV', baseEnvironment.name);
resources.workspaces.push(
importWorkspace(
workspaceToImport,
baseEnvironment ? parseVariables(baseEnvironment.data) : [],
),
);
console.log('IMPORTING ENVIRONMENTS', baseEnvironment.name);
const environmentsToImport = parsed.resources.filter(
(r) => isEnvironment(r) && r.parentId === baseEnvironment?._id,
);
console.log('FOUND', environmentsToImport.length, 'ENVIRONMENTS');
resources.environments.push(
...environmentsToImport.map((r) => importEnvironment(r, workspaceToImport._id)),
);
const nextFolder = (parentId) => {
const children = parsed.resources.filter((r) => r.parentId === parentId);
let sortPriority = 0;
for (const child of children) {
if (isRequestGroup(child)) {
resources.folders.push(importFolder(child, workspaceToImport._id));
nextFolder(child._id);
} else if (isRequest(child)) {
resources.requests.push(importRequest(child, workspaceToImport._id, sortPriority++));
}
}
};
// Import folders
nextFolder(workspaceToImport._id);
}
// Filter out any `null` values
resources.requests = resources.requests.filter(Boolean);
resources.environments = resources.environments.filter(Boolean);
resources.workspaces = resources.workspaces.filter(Boolean);
return resources;
}

View File

@@ -32,11 +32,11 @@ use tokio::sync::Mutex;
use window_ext::TrafficLightWindowExt;
mod menu;
mod models;
mod plugin;
mod render;
mod window_ext;
mod window_menu;
#[derive(serde::Serialize)]
pub struct CustomResponse {
@@ -266,16 +266,13 @@ async fn import_data(
window: Window<Wry>,
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
file_paths: Vec<&str>,
workspace_id: Option<&str>,
) -> Result<plugin::ImportedResources, String> {
let pool = &*db_instance.lock().await;
let workspace_id2 = workspace_id.unwrap_or_default();
let imported = plugin::run_plugin_import(
&window.app_handle(),
pool,
"insomnia-importer",
file_paths.first().unwrap(),
workspace_id2,
)
.await;
Ok(imported)
@@ -764,7 +761,6 @@ fn main() {
&pool,
"insomnia-importer",
arg_file,
"wk_WN8Nrm2Awm",
)
.await;
exit(0);
@@ -834,7 +830,7 @@ fn is_dev() -> bool {
}
fn create_window(handle: &AppHandle<Wry>, url: Option<&str>) -> Window<Wry> {
let mut app_menu = menu::os_default("Yaak".to_string().as_str());
let mut app_menu = window_menu::os_default("Yaak".to_string().as_str());
if is_dev() {
let submenu = Submenu::new(
"Developer",

View File

@@ -1,5 +1,6 @@
use std::fs;
use boa_engine::builtins::promise::PromiseState;
use boa_engine::{
js_string,
module::{ModuleLoader, SimpleModuleLoader},
@@ -12,7 +13,7 @@ use serde_json::json;
use sqlx::{Pool, Sqlite};
use tauri::AppHandle;
use crate::models::{self, Environment, HttpRequest, Workspace};
use crate::models::{self, Environment, Folder, HttpRequest, Workspace};
pub fn run_plugin_hello(app_handle: &AppHandle, plugin_name: &str) {
run_plugin(app_handle, plugin_name, "hello", &[]);
@@ -20,9 +21,10 @@ pub fn run_plugin_hello(app_handle: &AppHandle, plugin_name: &str) {
#[derive(Default, Debug, Deserialize, Serialize)]
pub struct ImportedResources {
requests: Vec<HttpRequest>,
environments: Vec<Environment>,
workspaces: Vec<Workspace>,
environments: Vec<Environment>,
folders: Vec<Folder>,
requests: Vec<HttpRequest>,
}
pub async fn run_plugin_import(
@@ -30,9 +32,9 @@ pub async fn run_plugin_import(
pool: &Pool<Sqlite>,
plugin_name: &str,
file_path: &str,
workspace_id: &str,
) -> ImportedResources {
let file = fs::read_to_string(file_path).expect("Unable to read file");
let file = fs::read_to_string(file_path)
.expect(format!("Unable to read file {}", file_path.to_string()).as_str());
let file_contents = file.as_str();
let result_json = run_plugin(
app_handle,
@@ -44,34 +46,35 @@ pub async fn run_plugin_import(
serde_json::from_value(result_json).expect("failed to parse result json");
let mut imported_resources = ImportedResources::default();
println!("Importing resources: {}", workspace_id.is_empty());
if workspace_id.is_empty() {
for w in resources.workspaces {
println!("Importing workspace: {:?}", w);
let x = models::upsert_workspace(&pool, w)
.await
.expect("Failed to create workspace");
imported_resources.workspaces.push(x.clone());
println!("Imported workspace: {}", x.name);
}
println!("Importing resources");
for w in resources.workspaces {
println!("Importing workspace: {:?}", w);
let x = models::upsert_workspace(&pool, w)
.await
.expect("Failed to create workspace");
imported_resources.workspaces.push(x.clone());
println!("Imported workspace: {}", x.name);
}
for mut e in resources.environments {
if !workspace_id.is_empty() {
e.workspace_id = workspace_id.to_string();
}
for e in resources.environments {
println!("Importing environment: {:?}", e);
let x = models::upsert_environment(&pool, e)
.await
.expect("Failed to create environment");
imported_resources.environments.push(x.clone());
imported_resources.environments.push(x.clone());
println!("Imported environment: {}", x.name);
}
for mut r in resources.requests {
if !workspace_id.is_empty() {
r.workspace_id = workspace_id.to_string();
}
for f in resources.folders {
println!("Importing folder: {:?}", f);
let x = models::upsert_folder(&pool, f)
.await
.expect("Failed to create folder");
imported_resources.folders.push(x.clone());
println!("Imported folder: {}", x.name);
}
for r in resources.requests {
println!("Importing request: {:?}", r);
let x = models::upsert_request(&pool, r)
.await
@@ -91,12 +94,12 @@ fn run_plugin(
) -> serde_json::Value {
let plugin_dir = app_handle
.path_resolver()
.resolve_resource("../plugins")
.resolve_resource("plugins")
.expect("failed to resolve plugin directory resource")
.join(plugin_name);
let plugin_index_file = plugin_dir.join("index.js");
println!("Plugin dir: {:?}", plugin_dir);
println!("Plugin dir={:?} file={:?}", plugin_dir, plugin_index_file);
// Module loader for the specific plugin
let loader = &SimpleModuleLoader::new(plugin_dir).expect("failed to create module loader");
@@ -119,23 +122,25 @@ fn run_plugin(
// TODO: Is this needed if loaded from file already?
loader.insert(plugin_index_file, module.clone());
let _promise_result = module
let promise_result = module
.load_link_evaluate(context)
.expect("failed to evaluate module");
// Very important to push forward the job queue after queueing promises.
context.run_jobs();
// // Checking if the final promise didn't return an error.
// match promise_result.state() {
// PromiseState::Pending => return Err("module didn't execute!".into()),
// PromiseState::Fulfilled(v) => {
// assert_eq!(v, JsValue::undefined())
// }
// PromiseState::Rejected(err) => {
// return Err(JsError::from_opaque(err).try_native(context)?.into())
// }
// }
// Checking if the final promise didn't return an error.
match promise_result.state().expect("failed to get promise state") {
PromiseState::Pending => {
panic!("Promise was pending");
}
PromiseState::Fulfilled(v) => {
assert_eq!(v, JsValue::undefined())
}
PromiseState::Rejected(err) => {
panic!("Failed to link: {}", err.display());
}
}
let namespace = module.namespace(context);

View File

@@ -13,11 +13,11 @@
"tauri": {
"windows": [],
"cli": {
"description": "Yaak CLI",
"longDescription": "This is the Yaak CLI, yo",
"description": "Yaak CLI",
"longDescription": "This is the Yaak CLI, yo",
"beforeHelp": "u can use it to build, develop and manage your Yaak application.",
"afterHelp": "Have fun!",
"args": [],
"args": [],
"subcommands": {
"import": {
"args": [{
@@ -75,7 +75,7 @@
"longDescription": "The best cross-platform visual API client",
"resources": [
"migrations/*",
"../plugins/*"
"plugins/*"
],
"shortDescription": "The best API client",
"targets": [