mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-16 22:16:49 +01:00
Recursive Insomnia import!
This commit is contained in:
4
src-tauri/plugins/hello-world/greet.js
Normal file
4
src-tauri/plugins/hello-world/greet.js
Normal file
@@ -0,0 +1,4 @@
|
||||
export function greet() {
|
||||
// Call Rust-provided fn!
|
||||
sayHello('Plugin');
|
||||
}
|
||||
7
src-tauri/plugins/hello-world/index.js
Normal file
7
src-tauri/plugins/hello-world/index.js
Normal 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]+/));
|
||||
}
|
||||
1133
src-tauri/plugins/insomnia-importer/Insomnia_bigger.json
Normal file
1133
src-tauri/plugins/insomnia-importer/Insomnia_bigger.json
Normal file
File diff suppressed because it is too large
Load Diff
100
src-tauri/plugins/insomnia-importer/Insomnia_hello-world.json
Normal file
100
src-tauri/plugins/insomnia-importer/Insomnia_hello-world.json
Normal 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"
|
||||
}
|
||||
]
|
||||
}
|
||||
19
src-tauri/plugins/insomnia-importer/helpers/types.js
Normal file
19
src-tauri/plugins/insomnia-importer/helpers/types.js
Normal 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]';
|
||||
}
|
||||
7
src-tauri/plugins/insomnia-importer/helpers/variables.js
Normal file
7
src-tauri/plugins/insomnia-importer/helpers/variables.js
Normal file
@@ -0,0 +1,7 @@
|
||||
export function parseVariables(data) {
|
||||
return Object.entries(data).map(([name, value]) => ({
|
||||
enabled: true,
|
||||
name,
|
||||
value: `${value}`,
|
||||
}));
|
||||
}
|
||||
21
src-tauri/plugins/insomnia-importer/importers/environment.js
Normal file
21
src-tauri/plugins/insomnia-importer/importers/environment.js
Normal 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}`,
|
||||
})),
|
||||
};
|
||||
}
|
||||
17
src-tauri/plugins/insomnia-importer/importers/folder.js
Normal file
17
src-tauri/plugins/insomnia-importer/importers/folder.js
Normal 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,
|
||||
};
|
||||
}
|
||||
30
src-tauri/plugins/insomnia-importer/importers/request.js
Normal file
30
src-tauri/plugins/insomnia-importer/importers/request.js
Normal 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,
|
||||
})),
|
||||
};
|
||||
}
|
||||
15
src-tauri/plugins/insomnia-importer/importers/workspace.js
Normal file
15
src-tauri/plugins/insomnia-importer/importers/workspace.js
Normal 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,
|
||||
};
|
||||
}
|
||||
78
src-tauri/plugins/insomnia-importer/index.js
Normal file
78
src-tauri/plugins/insomnia-importer/index.js
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user