homebridge-http-base
Advanced tools
Comparing version 2.1.6 to 2.1.7
@@ -20,2 +20,6 @@ import { IClientOptions, QoS } from "mqtt"; | ||
} | ||
export declare type HeaderKeyValues = { | ||
key: string; | ||
value: string; | ||
}; | ||
export declare type MQTTSubscriptionObject = { | ||
@@ -22,0 +26,0 @@ subscriptions: MQTTSubscription[]; |
@@ -105,3 +105,26 @@ "use strict"; | ||
throw new Error("'auth.username' and/or 'auth.password' was not set!"); | ||
// TODO validate property.headers; ensure it is object with key value pair of strings | ||
if (property.headers !== undefined) { | ||
if (typeof property.headers !== "object") { | ||
throw new Error("'auth.headers' must be an object"); | ||
} | ||
if (property.headers.constructor === Object) { // legacy style key value pairs | ||
const stringOnlyValues = Object.values(property.headers) | ||
.map(value => typeof value === "string") | ||
.reduce((prev, cur) => prev && cur); | ||
if (!stringOnlyValues) { | ||
throw new Error("'auth.headers' must only contain key-value pairs of type string!"); | ||
} | ||
} | ||
else if (property.headers.constructor === Array) { // object array style (engineered for homebridge config ui) | ||
const keyValueObjects = property.headers | ||
.map((pair) => pair.key !== undefined && pair.value !== undefined) | ||
.reduce((prev, cur) => prev && cur); | ||
if (!keyValueObjects) { | ||
throw new Error("'auth.headers' must only contain key-value pairs in proper object format!"); | ||
} | ||
} | ||
else { | ||
throw new Error("'auth.headers' has unknown constructor"); | ||
} | ||
} | ||
if (property.strictSSL !== undefined && typeof property.strictSSL !== "boolean") | ||
@@ -133,4 +156,12 @@ throw new Error("'strictSSL' must be a boolean!"); | ||
} | ||
if (property.headers) | ||
urlObject.headers = property.headers; | ||
if (property.headers) { | ||
if (property.headers.constructor === Array) { | ||
const headers = {}; | ||
property.headers.forEach((pair) => headers[pair.key] = pair.value); | ||
urlObject.headers = headers; | ||
} | ||
else { | ||
urlObject.headers = property.headers; | ||
} | ||
} | ||
if (property.strictSSL) | ||
@@ -137,0 +168,0 @@ urlObject.strictSSL = property.strictSSL; |
{ | ||
"name": "homebridge-http-base", | ||
"version": "2.1.6", | ||
"version": "2.1.7", | ||
"description": "Base and utils for homebridge-http devices", | ||
@@ -5,0 +5,0 @@ "license": "ISC", |
@@ -69,3 +69,6 @@ import * as configParser from './configparser'; | ||
}, | ||
headers: {"Content-Type": "text/html"}, | ||
headers: { | ||
"Content-Type": "text/html", | ||
"Content-Encoding": "gzip" | ||
}, | ||
strictSSL: true, | ||
@@ -84,2 +87,3 @@ requestTimeout: 1234 | ||
expect(urlObject.headers["Content-Type"]).toEqual("text/html"); | ||
expect(urlObject.headers["Content-Encoding"]).toEqual("gzip"); | ||
expect(urlObject.strictSSL).toEqual(object.strictSSL); | ||
@@ -104,2 +108,22 @@ expect(urlObject.requestTimeout).toEqual(object.requestTimeout); | ||
it('should properly convert key-value objects passed to headers', function () { | ||
const object = { | ||
url: "https://google.com", | ||
headers: [ | ||
{ | ||
"key": "Content-Type", | ||
"value": "text/html" | ||
}, | ||
{ | ||
"key": "Content-Encoding", | ||
"value": "gzip" | ||
} | ||
] | ||
}; | ||
const urlObject = configParser.parseUrlProperty(object); | ||
expect(urlObject.headers["Content-Type"]).toEqual("text/html"); | ||
expect(urlObject.headers["Content-Encoding"]).toEqual("gzip"); | ||
}); | ||
// TODO add test cases to ensure illegal data types for urlObject fields get rejected | ||
@@ -106,0 +130,0 @@ }); |
@@ -30,2 +30,7 @@ import {IClientOptions, QoS} from "mqtt"; | ||
export type HeaderKeyValues = { | ||
key: string, | ||
value: string, | ||
} | ||
export type MQTTSubscriptionObject = { | ||
@@ -134,3 +139,27 @@ subscriptions: MQTTSubscription[]; | ||
throw new Error("'auth.username' and/or 'auth.password' was not set!"); | ||
// TODO validate property.headers; ensure it is object with key value pair of strings | ||
if (property.headers !== undefined) { | ||
if (typeof property.headers !== "object") { | ||
throw new Error("'auth.headers' must be an object"); | ||
} | ||
if (property.headers.constructor === Object) { // legacy style key value pairs | ||
const stringOnlyValues = Object.values(property.headers) | ||
.map(value => typeof value === "string") | ||
.reduce((prev, cur) => prev && cur); | ||
if (!stringOnlyValues) { | ||
throw new Error("'auth.headers' must only contain key-value pairs of type string!"); | ||
} | ||
} else if (property.headers.constructor === Array) { // object array style (engineered for homebridge config ui) | ||
const keyValueObjects = property.headers | ||
.map((pair: HeaderKeyValues) => pair.key !== undefined && pair.value !== undefined) | ||
.reduce((prev: boolean, cur: boolean) => prev && cur); | ||
if (!keyValueObjects) { | ||
throw new Error("'auth.headers' must only contain key-value pairs in proper object format!") | ||
} | ||
} else { | ||
throw new Error("'auth.headers' has unknown constructor"); | ||
} | ||
} | ||
if (property.strictSSL !== undefined && typeof property.strictSSL !== "boolean") | ||
@@ -168,4 +197,11 @@ throw new Error("'strictSSL' must be a boolean!"); | ||
if (property.headers) | ||
urlObject.headers = property.headers; | ||
if (property.headers) { | ||
if (property.headers.constructor === Array) { | ||
const headers: Record<string, string> = {}; | ||
property.headers.forEach((pair: HeaderKeyValues) => headers[pair.key] = pair.value); | ||
urlObject.headers = headers; | ||
} else { | ||
urlObject.headers = property.headers; | ||
} | ||
} | ||
@@ -172,0 +208,0 @@ if (property.strictSSL) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
148970
102
2519