@salad-labs/loopz-typescript
Version:
The Official Loopz TypeScript SDK
147 lines • 6.06 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { DexieStorage } from "./core/app";
import { CLIENT_DB_NAME, CLIENT_DB_VERSION_LOCALSTORAGE_PROPERTY_NAME, } from "./constants/app";
import { v4 as uuid } from "uuid";
import { Auth } from "./auth";
import { Chat } from "./chat";
import { Oracle } from "./oracle";
import { Proposal } from "./proposal";
import { Order } from "./order";
import { Notification } from "./notification";
import { PrivyAdapter, AuthAdapter } from "./adapter";
export class Loopz {
static get devMode() {
return Loopz._devMode;
}
constructor(config, runAdapter, devMode) {
Loopz._apiKey = config.apiKey;
Loopz._privyAppId = config.privyAppId;
Loopz._privyClientConfig = config.privyClientConfig;
Loopz._storage = config.storage;
Loopz._randomLsname = `loopz_${uuid()}`;
if (typeof devMode !== "undefined" && devMode === true)
Loopz._devMode = true;
if (runAdapter === true || typeof runAdapter === "undefined") {
if (typeof window !== "undefined")
Loopz._privyAdapter = new PrivyAdapter({
appId: config.privyAppId,
options: typeof window === "undefined"
? undefined
: config.privyClientConfig,
});
Loopz._authAdapter = new AuthAdapter({
devMode: Loopz._devMode,
intl: config.intl,
apiKey: config.apiKey,
logoURL: config.logoURL,
tosURL: config.tosURL,
privacyURL: config.privacyURL,
});
}
Auth.config({
apiKey: config.apiKey,
privyAppId: config.privyAppId,
privyConfig: config.privyClientConfig,
storage: config.storage,
devMode: Loopz._devMode,
});
Oracle.config({
devMode: Loopz._devMode,
});
Proposal.config({
devMode: Loopz._devMode,
});
Order.config({
devMode: Loopz._devMode,
});
Chat.config({
storage: config.storage,
devMode: Loopz._devMode,
});
Notification.config({
devMode: Loopz._devMode,
});
if (Loopz._authAdapter)
Loopz._authAdapter.render();
if (Loopz._privyAdapter)
Loopz._privyAdapter.render();
}
/** static methods */
static createOrConnectToStorage() {
return __awaiter(this, void 0, void 0, function* () {
if (!window.localStorage)
throw new Error("localStorage is not supported. Use a browser that provides the window.localStorage feature.");
try {
localStorage.setItem(Loopz._randomLsname, "");
localStorage.removeItem(Loopz._randomLsname);
}
catch (error) {
throw new Error("localStorage is not supported. Use a browser that provides the window.localStorage feature.");
}
let DB_VERSION = localStorage.getItem(CLIENT_DB_VERSION_LOCALSTORAGE_PROPERTY_NAME)
? localStorage.getItem(CLIENT_DB_VERSION_LOCALSTORAGE_PROPERTY_NAME)
: "1";
try {
return DexieStorage.createOrConnect({
dbName: CLIENT_DB_NAME,
dbVersion: Number(DB_VERSION),
});
}
catch (error) {
console.log(error);
throw new Error("Error during the creation of the local database. See the console for more info.");
}
});
}
static boot(config, options) {
return __awaiter(this, void 0, void 0, function* () {
if (!Loopz._instance) {
let runAdapter = undefined;
let enableStorage = undefined;
let devMode = undefined;
if (options && "runAdapter" in options)
runAdapter = options.runAdapter;
if (options && "enableStorage" in options)
enableStorage = options.enableStorage;
if (options && "devMode" in options)
devMode = options.devMode;
const storage = yield Loopz.createOrConnectToStorage();
//storage is enabled by default
if (typeof enableStorage !== "undefined" && enableStorage === false)
storage.disableStorage();
Loopz._instance = new Loopz(Object.assign(Object.assign({}, config), { storage }), runAdapter, devMode);
}
return Loopz._instance;
});
}
/** public instance methods */
init() {
const auth = Auth.getInstance();
const order = Order.getInstance();
const proposal = Proposal.getInstance();
const oracle = Oracle.getInstance();
const chat = Chat.getInstance();
const notification = Notification.getInstance();
auth.on("__tryRebuildAccountOnRefresh", Auth.recoverAccountFromLocalDB.bind(Auth));
return {
auth,
order,
proposal,
oracle,
chat,
notification,
};
}
}
Loopz._privyAdapter = null;
Loopz._authAdapter = null;
Loopz._devMode = false;
//# sourceMappingURL=loopz.js.map