Skip to content

Commit

Permalink
chore: add info logs
Browse files Browse the repository at this point in the history
  • Loading branch information
rorlic committed Nov 14, 2023
1 parent 167dfff commit b97df3e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
}
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ 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',
body: body,
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);
}
Expand Down

0 comments on commit b97df3e

Please sign in to comment.