From 17bed59e52e5de573b82c3fa338d88724ffcacba Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 28 Jun 2019 10:33:07 -0400 Subject: [PATCH] refactor: Conditionally add the https new Agent --- src/infrastructure/KyRequester.ts | 35 ++++++++++++++++++------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/infrastructure/KyRequester.ts b/src/infrastructure/KyRequester.ts index 82c93967..9cfe57a7 100644 --- a/src/infrastructure/KyRequester.ts +++ b/src/infrastructure/KyRequester.ts @@ -23,34 +23,39 @@ function responseHeadersAsObject(response) { function defaultRequest(service: any, { body, query, sudo, method }) { const headers = new Headers(service.headers); let bod = body; + let agent; if (sudo) headers.append('sudo', `${sudo}`); - - if (typeof body === 'object' && !(body instanceof FormData)){ + + if (typeof body === 'object' && !(body instanceof FormData)) { bod = JSON.stringify(decamelizeKeys(body, skipAllCaps)); } + if (service.rejectUnauthorized) { + agent = new Agent({ + rejectUnauthorized: service.rejectUnauthorized, + }); + } + return { - timeout: service.requestTimeout, - headers, - method: (method === 'stream') ? 'get' : method, - onProgress: (method === 'stream') ? () => {} : undefined, - searchParams: stringify(decamelizeKeys(query || {}) as any, { arrayFormat: 'bracket' }), - prefixUrl: service.url, - body: bod, - agent: new Agent({ - rejectUnauthorized: service.rejectUnauthorized - }) - } + timeout: service.requestTimeout, + headers, + method: method === 'stream' ? 'get' : method, + onProgress: method === 'stream' ? () => {} : undefined, + searchParams: stringify(decamelizeKeys(query || {}) as any, { arrayFormat: 'bracket' }), + prefixUrl: service.url, + body: bod, + agent, + }; } async function processBody(response) { const contentType = response.headers.get('content-type') || ''; const content = await response.text(); - if(contentType.includes('json')) { + if (contentType.includes('json')) { try { - return JSON.parse(content || "{}"); + return JSON.parse(content || '{}'); } catch { return {}; }