@trayio/commons
Advanced tools
Comparing version 4.50.0 to 4.51.0
@@ -25,2 +25,9 @@ /// <reference types="node" /> | ||
export type HttpRequestBody = RegularHttpRequestBody | MultipartHttpRequestBody; | ||
export type HttpCertificate = { | ||
cert: string; | ||
key: string; | ||
}; | ||
export type HttpAgent = { | ||
certificate: O.Option<HttpCertificate>; | ||
}; | ||
export type HttpRequest = { | ||
@@ -31,2 +38,3 @@ headers: HttpHeaders; | ||
body: HttpRequestBody; | ||
agent: O.Option<HttpAgent>; | ||
}; | ||
@@ -62,2 +70,6 @@ export type HttpResponse = { | ||
} | ||
export interface HttpRequestInterface { | ||
create: (httpRequest: Partial<HttpRequest>) => HttpRequest; | ||
} | ||
export declare const HttpRequest: HttpRequestInterface; | ||
export interface HttpHeadersInterface { | ||
@@ -64,0 +76,0 @@ get: (headers: HttpHeaders, name: string) => O.Option<HttpHeaderValue>; |
@@ -26,6 +26,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isMultiPartBody = exports.serializationFormatToHttpContentType = exports.HttpHeaders = exports.HttpHeader = exports.HttpContentType = exports.HttpMethod = exports.MultipartHttpRequestBody = void 0; | ||
exports.isMultiPartBody = exports.serializationFormatToHttpContentType = exports.HttpHeaders = exports.HttpRequest = exports.HttpHeader = exports.HttpContentType = exports.HttpMethod = exports.MultipartHttpRequestBody = void 0; | ||
const O = __importStar(require("fp-ts/lib/Option")); | ||
const TE = __importStar(require("fp-ts/lib/TaskEither")); | ||
const function_1 = require("fp-ts/lib/function"); | ||
const stream_1 = require("stream"); | ||
const _ = __importStar(require("lodash")); | ||
@@ -95,2 +96,12 @@ const File_1 = require("../file/File"); | ||
})(HttpHeader || (exports.HttpHeader = HttpHeader = {})); | ||
exports.HttpRequest = { | ||
create: (httpRequest) => ({ | ||
headers: {}, | ||
pathParams: {}, | ||
queryString: {}, | ||
body: stream_1.Readable.from(''), | ||
agent: O.none, | ||
...httpRequest, | ||
}), | ||
}; | ||
exports.HttpHeaders = { | ||
@@ -97,0 +108,0 @@ get: (headers, name) => getHeader(headers, name), |
@@ -31,3 +31,5 @@ "use strict"; | ||
const E = __importStar(require("fp-ts/Either")); | ||
const O = __importStar(require("fp-ts/Option")); | ||
const stream_1 = require("stream"); | ||
const selfsigned_1 = __importDefault(require("selfsigned")); | ||
const function_1 = require("fp-ts/function"); | ||
@@ -37,2 +39,3 @@ const t = __importStar(require("io-ts")); | ||
const http_1 = require("http"); | ||
const https_1 = require("https"); | ||
const util_1 = require("util"); | ||
@@ -206,4 +209,19 @@ const TE = __importStar(require("fp-ts/TaskEither")); | ||
const port = 8000; // TODO: Get random port | ||
const httpsPort = 9001; | ||
const baseUrl = `http://${host}:${port}`; | ||
const httpsServerUrl = `https://${host}:${httpsPort}`; | ||
// Generate self-signed certificates for Https server, override type as it didn't include client certs | ||
const certs = selfsigned_1.default.generate([], { | ||
clientCertificate: true, | ||
clientCertificateCN: 'client', | ||
extensions: [{ name: 'basicConstraints', cA: true }], | ||
}); | ||
const server = (0, http_1.createServer)(requestListener); | ||
const httpsServer = (0, https_1.createServer)({ | ||
key: certs.private, | ||
cert: certs.cert, | ||
requestCert: true, | ||
rejectUnauthorized: true, | ||
ca: [certs.clientcert], | ||
}, requestListener); | ||
describe('HttpClient Test', () => { | ||
@@ -213,2 +231,3 @@ beforeAll(async () => { | ||
server.listen(port, host, resolve); | ||
httpsServer.listen(httpsPort, host); | ||
}); | ||
@@ -227,5 +246,15 @@ }); | ||
}); | ||
await new Promise((resolve, reject) => { | ||
httpsServer.close((error) => { | ||
if (error) { | ||
reject(error); | ||
} | ||
else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
}); | ||
test('should send a GET request', async () => { | ||
const response = await httpClient.execute(Http_1.HttpMethod.Get, baseUrl, { | ||
const response = await httpClient.execute(Http_1.HttpMethod.Get, baseUrl, Http_1.HttpRequest.create({ | ||
headers: { 'x-custom-header': 'customVal' }, | ||
@@ -235,3 +264,3 @@ pathParams: {}, | ||
body: BufferExtensions_1.BufferExtensions.arrayBufferToReadable(new ArrayBuffer(0)), | ||
})(); | ||
}))(); | ||
const testValue = { | ||
@@ -245,2 +274,23 @@ strField: 'customVal', | ||
}); | ||
test('should send a GET request with a certificate', async () => { | ||
const response = await httpClient.execute(Http_1.HttpMethod.Get, httpsServerUrl, Http_1.HttpRequest.create({ | ||
headers: { 'x-custom-header': 'customVal' }, | ||
pathParams: {}, | ||
queryString: {}, | ||
body: BufferExtensions_1.BufferExtensions.arrayBufferToReadable(new ArrayBuffer(0)), | ||
agent: O.some({ | ||
certificate: O.some({ | ||
key: certs.clientprivate, | ||
cert: certs.clientcert, | ||
}), | ||
}), | ||
}))(); | ||
const testValue = { | ||
strField: 'customVal', | ||
intField: 123, | ||
}; | ||
const result = (0, function_1.pipe)(response, TE.fromEither, TE.chain((httpResponse) => BufferExtensions_1.BufferExtensions.readableToArrayBuffer(httpResponse.body)), TE.match((error) => E.left(error), (buffer) => (0, function_1.pipe)(serialization.deserialize(buffer), E.chain((encoded) => testResponseCodec.decode(encoded))))); | ||
const resultE = await result(); | ||
expect(resultE).toStrictEqual(E.right(testValue)); | ||
}); | ||
test('should send a POST request', async () => { | ||
@@ -250,3 +300,3 @@ const testInput = { | ||
}; | ||
const response = await (0, function_1.pipe)((0, Task_1.createTaskEitherFromEither)(E.right(testInputCodec.encode(testInput))), TE.map((encoded) => serialization.serialize(encoded)), TE.chain((serializedInput) => httpClient.execute(Http_1.HttpMethod.Post, baseUrl, { | ||
const response = await (0, function_1.pipe)((0, Task_1.createTaskEitherFromEither)(E.right(testInputCodec.encode(testInput))), TE.map((encoded) => serialization.serialize(encoded)), TE.chain((serializedInput) => httpClient.execute(Http_1.HttpMethod.Post, baseUrl, Http_1.HttpRequest.create({ | ||
headers: { | ||
@@ -259,3 +309,3 @@ 'x-custom-header': 'customVal', | ||
body: BufferExtensions_1.BufferExtensions.arrayBufferToReadable(serializedInput), | ||
})))(); | ||
}))))(); | ||
const testValue = { | ||
@@ -310,3 +360,3 @@ strField: 'customVal', | ||
}; | ||
const response = await httpClient.execute(Http_1.HttpMethod.Post, baseUrl, { | ||
const response = await httpClient.execute(Http_1.HttpMethod.Post, baseUrl, Http_1.HttpRequest.create({ | ||
headers: { | ||
@@ -319,3 +369,3 @@ 'x-custom-header': 'customVal', | ||
body: testInput, | ||
})(); | ||
}))(); | ||
const result = (0, function_1.pipe)(response, TE.fromEither, TE.chain((httpResponse) => BufferExtensions_1.BufferExtensions.readableToArrayBuffer(httpResponse.body)), TE.match((error) => E.left(error), (buffer) => (0, function_1.pipe)(serialization.deserialize(buffer), E.chain((encoded) => testMultiPartBodyTypeCodec.decode(encoded))))); | ||
@@ -344,3 +394,3 @@ const resultE = await result(); | ||
test('should send a POST request with a formUrlEncoded body', async () => { | ||
const response = await (0, function_1.pipe)((0, Task_1.createTaskEitherFromEither)(E.right(testInputFormUrlEncodedCodec.encode(testInputFormUrlEncoded))), TE.map((encoded) => formUrlEncodedSerialization.serialize(encoded)), TE.chain((serializedInput) => httpClient.execute(Http_1.HttpMethod.Post, baseUrl, { | ||
const response = await (0, function_1.pipe)((0, Task_1.createTaskEitherFromEither)(E.right(testInputFormUrlEncodedCodec.encode(testInputFormUrlEncoded))), TE.map((encoded) => formUrlEncodedSerialization.serialize(encoded)), TE.chain((serializedInput) => httpClient.execute(Http_1.HttpMethod.Post, baseUrl, Http_1.HttpRequest.create({ | ||
headers: { | ||
@@ -352,3 +402,3 @@ [Http_1.HttpHeader.ContentType]: Http_1.HttpContentType.FormUrlEncoded, | ||
body: BufferExtensions_1.BufferExtensions.arrayBufferToReadable(serializedInput), | ||
})))(); | ||
}))))(); | ||
const testValue = { | ||
@@ -363,3 +413,3 @@ strField: 'customVal', | ||
test('should parse an internal error response', async () => { | ||
const response = await httpClient.execute(Http_1.HttpMethod.Get, baseUrl, { | ||
const response = await httpClient.execute(Http_1.HttpMethod.Get, baseUrl, Http_1.HttpRequest.create({ | ||
headers: { error: '500' }, | ||
@@ -369,3 +419,3 @@ pathParams: {}, | ||
body: BufferExtensions_1.BufferExtensions.arrayBufferToReadable(new ArrayBuffer(0)), | ||
})(); | ||
}))(); | ||
const testErrorResponse = { | ||
@@ -382,3 +432,3 @@ message: 'some error message 500', | ||
test('should parse a not found error response', async () => { | ||
const response = await httpClient.execute(Http_1.HttpMethod.Get, baseUrl, { | ||
const response = await httpClient.execute(Http_1.HttpMethod.Get, baseUrl, Http_1.HttpRequest.create({ | ||
headers: { error: '404' }, | ||
@@ -388,3 +438,3 @@ pathParams: {}, | ||
body: BufferExtensions_1.BufferExtensions.arrayBufferToReadable(new ArrayBuffer(0)), | ||
})(); | ||
}))(); | ||
const testErrorResponse = { | ||
@@ -391,0 +441,0 @@ message: 'some error message 404', |
{ | ||
"name": "@trayio/commons", | ||
"version": "4.50.0", | ||
"version": "4.51.0", | ||
"description": "Extensions to the standard/core libraries and basic features", | ||
@@ -5,0 +5,0 @@ "exports": { |
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
Network access
Supply chain riskThis module accesses the network.
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
223246
4607
2