@timberio/node
Advanced tools
Comparing version 0.32.0 to 0.33.0
@@ -11,3 +11,3 @@ /// <reference types="node" /> | ||
private _writeStream?; | ||
constructor(apiKey: string, options?: Partial<ITimberOptions>); | ||
constructor(apiKey: string, sourceKey: string, options?: Partial<ITimberOptions>); | ||
/** | ||
@@ -14,0 +14,0 @@ * Override `Base` log to enable Node.js streaming |
@@ -7,4 +7,2 @@ "use strict"; | ||
const cross_fetch_1 = __importDefault(require("cross-fetch")); | ||
// import Msgpack from "msgpack5"; | ||
const tools_1 = require("@timberio/tools"); | ||
const core_1 = require("@timberio/core"); | ||
@@ -14,7 +12,7 @@ // Namespace the msgpack library | ||
class Node extends core_1.Base { | ||
constructor(apiKey, options) { | ||
super(apiKey, options); | ||
constructor(apiKey, sourceKey, options) { | ||
super(apiKey, sourceKey, options); | ||
// Sync function | ||
const sync = async (logs) => { | ||
const res = await cross_fetch_1.default(this._options.endpoint, { | ||
const res = await cross_fetch_1.default(`${this._options.endpoint}/sources/${this._sourceKey}/frames`, { | ||
method: "POST", | ||
@@ -24,4 +22,4 @@ headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Basic ${tools_1.base64Encode(this._apiKey)}`, | ||
"User-Agent": "timber-js(node)" | ||
Authorization: `Bearer ${this._apiKey}`, | ||
"User-Agent": "timber-js(node)", | ||
}, | ||
@@ -31,3 +29,3 @@ // body: logs.map(log => `${log.level}: ${log.message}`).join("\n") | ||
// TODO - using JSON for now; switch to msgpack later | ||
body: JSON.stringify(logs) | ||
body: JSON.stringify(logs), | ||
}); | ||
@@ -34,0 +32,0 @@ if (res.ok) { |
@@ -27,3 +27,3 @@ "use strict"; | ||
level: types_1.LogLevel.Info, | ||
message | ||
message, | ||
}; | ||
@@ -33,8 +33,9 @@ } | ||
it("should echo log if timber sends 20x status code", async () => { | ||
const source = "someSource"; | ||
nock_1.default("https://logs.timber.io") | ||
.post("/frames") | ||
.post(`/sources/${source}/frames`) | ||
.reply(201); | ||
const message = String(Math.random()); | ||
const expectedLog = getRandomLog(message); | ||
const node = new node_1.Node("valid api key"); | ||
const node = new node_1.Node("valid api key", source); | ||
const echoedLog = await node.log(message); | ||
@@ -44,6 +45,7 @@ expect(echoedLog.message).toEqual(expectedLog.message); | ||
it("should throw error if timber sends non 200 status code", async () => { | ||
const source = "someSource"; | ||
nock_1.default("https://logs.timber.io") | ||
.post("/frames") | ||
.post(`/sources/${source}/frames`) | ||
.reply(401); | ||
const node = new node_1.Node("invalid api key"); | ||
const node = new node_1.Node("invalid api key", "someSource"); | ||
const message = String(Math.random); | ||
@@ -61,6 +63,6 @@ await expect(node.log(message)).rejects.toThrow(); | ||
callback(); | ||
} | ||
}, | ||
}); | ||
// Fixtures | ||
const timber = new node_1.Node("test"); | ||
const timber = new node_1.Node("test", "someSource"); | ||
timber.pipe(writeStream); | ||
@@ -81,3 +83,3 @@ const message = "This should be streamed"; | ||
// Pass write stream to Timber | ||
const timber = new node_1.Node("test"); | ||
const timber = new node_1.Node("test", "someSource"); | ||
timber.pipe(passThrough).pipe(writeStream); | ||
@@ -84,0 +86,0 @@ // Mock the sync method by simply returning the same logs |
@@ -11,3 +11,3 @@ /// <reference types="node" /> | ||
private _writeStream?; | ||
constructor(apiKey: string, options?: Partial<ITimberOptions>); | ||
constructor(apiKey: string, sourceKey: string, options?: Partial<ITimberOptions>); | ||
/** | ||
@@ -14,0 +14,0 @@ * Override `Base` log to enable Node.js streaming |
import fetch from "cross-fetch"; | ||
// import Msgpack from "msgpack5"; | ||
import { base64Encode } from "@timberio/tools"; | ||
import { Base } from "@timberio/core"; | ||
@@ -8,7 +6,7 @@ // Namespace the msgpack library | ||
export class Node extends Base { | ||
constructor(apiKey, options) { | ||
super(apiKey, options); | ||
constructor(apiKey, sourceKey, options) { | ||
super(apiKey, sourceKey, options); | ||
// Sync function | ||
const sync = async (logs) => { | ||
const res = await fetch(this._options.endpoint, { | ||
const res = await fetch(`${this._options.endpoint}/sources/${this._sourceKey}/frames`, { | ||
method: "POST", | ||
@@ -18,4 +16,4 @@ headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Basic ${base64Encode(this._apiKey)}`, | ||
"User-Agent": "timber-js(node)" | ||
Authorization: `Bearer ${this._apiKey}`, | ||
"User-Agent": "timber-js(node)", | ||
}, | ||
@@ -25,3 +23,3 @@ // body: logs.map(log => `${log.level}: ${log.message}`).join("\n") | ||
// TODO - using JSON for now; switch to msgpack later | ||
body: JSON.stringify(logs) | ||
body: JSON.stringify(logs), | ||
}); | ||
@@ -28,0 +26,0 @@ if (res.ok) { |
@@ -15,3 +15,3 @@ import * as os from "os"; | ||
level: LogLevel.Info, | ||
message | ||
message, | ||
}; | ||
@@ -21,8 +21,9 @@ } | ||
it("should echo log if timber sends 20x status code", async () => { | ||
const source = "someSource"; | ||
nock("https://logs.timber.io") | ||
.post("/frames") | ||
.post(`/sources/${source}/frames`) | ||
.reply(201); | ||
const message = String(Math.random()); | ||
const expectedLog = getRandomLog(message); | ||
const node = new Node("valid api key"); | ||
const node = new Node("valid api key", source); | ||
const echoedLog = await node.log(message); | ||
@@ -32,6 +33,7 @@ expect(echoedLog.message).toEqual(expectedLog.message); | ||
it("should throw error if timber sends non 200 status code", async () => { | ||
const source = "someSource"; | ||
nock("https://logs.timber.io") | ||
.post("/frames") | ||
.post(`/sources/${source}/frames`) | ||
.reply(401); | ||
const node = new Node("invalid api key"); | ||
const node = new Node("invalid api key", "someSource"); | ||
const message = String(Math.random); | ||
@@ -49,6 +51,6 @@ await expect(node.log(message)).rejects.toThrow(); | ||
callback(); | ||
} | ||
}, | ||
}); | ||
// Fixtures | ||
const timber = new Node("test"); | ||
const timber = new Node("test", "someSource"); | ||
timber.pipe(writeStream); | ||
@@ -69,3 +71,3 @@ const message = "This should be streamed"; | ||
// Pass write stream to Timber | ||
const timber = new Node("test"); | ||
const timber = new Node("test", "someSource"); | ||
timber.pipe(passThrough).pipe(writeStream); | ||
@@ -72,0 +74,0 @@ // Mock the sync method by simply returning the same logs |
{ | ||
"name": "@timberio/node", | ||
"version": "0.32.0", | ||
"version": "0.33.0", | ||
"description": "Timber.io - Node.js logger", | ||
@@ -43,5 +43,4 @@ "keywords": [ | ||
"dependencies": { | ||
"@timberio/core": "^0.32.0", | ||
"@timberio/tools": "^0.32.0", | ||
"@timberio/types": "^0.32.0", | ||
"@timberio/core": "^0.33.0", | ||
"@timberio/types": "^0.33.0", | ||
"@types/msgpack5": "^3.4.1", | ||
@@ -51,3 +50,3 @@ "cross-fetch": "^2.2.3", | ||
}, | ||
"gitHead": "40b5f1cf02fb2552da392a3e04cb06a66453a21e" | ||
"gitHead": "6ebcfc9c1d10df24abd9525aca1c2784e22646fc" | ||
} |
@@ -41,6 +41,6 @@ # 🌲 Timber - Node.js logging | ||
Simply pass your [Timber.io](https://timber.io) API key as a parameter to a new `Timber` instance: | ||
Simply pass your [Timber.io](https://timber.io) organization API + source keys as parameters to a new `Timber` instance (you can grab both from the Timber.io console): | ||
```typescript | ||
const timber = new Timber("api-goes-here"); | ||
const timber = new Timber("timber-organization-key", "timber-source-key"); | ||
``` | ||
@@ -76,3 +76,3 @@ | ||
// Create a new Timber instance, and pipe output to `logs.txt` | ||
const timber = new Timber("api-key-here"); | ||
const timber = new Timber("timber-organization-key", "timber-source-key"); | ||
timber.pipe(logsTxt); | ||
@@ -79,0 +79,0 @@ |
@@ -18,3 +18,3 @@ import * as os from "os"; | ||
level: LogLevel.Info, | ||
message | ||
message, | ||
}; | ||
@@ -25,4 +25,5 @@ } | ||
it("should echo log if timber sends 20x status code", async () => { | ||
const source = "someSource"; | ||
nock("https://logs.timber.io") | ||
.post("/frames") | ||
.post(`/sources/${source}/frames`) | ||
.reply(201); | ||
@@ -32,3 +33,3 @@ | ||
const expectedLog = getRandomLog(message); | ||
const node = new Node("valid api key"); | ||
const node = new Node("valid api key", source); | ||
const echoedLog = await node.log(message); | ||
@@ -39,7 +40,8 @@ expect(echoedLog.message).toEqual(expectedLog.message); | ||
it("should throw error if timber sends non 200 status code", async () => { | ||
const source = "someSource"; | ||
nock("https://logs.timber.io") | ||
.post("/frames") | ||
.post(`/sources/${source}/frames`) | ||
.reply(401); | ||
const node = new Node("invalid api key"); | ||
const node = new Node("invalid api key", "someSource"); | ||
const message: string = String(Math.random); | ||
@@ -55,3 +57,3 @@ await expect(node.log(message)).rejects.toThrow(); | ||
encoding: string, | ||
callback: (error?: Error | null) => void | ||
callback: (error?: Error | null) => void, | ||
): void { | ||
@@ -65,7 +67,7 @@ // Will be a buffered JSON string -- parse | ||
callback(); | ||
} | ||
}, | ||
}); | ||
// Fixtures | ||
const timber = new Node("test"); | ||
const timber = new Node("test", "someSource"); | ||
timber.pipe(writeStream); | ||
@@ -93,3 +95,3 @@ | ||
// Pass write stream to Timber | ||
const timber = new Node("test"); | ||
const timber = new Node("test", "someSource"); | ||
timber.pipe(passThrough).pipe(writeStream); | ||
@@ -96,0 +98,0 @@ |
@@ -6,3 +6,2 @@ import { Duplex, Writable } from "stream"; | ||
import { base64Encode } from "@timberio/tools"; | ||
import { ITimberLog, Context, ITimberOptions, LogLevel } from "@timberio/types"; | ||
@@ -21,22 +20,29 @@ import { Base } from "@timberio/core"; | ||
public constructor(apiKey: string, options?: Partial<ITimberOptions>) { | ||
super(apiKey, options); | ||
public constructor( | ||
apiKey: string, | ||
sourceKey: string, | ||
options?: Partial<ITimberOptions>, | ||
) { | ||
super(apiKey, sourceKey, options); | ||
// Sync function | ||
const sync = async (logs: ITimberLog[]): Promise<ITimberLog[]> => { | ||
const res = await fetch(this._options.endpoint, { | ||
method: "POST", | ||
headers: { | ||
// "Content-Type": "application/msgpack", | ||
"Content-Type": "application/json", | ||
Authorization: `Basic ${base64Encode(this._apiKey)}`, | ||
"User-Agent": "timber-js(node)" | ||
const res = await fetch( | ||
`${this._options.endpoint}/sources/${this._sourceKey}/frames`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
// "Content-Type": "application/msgpack", | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${this._apiKey}`, | ||
"User-Agent": "timber-js(node)", | ||
}, | ||
// body: logs.map(log => `${log.level}: ${log.message}`).join("\n") | ||
// body: msgpack.encode(logsWithSchema).slice() | ||
// TODO - using JSON for now; switch to msgpack later | ||
body: JSON.stringify(logs), | ||
}, | ||
// body: logs.map(log => `${log.level}: ${log.message}`).join("\n") | ||
// body: msgpack.encode(logsWithSchema).slice() | ||
); | ||
// TODO - using JSON for now; switch to msgpack later | ||
body: JSON.stringify(logs) | ||
}); | ||
if (res.ok) { | ||
@@ -68,3 +74,3 @@ return logs; | ||
level?: LogLevel, | ||
context: TContext = {} as TContext | ||
context: TContext = {} as TContext, | ||
) { | ||
@@ -71,0 +77,0 @@ // Process/sync the log, per `Base` logic |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
37052
5
568
+ Added@timberio/core@0.33.0(transitive)
+ Added@timberio/tools@0.33.0(transitive)
+ Added@timberio/types@0.33.0(transitive)
- Removed@timberio/tools@^0.32.0
- Removed@timberio/core@0.32.0(transitive)
- Removed@timberio/tools@0.32.0(transitive)
- Removed@timberio/types@0.32.0(transitive)
Updated@timberio/core@^0.33.0
Updated@timberio/types@^0.33.0