@ns8/ns8-switchboard-operator
Advanced tools
Comparing version
@@ -17,3 +17,4 @@ /// <reference types="node" /> | ||
private request; | ||
private safeParseJSON; | ||
private checkForErrorResponse; | ||
} |
@@ -37,4 +37,5 @@ "use strict"; | ||
const url = `${this.baseUrl}/merchants/current`; | ||
const response = await fetch(url, { method, headers }); | ||
await this.checkForErrorResponse(response); | ||
const request = { method, headers }; | ||
const response = await fetch(url, request); | ||
await this.checkForErrorResponse(request, response, url); | ||
} | ||
@@ -53,16 +54,32 @@ async getOrderByName(orderName) { | ||
}; | ||
let requestObj = { method, headers }; | ||
let request = { method, headers }; | ||
if (method !== HTTPMethod.GET && obj) { | ||
const body = JSON.stringify(obj); | ||
requestObj = { method, body, headers }; | ||
request = { method, body, headers }; | ||
} | ||
const url = `${this.baseUrl}/${path}`; | ||
const response = await fetch(url, requestObj); | ||
await this.checkForErrorResponse(response); | ||
return response.json(); | ||
const response = await fetch(url, request); | ||
await this.checkForErrorResponse(request, response, url); | ||
return this.safeParseJSON(response); | ||
} | ||
async checkForErrorResponse(response) { | ||
safeParseJSON(response) { | ||
try { | ||
return response.json(); | ||
} | ||
catch (error) { | ||
throw new Error(`Operator V2Client response json parsing error: | ||
response: ${JSON.stringify(response)} | ||
error: ${error}`); | ||
} | ||
} | ||
async checkForErrorResponse(request, response, url) { | ||
if (response.status > V2Client.MAX_SUCCESS_CODE) { | ||
const err = { status: response.status, body: await response.json() }; | ||
throw new Error(`Protect API error: ${JSON.stringify(err)}`); | ||
const err = { | ||
status: response.status, | ||
body: await this.safeParseJSON(response), | ||
}; | ||
throw new Error(`Operator V2Client - Protect API error: ${JSON.stringify(err)} | ||
url: ${url}, | ||
request: ${JSON.stringify(request)} | ||
response: ${JSON.stringify(response)}`); | ||
} | ||
@@ -69,0 +86,0 @@ } |
{ | ||
"name": "@ns8/ns8-switchboard-operator", | ||
"version": "1.0.108", | ||
"version": "1.0.109", | ||
"description": "Operator project contains switchboard interfaces and orchestration code", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
31259
3.2%394
4.79%