UNPKG

nft-barter-sdk

Version:

Javascript SDK for abstracting complexities of interacting with NFT protocols.

325 lines (324 loc) 15.8 kB
"use strict"; /** * Firebase * @author Ebube Ud <kripsonud@gmail.com> */ 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); var app_1 = require("firebase/app"); var firestore_1 = require("firebase/firestore"); var Firebase = /** @class */ (function () { function Firebase() { var _this = this; // Initialize Firebase this.initializeFirebaseDatabase = function () { (0, app_1.initializeApp)(Firebase.firebaseConfig); return (0, firestore_1.getFirestore)(); }; /** * * @param ref The path reference of the collection document to write * @param data The data object to write * @returns Response status object */ this.addData = function (ref, data) { return __awaiter(_this, void 0, void 0, function () { var docRef, e_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); docRef = (0, firestore_1.doc)(this.db, ref); data.datatimeCreated = firestore_1.Timestamp.now(); data.isDeleted = false; // set data, merge if exist! return [4 /*yield*/, (0, firestore_1.setDoc)(docRef, data, { merge: true })]; case 1: // set data, merge if exist! _a.sent(); return [3 /*break*/, 3]; case 2: e_1 = _a.sent(); // log error console.log(e_1); return [3 /*break*/, 3]; case 3: // return response return [2 /*return*/, { status: 'success', message: 'Record added', }]; } }); }); }; /** * * @param ref The path reference of the collection document to read * @returns Retrieved object of data or a message response */ this.getData = function (ref) { return __awaiter(_this, void 0, void 0, function () { var docRef, data, e_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); docRef = (0, firestore_1.doc)(this.db, ref); return [4 /*yield*/, (0, firestore_1.getDoc)(docRef)]; case 1: data = _a.sent(); if (data.exists()) { // return data return [2 /*return*/, { id: data.id, data: data.data(), }]; } return [3 /*break*/, 3]; case 2: e_2 = _a.sent(); // log error console.log(e_2); return [3 /*break*/, 3]; case 3: // return response return [2 /*return*/, { message: 'No data found in the specified path.', }]; } }); }); }; /** * * @param ref The path reference of the collection document to write * @param data The data object to write * @returns Response status object */ this.updateData = function (ref, data) { return __awaiter(_this, void 0, void 0, function () { var docRef, e_3; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); docRef = (0, firestore_1.doc)(this.db, ref); // set data, merge if exist! return [4 /*yield*/, (0, firestore_1.updateDoc)(docRef, data)]; case 1: // set data, merge if exist! _a.sent(); return [3 /*break*/, 3]; case 2: e_3 = _a.sent(); // log error console.log(e_3); return [3 /*break*/, 3]; case 3: // return response return [2 /*return*/, { status: 'success', message: 'Record updated', }]; } }); }); }; /** * * @param ref The path reference of the collection document to write * @returns Response status object */ this.deleteData = function (ref) { return __awaiter(_this, void 0, void 0, function () { var docRef, e_4; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); docRef = (0, firestore_1.doc)(this.db, ref); // soft delete document return [4 /*yield*/, (0, firestore_1.updateDoc)(docRef, { isDeleted: true })]; case 1: // soft delete document _a.sent(); return [3 /*break*/, 3]; case 2: e_4 = _a.sent(); // log error console.log(e_4); return [3 /*break*/, 3]; case 3: // return response return [2 /*return*/, { status: 'success', message: 'Record deleted', }]; } }); }); }; /** * * @param ref The path reference of the collection or sub-collections to read * @returns Retrieves object of data in arrary or a message response */ this.getCollections = function (ref) { return __awaiter(_this, void 0, void 0, function () { var splitRef, splitRefLength, collectionSubPaths, collections, responseData_1, responseData_2, collections, e_5; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 7, , 8]); splitRef = ref.split('/'); splitRefLength = splitRef.length; if (!(splitRefLength > 1)) return [3 /*break*/, 3]; collectionSubPaths = splitRef.splice(1); return [4 /*yield*/, (0, firestore_1.getDocs)(firestore_1.collection.apply(void 0, __spreadArray([this.db, splitRef[0]], collectionSubPaths, false)))]; case 1: collections = _a.sent(); responseData_1 = []; return [4 /*yield*/, collections.forEach(function (document) { responseData_1.push({ id: document.id, data: document.data(), }); })]; case 2: _a.sent(); // return data return [2 /*return*/, responseData_1]; case 3: responseData_2 = []; return [4 /*yield*/, (0, firestore_1.getDocs)((0, firestore_1.collection)(this.db, splitRef[0]))]; case 4: collections = _a.sent(); return [4 /*yield*/, collections.forEach(function (document) { responseData_2.push({ id: document.id, data: document.data(), }); })]; case 5: _a.sent(); // return data return [2 /*return*/, responseData_2]; case 6: return [3 /*break*/, 8]; case 7: e_5 = _a.sent(); // log error console.log(e_5); return [3 /*break*/, 8]; case 8: // return response return [2 /*return*/, { message: 'No document found in the specified collection', }]; } }); }); }; /** * * @param ref - The path reference of the collection or sub-collections to read * @param options - The args for the query which comprises of the conditions, the ordering and the limit * @returns Retrieves object of data in arrary or a message response */ this.queryCollection = function (ref, options) { return __awaiter(_this, void 0, void 0, function () { var conditions, _a, order, _b, limitCount, whereClauses, splitRef, subCollectionPaths, collectionRef, _i, conditions_1, condition, clause, preparedQuery, collections, responseData_3, e_6; return __generator(this, function (_c) { switch (_c.label) { case 0: _c.trys.push([0, 3, , 4]); conditions = options.conditions, _a = options.order, order = _a === void 0 ? { field: 'datatimeCreated', direction: 'asc' } : _a, _b = options.limitCount, limitCount = _b === void 0 ? 50 : _b; whereClauses = []; splitRef = ref.split('/'); subCollectionPaths = splitRef.splice(1); collectionRef = firestore_1.collection.apply(void 0, __spreadArray([this.db, splitRef[0]], subCollectionPaths, false)); // prepared all where clauses if (conditions) { for (_i = 0, conditions_1 = conditions; _i < conditions_1.length; _i++) { condition = conditions_1[_i]; clause = (0, firestore_1.where)(condition.field, condition.operator, condition.value); whereClauses.push(clause); } } else { whereClauses.push((0, firestore_1.where)('isDeleted', '==', false)); } preparedQuery = firestore_1.query.apply(void 0, __spreadArray(__spreadArray([collectionRef], whereClauses, false), [(0, firestore_1.orderBy)(order.field, order.direction), (0, firestore_1.limit)(limitCount)], false)); return [4 /*yield*/, (0, firestore_1.getDocs)(preparedQuery)]; case 1: collections = _c.sent(); responseData_3 = []; // format response document return [4 /*yield*/, collections.forEach(function (document) { responseData_3.push({ id: document.id, data: document.data(), }); })]; case 2: // format response document _c.sent(); // return data return [2 /*return*/, responseData_3]; case 3: e_6 = _c.sent(); // log error console.log(e_6); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; this.db = this.initializeFirebaseDatabase(); } Firebase.firebaseConfig = { apiKey: 'AIzaSyDUAAi2IP_Cf95mF7-tk0ALWOzj1mPn94Y', authDomain: 'nft-barter-2345.firebaseapp.com', projectId: 'nft-barter-2345', storageBucket: 'nft-barter-2345.appspot.com', messagingSenderId: '770950410692', appId: '1:770950410692:web:6c5a0445acdbeecb1ff491', measurementId: 'G-YQ4J06WTZT', }; return Firebase; }()); exports.default = Firebase;