node-ebics-client/lib/storages/tracesStorage.js

64 lines
1.0 KiB
JavaScript
Raw Normal View History

2018-08-31 05:50:18 +00:00
'use strict';
const fs = require('fs');
2020-03-06 04:47:56 +00:00
const { v1: uuidv1 } = require('uuid');
2018-08-31 05:50:18 +00:00
2019-11-05 03:54:09 +00:00
const traceName = (uuid, label, type, ext = 'xml') => `${uuid}_${label}_${type}.${ext}`;
2018-08-31 05:50:18 +00:00
module.exports = dir => ({
traceData: '',
traceLabel: '',
lastTraceID: null,
connectToLastTrace: false,
label(str) {
this.traceLabel = str;
return this;
},
data(data) {
if (!data)
throw Error('No trace given to be persisted.');
this.traceData = data;
return this;
},
ofType(type) {
this.type = type;
return this;
},
new() {
this.connectToLastTrace = false;
return this;
},
connect() {
this.connectToLastTrace = true;
return this;
},
persist() {
if (!dir)
throw Error('No directory to save the traces to provided.');
this.lastTraceID = this.connectToLastTrace ? this.lastTraceID : uuidv1();
const name = traceName(this.lastTraceID, this.traceLabel, this.type);
const path = `${dir}/${name}`;
try {
fs.writeFileSync(path, this.traceData);
} catch (error) {
throw error;
}
},
});