added logging interface

This commit is contained in:
balzack 2024-07-08 08:56:46 -07:00
parent a40456de5c
commit 7b02b5a33b
16 changed files with 101 additions and 48 deletions

View File

@ -86,7 +86,7 @@ jest.mock('../src/ring', () => {
test('allocates session correctly', async () => {
let status: string = '';
const sdk = new DatabagSDK(null);
const sdk = new DatabagSDK(null, null);
const params: SessionParams = { topicBatch: 0, tagBatch: 0, channelTypes: [], pushType: '', deviceToken: '', notifications: [], deviceId: '', version: '', appName: '' };
const session = await sdk.login('handle', 'password', 'url', null, params);
session.addStatusListener((ev: string) => { status = ev; });

View File

@ -1,5 +1,5 @@
import { EventEmitter } from 'eventemitter3';
import { type Account } from './api';
import type { Account, Logging } from './api';
import type { AccountStatus } from './types';
export class AccountModule implements Account {
@ -7,8 +7,10 @@ export class AccountModule implements Account {
private emitter: EventEmitter;
private token: string;
private url: string;
private log: Logging;
constructor(token: string, url: string) {
constructor(log: Logging, token: string, url: string) {
this.log = log;
this.emitter = new EventEmitter();
this.token = token;
this.url = url;

View File

@ -1,17 +1,19 @@
import { EventEmitter } from 'eventemitter3';
import type { Alias, Account } from './api';
import type { Alias, Account, Logging } from './api';
import type { Group } from './types';
export class AliasModule implements Alias {
private log: Logging;
private token: string;
private url: string;
private account: Account;
private emitter: EventEmitter;
constructor(token: string, url: string, account: Account) {
constructor(log: Logging, token: string, url: string, account: Account) {
this.token = token;
this.url = url;
this.log = log;
this.account = account;
this.emitter = new EventEmitter();
}

View File

@ -21,6 +21,12 @@ export interface WebStore {
export interface Crypto {
}
export interface Logging {
error(m: any): void;
warn(m: any): void;
info(m: any): void;
}
export interface Session {
close(): void;

View File

@ -1,17 +1,19 @@
import { EventEmitter } from 'eventemitter3';
import type { Attribute, Account } from './api';
import type { Attribute, Account, Logging } from './api';
import type { Article } from './types';
export class AttributeModule implements Attribute {
private log: Logging;
private token: string;
private url: string;
private account: Account;
private emitter: EventEmitter;
constructor(token: string, url: string, account: Account) {
constructor(log: Logging, token: string, url: string, account: Account) {
this.token = token;
this.url = url;
this.log = log;
this.account = account;
this.emitter = new EventEmitter();
}

View File

@ -1,9 +1,12 @@
import { type Bot } from './api';
import type { Bot, Logging } from './api';
import type { Asset } from './types';
export class BotModule implements Bot {
constructor() {
private log: Logging;
constructor(log: Logging) {
this.log = log;
}
public async addTopic(server: string, token: string, type: string, message: string, assets: Asset[]): Promise<string> {

View File

@ -1,15 +1,18 @@
import { EventEmitter } from 'eventemitter3';
import { Logging } from './api';
import { Revision } from './entities';
import { Call } from './types';
export class Connection {
private log: Logging;
private closed: boolean;
private emitter: EventEmitter;
private websocket: WebSocket;
constructor(token: string, url: string) {
constructor(log: Logging, token: string, url: string) {
this.closed = false;
this.log = log;
this.emitter = new EventEmitter();
this.websocket = this.setWebSocket(token, url);
}

View File

@ -1,16 +1,18 @@
import { EventEmitter } from 'eventemitter3';
import type { Contact } from './api';
import type { Contact, Logging } from './api';
import type { Card, Topic, Asset, Tag, Profile, Repeater} from './types';
export class ContactModule implements Contact {
private log: Logging;
private token: string;
private url: string;
private emitter: EventEmitter;
constructor(token: string, url: string) {
constructor(log: Logging, token: string, url: string) {
this.token = token;
this.url = url;
this.log = log;
this.emitter = new EventEmitter();
}

View File

@ -1,17 +1,19 @@
import { EventEmitter } from 'eventemitter3';
import type { Content, Account } from './api';
import type { Content, Account, Logging } from './api';
import type { Channel, Topic, Asset, Tag, Repeater } from './types';
export class ContentModule implements Content {
private log: Logging;
private token: string;
private url: string;
private account: Account;
private emitter: EventEmitter;
constructor(token: string, url: string, account: Account) {
constructor(log: Logging, token: string, url: string, account: Account) {
this.token = token;
this.url = url;
this.log = log;
this.account = account;
this.emitter = new EventEmitter();
}

View File

@ -1,5 +1,5 @@
import { EventEmitter } from 'eventemitter3';
import type { Identity, Contact, Content, Focus } from './api';
import type { Identity, Contact, Content, Focus, Logging } from './api';
import type { Topic, Asset, Repeater } from './types';
export class FocusModule implements Focus {
@ -9,14 +9,16 @@ export class FocusModule implements Focus {
private content: Content;
private cardId: string | null;
private channelId: string;
private log: Logging;
private emitter: EventEmitter;
constructor(identity: Identity, contact: Contact, content: Content, cardId: string | null, channelId: string) {
constructor(log: Logging, identity: Identity, contact: Contact, content: Content, cardId: string | null, channelId: string) {
this.identity = identity;
this.contact = contact;
this.content = content;
this.cardId = cardId;
this.channelId = channelId;
this.log = log;
this.emitter = new EventEmitter();
}

View File

@ -1,16 +1,18 @@
import { EventEmitter } from 'eventemitter3';
import { type Identity } from './api';
import { type Profile } from './types';
import type { Identity, Logging } from './api';
import type { Profile } from './types';
export class IdentityModule implements Identity {
private token: string;
private url: string;
private log: Logging;
private emitter: EventEmitter;
constructor(token: string, url: string) {
constructor(log: Logging, token: string, url: string) {
this.token = token;
this.url = url;
this.log = log;
this.emitter = new EventEmitter();
}

View File

@ -1,8 +1,9 @@
import { SessionModule } from './session';
import { NodeModule } from './node';
import { BotModule } from './bot';
import { ConsoleLogging } from './logging';
import type { Session, Node, Bot, SqlStore, WebStore } from './api';
import type { Session, Node, Bot, SqlStore, WebStore, Crypto, Logging } from './api';
import type { SessionParams } from './types';
export * from './api';
@ -10,36 +11,38 @@ export * from './types';
export class DatabagSDK {
private logging: Logging;
private crypto: Crypto | null;
private store: SqlStore | WebStore | null = null;
constructor(crypto: Crypto | null) {
console.log("databag sdk");
constructor(crypto: Crypto | null, logging: Logging | null) {
this.crypto = crypto;
this.logging = logging ? logging : new ConsoleLogging();
this.logging.info("databag sdk");
}
public async initOfflineStore(sql: SqlStore): Promise<Session | null> {
this.store = sql;
// initialize
return new SessionModule(this.store, this.crypto, '', '');
return new SessionModule(this.store, this.crypto, this.logging, '', '');
}
public async initOnlineStore(web: WebStore): Promise<Session | null> {
this.store = web;
// initialize
return new SessionModule(this.store, this.crypto, '', '');
return new SessionModule(this.store, this.crypto, this.logging, '', '');
}
public async login(handle: string, password: string, url: string, mfaCode: string | null, params: SessionParams): Promise<Session> {
return new SessionModule(this.store, this.crypto, '', '');
return new SessionModule(this.store, this.crypto, this.logging, '', '');
}
public async access(url: string, token: string, params: SessionParams): Promise<Session> {
return new SessionModule(this.store, this.crypto, '', '');
return new SessionModule(this.store, this.crypto, this.logging, '', '');
}
public async create(handle: string, password: string, url: string, token: string | null, params: SessionParams): Promise<Session> {
return new SessionModule(this.store, this.crypto, '', '');
return new SessionModule(this.store, this.crypto, this.logging, '', '');
}
public async logout(session: Session): Promise<void> {
@ -47,10 +50,10 @@ export class DatabagSDK {
}
public async configure(token: string, url: string, mfaCode: string | null): Promise<Node> {
return new NodeModule('', '');
return new NodeModule(this.logging, '', '');
}
public async automate() {
return new BotModule();
return new BotModule(this.logging);
}
}

View File

@ -1,14 +1,16 @@
import { type Node } from './api';
import type { Node, Logging } from './api';
import type { NodeAccount, NodeConfig } from './types';
export class NodeModule implements Node {
private log: Logging;
private token: string;
private url: string;
constructor(token: string, url: string) {
constructor(log: Logging, token: string, url: string) {
this.token = token;
this.url = url;
this.log = log;
}
public async createAccountAccess(): Promise<string> {

View File

@ -1,12 +1,14 @@
import { EventEmitter } from 'eventemitter3';
import type { Ring } from './api';
import type { Ring, Logging } from './api';
import type { Call } from './types';
export class RingModule implements Ring {
private log: Logging;
private emitter: EventEmitter;
constructor() {
constructor(log: Logging) {
this.log = log;
this.emitter = new EventEmitter();
}

View File

@ -12,7 +12,7 @@ import { RingModule } from './ring';
import { Connection } from './connection';
import type { Session, SqlStore, WebStore, Crypto, Account, Identity, Contact, Ring, Alias, Attribute, Content, Stream, Focus } from './api';
import type { Session, SqlStore, WebStore, Account, Identity, Contact, Ring, Alias, Attribute, Content, Stream, Focus, Crypto, Logging } from './api';
import { Revision } from './entities';
import { Call } from './types';
@ -21,6 +21,7 @@ export class SessionModule implements Session {
private emitter: EventEmitter;
private store: SqlStore | WebStore | null;
private crypto: Crypto | null;
private log: Logging;
private token: string;
private url: string;
private syncRevision: Revision | null;
@ -35,25 +36,26 @@ export class SessionModule implements Session {
private ring: RingModule;
private connection: Connection;
constructor(store: SqlStore | WebStore | null, crypto: Crypto | null, token: string, url: string) {
constructor(store: SqlStore | WebStore | null, crypto: Crypto | null, log: Logging, token: string, url: string) {
this.store = store;
this.crypto = crypto;
this.log = log;
this.token = token;
this.url = url;
this.syncRevision = null;
this.status = 'connecting'
this.emitter = new EventEmitter();
this.account = new AccountModule(token, url);
this.identity = new IdentityModule(token, url);
this.contact = new ContactModule(token, url);
this.alias = new AliasModule(token, url, this.account);
this.attribute = new AttributeModule(token, url, this.account);
this.content = new ContentModule(token, url, this.account);
this.stream = new StreamModule(this.contact, this.content);
this.ring = new RingModule();
this.connection = new Connection(token, url);
this.account = new AccountModule(log, token, url);
this.identity = new IdentityModule(log, token, url);
this.contact = new ContactModule(log, token, url);
this.alias = new AliasModule(log, token, url, this.account);
this.attribute = new AttributeModule(log, token, url, this.account);
this.content = new ContentModule(log, token, url, this.account);
this.stream = new StreamModule(log, this.contact, this.content);
this.ring = new RingModule(log);
this.connection = new Connection(log, token, url);
const onStatus = (ev: string) => {
this.status = ev;
@ -74,6 +76,7 @@ export class SessionModule implements Session {
}
}
catch(err) {
this.log.warn(err);
this.syncRevision = ev;
this.emitter.emit('status', this.getStatus());
}
@ -103,7 +106,22 @@ export class SessionModule implements Session {
return this.status;
}
public resync() {
public async resync() {
if (this.syncRevision) {
try {
await this.identity.setRevision(this.syncRevision.profile);
await this.account.setRevision(this.syncRevision.account);
await this.contact.setRevision(this.syncRevision.card);
await this.attribute.setRevision(this.syncRevision.article);
await this.alias.setRevision(this.syncRevision.group);
await this.content.setRevision(this.syncRevision.channel);
this.syncRevision = null
this.emitter.emit('status', this.getStatus());
}
catch(err) {
this.log.warn(err);
}
}
}
public close() {
@ -150,7 +168,7 @@ export class SessionModule implements Session {
}
public addFocus(cardId: string | null, channelId: string): Focus {
return new FocusModule(this.identity, this.contact, this.content, cardId, channelId);
return new FocusModule(this.log, this.identity, this.contact, this.content, cardId, channelId);
}
public removeFocus(focus: Focus): void {

View File

@ -1,16 +1,18 @@
import { EventEmitter } from 'eventemitter3';
import type { Contact, Content, Stream } from './api';
import type { Contact, Content, Stream, Logging } from './api';
import type { Channel } from './types';
export class StreamModule implements Stream {
private log: Logging;
private contact: Contact;
private content: Content;
private emitter: EventEmitter;
constructor(contact: Contact, content: Content) {
constructor(log: Logging, contact: Contact, content: Content) {
this.contact = contact;
this.content = content;
this.log = log;
this.emitter = new EventEmitter();
}