@ns8/ns8-switchboard-operator
Advanced tools
Comparing version
@@ -22,2 +22,3 @@ /// <reference types="node" /> | ||
private isAwsElbBadGateway; | ||
private fetch; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fetch = require("isomorphic-fetch"); | ||
const superagent_1 = require("superagent"); | ||
const url_1 = require("url"); | ||
var HTTPMethod; | ||
(function (HTTPMethod) { | ||
HTTPMethod["GET"] = "GET"; | ||
HTTPMethod["POST"] = "POST"; | ||
HTTPMethod["PUT"] = "PUT"; | ||
HTTPMethod["PATCH"] = "PATCH"; | ||
})(HTTPMethod || (HTTPMethod = {})); | ||
const HTTPMethod_1 = require("./HTTPMethod"); | ||
class V2Client { | ||
@@ -22,46 +16,50 @@ withBaseUrl(baseUrl) { | ||
async postOrder(order) { | ||
return this.request(HTTPMethod.POST, 'orders', order); | ||
return this.request(HTTPMethod_1.HTTPMethod.POST, 'orders', order); | ||
} | ||
async postFraudAssessment(fraudsAssessmentCreate, orderId) { | ||
const path = `orders/${orderId}/fraud-assessments`; | ||
return this.request(HTTPMethod.POST, path, fraudsAssessmentCreate); | ||
return this.request(HTTPMethod_1.HTTPMethod.POST, path, fraudsAssessmentCreate); | ||
} | ||
async updateMerchant(merchantUpdate) { | ||
return this.request(HTTPMethod.PATCH, 'merchants/current', merchantUpdate); | ||
return this.request(HTTPMethod_1.HTTPMethod.PATCH, 'merchants/current', merchantUpdate); | ||
} | ||
async uninstall() { | ||
const method = 'DELETE'; | ||
const headers = { | ||
Authorization: `Bearer ${this.authToken}`, | ||
const method = HTTPMethod_1.HTTPMethod.DELETE; | ||
const url = `${this.baseUrl}/merchants/current`; | ||
const requestPacket = { | ||
url, | ||
method, | ||
headers: { | ||
Authorization: `Bearer ${this.authToken}`, | ||
}, | ||
body: null, | ||
}; | ||
const url = `${this.baseUrl}/merchants/current`; | ||
const request = { method, headers }; | ||
const response = await fetch(url, request); | ||
await this.checkForErrorResponse(request, response, url); | ||
const response = await this.fetch(requestPacket); | ||
this.checkForErrorResponse(requestPacket, response, url); | ||
} | ||
async getOrderByName(orderName) { | ||
const encodedOrderName = encodeURIComponent(orderName); | ||
return this.request(HTTPMethod.GET, `orders/order-name/${encodedOrderName}`); | ||
return this.request(HTTPMethod_1.HTTPMethod.GET, `orders/order-name/${encodedOrderName}`); | ||
} | ||
async updateOrderStatus(orderId, orderUpdate) { | ||
return this.request(HTTPMethod.PUT, `orders/${orderId}`, orderUpdate); | ||
return this.request(HTTPMethod_1.HTTPMethod.PUT, `orders/${orderId}`, orderUpdate); | ||
} | ||
async request(method, path, obj) { | ||
const headers = { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${this.authToken}`, | ||
const url = `${this.baseUrl}/${path}`; | ||
const requestPacket = { | ||
url, | ||
method, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${this.authToken}`, | ||
}, | ||
body: method !== HTTPMethod_1.HTTPMethod.GET && obj ? JSON.stringify(obj) : null, | ||
}; | ||
let request = { method, headers }; | ||
if (method !== HTTPMethod.GET && obj) { | ||
const body = JSON.stringify(obj); | ||
request = { method, body, headers }; | ||
} | ||
const url = `${this.baseUrl}/${path}`; | ||
const response = await fetch(url, request); | ||
await this.checkForErrorResponse(request, response, url); | ||
const response = await this.fetch(requestPacket); | ||
this.checkForErrorResponse(requestPacket, response, url); | ||
return this.safeParseJSON(response); | ||
} | ||
async safeParseJSON(response) { | ||
safeParseJSON(response) { | ||
try { | ||
return await response.json(); | ||
return JSON.parse(response.text); | ||
} | ||
@@ -75,3 +73,3 @@ catch (error) { | ||
} | ||
async checkForErrorResponse(request, response, url) { | ||
checkForErrorResponse(requestPacket, response, url) { | ||
const baseMessage = 'Operator V2Client - Protect API error:'; | ||
@@ -82,3 +80,3 @@ if (this.isAwsElbBadGateway(response)) { | ||
if (response.status > V2Client.MAX_SUCCESS_CODE) { | ||
const body = await this.safeParseJSON(response); | ||
const body = this.safeParseJSON(response); | ||
const errorDetails = this.formatObjectForOutput({ | ||
@@ -88,3 +86,3 @@ body, | ||
}); | ||
const requestDetails = this.formatObjectForOutput({ url, request, response }); | ||
const requestDetails = this.formatObjectForOutput({ url, requestPacket, response }); | ||
throw new Error(`${baseMessage} ${errorDetails} ${requestDetails}`); | ||
@@ -111,5 +109,36 @@ } | ||
const awsElbLabel = 'awselb/2.0'; | ||
const responseServer = response.headers.get('server'); | ||
const responseServer = response.header.server; | ||
return !response.ok && response.status === 502 && responseServer === awsElbLabel; | ||
} | ||
async fetch(requestPacket) { | ||
switch (requestPacket.method) { | ||
case HTTPMethod_1.HTTPMethod.GET: { | ||
return superagent_1.get(requestPacket.url) | ||
.set(requestPacket.headers); | ||
} | ||
case HTTPMethod_1.HTTPMethod.POST: { | ||
return superagent_1.post(requestPacket.url) | ||
.send(requestPacket.body) | ||
.set(requestPacket.headers); | ||
} | ||
case HTTPMethod_1.HTTPMethod.PUT: { | ||
return superagent_1.put(requestPacket.url) | ||
.send(requestPacket.body) | ||
.set(requestPacket.headers); | ||
} | ||
case HTTPMethod_1.HTTPMethod.PATCH: { | ||
return superagent_1.patch(requestPacket.url) | ||
.send(requestPacket.body) | ||
.set(requestPacket.headers); | ||
} | ||
case HTTPMethod_1.HTTPMethod.DELETE: { | ||
return superagent_1.del(requestPacket.url) | ||
.send(requestPacket.body) | ||
.set(requestPacket.headers); | ||
} | ||
default: { | ||
throw new Error(`Unexpected Request Method Type: ${JSON.stringify(requestPacket)}`); | ||
} | ||
} | ||
} | ||
} | ||
@@ -116,0 +145,0 @@ exports.default = V2Client; |
{ | ||
"name": "@ns8/ns8-switchboard-operator", | ||
"version": "1.0.115", | ||
"version": "1.0.116", | ||
"description": "Operator project contains switchboard interfaces and orchestration code", | ||
@@ -28,2 +28,3 @@ "main": "dist/index.js", | ||
"@types/sinon": "7.5.1", | ||
"@types/superagent": "4.1.4", | ||
"@typescript-eslint/eslint-plugin": "2.11.0", | ||
@@ -48,6 +49,6 @@ "@typescript-eslint/parser": "2.11.0", | ||
"aws-sdk": "2.590.0", | ||
"isomorphic-fetch": "2.2.1", | ||
"ns8-protect-models": "1.0.217", | ||
"ns8-switchboard-interfaces": "1.0.94" | ||
"ns8-switchboard-interfaces": "1.0.94", | ||
"superagent": "5.1.3" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
39447
8.93%38
18.75%510
12.09%0
-100%23
4.55%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed