logflare-transport-core
Advanced tools
Comparing version 0.1.3 to 0.2.0
/// <reference types="node" /> | ||
import { AxiosInstance } from "axios"; | ||
import stream from "stream"; | ||
interface LogflareUserOptions { | ||
interface LogflareUserOptionsI { | ||
sourceToken: string; | ||
apiKey: string; | ||
apiBaseUrl?: string; | ||
transforms?: object; | ||
endpoint: string; | ||
} | ||
@@ -12,3 +14,5 @@ declare class LogflareHttpClient { | ||
protected readonly sourceToken: string; | ||
constructor(options: LogflareUserOptions); | ||
protected readonly transforms?: object; | ||
protected readonly endpoint?: string; | ||
constructor(options: LogflareUserOptionsI); | ||
addLogEvent(logEvent: object | object[]): Promise<object>; | ||
@@ -21,2 +25,2 @@ insertStream(): stream.Writable; | ||
} | ||
export { LogflareHttpClient }; | ||
export { LogflareHttpClient, LogflareUserOptionsI }; |
@@ -44,2 +44,4 @@ "use strict"; | ||
var axios_1 = __importDefault(require("axios")); | ||
var lodash_1 = __importDefault(require("lodash")); | ||
var typecasting_1 = require("./typecasting"); | ||
var stream_1 = __importDefault(require("stream")); | ||
@@ -60,3 +62,3 @@ var defaultOptions = { | ||
this._handleError = function (error) { return Promise.reject(error); }; | ||
var sourceToken = options.sourceToken, apiKey = options.apiKey; | ||
var sourceToken = options.sourceToken, apiKey = options.apiKey, transforms = options.transforms, endpoint = options.endpoint; | ||
if (!sourceToken || sourceToken == "") { | ||
@@ -68,3 +70,5 @@ throw "Logflare API logging transport source token is NOT configured!"; | ||
} | ||
this.transforms = transforms; | ||
this.sourceToken = sourceToken; | ||
this.endpoint = endpoint; | ||
this.axiosInstance = axios_1.default.create({ | ||
@@ -80,6 +84,10 @@ baseURL: options.apiBaseUrl || defaultOptions.apiBaseUrl, | ||
LogflareHttpClient.prototype.addLogEvent = function (logEvent) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var logEvents; | ||
return __generator(this, function (_a) { | ||
return __generator(this, function (_b) { | ||
logEvents = Array.isArray(logEvent) ? logEvent : [logEvent]; | ||
if ((_a = this === null || this === void 0 ? void 0 : this.transforms) === null || _a === void 0 ? void 0 : _a.jsNumbers) { | ||
logEvents = lodash_1.default.map(logEvents, typecasting_1.applyNumberToStringTypecasting); | ||
} | ||
return [2 /*return*/, this.postLogEvents(logEvents)]; | ||
@@ -106,6 +114,12 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var payload, e_1; | ||
var url, payload, e_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (this.endpoint === "typecasting") { | ||
url = "/logs/typecasts"; | ||
} | ||
else { | ||
url = "/logs"; | ||
} | ||
payload = { | ||
@@ -118,7 +132,15 @@ batch: batch, | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, this.axiosInstance.post("/logs", payload)]; | ||
return [4 /*yield*/, this.axiosInstance.post(url, payload)]; | ||
case 2: return [2 /*return*/, _a.sent()]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
console.error("Logflare API request failed with " + e_1.response.status + " status: " + JSON.stringify(e_1.response.data)); | ||
if (e_1.response) { | ||
console.error("Logflare API request failed with " + e_1.response.status + " status: " + JSON.stringify(e_1.response.data)); | ||
} | ||
else if (e_1.request) { | ||
console.error("Logflare API request failed: " + e_1.request); | ||
} | ||
else { | ||
console.error(e_1.message); | ||
} | ||
return [2 /*return*/, e_1]; | ||
@@ -125,0 +147,0 @@ case 4: return [2 /*return*/]; |
@@ -1,2 +0,2 @@ | ||
import { LogflareHttpClient } from "./http_client"; | ||
export { LogflareHttpClient }; | ||
import { LogflareHttpClient, LogflareUserOptionsI } from "./http_client"; | ||
export { LogflareHttpClient, LogflareUserOptionsI }; |
{ | ||
"name": "logflare-transport-core", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "A common core for Logflare javascript transports.", | ||
@@ -12,3 +12,8 @@ "keywords": [ | ||
"dependencies": { | ||
"axios": "^0.19.2" | ||
"@types/lodash": "^4.14.153", | ||
"axios": "^0.19.2", | ||
"big-integer": "^1.6.48", | ||
"bignumber.js": "^9.0.0", | ||
"decimal.js": "^10.2.0", | ||
"lodash": "^4.17.15" | ||
}, | ||
@@ -19,2 +24,3 @@ "devDependencies": { | ||
"@babel/preset-typescript": "^7.9.0", | ||
"@types/big.js": "^4.0.5", | ||
"@types/node": "^14.0", | ||
@@ -27,3 +33,2 @@ "babel-jest": "^26.0.1", | ||
"scripts": { | ||
"prepublish": "tsc", | ||
"test": "jest", | ||
@@ -30,0 +35,0 @@ "test.watch": "jest --watch" |
import axios, {AxiosInstance, AxiosResponse} from "axios" | ||
import _ from "lodash" | ||
import { | ||
applyNumberToStringTypecasting, | ||
applyCustomTypecasting, | ||
} from "./typecasting" | ||
import stream from "stream" | ||
interface LogflareUserOptions { | ||
interface LogflareUserOptionsI { | ||
sourceToken: string | ||
apiKey: string | ||
apiBaseUrl?: string | ||
transforms?: object | ||
endpoint: string | ||
} | ||
@@ -17,5 +24,7 @@ | ||
protected readonly sourceToken: string | ||
protected readonly transforms?: object | ||
protected readonly endpoint?: string | ||
public constructor(options: LogflareUserOptions) { | ||
const {sourceToken, apiKey} = options | ||
public constructor(options: LogflareUserOptionsI) { | ||
const {sourceToken, apiKey, transforms, endpoint} = options | ||
if (!sourceToken || sourceToken == "") { | ||
@@ -27,3 +36,5 @@ throw "Logflare API logging transport source token is NOT configured!" | ||
} | ||
this.transforms = transforms | ||
this.sourceToken = sourceToken | ||
this.endpoint = endpoint | ||
this.axiosInstance = axios.create({ | ||
@@ -41,3 +52,6 @@ baseURL: options.apiBaseUrl || defaultOptions.apiBaseUrl, | ||
public async addLogEvent(logEvent: object | object[]): Promise<object> { | ||
const logEvents = Array.isArray(logEvent) ? logEvent : [logEvent] | ||
let logEvents = Array.isArray(logEvent) ? logEvent : [logEvent] | ||
if (this?.transforms?.jsNumbers) { | ||
logEvents = _.map(logEvents, applyNumberToStringTypecasting) | ||
} | ||
return this.postLogEvents(logEvents) | ||
@@ -63,2 +77,8 @@ } | ||
private async postLogEvents(batch: object[]) { | ||
let url | ||
if (this.endpoint === "typecasting") { | ||
url = "/logs/typecasts" | ||
} else { | ||
url = "/logs" | ||
} | ||
const payload = { | ||
@@ -69,5 +89,16 @@ batch, | ||
try { | ||
return await this.axiosInstance.post("/logs", payload) | ||
return await this.axiosInstance.post(url, payload) | ||
} catch (e) { | ||
console.error(`Logflare API request failed with ${e.response.status} status: ${JSON.stringify(e.response.data)}`) | ||
if (e.response) { | ||
console.error( | ||
`Logflare API request failed with ${ | ||
e.response.status | ||
} status: ${JSON.stringify(e.response.data)}` | ||
) | ||
} else if (e.request) { | ||
console.error(`Logflare API request failed: ${e.request}`) | ||
} else { | ||
console.error(e.message) | ||
} | ||
return e | ||
@@ -88,2 +119,2 @@ } | ||
export {LogflareHttpClient} | ||
export {LogflareHttpClient, LogflareUserOptionsI} |
@@ -77,1 +77,51 @@ import moxios from "moxios" | ||
}) | ||
describe("LogflareHttpClient with options", () => { | ||
let httpClient | ||
let axiosInstance | ||
beforeEach(() => { | ||
httpClient = new LogflareHttpClient({ | ||
apiKey: "testApiKey", | ||
sourceToken: "2222-2222", | ||
apiBaseUrl: "http://non-existing.domain", | ||
transforms: {jsNumbers: true}, | ||
}) | ||
const axiosInstance = httpClient.axiosInstance | ||
moxios.install(axiosInstance) | ||
}) | ||
afterEach(() => { | ||
moxios.uninstall(axiosInstance) | ||
}) | ||
it("trarnsforms js numbers if configured", async (done) => { | ||
const le = { | ||
message: "info log msg", | ||
metadata: {number: 1, number2: 1.0}, | ||
} | ||
moxios.wait(async () => { | ||
let request = moxios.requests.mostRecent() | ||
expect(request.config.baseURL).toBe(testBaseUrl) | ||
expect(request.headers).toMatchObject({ | ||
Accept: "application/json, text/plain, */*", | ||
"Content-Type": "application/json", | ||
"X-API-KEY": "testApiKey", | ||
}) | ||
expect(request.config.data).toBe( | ||
'{"batch":[{"body":{"message":"info log msg","metadata":{"number":"1","number2":"1"}},"typecasts":[{"path":["metadata","number"],"from":"string","to":"float"},{"path":["metadata","number2"],"from":"string","to":"float"}]}],"source":"2222-2222"}' | ||
) | ||
await request.respondWith({ | ||
status: 200, | ||
response: apiResponseSuccess, | ||
}) | ||
done() | ||
}) | ||
const response = await httpClient.addLogEvent(le) | ||
expect(response).toMatchObject(apiResponseSuccess) | ||
}) | ||
}) |
@@ -1,3 +0,3 @@ | ||
import {LogflareHttpClient} from "./http_client" | ||
import {LogflareHttpClient, LogflareUserOptionsI} from "./http_client" | ||
export {LogflareHttpClient} | ||
export {LogflareHttpClient, LogflareUserOptionsI} |
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
45078
18
1036
6
9
+ Added@types/lodash@^4.14.153
+ Addedbig-integer@^1.6.48
+ Addedbignumber.js@^9.0.0
+ Addeddecimal.js@^10.2.0
+ Addedlodash@^4.17.15
+ Added@types/lodash@4.17.13(transitive)
+ Addedbig-integer@1.6.52(transitive)
+ Addedbignumber.js@9.1.2(transitive)
+ Addeddecimal.js@10.4.3(transitive)
+ Addedlodash@4.17.21(transitive)