insomnia/packages/insomnia-app/app/common/send-request.js
Gregory Schier 45861b6393
POC to get Insomnia networking available outside of app (#2329)
* Hacked insomnia lib package to get Insomnia's networking in tests

* Some small tweaks

* Got it mostly working

* Tweak

* Fix

* Fix pkg

* Fix some things

* Add comment

* Comment out log

* Some tweaks after doing a self code review

* Fix test

* Update app name for `insomnia-send-request`

* Update package-lock

* Update package-locks

* Fix tests

* Add gitignore

* Fix tests hopefully

* Fix accidental dep deletions

* Fix tests again

* Mock for all tests

* Update tests again

* Don't compile for Electron for tests

* Update release scripts to disable tests
2020-06-30 12:36:15 -07:00

38 lines
1.1 KiB
JavaScript

import * as db from './database';
import { types as modelTypes } from '../models';
import { send } from '../network/network';
import { getBodyBuffer } from '../models/response';
export async function getSendRequestCallback(environmentId, memDB) {
// Initialize the DB in-memory and fill it with data
await db.init(modelTypes(), { inMemoryOnly: true }, true);
const docs = [];
for (const type of Object.keys(memDB)) {
for (const doc of memDB[type]) {
docs.push(doc);
}
}
await db.batchModifyDocs({ upsert: docs, remove: [] });
// Return callback helper to send requests
return async function sendRequest(requestId) {
const res = await send(requestId, environmentId);
const headersObj = {};
for (const h of res.headers || []) {
const name = h.name || '';
headersObj[name.toLowerCase()] = h.value || '';
}
const bodyBuffer = await getBodyBuffer(res);
return {
status: res.statusCode,
statusMessage: res.statusMessage,
data: bodyBuffer ? bodyBuffer.toString('utf8') : undefined,
headers: headersObj,
};
};
}