Hacky Yaak import complete!

This commit is contained in:
Gregory Schier
2023-11-09 19:40:31 -08:00
parent 4306294a72
commit bf8aad04c7
15 changed files with 246 additions and 197 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,4 +1,4 @@
function O(e, t) {
function I(e, t) {
return (
console.log('IMPORTING Environment', e._id, e.name, JSON.stringify(e, null, 2)),
{
@@ -8,64 +8,64 @@ function O(e, t) {
workspaceId: t,
model: 'environment',
name: e.name,
variables: Object.entries(e.data).map(([i, s]) => ({
variables: Object.entries(e.data).map(([n, s]) => ({
enabled: !0,
name: i,
name: n,
value: `${s}`,
})),
}
);
}
function g(e) {
function S(e) {
return m(e) && e._type === 'workspace';
}
function y(e) {
function O(e) {
return m(e) && e._type === 'request_group';
}
function _(e) {
function g(e) {
return m(e) && e._type === 'request';
}
function I(e) {
function f(e) {
return m(e) && e._type === 'environment';
}
function m(e) {
return Object.prototype.toString.call(e) === '[object Object]';
}
function h(e) {
function y(e) {
return Object.prototype.toString.call(e) === '[object String]';
}
function N(e) {
return Object.entries(e).map(([t, i]) => ({
function h(e) {
return Object.entries(e).map(([t, n]) => ({
enabled: !0,
name: t,
value: `${i}`,
value: `${n}`,
}));
}
function p(e) {
return h(e) ? e.replaceAll(/{{\s*(_\.)?([^}]+)\s*}}/g, '${[$2]}') : e;
function d(e) {
return y(e) ? e.replaceAll(/{{\s*(_\.)?([^}]+)\s*}}/g, '${[$2]}') : e;
}
function D(e, t, i = 0) {
var a, d;
function _(e, t, n = 0) {
var u, r;
console.log('IMPORTING REQUEST', e._id, e.name, JSON.stringify(e, null, 2));
let s = null,
n = null;
((a = e.body) == null ? void 0 : a.mimeType) === 'application/graphql'
? ((s = 'graphql'), (n = p(e.body.text)))
: ((d = e.body) == null ? void 0 : d.mimeType) === 'application/json' &&
((s = 'application/json'), (n = p(e.body.text)));
let u = null,
r = {};
o = null;
((u = e.body) == null ? void 0 : u.mimeType) === 'application/graphql'
? ((s = 'graphql'), (o = d(e.body.text)))
: ((r = e.body) == null ? void 0 : r.mimeType) === 'application/json' &&
((s = 'application/json'), (o = d(e.body.text)));
let a = null,
l = {};
return (
e.authentication.type === 'bearer'
? ((u = 'bearer'),
(r = {
token: p(e.authentication.token),
? ((a = 'bearer'),
(l = {
token: d(e.authentication.token),
}))
: e.authentication.type === 'basic' &&
((u = 'basic'),
(r = {
username: p(e.authentication.username),
password: p(e.authentication.password),
((a = 'basic'),
(l = {
username: d(e.authentication.username),
password: d(e.authentication.password),
})),
{
id: e._id,
@@ -74,25 +74,25 @@ function D(e, t, i = 0) {
workspaceId: t,
folderId: e.parentId === t ? null : e.parentId,
model: 'http_request',
sortPriority: i,
sortPriority: n,
name: e.name,
url: p(e.url),
body: n,
url: d(e.url),
body: o,
bodyType: s,
authentication: r,
authenticationType: u,
authentication: l,
authenticationType: a,
method: e.method,
headers: (e.headers ?? [])
.map(({ name: c, value: o, disabled: f }) => ({
enabled: !f,
.map(({ name: c, value: p, disabled: i }) => ({
enabled: !i,
name: c,
value: o,
value: p,
}))
.filter(({ name: c, value: o }) => c !== '' || o !== ''),
.filter(({ name: c, value: p }) => c !== '' || p !== ''),
}
);
}
function w(e, t) {
function N(e, t) {
return (
console.log('IMPORTING Workspace', e._id, e.name, JSON.stringify(e, null, 2)),
{
@@ -105,7 +105,7 @@ function w(e, t) {
}
);
}
function b(e, t) {
function D(e, t) {
return (
console.log('IMPORTING FOLDER', e._id, e.name, JSON.stringify(e, null, 2)),
{
@@ -119,34 +119,37 @@ function b(e, t) {
}
);
}
function T(e) {
const t = JSON.parse(e);
if (!m(t)) return;
const { _type: i, __export_format: s } = t;
if (i !== 'export' || s !== 4 || !Array.isArray(t.resources)) return;
function w(e) {
let t;
try {
t = JSON.parse(e);
} catch {
return;
}
if (!m(t) || !Array.isArray(t.requests)) return;
const n = {
workspaces: [],
requests: [],
environments: [],
folders: [],
},
u = t.resources.filter(g);
for (const r of u) {
console.log('IMPORTING WORKSPACE', r.name);
const a = t.resources.find((o) => I(o) && o.parentId === r._id);
s = t.resources.filter(S);
for (const o of s) {
console.log('IMPORTING WORKSPACE', o.name);
const a = t.resources.find((r) => f(r) && r.parentId === o._id);
console.log('FOUND BASE ENV', a.name),
n.workspaces.push(w(r, a ? N(a.data) : [])),
n.workspaces.push(N(o, a ? h(a.data) : [])),
console.log('IMPORTING ENVIRONMENTS', a.name);
const d = t.resources.filter((o) => I(o) && o.parentId === (a == null ? void 0 : a._id));
console.log('FOUND', d.length, 'ENVIRONMENTS'),
n.environments.push(...d.map((o) => O(o, r._id)));
const c = (o) => {
const f = t.resources.filter((l) => l.parentId === o);
let S = 0;
for (const l of f)
y(l) ? (n.folders.push(b(l, r._id)), c(l._id)) : _(l) && n.requests.push(D(l, r._id, S++));
const l = t.resources.filter((r) => f(r) && r.parentId === (a == null ? void 0 : a._id));
console.log('FOUND', l.length, 'ENVIRONMENTS'),
n.environments.push(...l.map((r) => I(r, o._id)));
const u = (r) => {
const c = t.resources.filter((i) => i.parentId === r);
let p = 0;
for (const i of c)
O(i) ? (n.folders.push(D(i, o._id)), u(i._id)) : g(i) && n.requests.push(_(i, o._id, p++));
};
c(r._id);
u(o._id);
}
return (
(n.requests = n.requests.filter(Boolean)),
@@ -155,4 +158,4 @@ function T(e) {
n
);
}
export { T as pluginHookImport };
export { w as pluginHookImport };

View File

@@ -12,14 +12,19 @@ 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;
let parsed;
try {
parsed = JSON.parse(contents);
} catch (e) {
return undefined;
}
const { _type, __export_format } = parsed;
if (_type !== 'export' || __export_format !== 4 || !Array.isArray(parsed.resources)) {
return;
if (!isJSObject(parsed)) {
return undefined;
}
if (!Array.isArray(parsed.requests)) {
return undefined;
}
const resources = {

View File

@@ -0,0 +1,13 @@
function u(r) {
let e;
try {
e = JSON.parse(r);
} catch {
return;
}
if (t(e) && e.yaakSchema === 1) return e.resources;
}
function t(r) {
return Object.prototype.toString.call(r) === '[object Object]';
}
export { t as isJSObject, u as pluginHookImport };

View File

@@ -0,0 +1,20 @@
export function pluginHookImport(contents) {
let parsed;
try {
parsed = JSON.parse(contents);
} catch (err) {
return undefined;
}
if (!isJSObject(parsed)) {
return undefined;
}
if (parsed.yaakSchema !== 1) return undefined;
return parsed.resources; // Should already be in the correct format
}
export function isJSObject(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}

View File

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