From b97df3ed466a071fc003db0689dc06012aea394e Mon Sep 17 00:00:00 2001 From: Ranko Orlic Date: Tue, 14 Nov 2023 11:48:44 +0100 Subject: [PATCH] chore: add info logs --- src/generator.ts | 4 ++-- src/index.ts | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/generator.ts b/src/generator.ts index 5a3d7d9..49ae89a 100644 --- a/src/generator.ts +++ b/src/generator.ts @@ -13,13 +13,13 @@ export class Generator { return new Date().toISOString(); } - public createNext(): any { + public createNext(): string { var data = { index: this.index(), timestamp: this.timestamp() }; var next = Mustache.render(this._template, data); return next; } - public createRange(range: number): any[] { + public createRange(range: number): string[] { var timestamp = this.timestamp(); return Array.from(Array(range).keys()).map(index => Mustache.render(this._template, { index: index + 1, timestamp: timestamp })); } diff --git a/src/index.ts b/src/index.ts index 57ac84e..7644c42 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,11 +17,13 @@ const generator: Generator = new Generator(template); const range = Number.parseInt(args['range'] || '0'); const job = new CronJob(cron, async () => { + const timestamp = new Date().toISOString(); const messages = range ? generator.createRange(range) : [generator.createNext()]; for await (const body of messages) { const targetUrl = args['targetUrl'] || (existsSync('./TARGETURL') && readFileSync('./TARGETURL', 'utf-8').trimEnd()); if (targetUrl) { if (!mimeType) throw new Error('Missing mimeType'); + if (!silent) console.debug(`Sending to '${targetUrl}':`, body); const response = await fetch(targetUrl, { method: 'post', @@ -29,6 +31,7 @@ const job = new CronJob(cron, async () => { headers: {'Content-Type': mimeType} }); if (!silent) console.debug(`Response: ${response.statusText}`); + if (silent) console.info(`[${timestamp}] POST ${targetUrl} ${response.status}`); } else { // if no targetUrl specified, send to console console.info(body); }