import { Observable } from 'rxjs'; import { RayconnectBase } from './core/base'; import { RayconnectSdk } from './core/sdk' import { Options, CallType, ExecuteType, QueryType, ServerData } from './core/types' import { NimkatDB } from './storage/nimkat' class Rayconnect extends RayconnectSdk { private plugin: any = undefined private nimkat: NimkatDB = new NimkatDB() public fn: any = {}; static abi = false; constructor(ops: Options = RayconnectBase.DefaultOps, token: any = undefined, config: any = true) { super(ops, token, config); this.findABI().then((data: any) => { if (data.value) { this.ParseFn(data.value) } }) .catch((e: any) => { // console.log(e) }) } private ParseFn(abis: any) { for (const abi of abis) { const address = escape(abi.address); const scope = escape(abi.scope); const method = escape(abi.method); const user = escape(abi.user); const token = escape(abi.token); const name = escape(abi.name.replace(/\ /g, "").replace(/\/n/g, "")); eval(` const self = this; this.fn.${name} = function(data){ return self.Run({ address:'${address}', scope:'${scope}', method:'${method}', user:'${user}', token:'${token}', data:data }) } `); } } /** * * @param string * @name Plugin * use plugin */ Plugin(param: string) { this.plugin = param return this } /** * * @param CallType * @name Call * call query without response :) */ Call(param: CallType) { const exec: ExecuteType = this.makeExec(param, this.plugin); return this.execQuery(exec) } /** * * @param CallType * @name Run * call query with response :) */ Run(param: CallType) { const exec: ExecuteType = this.makeExec(param, this.plugin); return this.RequestBack(exec) } account = { otp: { verify: this.VerifyPhone, getCode: this.RequestOTP }, signIn: async (email: string, password: string) => { const result = await this.LoginWithPassword({ username: email, password }) const token_key = `rayconnect-client-${this.ops.appID}-token`; if (result.status) { localStorage.setItem(token_key, result.data.token) } return result; }, signUp: (email: string, password: string, phone: string) => { return this.RegisterUser({ email: email, password, phone }) }, signOut: () => { }, changePassword: () => { }, changeUsername: () => { }, resetPassword: () => { }, destroy: () => { }, change: () => { } } store = { run: () => { return this.nimkat.loadPermission(); }, add: (type: string, attributes: object) => { return this.nimkat.create(type, attributes) }, find: (type: string, id: string) => { return this.nimkat.find(type, id); }, findByQuery: (type: string, query: any) => { return this.nimkat.findByQuery(type, query); }, findAll: async (type: string = "+all") => { if (!this.authed) { if (this.device?.type == 'browser') { const data = await this.getFromNimkatApi(type); const result = await data?.json() return result['refs']; } } return this.nimkat.findAll(type); }, remove: (type: string, id: string) => { return this.nimkat.delete(type, id); }, update: (type: string, id: string, attributes: object) => { return this.nimkat.update(type, attributes, id); }, on: (event: string, callback: Function) => { }, changeAccess: (type: string, action: string, userID: string) => { return this.nimkat.changePermissionAccess(action, type, userID); }, hasAccess: (action: string) => { return this.nimkat.hasPermission(action); } } Func(queryObj: QueryType): Observable { const self = this const event = `exec_gram_${queryObj.address}` const observable: Observable = new Observable(function subscribe(subscriber) { const Event = async (msg: any, ack: any) => { try { const data: any = msg if (self.IsQuery(data, queryObj)) { // send ack // this.Ack(data.info.reqID) const objectData = JSON.parse(data.info.data) // callback const send = (sendData: any, token?: any) => { let address = queryObj.address + "/back" if (data.info.msg_id) { address = address + '/' + data.info.msg_id } self.execQuery({ scope: queryObj.scope, uniqueID: token ? '*' : data.uid, TokenID: token ? data.token : '*', address: address, info: { method: queryObj.method, data: sendData } }); } self.services.query.executeMiddleware (self.middlewares, { query: queryObj, msg: msg, send: send }, (info: any, next: any) => { subscriber.next({ sender: data.uid, token: data.token, data: objectData, body: self.requestDataConvertor(objectData), date: data.info.date, send: send, }); }) // callback } } catch (error) { } } self.socket.on(event, Event) // Provide a way of canceling event return function unsubscribe() { self.socket.off(event, Event); }; }); return observable } CloneCloudStorage(token: string, TimeOutNumber = 30000) { return this.CloudStorageservice.CloneCloudStorage(token, TimeOutNumber) } } // export rayconnect export default Rayconnect