mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-31 06:23:08 +02:00
Improved Postman and OpenAPI import
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "importer-postman",
|
||||
"name": "importer-openapi",
|
||||
"version": "0.0.1",
|
||||
"scripts": {
|
||||
"build": "yaakcli ./src/index.js"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { convert } from 'openapi-to-postmanv2';
|
||||
import { pluginHookImport as pluginHookImportPostman } from '../../importer-postman/build';
|
||||
import { pluginHookImport as pluginHookImportPostman } from '../../importer-postman';
|
||||
import { Folder, HttpRequest, Workspace, Environment } from '../../../types/models';
|
||||
|
||||
type AtLeast<T, K extends keyof T> = Partial<T> & Pick<T, K>;
|
||||
@@ -15,15 +15,21 @@ export async function pluginHookImport(
|
||||
ctx: any,
|
||||
contents: string,
|
||||
): Promise<{ resources: ExportResources } | undefined> {
|
||||
const result = await new Promise((resolve, reject) => {
|
||||
convert({ type: 'string', data: contents }, {}, (err, result) => {
|
||||
if (err != null) reject(err);
|
||||
let postmanCollection;
|
||||
try {
|
||||
postmanCollection = await new Promise((resolve, reject) => {
|
||||
convert({ type: 'string', data: contents }, {}, (err, result) => {
|
||||
if (err != null) reject(err);
|
||||
|
||||
if (Array.isArray(result.output) && result.output.length > 0) {
|
||||
resolve(JSON.stringify(result.output[0].data));
|
||||
}
|
||||
if (Array.isArray(result.output) && result.output.length > 0) {
|
||||
resolve(result.output[0].data);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
} catch (err) {
|
||||
// Probably not an OpenAPI file, so skip it
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return pluginHookImportPostman(ctx, result);
|
||||
return pluginHookImportPostman(ctx, JSON.stringify(postmanCollection));
|
||||
}
|
||||
|
||||
@@ -7,6 +7,11 @@ describe('importer-openapi', () => {
|
||||
const p = path.join(__dirname, 'fixtures');
|
||||
const fixtures = fs.readdirSync(p);
|
||||
|
||||
test('Skips invalid file', async () => {
|
||||
const imported = await pluginHookImport({}, '{}');
|
||||
expect(imported).toBeUndefined();
|
||||
})
|
||||
|
||||
for (const fixture of fixtures) {
|
||||
test('Imports ' + fixture, async () => {
|
||||
const contents = fs.readFileSync(path.join(p, fixture), 'utf-8');
|
||||
@@ -14,6 +19,7 @@ describe('importer-openapi', () => {
|
||||
expect(imported?.resources.workspaces).toEqual([
|
||||
expect.objectContaining({
|
||||
name: 'Swagger Petstore - OpenAPI 3.0',
|
||||
description: expect.stringContaining('This is a sample Pet Store Server'),
|
||||
}),
|
||||
]);
|
||||
expect(imported?.resources.httpRequests.length).toBe(19);
|
||||
|
||||
Reference in New Issue
Block a user