From bab945a6a10731dd848537e5b87fd46995540feb Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Tue, 30 Jun 2020 15:40:15 -0700 Subject: [PATCH] Alternate send request callback for app to use (#2334) --- .../insomnia-app/app/common/send-request.js | 44 ++++++++++++------- packages/insomnia-app/send-request/index.js | 2 +- .../flow-typed/insomnia-send-request.js | 3 +- .../insomnia-cli/src/commands/run-tests.js | 4 +- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/packages/insomnia-app/app/common/send-request.js b/packages/insomnia-app/app/common/send-request.js index 303344647..3110ef390 100644 --- a/packages/insomnia-app/app/common/send-request.js +++ b/packages/insomnia-app/app/common/send-request.js @@ -3,8 +3,8 @@ 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 +export async function getSendRequestCallbackMemDb(environmentId, memDB) { + // Initialize the DB in-memory and fill it with data if we're given one await db.init(modelTypes(), { inMemoryOnly: true }, true); const docs = []; @@ -18,20 +18,30 @@ export async function getSendRequestCallback(environmentId, memDB) { // 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, - }; + return sendAndTransform(requestId, environmentId); + }; +} + +export function getSendRequestCallback(environmentId) { + return async function sendRequest(requestId) { + return sendAndTransform(requestId, environmentId); + }; +} + +async function sendAndTransform(requestId, environmentId) { + 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, }; } diff --git a/packages/insomnia-app/send-request/index.js b/packages/insomnia-app/send-request/index.js index 9c75a78d5..f3b49c9f0 100644 --- a/packages/insomnia-app/send-request/index.js +++ b/packages/insomnia-app/send-request/index.js @@ -1 +1 @@ -export { getSendRequestCallback } from '../app/common/send-request'; +export { getSendRequestCallbackMemDb, getSendRequestCallback } from '../app/common/send-request'; diff --git a/packages/insomnia-cli/flow-typed/insomnia-send-request.js b/packages/insomnia-cli/flow-typed/insomnia-send-request.js index 36d08c255..5465184cf 100644 --- a/packages/insomnia-cli/flow-typed/insomnia-send-request.js +++ b/packages/insomnia-cli/flow-typed/insomnia-send-request.js @@ -2,7 +2,8 @@ declare module 'insomnia-send-request' { declare module.exports: { - getSendRequestCallback: ( + getSendRequestCallback: ( environmentId: string ) => Promise, + getSendRequestCallbackMemDb: ( environmentId: string, memDb: { [string]: Array }, ) => Promise, diff --git a/packages/insomnia-cli/src/commands/run-tests.js b/packages/insomnia-cli/src/commands/run-tests.js index 1a5b7611d..5ae6f7449 100644 --- a/packages/insomnia-cli/src/commands/run-tests.js +++ b/packages/insomnia-cli/src/commands/run-tests.js @@ -1,7 +1,7 @@ // @flow import { generate, runTestsCli } from 'insomnia-testing'; -import { getSendRequestCallback } from 'insomnia-send-request'; +import { getSendRequestCallbackMemDb } from 'insomnia-send-request'; import type { GlobalOptions } from '../util'; import { loadDb } from '../db'; @@ -63,6 +63,6 @@ export async function runInsomniaTests(options: RunTestsOptions): Promise