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

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

View File

@@ -1,7 +0,0 @@
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]+/));
}

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,23 +0,0 @@
/**
* Import an Insomnia environment object.
* @param {Object} e - The environment object to import.
*/
export function importEnvironment(e) {
if (e.parentId.startsWith('env_')) {
return null;
}
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: e.parentId,
model: 'environment',
name: e.name,
variables: Object.entries(e.data).map(([name, value]) => ({
enabled: true,
name,
value: `${value}`,
})),
};
}

View File

@@ -1,28 +0,0 @@
/**
* Import an Insomnia request object.
* @param {Object} r - The request object to import.
* @param {number} sortPriority - The sort priority to use for the request.
*/
export function importRequest(r, 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: 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

@@ -1,14 +0,0 @@
/**
* Import an Insomnia workspace object.
* @param {Object} w - The workspace object to import.
*/
export function importWorkspace(w) {
console.log('IMPORTING Workpace', 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,
};
}

View File

@@ -1,50 +0,0 @@
import { importEnvironment } from './importers/environment.js';
import { importRequest } from './importers/request.js';
import { importWorkspace } from './importers/workspace.js';
const TYPES = {
workspace: 'workspace',
request: 'request',
environment: 'environment',
};
export function pluginHookImport(contents) {
const parsed = JSON.parse(contents);
if (!isObject(parsed)) {
return;
}
const { _type, __export_format } = parsed;
if (_type !== 'export' || __export_format !== 4 || !Array.isArray(parsed.resources)) {
return;
}
const resources = {
workspaces: [],
requests: [],
environments: [],
};
for (const v of parsed.resources) {
if (v._type === TYPES.workspace) {
resources.workspaces.push(importWorkspace(v));
} else if (v._type === TYPES.environment) {
resources.environments.push(importEnvironment(v));
} else if (v._type === TYPES.request) {
resources.requests.push(importRequest(v));
} else {
console.log('UNKNOWN TYPE', v._type, JSON.stringify(v, null, 2));
}
}
// 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;
}
function isObject(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}