Update plugins

This commit is contained in:
Gregory Schier
2025-01-20 13:07:04 -08:00
parent 26cce077bb
commit d142966d0c
16 changed files with 855 additions and 504 deletions

1096
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -19,6 +19,6 @@
"workspaces-run": "^1.0.2" "workspaces-run": "^1.0.2"
}, },
"dependencies": { "dependencies": {
"@yaakapp/api": "^0.3.2" "@yaakapp/api": "^0.3.4"
} }
} }

View File

@@ -1,4 +1,4 @@
import { Context, HttpRequest, PluginDefinition } from '@yaakapp/api'; import { HttpRequest, PluginDefinition } from '@yaakapp/api';
const NEWLINE = '\\\n '; const NEWLINE = '\\\n ';
@@ -9,14 +9,14 @@ export const plugin: PluginDefinition = {
icon: 'copy', icon: 'copy',
async onSelect(ctx, args) { async onSelect(ctx, args) {
const rendered_request = await ctx.httpRequest.render({ httpRequest: args.httpRequest, purpose: 'preview' }); const rendered_request = await ctx.httpRequest.render({ httpRequest: args.httpRequest, purpose: 'preview' });
const data = await pluginHookExport(ctx, rendered_request); const data = await convertToCurl(rendered_request);
ctx.clipboard.copyText(data); ctx.clipboard.copyText(data);
ctx.toast.show({ message: 'Curl copied to clipboard', icon: 'copy' }); ctx.toast.show({ message: 'Curl copied to clipboard', icon: 'copy' });
}, },
}], }],
}; };
export async function pluginHookExport(_ctx: Context, request: Partial<HttpRequest>) { export async function convertToCurl(request: Partial<HttpRequest>) {
const xs = ['curl']; const xs = ['curl'];
// Add method and URL all on first line // Add method and URL all on first line

View File

@@ -1,13 +1,10 @@
import { describe, expect, test } from 'vitest'; import { describe, expect, test } from 'vitest';
import { Context } from '@yaakapp/api'; import { convertToCurl } from '../src';
import { pluginHookExport } from '../src';
const ctx = {} as Context;
describe('exporter-curl', () => { describe('exporter-curl', () => {
test('Exports GET with params', async () => { test('Exports GET with params', async () => {
expect( expect(
await pluginHookExport(ctx, { await convertToCurl({
url: 'https://yaak.app', url: 'https://yaak.app',
urlParameters: [ urlParameters: [
{ name: 'a', value: 'aaa' }, { name: 'a', value: 'aaa' },
@@ -21,7 +18,7 @@ describe('exporter-curl', () => {
}); });
test('Exports POST with url form data', async () => { test('Exports POST with url form data', async () => {
expect( expect(
await pluginHookExport(ctx, { await convertToCurl({
url: 'https://yaak.app', url: 'https://yaak.app',
method: 'POST', method: 'POST',
bodyType: 'application/x-www-form-urlencoded', bodyType: 'application/x-www-form-urlencoded',
@@ -40,12 +37,12 @@ describe('exporter-curl', () => {
test('Exports POST with GraphQL data', async () => { test('Exports POST with GraphQL data', async () => {
expect( expect(
await pluginHookExport(ctx, { await convertToCurl({
url: 'https://yaak.app', url: 'https://yaak.app',
method: 'POST', method: 'POST',
bodyType: 'graphql', bodyType: 'graphql',
body: { body: {
query : '{foo,bar}', query: '{foo,bar}',
variables: '{"a": "aaa", "b": "bbb"}', variables: '{"a": "aaa", "b": "bbb"}',
}, },
}), }),
@@ -56,12 +53,12 @@ describe('exporter-curl', () => {
test('Exports POST with GraphQL data no variables', async () => { test('Exports POST with GraphQL data no variables', async () => {
expect( expect(
await pluginHookExport(ctx, { await convertToCurl({
url: 'https://yaak.app', url: 'https://yaak.app',
method: 'POST', method: 'POST',
bodyType: 'graphql', bodyType: 'graphql',
body: { body: {
query : '{foo,bar}', query: '{foo,bar}',
}, },
}), }),
).toEqual( ).toEqual(
@@ -71,7 +68,7 @@ describe('exporter-curl', () => {
test('Exports PUT with multipart form', async () => { test('Exports PUT with multipart form', async () => {
expect( expect(
await pluginHookExport(ctx, { await convertToCurl({
url: 'https://yaak.app', url: 'https://yaak.app',
method: 'PUT', method: 'PUT',
bodyType: 'multipart/form-data', bodyType: 'multipart/form-data',
@@ -96,7 +93,7 @@ describe('exporter-curl', () => {
test('Exports JSON body', async () => { test('Exports JSON body', async () => {
expect( expect(
await pluginHookExport(ctx, { await convertToCurl({
url: 'https://yaak.app', url: 'https://yaak.app',
method: 'POST', method: 'POST',
bodyType: 'application/json', bodyType: 'application/json',
@@ -116,7 +113,7 @@ describe('exporter-curl', () => {
test('Exports multi-line JSON body', async () => { test('Exports multi-line JSON body', async () => {
expect( expect(
await pluginHookExport(ctx, { await convertToCurl({
url: 'https://yaak.app', url: 'https://yaak.app',
method: 'POST', method: 'POST',
bodyType: 'application/json', bodyType: 'application/json',
@@ -136,7 +133,7 @@ describe('exporter-curl', () => {
test('Exports headers', async () => { test('Exports headers', async () => {
expect( expect(
await pluginHookExport(ctx, { await convertToCurl({
headers: [ headers: [
{ name: 'a', value: 'aaa' }, { name: 'a', value: 'aaa' },
{ name: 'b', value: 'bbb', enabled: true }, { name: 'b', value: 'bbb', enabled: true },
@@ -148,7 +145,7 @@ describe('exporter-curl', () => {
test('Basic auth', async () => { test('Basic auth', async () => {
expect( expect(
await pluginHookExport(ctx, { await convertToCurl({
url: 'https://yaak.app', url: 'https://yaak.app',
authenticationType: 'basic', authenticationType: 'basic',
authentication: { authentication: {
@@ -161,7 +158,7 @@ describe('exporter-curl', () => {
test('Broken basic auth', async () => { test('Broken basic auth', async () => {
expect( expect(
await pluginHookExport(ctx, { await convertToCurl({
url: 'https://yaak.app', url: 'https://yaak.app',
authenticationType: 'basic', authenticationType: 'basic',
authentication: {}, authentication: {},
@@ -171,7 +168,7 @@ describe('exporter-curl', () => {
test('Digest auth', async () => { test('Digest auth', async () => {
expect( expect(
await pluginHookExport(ctx, { await convertToCurl({
url: 'https://yaak.app', url: 'https://yaak.app',
authenticationType: 'digest', authenticationType: 'digest',
authentication: { authentication: {
@@ -184,7 +181,7 @@ describe('exporter-curl', () => {
test('Bearer auth', async () => { test('Bearer auth', async () => {
expect( expect(
await pluginHookExport(ctx, { await convertToCurl({
url: 'https://yaak.app', url: 'https://yaak.app',
authenticationType: 'bearer', authenticationType: 'bearer',
authentication: { authentication: {
@@ -196,7 +193,7 @@ describe('exporter-curl', () => {
test('Broken bearer auth', async () => { test('Broken bearer auth', async () => {
expect( expect(
await pluginHookExport(ctx, { await convertToCurl({
url: 'https://yaak.app', url: 'https://yaak.app',
authenticationType: 'bearer', authenticationType: 'bearer',
authentication: { authentication: {

View File

@@ -1,8 +1,14 @@
import { Context } from '@yaakapp/api'; import { PluginDefinition } from '@yaakapp/api';
import { JSONPath } from 'jsonpath-plus'; import { JSONPath } from 'jsonpath-plus';
export function pluginHookResponseFilter(_ctx: Context, args: { filter: string; body: string }) { export const plugin: PluginDefinition = {
const parsed = JSON.parse(args.body); filter: {
const filtered = JSONPath({ path: args.filter, json: parsed }); name: 'JSONPath',
return JSON.stringify(filtered, null, 2); description: 'Filter JSONPath',
} onFilter(_ctx, args) {
const parsed = JSON.parse(args.payload);
const filtered = JSONPath({ path: args.filter, json: parsed });
return { filtered: JSON.stringify(filtered, null, 2) };
},
},
};

View File

@@ -1,17 +1,21 @@
import { DOMParser } from '@xmldom/xmldom'; import { DOMParser } from '@xmldom/xmldom';
import { Context } from '@yaakapp/api'; import { PluginDefinition } from '@yaakapp/api';
import xpath from 'xpath'; import xpath from 'xpath';
export function pluginHookResponseFilter( export const plugin: PluginDefinition = {
_ctx: Context, filter: {
{ filter, body }: { filter: string; body: string }, name: 'XPath',
) { description: 'Filter XPath',
const doc = new DOMParser().parseFromString(body, 'text/xml'); onFilter(_ctx, args) {
const result = xpath.select(filter, doc, false); const doc = new DOMParser().parseFromString(args.payload, 'text/xml');
if (Array.isArray(result)) { const result = xpath.select(args.filter, doc, false);
return result.map(r => String(r)).join('\n');
} else { if (Array.isArray(result)) {
// Not sure what cases this happens in (?) return { filtered: result.map(r => String(r)).join('\n') };
return String(result); } else {
} // Not sure what cases this happens in (?)
} return { filtered: String(result) };
}
},
},
};

View File

@@ -1,4 +1,4 @@
import { Context, Environment, Folder, HttpRequest, HttpUrlParameter, Workspace } from '@yaakapp/api'; import { Context, Environment, Folder, HttpRequest, HttpUrlParameter, PluginDefinition, Workspace } from '@yaakapp/api';
import { ControlOperator, parse, ParseEntry } from 'shell-quote'; import { ControlOperator, parse, ParseEntry } from 'shell-quote';
type AtLeast<T, K extends keyof T> = Partial<T> & Pick<T, K>; type AtLeast<T, K extends keyof T> = Partial<T> & Pick<T, K>;
@@ -35,7 +35,17 @@ type FlagValue = string | boolean;
type FlagsByName = Record<string, FlagValue[]>; type FlagsByName = Record<string, FlagValue[]>;
export function pluginHookImport(_ctx: Context, rawData: string) { export const plugin: PluginDefinition = {
importer: {
name: 'cURL',
description: 'Import cURL commands',
onImport(_ctx: Context, args: { text: string }) {
return convertCurl(args.text) as any;
},
},
};
export function convertCurl(rawData: string) {
if (!rawData.match(/^\s*curl /)) { if (!rawData.match(/^\s*curl /)) {
return null; return null;
} }

View File

@@ -1,12 +1,10 @@
import { Context, HttpRequest, Workspace } from '@yaakapp/api'; import { HttpRequest, Workspace } from '@yaakapp/api';
import { describe, expect, test } from 'vitest'; import { describe, expect, test } from 'vitest';
import { pluginHookImport } from '../src'; import { convertCurl } from '../src';
const ctx = {} as Context;
describe('importer-curl', () => { describe('importer-curl', () => {
test('Imports basic GET', () => { test('Imports basic GET', () => {
expect(pluginHookImport(ctx, 'curl https://yaak.app')).toEqual({ expect(convertCurl('curl https://yaak.app')).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
httpRequests: [ httpRequests: [
@@ -19,7 +17,7 @@ describe('importer-curl', () => {
}); });
test('Explicit URL', () => { test('Explicit URL', () => {
expect(pluginHookImport(ctx, 'curl --url https://yaak.app')).toEqual({ expect(convertCurl('curl --url https://yaak.app')).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
httpRequests: [ httpRequests: [
@@ -32,7 +30,7 @@ describe('importer-curl', () => {
}); });
test('Missing URL', () => { test('Missing URL', () => {
expect(pluginHookImport(ctx, 'curl -X POST')).toEqual({ expect(convertCurl('curl -X POST')).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
httpRequests: [ httpRequests: [
@@ -45,7 +43,7 @@ describe('importer-curl', () => {
}); });
test('URL between', () => { test('URL between', () => {
expect(pluginHookImport(ctx, 'curl -v https://yaak.app -X POST')).toEqual({ expect(convertCurl('curl -v https://yaak.app -X POST')).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
httpRequests: [ httpRequests: [
@@ -59,7 +57,7 @@ describe('importer-curl', () => {
}); });
test('Random flags', () => { test('Random flags', () => {
expect(pluginHookImport(ctx, 'curl --random -Z -Y -S --foo https://yaak.app')).toEqual({ expect(convertCurl('curl --random -Z -Y -S --foo https://yaak.app')).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
httpRequests: [ httpRequests: [
@@ -72,7 +70,7 @@ describe('importer-curl', () => {
}); });
test('Imports --request method', () => { test('Imports --request method', () => {
expect(pluginHookImport(ctx, 'curl --request POST https://yaak.app')).toEqual({ expect(convertCurl('curl --request POST https://yaak.app')).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
httpRequests: [ httpRequests: [
@@ -86,7 +84,7 @@ describe('importer-curl', () => {
}); });
test('Imports -XPOST method', () => { test('Imports -XPOST method', () => {
expect(pluginHookImport(ctx, 'curl -XPOST --request POST https://yaak.app')).toEqual({ expect(convertCurl('curl -XPOST --request POST https://yaak.app')).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
httpRequests: [ httpRequests: [
@@ -101,10 +99,7 @@ describe('importer-curl', () => {
test('Imports multiple requests', () => { test('Imports multiple requests', () => {
expect( expect(
pluginHookImport( convertCurl('curl \\\n https://yaak.app\necho "foo"\ncurl example.com;curl foo.com'),
ctx,
'curl \\\n https://yaak.app\necho "foo"\ncurl example.com;curl foo.com',
),
).toEqual({ ).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
@@ -119,7 +114,7 @@ describe('importer-curl', () => {
test('Imports form data', () => { test('Imports form data', () => {
expect( expect(
pluginHookImport(ctx, 'curl -X POST -F "a=aaa" -F b=bbb" -F f=@filepath https://yaak.app'), convertCurl('curl -X POST -F "a=aaa" -F b=bbb" -F f=@filepath https://yaak.app'),
).toEqual({ ).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
@@ -149,7 +144,7 @@ describe('importer-curl', () => {
}); });
test('Imports data params as form url-encoded', () => { test('Imports data params as form url-encoded', () => {
expect(pluginHookImport(ctx, 'curl -d a -d b -d c=ccc https://yaak.app')).toEqual({ expect(convertCurl('curl -d a -d b -d c=ccc https://yaak.app')).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
httpRequests: [ httpRequests: [
@@ -179,7 +174,7 @@ describe('importer-curl', () => {
test('Imports data params as text', () => { test('Imports data params as text', () => {
expect( expect(
pluginHookImport(ctx, 'curl -H Content-Type:text/plain -d a -d b -d c=ccc https://yaak.app'), convertCurl('curl -H Content-Type:text/plain -d a -d b -d c=ccc https://yaak.app'),
).toEqual({ ).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
@@ -198,7 +193,7 @@ describe('importer-curl', () => {
test('Imports post data into URL', () => { test('Imports post data into URL', () => {
expect( expect(
pluginHookImport(ctx, 'curl -G https://api.stripe.com/v1/payment_links -d limit=3'), convertCurl('curl -G https://api.stripe.com/v1/payment_links -d limit=3'),
).toEqual({ ).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
@@ -210,7 +205,7 @@ describe('importer-curl', () => {
enabled: true, enabled: true,
name: 'limit', name: 'limit',
value: '3', value: '3',
}] }],
}), }),
], ],
}, },
@@ -219,10 +214,7 @@ describe('importer-curl', () => {
test('Imports multi-line JSON', () => { test('Imports multi-line JSON', () => {
expect( expect(
pluginHookImport( convertCurl(`curl -H Content-Type:application/json -d $'{\n "foo":"bar"\n}' https://yaak.app`),
ctx,
`curl -H Content-Type:application/json -d $'{\n "foo":"bar"\n}' https://yaak.app`,
),
).toEqual({ ).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
@@ -241,7 +233,7 @@ describe('importer-curl', () => {
test('Imports multiple headers', () => { test('Imports multiple headers', () => {
expect( expect(
pluginHookImport(ctx, 'curl -H Foo:bar --header Name -H AAA:bbb -H :ccc https://yaak.app'), convertCurl('curl -H Foo:bar --header Name -H AAA:bbb -H :ccc https://yaak.app'),
).toEqual({ ).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
@@ -261,7 +253,7 @@ describe('importer-curl', () => {
}); });
test('Imports basic auth', () => { test('Imports basic auth', () => {
expect(pluginHookImport(ctx, 'curl --user user:pass https://yaak.app')).toEqual({ expect(convertCurl('curl --user user:pass https://yaak.app')).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
httpRequests: [ httpRequests: [
@@ -279,7 +271,7 @@ describe('importer-curl', () => {
}); });
test('Imports digest auth', () => { test('Imports digest auth', () => {
expect(pluginHookImport(ctx, 'curl --digest --user user:pass https://yaak.app')).toEqual({ expect(convertCurl('curl --digest --user user:pass https://yaak.app')).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
httpRequests: [ httpRequests: [
@@ -297,7 +289,7 @@ describe('importer-curl', () => {
}); });
test('Imports cookie as header', () => { test('Imports cookie as header', () => {
expect(pluginHookImport(ctx, 'curl --cookie "foo=bar" https://yaak.app')).toEqual({ expect(convertCurl('curl --cookie "foo=bar" https://yaak.app')).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
httpRequests: [ httpRequests: [
@@ -311,7 +303,7 @@ describe('importer-curl', () => {
}); });
test('Imports query params', () => { test('Imports query params', () => {
expect(pluginHookImport(ctx, 'curl "https://yaak.app" --url-query foo=bar --url-query baz=qux')).toEqual({ expect(convertCurl('curl "https://yaak.app" --url-query foo=bar --url-query baz=qux')).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
httpRequests: [ httpRequests: [
@@ -328,7 +320,7 @@ describe('importer-curl', () => {
}); });
test('Imports query params from the URL', () => { test('Imports query params from the URL', () => {
expect(pluginHookImport(ctx, 'curl "https://yaak.app?foo=bar&baz=a%20a"')).toEqual({ expect(convertCurl('curl "https://yaak.app?foo=bar&baz=a%20a"')).toEqual({
resources: { resources: {
workspaces: [baseWorkspace()], workspaces: [baseWorkspace()],
httpRequests: [ httpRequests: [

View File

@@ -1,4 +1,4 @@
import { Context, Environment, Folder, GrpcRequest, HttpRequest, Workspace } from '@yaakapp/api'; import { Context, Environment, Folder, GrpcRequest, HttpRequest, PluginDefinition, Workspace } from '@yaakapp/api';
import YAML from 'yaml'; import YAML from 'yaml';
type AtLeast<T, K extends keyof T> = Partial<T> & Pick<T, K>; type AtLeast<T, K extends keyof T> = Partial<T> & Pick<T, K>;
@@ -11,7 +11,17 @@ export interface ExportResources {
folders: AtLeast<Folder, 'name' | 'id' | 'model' | 'workspaceId'>[]; folders: AtLeast<Folder, 'name' | 'id' | 'model' | 'workspaceId'>[];
} }
export function pluginHookImport(ctx: Context, contents: string) { export const plugin: PluginDefinition = {
importer: {
name: 'Insomnia',
description: 'Import Insomnia workspaces',
onImport(_ctx: Context, args: { text: string }) {
return convertInsomnia(args.text) as any;
},
},
};
export function convertInsomnia(contents: string) {
let parsed: any; let parsed: any;
try { try {

View File

@@ -1,10 +1,7 @@
import { Context } from '@yaakapp/api';
import * as fs from 'node:fs'; import * as fs from 'node:fs';
import * as path from 'node:path'; import * as path from 'node:path';
import { describe, expect, test } from 'vitest'; import { describe, expect, test } from 'vitest';
import { pluginHookImport } from '../src'; import { convertInsomnia } from '../src';
const ctx = {} as Context;
describe('importer-yaak', () => { describe('importer-yaak', () => {
const p = path.join(__dirname, 'fixtures'); const p = path.join(__dirname, 'fixtures');
@@ -18,7 +15,7 @@ describe('importer-yaak', () => {
test('Imports ' + fixture, () => { test('Imports ' + fixture, () => {
const contents = fs.readFileSync(path.join(p, fixture), 'utf-8'); const contents = fs.readFileSync(path.join(p, fixture), 'utf-8');
const expected = fs.readFileSync(path.join(p, fixture.replace('.input', '.output')), 'utf-8'); const expected = fs.readFileSync(path.join(p, fixture.replace('.input', '.output')), 'utf-8');
const result = pluginHookImport(ctx, contents); const result = convertInsomnia(contents);
// console.log(JSON.stringify(result, null, 2)) // console.log(JSON.stringify(result, null, 2))
expect(result).toEqual(JSON.parse(expected)); expect(result).toEqual(JSON.parse(expected));
}); });

View File

@@ -1,7 +1,6 @@
import { Context } from '@yaakapp/api'; import { Context, Environment, Folder, HttpRequest, PluginDefinition, Workspace } from '@yaakapp/api';
import { convert } from 'openapi-to-postmanv2'; import { convert } from 'openapi-to-postmanv2';
import { pluginHookImport as pluginHookImportPostman } from '../../importer-postman/src/index'; import { convertPostman } from '@yaakapp/importer-postman/src';
import { Folder, HttpRequest, Workspace, Environment } from '@yaakapp/api';
type AtLeast<T, K extends keyof T> = Partial<T> & Pick<T, K>; type AtLeast<T, K extends keyof T> = Partial<T> & Pick<T, K>;
@@ -12,8 +11,17 @@ interface ExportResources {
folders: AtLeast<Folder, 'name' | 'id' | 'model' | 'workspaceId'>[]; folders: AtLeast<Folder, 'name' | 'id' | 'model' | 'workspaceId'>[];
} }
export async function pluginHookImport( export const plugin: PluginDefinition = {
ctx: Context, importer: {
name: 'OpenAPI',
description: 'Import OpenAPI collections',
onImport(_ctx: Context, args: { text: string }) {
return convertOpenApi(args.text) as any;
},
},
};
export async function convertOpenApi(
contents: string, contents: string,
): Promise<{ resources: ExportResources } | undefined> { ): Promise<{ resources: ExportResources } | undefined> {
let postmanCollection; let postmanCollection;
@@ -32,5 +40,5 @@ export async function pluginHookImport(
return undefined; return undefined;
} }
return pluginHookImportPostman(ctx, JSON.stringify(postmanCollection)); return convertPostman(JSON.stringify(postmanCollection));
} }

View File

@@ -1,24 +1,21 @@
import { Context } from '@yaakapp/api';
import * as fs from 'node:fs'; import * as fs from 'node:fs';
import * as path from 'node:path'; import * as path from 'node:path';
import { describe, expect, test } from 'vitest'; import { describe, expect, test } from 'vitest';
import { pluginHookImport } from '../src'; import { convertOpenApi } from '../src';
const ctx = {} as Context;
describe('importer-openapi', () => { describe('importer-openapi', () => {
const p = path.join(__dirname, 'fixtures'); const p = path.join(__dirname, 'fixtures');
const fixtures = fs.readdirSync(p); const fixtures = fs.readdirSync(p);
test('Skips invalid file', async () => { test('Skips invalid file', async () => {
const imported = await pluginHookImport(ctx, '{}'); const imported = await convertOpenApi('{}');
expect(imported).toBeUndefined(); expect(imported).toBeUndefined();
}) });
for (const fixture of fixtures) { for (const fixture of fixtures) {
test('Imports ' + fixture, async () => { test('Imports ' + fixture, async () => {
const contents = fs.readFileSync(path.join(p, fixture), 'utf-8'); const contents = fs.readFileSync(path.join(p, fixture), 'utf-8');
const imported = await pluginHookImport(ctx, contents); const imported = await convertOpenApi(contents);
expect(imported?.resources.workspaces).toEqual([ expect(imported?.resources.workspaces).toEqual([
expect.objectContaining({ expect.objectContaining({
name: 'Swagger Petstore - OpenAPI 3.0', name: 'Swagger Petstore - OpenAPI 3.0',

View File

@@ -5,6 +5,7 @@ import {
HttpRequest, HttpRequest,
HttpRequestHeader, HttpRequestHeader,
HttpUrlParameter, HttpUrlParameter,
PluginDefinition,
Workspace, Workspace,
} from '@yaakapp/api'; } from '@yaakapp/api';
@@ -21,8 +22,17 @@ interface ExportResources {
folders: AtLeast<Folder, 'name' | 'id' | 'model' | 'workspaceId'>[]; folders: AtLeast<Folder, 'name' | 'id' | 'model' | 'workspaceId'>[];
} }
export function pluginHookImport( export const plugin: PluginDefinition = {
_ctx: Context, importer: {
name: 'Postman',
description: 'Import postman collections',
onImport(_ctx: Context, args: { text: string }) {
return convertPostman(args.text) as any;
},
},
};
export function convertPostman(
contents: string, contents: string,
): { resources: ExportResources } | undefined { ): { resources: ExportResources } | undefined {
const root = parseJSONToRecord(contents); const root = parseJSONToRecord(contents);

View File

@@ -1,10 +1,7 @@
import { Context } from '@yaakapp/api';
import * as fs from 'node:fs'; import * as fs from 'node:fs';
import * as path from 'node:path'; import * as path from 'node:path';
import { describe, expect, test } from 'vitest'; import { describe, expect, test } from 'vitest';
import { pluginHookImport } from '../src'; import { convertPostman } from '../src';
const ctx = {} as Context;
describe('importer-postman', () => { describe('importer-postman', () => {
const p = path.join(__dirname, 'fixtures'); const p = path.join(__dirname, 'fixtures');
@@ -18,7 +15,7 @@ describe('importer-postman', () => {
test('Imports ' + fixture, () => { test('Imports ' + fixture, () => {
const contents = fs.readFileSync(path.join(p, fixture), 'utf-8'); const contents = fs.readFileSync(path.join(p, fixture), 'utf-8');
const expected = fs.readFileSync(path.join(p, fixture.replace('.input', '.output')), 'utf-8'); const expected = fs.readFileSync(path.join(p, fixture.replace('.input', '.output')), 'utf-8');
const result = pluginHookImport(ctx, contents); const result = convertPostman(contents);
// console.log(JSON.stringify(result, null, 2)) // console.log(JSON.stringify(result, null, 2))
expect(result).toEqual(JSON.parse(expected)); expect(result).toEqual(JSON.parse(expected));
}); });

View File

@@ -1,6 +1,16 @@
import { Context, Environment } from '@yaakapp/api'; import { Environment, PluginDefinition } from '@yaakapp/api';
export function pluginHookImport(_ctx: Context, contents: string) { export const plugin: PluginDefinition = {
importer: {
name: 'Yaak',
description: 'Yaak official format',
onImport(_ctx, args) {
return migrateImport(args.text) as any;
},
},
};
export function migrateImport(contents: string) {
let parsed; let parsed;
try { try {
parsed = JSON.parse(contents); parsed = JSON.parse(contents);

View File

@@ -1,19 +1,15 @@
import { Context } from '@yaakapp/api';
import { describe, expect, test } from 'vitest'; import { describe, expect, test } from 'vitest';
import { pluginHookImport } from '../src'; import { migrateImport } from '../src';
const ctx = {} as Context;
describe('importer-yaak', () => { describe('importer-yaak', () => {
test('Skips invalid imports', () => { test('Skips invalid imports', () => {
expect(pluginHookImport(ctx, 'not JSON')).toBeUndefined(); expect(migrateImport('not JSON')).toBeUndefined();
expect(pluginHookImport(ctx, '[]')).toBeUndefined(); expect(migrateImport('[]')).toBeUndefined();
expect(pluginHookImport(ctx, JSON.stringify({ resources: {} }))).toBeUndefined(); expect(migrateImport(JSON.stringify({ resources: {} }))).toBeUndefined();
}); });
test('converts schema 1 to 2', () => { test('converts schema 1 to 2', () => {
const imported = pluginHookImport( const imported = migrateImport(
ctx,
JSON.stringify({ JSON.stringify({
yaakSchema: 1, yaakSchema: 1,
resources: { resources: {
@@ -31,8 +27,7 @@ describe('importer-yaak', () => {
); );
}); });
test('converts schema 2 to 3', () => { test('converts schema 2 to 3', () => {
const imported = pluginHookImport( const imported = migrateImport(
ctx,
JSON.stringify({ JSON.stringify({
yaakSchema: 2, yaakSchema: 2,
resources: { resources: {