logflare-transport-core
Advanced tools
Comparing version 0.1.1 to 0.1.3
@@ -7,20 +7,11 @@ /// <reference types="node" /> | ||
apiKey: string; | ||
batchFlushInterval?: number; | ||
batchMaxSize?: number; | ||
apiBaseUrl?: string; | ||
} | ||
interface LogflareTransport { | ||
readonly httpClient: LogflareHttpClient; | ||
} | ||
declare class LogflareHttpClient { | ||
protected readonly axiosInstance: AxiosInstance; | ||
protected axiosInstance: AxiosInstance; | ||
protected readonly sourceToken: string; | ||
protected readonly batchFlushInterval: number; | ||
protected readonly maxBatchSize: number; | ||
protected batch: object[]; | ||
protected readonly batchFlushTimer: any; | ||
constructor(options: LogflareUserOptions); | ||
addLogEvent(logEvent: object | [object]): Promise<void>; | ||
addLogEvent(logEvent: object | object[]): Promise<object>; | ||
insertStream(): stream.Writable; | ||
private flushBatch; | ||
private postLogEvents; | ||
private _initializeResponseInterceptor; | ||
@@ -30,2 +21,2 @@ private _handleResponse; | ||
} | ||
export { LogflareHttpClient, LogflareTransport }; | ||
export { LogflareHttpClient }; |
@@ -38,9 +38,2 @@ "use strict"; | ||
}; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -55,4 +48,2 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
apiBaseUrl: "https://api.logflare.app", | ||
batchFlushInterval: 1000, | ||
maxBatchSize: 100, | ||
}; | ||
@@ -62,3 +53,2 @@ var LogflareHttpClient = /** @class */ (function () { | ||
var _this = this; | ||
var _a, _b; | ||
this._initializeResponseInterceptor = function () { | ||
@@ -72,21 +62,17 @@ _this.axiosInstance.interceptors.response.use(_this._handleResponse, _this._handleError); | ||
this._handleError = function (error) { return Promise.reject(error); }; | ||
this.sourceToken = options.sourceToken; | ||
if (!options.sourceToken) { | ||
var sourceToken = options.sourceToken, apiKey = options.apiKey; | ||
if (!sourceToken || sourceToken == "") { | ||
throw "Logflare API logging transport source token is NOT configured!"; | ||
} | ||
if (!options.apiKey) { | ||
if (!apiKey || apiKey == "") { | ||
throw "Logflare API logging transport api key is NOT configured!"; | ||
} | ||
this.batch = []; | ||
this.sourceToken = sourceToken; | ||
this.axiosInstance = axios_1.default.create({ | ||
baseURL: options.apiBaseUrl, | ||
baseURL: options.apiBaseUrl || defaultOptions.apiBaseUrl, | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-API-KEY": options.apiKey, | ||
"X-API-KEY": apiKey, | ||
}, | ||
}); | ||
this.maxBatchSize = (_a = options.batchMaxSize) !== null && _a !== void 0 ? _a : defaultOptions.maxBatchSize; | ||
this.batchFlushInterval = (_b = options.batchFlushInterval) !== null && _b !== void 0 ? _b : defaultOptions.batchFlushInterval; | ||
this.batchFlushTimer = setInterval(function () { return _this.flushBatch(); }, this.batchFlushInterval); | ||
this.batchFlushTimer.unref(); | ||
this._initializeResponseInterceptor(); | ||
@@ -96,10 +82,6 @@ } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var toConcat; | ||
var logEvents; | ||
return __generator(this, function (_a) { | ||
toConcat = Array.isArray(logEvent) ? logEvent : [logEvent]; | ||
this.batch = this.batch.concat(toConcat); | ||
if (this.batch.length >= this.maxBatchSize) { | ||
this.flushBatch(); | ||
} | ||
return [2 /*return*/]; | ||
logEvents = Array.isArray(logEvent) ? logEvent : [logEvent]; | ||
return [2 /*return*/, this.postLogEvents(logEvents)]; | ||
}); | ||
@@ -123,16 +105,23 @@ }); | ||
}; | ||
LogflareHttpClient.prototype.flushBatch = function () { | ||
LogflareHttpClient.prototype.postLogEvents = function (batch) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var batchInFlight, payload; | ||
var payload, e_1; | ||
return __generator(this, function (_a) { | ||
if (this.batch.length > 0) { | ||
batchInFlight = __spreadArrays(this.batch); | ||
this.batch = []; | ||
payload = { | ||
batch: batchInFlight, | ||
source: this.sourceToken, | ||
}; | ||
return [2 /*return*/, this.axiosInstance.post("/logs", payload)]; | ||
switch (_a.label) { | ||
case 0: | ||
payload = { | ||
batch: batch, | ||
source: this.sourceToken, | ||
}; | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, this.axiosInstance.post("/logs", 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)); | ||
return [2 /*return*/, e_1]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
@@ -139,0 +128,0 @@ }); |
{ | ||
"name": "logflare-transport-core", | ||
"version": "0.1.1", | ||
"version": "0.1.3", | ||
"description": "A common core for Logflare javascript transports.", | ||
@@ -15,9 +15,15 @@ "keywords": [ | ||
"devDependencies": { | ||
"@babel/core": "^7.10.0", | ||
"@babel/preset-env": "^7.10.0", | ||
"@babel/preset-typescript": "^7.9.0", | ||
"@types/node": "^14.0", | ||
"@types/axios": "^0.14.0", | ||
"@types/es6-promise": "^3.3.0", | ||
"babel-jest": "^26.0.1", | ||
"jest": "^26.0.1", | ||
"moxios": "^0.4.0", | ||
"prettier": "^2.0.5" | ||
}, | ||
"scripts": { | ||
"prepublish": "tsc" | ||
"prepublish": "tsc", | ||
"test": "jest", | ||
"test.watch": "jest --watch" | ||
}, | ||
@@ -31,3 +37,6 @@ "repository": { | ||
}, | ||
"types": "dist/main.d.ts" | ||
"types": "dist/main.d.ts", | ||
"jest": { | ||
"rootDir": "src" | ||
} | ||
} |
@@ -7,63 +7,36 @@ import axios, {AxiosInstance, AxiosResponse} from "axios" | ||
apiKey: string | ||
batchFlushInterval?: number | ||
batchMaxSize?: number | ||
apiBaseUrl?: string | ||
} | ||
interface LogflareTransport { | ||
readonly httpClient: LogflareHttpClient | ||
} | ||
const defaultOptions = { | ||
apiBaseUrl: "https://api.logflare.app", | ||
batchFlushInterval: 1000, | ||
maxBatchSize: 100, | ||
} | ||
class LogflareHttpClient { | ||
protected readonly axiosInstance: AxiosInstance | ||
protected axiosInstance: AxiosInstance | ||
protected readonly sourceToken: string | ||
protected readonly batchFlushInterval: number | ||
protected readonly maxBatchSize: number | ||
protected batch: object[] | ||
protected readonly batchFlushTimer: any | ||
public constructor(options: LogflareUserOptions) { | ||
this.sourceToken = options.sourceToken | ||
if (!options.sourceToken) { | ||
const {sourceToken, apiKey} = options | ||
if (!sourceToken || sourceToken == "") { | ||
throw "Logflare API logging transport source token is NOT configured!" | ||
} | ||
if (!options.apiKey) { | ||
if (!apiKey || apiKey == "") { | ||
throw "Logflare API logging transport api key is NOT configured!" | ||
} | ||
this.batch = [] | ||
this.sourceToken = sourceToken | ||
this.axiosInstance = axios.create({ | ||
baseURL: options.apiBaseUrl, | ||
baseURL: options.apiBaseUrl || defaultOptions.apiBaseUrl, | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-API-KEY": options.apiKey, | ||
"X-API-KEY": apiKey, | ||
}, | ||
}) | ||
this.maxBatchSize = options.batchMaxSize ?? defaultOptions.maxBatchSize | ||
this.batchFlushInterval = | ||
options.batchFlushInterval ?? defaultOptions.batchFlushInterval | ||
this.batchFlushTimer = setInterval( | ||
() => this.flushBatch(), | ||
this.batchFlushInterval | ||
) | ||
this.batchFlushTimer.unref() | ||
this._initializeResponseInterceptor() | ||
} | ||
public async addLogEvent(logEvent: object | [object]) { | ||
const toConcat = Array.isArray(logEvent) ? logEvent : [logEvent] | ||
this.batch = this.batch.concat(toConcat) | ||
if (this.batch.length >= this.maxBatchSize) { | ||
this.flushBatch() | ||
} | ||
public async addLogEvent(logEvent: object | object[]): Promise<object> { | ||
const logEvents = Array.isArray(logEvent) ? logEvent : [logEvent] | ||
return this.postLogEvents(logEvents) | ||
} | ||
@@ -87,12 +60,13 @@ | ||
private async flushBatch() { | ||
if (this.batch.length > 0) { | ||
const batchInFlight = [...this.batch] | ||
this.batch = [] | ||
const payload = { | ||
batch: batchInFlight, | ||
source: this.sourceToken, | ||
} | ||
return this.axiosInstance.post("/logs", payload) | ||
private async postLogEvents(batch: object[]) { | ||
const payload = { | ||
batch, | ||
source: this.sourceToken, | ||
} | ||
try { | ||
return await this.axiosInstance.post("/logs", payload) | ||
} catch (e) { | ||
console.error(`Logflare API request failed with ${e.response.status} status: ${JSON.stringify(e.response.data)}`) | ||
return e | ||
} | ||
} | ||
@@ -111,2 +85,2 @@ | ||
export {LogflareHttpClient, LogflareTransport} | ||
export {LogflareHttpClient} |
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
21280
13
377
8