Comparing version 1.0.8 to 1.0.9
@@ -53,3 +53,3 @@ export class Telogger { | ||
head: string; | ||
body: string; | ||
body: (string | null)[]; | ||
}; | ||
@@ -89,4 +89,4 @@ fatal: { | ||
emergency(...args: any[]): Promise<any> | undefined; | ||
send(name: any, ...args: any[]): Promise<any> | undefined; | ||
send(name: any, ...init_args: any[]): Promise<any> | undefined; | ||
#private; | ||
} |
@@ -19,3 +19,3 @@ "use strict"; | ||
}; | ||
var _Telogger_instances, _Telogger_apiUrl, _Telogger_spacer, _Telogger_replRe, _Telogger_funcRe, _Telogger_assign, _Telogger_sample, _Telogger_mustBeSent, _Telogger_template, _Telogger_joinTemplate, _Telogger_format, _Telogger_substitution, _Telogger_json, _Telogger_location, _Telogger_sendMessage, _Telogger_request; | ||
var _Telogger_instances, _Telogger_apiUrl, _Telogger_spacer, _Telogger_replRe, _Telogger_funcRe, _Telogger_assign, _Telogger_sample, _Telogger_mustBeSent, _Telogger_template, _Telogger_joinTemplate, _Telogger_adjustArgs, _Telogger_format, _Telogger_substitution, _Telogger_json, _Telogger_location, _Telogger_sendMessage, _Telogger_request; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -30,3 +30,3 @@ exports.Telogger = void 0; | ||
_Telogger_spacer.set(this, ' ›› '); | ||
_Telogger_replRe.set(this, /{{([a-z\(\d\+\-\) >]+)}}/gi); | ||
_Telogger_replRe.set(this, /{{([a-z\(\d\+\-\) >]+)}(\?)?}/gi); | ||
_Telogger_funcRe.set(this, /at\s+[^\(/)]*\(?/i); | ||
@@ -81,3 +81,3 @@ this.channels = { | ||
head: '{{code > location(0)}}', | ||
body: '{{text}}' | ||
body: ['{{text}?}', null, '{{blockquote}}'] | ||
}, | ||
@@ -88,3 +88,3 @@ fatal: { | ||
head: '{{code > location(0)}}', | ||
body: ['{{text}}', null, '{{pre > location(1+)}}'] | ||
body: ['{{text}?}', null, '{{blockquote}}', null, '{{pre > location(1+)}}'] | ||
} | ||
@@ -126,3 +126,3 @@ }; | ||
emergency() { return this.send('emergency', ...Object.values(arguments)); } | ||
send(name, ...args) { | ||
send(name, ...init_args) { | ||
var _a, _b, _c, _d, _e, _f; | ||
@@ -142,2 +142,3 @@ const d = (_a = this.destinations[name]) !== null && _a !== void 0 ? _a : null; | ||
const template = __classPrivateFieldGet(this, _Telogger_instances, "m", _Telogger_joinTemplate).call(this, icon, head, body); | ||
const args = __classPrivateFieldGet(this, _Telogger_instances, "m", _Telogger_adjustArgs).call(this, init_args); | ||
const text = __classPrivateFieldGet(this, _Telogger_instances, "m", _Telogger_format).call(this, template, args); | ||
@@ -220,5 +221,25 @@ const result = teleform_1.html.to_entities(text, true); | ||
return template; | ||
}, _Telogger_adjustArgs = function _Telogger_adjustArgs(init_args) { | ||
const args = []; | ||
for (const ia of init_args) { | ||
if (ia instanceof Error) { | ||
args.push(ia.message); | ||
if (ia.cause) | ||
args.push(ia.cause.message); | ||
} | ||
else | ||
args.push(ia); | ||
} | ||
return args; | ||
}, _Telogger_format = function _Telogger_format(template, args) { | ||
let i = 0; | ||
return template.replace(__classPrivateFieldGet(this, _Telogger_replRe, "f"), (full, expr) => { | ||
const sum = { | ||
args: args.length, | ||
params: template.match(__classPrivateFieldGet(this, _Telogger_replRe, "f")) | ||
.filter(match => !match.includes('location')).length | ||
}; | ||
const replacement = 'telegram-logger‐null'; | ||
return template.replace(__classPrivateFieldGet(this, _Telogger_replRe, "f"), (full, expr, optional) => { | ||
if (optional && sum.args < sum.params--) | ||
return replacement; | ||
const arg = expr.includes('location') ? '' : args[i++]; | ||
@@ -234,3 +255,3 @@ if (arg === undefined) | ||
}, arg); | ||
}); | ||
}).replace(new RegExp(`${replacement}(\\s|${__classPrivateFieldGet(this, _Telogger_spacer, "f")})*`, 'g'), ''); | ||
}, _Telogger_substitution = function _Telogger_substitution(func, text, escape) { | ||
@@ -237,0 +258,0 @@ const regexp = '(bold|italic|underline|' + |
@@ -53,3 +53,3 @@ export class Telogger { | ||
head: string; | ||
body: string; | ||
body: (string | null)[]; | ||
}; | ||
@@ -89,4 +89,4 @@ fatal: { | ||
emergency(...args: any[]): Promise<any> | undefined; | ||
send(name: any, ...args: any[]): Promise<any> | undefined; | ||
send(name: any, ...init_args: any[]): Promise<any> | undefined; | ||
#private; | ||
} |
@@ -7,3 +7,3 @@ import axios from 'axios'; | ||
#spacer = ' ›› '; | ||
#replRe = /{{([a-z\(\d\+\-\) >]+)}}/gi; | ||
#replRe = /{{([a-z\(\d\+\-\) >]+)}(\?)?}/gi; | ||
#funcRe = /at\s+[^\(/)]*\(?/i; | ||
@@ -60,3 +60,3 @@ appTitle; | ||
head: '{{code > location(0)}}', | ||
body: '{{text}}' | ||
body: ['{{text}?}', null, '{{blockquote}}'] | ||
}, | ||
@@ -67,3 +67,3 @@ fatal: { | ||
head: '{{code > location(0)}}', | ||
body: ['{{text}}', null, '{{pre > location(1+)}}'] | ||
body: ['{{text}?}', null, '{{blockquote}}', null, '{{pre > location(1+)}}'] | ||
} | ||
@@ -143,3 +143,3 @@ }; | ||
emergency() { return this.send('emergency', ...Object.values(arguments)); } | ||
send(name, ...args) { | ||
send(name, ...init_args) { | ||
const d = this.destinations[name] ?? null; | ||
@@ -158,2 +158,3 @@ if (!d || !d?.to) | ||
const template = this.#joinTemplate(icon, head, body); | ||
const args = this.#adjustArgs(init_args); | ||
const text = this.#format(template, args); | ||
@@ -201,5 +202,26 @@ const result = html.to_entities(text, true); | ||
} | ||
#adjustArgs(init_args) { | ||
const args = []; | ||
for (const ia of init_args) { | ||
if (ia instanceof Error) { | ||
args.push(ia.message); | ||
if (ia.cause) | ||
args.push(ia.cause.message); | ||
} | ||
else | ||
args.push(ia); | ||
} | ||
return args; | ||
} | ||
#format(template, args) { | ||
let i = 0; | ||
return template.replace(this.#replRe, (full, expr) => { | ||
const sum = { | ||
args: args.length, | ||
params: template.match(this.#replRe) | ||
.filter(match => !match.includes('location')).length | ||
}; | ||
const replacement = 'telegram-logger‐null'; | ||
return template.replace(this.#replRe, (full, expr, optional) => { | ||
if (optional && sum.args < sum.params--) | ||
return replacement; | ||
const arg = expr.includes('location') ? '' : args[i++]; | ||
@@ -215,3 +237,3 @@ if (arg === undefined) | ||
}, arg); | ||
}); | ||
}).replace(new RegExp(`${replacement}(\\s|${this.#spacer})*`, 'g'), ''); | ||
} | ||
@@ -218,0 +240,0 @@ #substitution(func, text, escape) { |
{ | ||
"name": "telogger", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "Log information to Telegram.", | ||
@@ -14,3 +14,3 @@ "main": "dist/cjs/main.js", | ||
"scripts": { | ||
"test": "exit 1", | ||
"test": "node test/telogger.test.js", | ||
"build": "rm -fr dist/* && tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && ./addtype" | ||
@@ -17,0 +17,0 @@ }, |
@@ -25,4 +25,16 @@ # Telogger | ||
telogger.error('Unfortunately, errors are inevitable.') | ||
telogger.fatal('Something really bad happened. Goodbye!') | ||
telogger.fatal('Something really bad happened. Goodbye!' | ||
``` | ||
You can pass `Error` to Telogger as well: | ||
``` | ||
try { | ||
// db.connect() | ||
} catch(error) { | ||
// telogger.error(error) | ||
// or | ||
// telogger.error('Connecting to database failed.', error) | ||
// or | ||
telogger.error(new Error('Connecting to database failed.', { cause: error })) | ||
} | ||
``` | ||
@@ -145,3 +157,3 @@ ### Advanced Setups | ||
You can combine several keywords in your templates, for example: `{{code > location}}`. | ||
You can also combine several keywords in your templates (`{{code > location}}`) and use optional blocks (`{{text}?}`). | ||
@@ -148,0 +160,0 @@ ## Contributing |
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
37561
816
162