snapshot of https://github.com/mountain-loop/yaak.git (main, 2026-09-23; blobs > 131072B omitted)
This commit is contained in:
12
plugins/importer-yaak/package.json
Normal file
12
plugins/importer-yaak/package.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "@yaak/importer-yaak",
|
||||
"displayName": "Yaak Importer",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"description": "Import data from Yaak export files",
|
||||
"scripts": {
|
||||
"build": "yaakcli build",
|
||||
"dev": "yaakcli dev",
|
||||
"test": "vp test --run tests"
|
||||
}
|
||||
}
|
||||
96
plugins/importer-yaak/src/index.ts
Normal file
96
plugins/importer-yaak/src/index.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import type { Environment, PluginDefinition } from "@yaakapp/api";
|
||||
|
||||
export const plugin: PluginDefinition = {
|
||||
importer: {
|
||||
name: "Yaak",
|
||||
description: "Yaak official format",
|
||||
onImport(_ctx, args) {
|
||||
return migrateImport(args.text);
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export function migrateImport(contents: string) {
|
||||
// oxlint-disable-next-line no-explicit-any
|
||||
let parsed: any;
|
||||
try {
|
||||
parsed = JSON.parse(contents);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!isJSObject(parsed)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const isYaakExport = "yaakSchema" in parsed;
|
||||
if (!isYaakExport) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Migrate v1 to v2 -- changes requests to httpRequests
|
||||
if ("requests" in parsed.resources) {
|
||||
parsed.resources.httpRequests = parsed.resources.requests;
|
||||
parsed.resources.requests = undefined;
|
||||
}
|
||||
|
||||
// Migrate v2 to v3
|
||||
for (const workspace of parsed.resources.workspaces ?? []) {
|
||||
if ("variables" in workspace) {
|
||||
// Create the base environment
|
||||
const baseEnvironment: Partial<Environment> = {
|
||||
id: `GENERATE_ID::base_env_${workspace.id}`,
|
||||
name: "Global Variables",
|
||||
variables: workspace.variables,
|
||||
workspaceId: workspace.id,
|
||||
};
|
||||
parsed.resources.environments = parsed.resources.environments ?? [];
|
||||
parsed.resources.environments.push(baseEnvironment);
|
||||
|
||||
// Delete variables key from the workspace
|
||||
workspace.variables = undefined;
|
||||
|
||||
// Add environmentId to relevant environments
|
||||
for (const environment of parsed.resources.environments) {
|
||||
if (environment.workspaceId === workspace.id && environment.id !== baseEnvironment.id) {
|
||||
environment.environmentId = baseEnvironment.id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Migrate v3 to v4
|
||||
for (const environment of parsed.resources.environments ?? []) {
|
||||
if ("environmentId" in environment) {
|
||||
environment.base = environment.environmentId == null;
|
||||
environment.environmentId = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
// Migrate v4 to v5
|
||||
for (const environment of parsed.resources.environments ?? []) {
|
||||
if ("base" in environment && environment.base && environment.parentModel == null) {
|
||||
environment.parentModel = "workspace";
|
||||
environment.parentId = null;
|
||||
environment.base = undefined;
|
||||
} else if ("base" in environment && !environment.base && environment.parentModel == null) {
|
||||
environment.parentModel = "environment";
|
||||
environment.parentId = null;
|
||||
environment.base = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
const sourceKeys: Record<string, string> = {};
|
||||
for (const models of Object.values(parsed.resources)) {
|
||||
if (!Array.isArray(models)) continue;
|
||||
for (const model of models) {
|
||||
if (typeof model?.id === "string") sourceKeys[model.id] = model.id;
|
||||
}
|
||||
}
|
||||
|
||||
return { resources: parsed.resources, sourceKeys };
|
||||
}
|
||||
|
||||
function isJSObject(obj: unknown) {
|
||||
return Object.prototype.toString.call(obj) === "[object Object]";
|
||||
}
|
||||
179
plugins/importer-yaak/tests/index.test.ts
Normal file
179
plugins/importer-yaak/tests/index.test.ts
Normal file
@@ -0,0 +1,179 @@
|
||||
import { describe, expect, test } from "vite-plus/test";
|
||||
import { migrateImport } from "../src";
|
||||
|
||||
describe("importer-yaak", () => {
|
||||
test("Skips invalid imports", () => {
|
||||
expect(migrateImport("not JSON")).toBeUndefined();
|
||||
expect(migrateImport("[]")).toBeUndefined();
|
||||
expect(migrateImport(JSON.stringify({ resources: {} }))).toBeUndefined();
|
||||
});
|
||||
|
||||
test("converts schema 1 to 2", () => {
|
||||
const imported = migrateImport(
|
||||
JSON.stringify({
|
||||
yaakSchema: 1,
|
||||
resources: {
|
||||
requests: [],
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
expect(imported).toEqual(
|
||||
expect.objectContaining({
|
||||
resources: {
|
||||
httpRequests: [],
|
||||
},
|
||||
}),
|
||||
);
|
||||
});
|
||||
test("converts schema 2 to 3", () => {
|
||||
const imported = migrateImport(
|
||||
JSON.stringify({
|
||||
yaakSchema: 2,
|
||||
resources: {
|
||||
environments: [
|
||||
{
|
||||
id: "e_1",
|
||||
workspaceId: "w_1",
|
||||
name: "Production",
|
||||
variables: [{ name: "E1", value: "E1!" }],
|
||||
},
|
||||
],
|
||||
workspaces: [
|
||||
{
|
||||
id: "w_1",
|
||||
variables: [{ name: "W1", value: "W1!" }],
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
expect(imported).toEqual(
|
||||
expect.objectContaining({
|
||||
resources: {
|
||||
workspaces: [
|
||||
{
|
||||
id: "w_1",
|
||||
},
|
||||
],
|
||||
environments: [
|
||||
{
|
||||
id: "e_1",
|
||||
workspaceId: "w_1",
|
||||
name: "Production",
|
||||
variables: [{ name: "E1", value: "E1!" }],
|
||||
parentModel: "environment",
|
||||
parentId: null,
|
||||
},
|
||||
{
|
||||
id: "GENERATE_ID::base_env_w_1",
|
||||
workspaceId: "w_1",
|
||||
name: "Global Variables",
|
||||
variables: [{ name: "W1", value: "W1!" }],
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
test("converts schema 4 to 5", () => {
|
||||
const imported = migrateImport(
|
||||
JSON.stringify({
|
||||
yaakSchema: 2,
|
||||
resources: {
|
||||
environments: [
|
||||
{
|
||||
id: "e_1",
|
||||
workspaceId: "w_1",
|
||||
base: false,
|
||||
name: "Production",
|
||||
variables: [{ name: "E1", value: "E1!" }],
|
||||
},
|
||||
{
|
||||
id: "e_1",
|
||||
workspaceId: "w_1",
|
||||
base: true,
|
||||
name: "Global Variables",
|
||||
variables: [{ name: "G1", value: "G1!" }],
|
||||
},
|
||||
],
|
||||
folders: [
|
||||
{
|
||||
id: "f_1",
|
||||
},
|
||||
],
|
||||
workspaces: [
|
||||
{
|
||||
id: "w_1",
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
expect(imported).toEqual(
|
||||
expect.objectContaining({
|
||||
resources: {
|
||||
workspaces: [
|
||||
{
|
||||
id: "w_1",
|
||||
},
|
||||
],
|
||||
folders: [
|
||||
{
|
||||
id: "f_1",
|
||||
},
|
||||
],
|
||||
environments: [
|
||||
{
|
||||
id: "e_1",
|
||||
workspaceId: "w_1",
|
||||
name: "Production",
|
||||
variables: [{ name: "E1", value: "E1!" }],
|
||||
parentModel: "environment",
|
||||
parentId: null,
|
||||
},
|
||||
{
|
||||
id: "e_1",
|
||||
workspaceId: "w_1",
|
||||
name: "Global Variables",
|
||||
parentModel: "workspace",
|
||||
parentId: null,
|
||||
variables: [{ name: "G1", value: "G1!" }],
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
test("Keys models by their Yaak ID, unchanged by a rename", () => {
|
||||
const exported = (requestName: string) =>
|
||||
JSON.stringify({
|
||||
yaakSchema: 5,
|
||||
resources: {
|
||||
workspaces: [{ id: "wk_1", model: "workspace", name: "Keys" }],
|
||||
httpRequests: [
|
||||
{
|
||||
id: "rq_1",
|
||||
model: "http_request",
|
||||
workspaceId: "wk_1",
|
||||
name: requestName,
|
||||
url: "https://yaak.app",
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
expect(migrateImport(exported("Original"))?.sourceKeys).toEqual({
|
||||
wk_1: "wk_1",
|
||||
rq_1: "rq_1",
|
||||
});
|
||||
expect(migrateImport(exported("Renamed"))?.sourceKeys).toEqual({
|
||||
wk_1: "wk_1",
|
||||
rq_1: "rq_1",
|
||||
});
|
||||
});
|
||||
});
|
||||
3
plugins/importer-yaak/tsconfig.json
Normal file
3
plugins/importer-yaak/tsconfig.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json"
|
||||
}
|
||||
Reference in New Issue
Block a user