Fix postman import and import Insomnia gRPC

This commit is contained in:
Gregory Schier
2024-03-18 08:18:04 -07:00
parent e47c2513a8
commit 8afe0c0755
12 changed files with 1861 additions and 83 deletions

View File

@@ -6,10 +6,14 @@ export function isRequestGroup(obj) {
return isJSObject(obj) && obj._type === 'request_group';
}
export function isRequest(obj) {
export function isHttpRequest(obj) {
return isJSObject(obj) && obj._type === 'request';
}
export function isGrpcRequest(obj) {
return isJSObject(obj) && obj._type === 'grpc_request';
}
export function isEnvironment(obj) {
return isJSObject(obj) && obj._type === 'environment';
}

View File

@@ -0,0 +1,37 @@
import { convertSyntax } from '../helpers/variables.js';
/**
* Import an Insomnia GRPC request object.
* @param {Object} r - The request object to import.
* @param workspaceId - The workspace ID to use for the request.
* @param {number} sortPriority - The sort priority to use for the request.
*/
export function importGrpcRequest(r, workspaceId, sortPriority = 0) {
console.log('IMPORTING GRPC REQUEST', r._id, r.name, JSON.stringify(r, null, 2));
const parts = r.protoMethodName.split('/').filter((p) => p !== '');
const service = parts[0] ?? null;
const method = parts[1] ?? null;
return {
id: r._id,
createdAt: new Date(r.created ?? Date.now()).toISOString().replace('Z', ''),
updatedAt: new Date(r.updated ?? Date.now()).toISOString().replace('Z', ''),
workspaceId,
folderId: r.parentId === workspaceId ? null : r.parentId,
model: 'grpc_request',
sortPriority,
name: r.name,
url: convertSyntax(r.url),
service,
method,
message: r.body?.text ?? '',
metadata: (r.metadata ?? [])
.map(({ name, value, disabled }) => ({
enabled: !disabled,
name,
value,
}))
.filter(({ name, value }) => name !== '' || value !== ''),
};
}

View File

@@ -6,7 +6,7 @@ import { convertSyntax } from '../helpers/variables.js';
* @param workspaceId - The workspace ID to use for the request.
* @param {number} sortPriority - The sort priority to use for the request.
*/
export function importRequest(r, workspaceId, sortPriority = 0) {
export function importHttpRequest(r, workspaceId, sortPriority = 0) {
console.log('IMPORTING REQUEST', r._id, r.name, JSON.stringify(r, null, 2));
let bodyType = null;

View File

@@ -1,17 +1,18 @@
import { importEnvironment } from './importers/environment.js';
import { importRequest } from './importers/request.js';
import { importEnvironment } from './importers/environment';
import { importHttpRequest } from './importers/httpRequest';
import {
isEnvironment,
isJSObject,
isRequest,
isHttpRequest,
isRequestGroup,
isWorkspace,
isGrpcRequest,
} from './helpers/types.js';
import { parseVariables } from './helpers/variables.js';
import { importFolder } from './importers/folder.js';
import { importGrpcRequest } from './importers/grpcRequest';
export function pluginHookImport(contents) {
console.log('RUNNING INSOMNIA');
let parsed;
try {
parsed = JSON.parse(contents);
@@ -24,7 +25,8 @@ export function pluginHookImport(contents) {
const resources = {
workspaces: [],
requests: [],
httpRequests: [],
grpcRequests: [],
environments: [],
folders: [],
};
@@ -57,8 +59,15 @@ export function pluginHookImport(contents) {
if (isRequestGroup(child)) {
resources.folders.push(importFolder(child, workspaceToImport._id));
nextFolder(child._id);
} else if (isRequest(child)) {
resources.requests.push(importRequest(child, workspaceToImport._id, sortPriority++));
} else if (isHttpRequest(child)) {
resources.httpRequests.push(
importHttpRequest(child, workspaceToImport._id, sortPriority++),
);
} else if (isGrpcRequest(child)) {
console.log('GRPC', JSON.stringify(child, null, 1));
resources.grpcRequests.push(
importGrpcRequest(child, workspaceToImport._id, sortPriority++),
);
}
}
};
@@ -68,7 +77,8 @@ export function pluginHookImport(contents) {
}
// Filter out any `null` values
resources.requests = resources.requests.filter(Boolean);
resources.httpRequests = resources.httpRequests.filter(Boolean);
resources.grpcRequests = resources.grpcRequests.filter(Boolean);
resources.environments = resources.environments.filter(Boolean);
resources.workspaces = resources.workspaces.filter(Boolean);

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,7 @@
{
"name": "importer-postman",
"version": "0.0.1"
"version": "0.0.1",
"devDependencies": {
"vitest": "^1.4.0"
}
}

View File

@@ -9,7 +9,7 @@ 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'>[];
requests: AtLeast<HttpRequest, 'name' | 'id' | 'model' | 'workspaceId'>[];
httpRequests: AtLeast<HttpRequest, 'name' | 'id' | 'model' | 'workspaceId'>[];
folders: AtLeast<Folder, 'name' | 'id' | 'model' | 'workspaceId'>[];
}
@@ -26,7 +26,7 @@ export function pluginHookImport(contents: string): { resources: ExportResources
const exportResources: ExportResources = {
workspaces: [],
environments: [],
requests: [],
httpRequests: [],
folders: [],
};
@@ -55,7 +55,7 @@ export function pluginHookImport(contents: string): { resources: ExportResources
const r = toRecord(v.request);
const bodyPatch = importBody(r.body);
const authPatch = importAuth(r.auth);
const request: ExportResources['requests'][0] = {
const request: ExportResources['httpRequests'][0] = {
model: 'http_request',
id: generateId('rq'),
workspaceId: workspace.id,
@@ -79,7 +79,7 @@ export function pluginHookImport(contents: string): { resources: ExportResources
}),
],
};
exportResources.requests.push(request);
exportResources.httpRequests.push(request);
} else {
console.log('Unknown item', v, folderId);
}
@@ -213,7 +213,7 @@ function convertTemplateSyntax<T>(obj: T): T {
}
}
function generateId(prefix: 'wk' | 'rq' | 'fl'): string {
export function generateId(prefix: 'wk' | 'rq' | 'fl'): string {
const alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
let id = `${prefix}_`;
for (let i = 0; i < 10; i++) {

View File

@@ -0,0 +1,164 @@
{
"info": {
"_postman_id": "9e6dfada-256c-49ea-a38f-7d1b05b7ca2d",
"name": "New Collection",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json",
"_exporter_id": "18798"
},
"item": [
{
"name": "New Folder",
"item": [
{
"name": "FOlder 2",
"item": [
{
"name": "Basic Auth",
"request": {
"auth": {
"type": "basic",
"basic": {
"password": "pass",
"username": "user"
}
},
"method": "GET",
"header": [],
"url": "https://schier.co"
},
"response": []
}
]
},
{
"name": "Form Data",
"request": {
"method": "POST",
"header": [
{
"key": "X-foo",
"value": "bar",
"description": "description",
"type": "text"
},
{
"key": "Disabled",
"value": "tnroant",
"description": "ntisorantosra",
"type": "text",
"disabled": true
}
],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "Form",
"value": "Value",
"description": "descirption",
"type": "text"
},
{
"key": "Disabled",
"value": "foo",
"description": "bar",
"type": "text",
"disabled": true
},
{
"key": "file",
"type": "file",
"src": "/Users/gschier/Desktop/foo.json"
},
{
"key": "Rendered",
"value": "{{Foo}}",
"type": "text"
}
]
},
"url": {
"raw": "schier.co?firstkey=firstvalue&hi=there",
"host": [
"schier",
"co"
],
"query": [
{
"key": "disabled",
"value": "secondvalue",
"description": "this is disabled",
"disabled": true
},
{
"key": "firstkey",
"value": "firstvalue"
},
{
"key": "hi",
"value": "there"
}
]
}
},
"response": []
}
]
},
{
"name": "Form URL",
"request": {
"method": "POST",
"header": [
{
"key": "X-foo",
"value": "bar",
"description": "description",
"type": "text"
},
{
"key": "Disabled",
"value": "tnroant",
"description": "ntisorantosra",
"type": "text",
"disabled": true
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "foo",
"value": "bar",
"type": "text"
}
]
},
"url": {
"raw": "schier.co?firstkey=firstvalue&hi=there",
"host": [
"schier",
"co"
],
"query": [
{
"key": "disabled",
"value": "secondvalue",
"description": "this is disabled",
"disabled": true
},
{
"key": "firstkey",
"value": "firstvalue"
},
{
"key": "hi",
"value": "there"
}
]
}
},
"response": []
}
]
}

View File

@@ -0,0 +1,35 @@
import * as fs from 'node:fs';
import * as path from 'node:path';
import { expect, test, describe, beforeEach, afterEach, vi } from 'vitest';
import { pluginHookImport } from '../src';
let originalRandom = Math.random;
describe('importer-postman', () => {
beforeEach(() => {
let i = 0;
const mocked = vi.fn(() => ((i++ * 1000) % 133) / 100);
Math.random = mocked;
});
afterEach(() => {
Math.random = originalRandom;
});
const p = path.join(__dirname, 'fixtures');
const fixtures = fs.readdirSync(p);
console.log('FIXTURES', fixtures);
for (const fixture of fixtures) {
test('Imports ' + fixture, () => {
const contents = fs.readFileSync(path.join(p, fixture), 'utf-8');
const imported = pluginHookImport(contents);
expect(imported).toEqual({
resources: {
environments: [],
requests: [],
},
});
});
}
});