integreat
Version:
Node.js integration layer
38 lines • 1.27 kB
JavaScript
const isSuccessStatus = (status) => typeof status === 'string' && ['ok', 'noaction'].includes(status);
export default class Connection {
#transporter;
#options;
#connection;
#emit;
constructor(transporter, options, emit) {
this.#transporter = transporter;
this.#options = options;
this.#connection = null;
this.#emit = emit;
}
async connect(auth) {
if (typeof this.#transporter.connect === 'function') {
this.#connection = (await this.#transporter.connect(this.#options, auth || null, this.#connection?.status === 'ok' ? this.#connection : null, this.#emit)) || { status: 'ok' };
}
else {
this.#connection = { status: 'ok' };
}
return isSuccessStatus(this.#connection?.status);
}
async disconnect() {
if (typeof this.#transporter.disconnect === 'function') {
await this.#transporter.disconnect(this.#connection);
}
this.#connection = null;
}
get status() {
return this.#connection?.status || null;
}
get error() {
return this.#connection?.error || null;
}
get object() {
return this.#connection || null;
}
}
//# sourceMappingURL=Connection.js.map