mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-25 02:41:07 +01:00
OpenAPI import plugins
This commit is contained in:
29
plugins/importer-openapi/src/index.ts
Normal file
29
plugins/importer-openapi/src/index.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { convert } from 'openapi-to-postmanv2';
|
||||
import { pluginHookImport as pluginHookImportPostman } from '../../importer-postman/build';
|
||||
import { Folder, HttpRequest, Workspace, Environment } from '../../../types/models';
|
||||
|
||||
type AtLeast<T, K extends keyof T> = Partial<T> & Pick<T, K>;
|
||||
|
||||
interface ExportResources {
|
||||
workspaces: AtLeast<Workspace, 'name' | 'id' | 'model'>[];
|
||||
environments: AtLeast<Environment, 'name' | 'id' | 'model' | 'workspaceId'>[];
|
||||
httpRequests: AtLeast<HttpRequest, 'name' | 'id' | 'model' | 'workspaceId'>[];
|
||||
folders: AtLeast<Folder, 'name' | 'id' | 'model' | 'workspaceId'>[];
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (Array.isArray(result.output) && result.output.length > 0) {
|
||||
resolve(JSON.stringify(result.output[0].data));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return pluginHookImportPostman(ctx, result);
|
||||
}
|
||||
Reference in New Issue
Block a user