From daa4a628e1b4e73bc19e8cf230dec2e23d932202 Mon Sep 17 00:00:00 2001 From: Graham Lyus Date: Sun, 20 Aug 2017 13:06:06 -0700 Subject: [PATCH 1/2] remove lib and add a prepare script to build before publish/pack --- lib/authClient.js | 71 -------------- lib/index.js | 19 ---- lib/restClient.js | 180 ------------------------------------ package.json | 5 +- tests/authClientLib.test.js | 35 ------- tests/restClientLib.test.js | 65 ------------- 6 files changed, 3 insertions(+), 372 deletions(-) delete mode 100644 lib/authClient.js delete mode 100644 lib/index.js delete mode 100644 lib/restClient.js delete mode 100644 tests/authClientLib.test.js delete mode 100644 tests/restClientLib.test.js diff --git a/lib/authClient.js b/lib/authClient.js deleted file mode 100644 index 46b3b7a..0000000 --- a/lib/authClient.js +++ /dev/null @@ -1,71 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _adminOnRest = require('admin-on-rest'); - -var _firebase = require('firebase'); - -var _firebase2 = _interopRequireDefault(_firebase); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -/* globals localStorage */ -function firebaseAuthCheck(auth, resolve, reject) { - if (auth) { - // TODO make it a parameter - _firebase2.default.database().ref('/users/' + auth.uid).once('value').then(function (snapshot) { - var profile = snapshot.val(); - // TODO make it a parameter - if (profile && profile.isAdmin) { - auth.getIdToken().then(function (firebaseToken) { - var user = { auth: auth, profile: profile, firebaseToken: firebaseToken - - // TODO improve this! Save it on redux or something - };localStorage.setItem('firebaseToken', firebaseToken); - resolve(user); - }).catch(function (err) { - reject(err); - }); - } else { - _firebase2.default.auth().signOut(); - reject(new Error('Access Denied!')); - } - }).catch(function (err) { - reject(err); - }); - } else { - reject(new Error('Login failed!')); - } -} - -exports.default = function (type, params) { - if (type === _adminOnRest.AUTH_LOGOUT) { - return _firebase2.default.auth().signOut(); - } - if (type === _adminOnRest.AUTH_CHECK) { - return new Promise(function (resolve, reject) { - if (_firebase2.default.auth().currentUser) { - resolve(); - } else { - reject(new Error('User not found')); - } - }); - } - if (type === _adminOnRest.AUTH_LOGIN) { - var username = params.username, - password = params.password; - - - return new Promise(function (resolve, reject) { - _firebase2.default.auth().signInWithEmailAndPassword(username, password).then(function (auth) { - return firebaseAuthCheck(auth, resolve, reject); - }).catch(function (e) { - return reject(new Error('User not found')); - }); - }); - } - return Promise.resolve(); -}; \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index f2d768c..0000000 --- a/lib/index.js +++ /dev/null @@ -1,19 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); -exports.AuthClient = exports.RestClient = undefined; - -var _restClient = require('./restClient'); - -var _restClient2 = _interopRequireDefault(_restClient); - -var _authClient = require('./authClient'); - -var _authClient2 = _interopRequireDefault(_authClient); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -exports.RestClient = _restClient2.default; -exports.AuthClient = _authClient2.default; \ No newline at end of file diff --git a/lib/restClient.js b/lib/restClient.js deleted file mode 100644 index 174c992..0000000 --- a/lib/restClient.js +++ /dev/null @@ -1,180 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _firebase = require('firebase'); - -var _firebase2 = _interopRequireDefault(_firebase); - -var _adminOnRest = require('admin-on-rest'); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -exports.default = function () { - var trackedResources = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; - var firebaseConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; - - /** TODO Move this to the Redux Store */ - var resourcesStatus = {}; - var resourcesReferences = {}; - var resourcesData = {}; - - if (_firebase2.default.apps.length === 0) { - _firebase2.default.initializeApp(firebaseConfig); - } - - trackedResources.map(function (resource) { - resourcesData[resource] = {}; - resourcesStatus[resource] = new Promise(function (resolve) { - var ref = resourcesReferences[resource] = _firebase2.default.database().ref(resource); - - ref.on('value', function (childSnapshot) { - /** Uses "value" to fetch initial data. Avoid the AOR to show no results */ - if (childSnapshot.key === resource) { - resourcesData[resource] = childSnapshot.val(); - } - Object.keys(resourcesData[resource]).map(function (key) { - resourcesData[resource][key].id = key; - }); - ref.on('child_added', function (childSnapshot) { - resourcesData[resource][childSnapshot.key] = childSnapshot.val(); - resourcesData[resource][childSnapshot.key].id = childSnapshot.key; - }); - ref.on('child_removed', function (oldChildSnapshot) { - if (resourcesData[resource][oldChildSnapshot.key]) { - delete resourcesData[resource][oldChildSnapshot.key]; - } - }); - ref.on('child_changed', function (childSnapshot) { - resourcesData[resource][childSnapshot.key] = childSnapshot.val(); - }); - resolve(); - }); - }); - }); - - /** - * @param {string} type Request type, e.g GET_LIST - * @param {string} resource Resource name, e.g. "posts" - * @param {Object} payload Request parameters. Depends on the request type - * @returns {Promise} the Promise for a REST response - */ - - return function (type, resource, params) { - return new Promise(function (resolve, reject) { - resourcesStatus[resource].then(function () { - switch (type) { - case _adminOnRest.GET_LIST: - case _adminOnRest.GET_MANY: - case _adminOnRest.GET_MANY_REFERENCE: - - var ids = []; - var data = []; - var total = 0; - - if (params.ids) { - /** GET_MANY */ - params.ids.map(function (key) { - if (resourcesData[resource][key]) { - ids.push(key); - data.push(resourcesData[resource][key]); - total++; - } - }); - } else if (params.pagination) { - /** GET_LIST / GET_MANY_REFERENCE */ - var values = []; - - var filterKeys = Object.keys(params.filter || {}); - /* TODO Must have a better way */ - if (filterKeys.length) { - Object.values(resourcesData[resource]).map(function (value) { - var filterIndex = 0; - while (filterIndex < filterKeys.length) { - var property = filterKeys[filterIndex]; - if (property != 'q' && value[property] != params.filter[property]) { - return; - } else if (property === 'q') { - if (JSON.stringify(value).indexOf(params.filter['q']) == -1) { - return; - } - } - filterIndex++; - } - values.push(value); - }); - } else { - values = Object.values(resourcesData[resource]); - } - - var _params$pagination = params.pagination, - page = _params$pagination.page, - perPage = _params$pagination.perPage; - - var _start = (page - 1) * perPage; - var _end = page * perPage; - data = values.slice(_start, _end); - ids = Object.keys(resourcesData[resource]).slice(_start, _end); - total = values.length; - } else { - console.error('Unexpected parameters: ', params, type); - reject(new Error('Error processing request')); - } - resolve({ data: data, ids: ids, total: total }); - return; - - case _adminOnRest.GET_ONE: - var key = params.id; - if (key && resourcesData[resource][key]) { - resolve({ - data: resourcesData[resource][key] - }); - } else { - reject(new Error('Key not found')); - } - return; - - case _adminOnRest.DELETE: - _firebase2.default.database().ref(resource + '/' + params.id).remove().then(function () { - resolve({ data: params.id }); - }).catch(reject); - return; - - case _adminOnRest.UPDATE: - var dataUpdate = Object.assign({ updated_at: Date.now() }, resourcesData[resource][params.id], params.data); - - _firebase2.default.database().ref(resource + '/' + params.id).update(dataUpdate).then(function () { - return resolve({ data: dataUpdate }); - }).catch(reject); - return; - - case _adminOnRest.CREATE: - var newItemKey = params.data.id; - if (!newItemKey) { - var _newItemKey = _firebase2.default.database().ref().child(resource).push().key; - } else if (resourcesData[resource] && resourcesData[resource][newItemKey]) { - reject(new Error('ID already in use')); - return; - } - var dataCreate = Object.assign({ - created_at: Date.now(), - updated_at: Date.now() - }, params.data, { - id: newItemKey, - key: newItemKey - }); - _firebase2.default.database().ref(resource + '/' + newItemKey).update(dataCreate).then(function () { - return resolve({ data: dataCreate }); - }).catch(reject); - return; - - default: - console.error('Undocumented method: ', type); - return { data: [] }; - } - }); - }); - }; -}; \ No newline at end of file diff --git a/package.json b/package.json index 5ae9676..68490f2 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "lib/index.js", "scripts": { "test": "jest", - "build": "babel src -d lib" + "build": "babel src -d lib", + "prepare": "yarn run build" }, "author": "Sid Ferreira", "license": "ISC", @@ -35,7 +36,7 @@ "jest-localstorage-mock": "^1.2.0", "react-test-renderer": "^15.6.1" }, - "jest": { + "jest": { "testEnvironment": "node", "setupFiles": [ "jest-localstorage-mock" diff --git a/tests/authClientLib.test.js b/tests/authClientLib.test.js deleted file mode 100644 index c530012..0000000 --- a/tests/authClientLib.test.js +++ /dev/null @@ -1,35 +0,0 @@ -/* globals jest, test, expect, jasmine, debugger */ - -import { AUTH_LOGIN, AUTH_LOGOUT, AUTH_CHECK } from 'admin-on-rest' -import firebase from 'firebase' - -import { AuthClient } from '../lib' - -jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000 - -debugger; - -const firebaseConfig = { - apiKey: 'AIzaSyAwoZ5Ph6Hx3-DplWzaouqUOnu4lNKeAFQ', - authDomain: 'aor-firebase-client.firebaseapp.com', - databaseURL: 'https://aor-firebase-client.firebaseio.com', - projectId: 'aor-firebase-client', - storageBucket: 'aor-firebase-client.appspot.com', - messagingSenderId: '1092760245154' -} - -firebase.initializeApp(firebaseConfig) - -test('AuthClient is defined', () => { - expect(AuthClient).toBeDefined() -}) - -test('AuthClient from Non Admin Fails', () => { - return expect(AuthClient(AUTH_LOGIN, - { username: 'is_not_admin@aor-firebase-client.nu', password: 'is_not_admin' })).rejects.toBeDefined() -}) - -test('AuthClient from Admin Succedes', () => { - return expect(AuthClient(AUTH_LOGIN, - { username: 'is_admin@aor-firebase-client.nu', password: 'is_admin' })).resolves.toBeDefined() -}) \ No newline at end of file diff --git a/tests/restClientLib.test.js b/tests/restClientLib.test.js deleted file mode 100644 index 7c13acc..0000000 --- a/tests/restClientLib.test.js +++ /dev/null @@ -1,65 +0,0 @@ -/* globals jest, test, expect, jasmine, debugger */ - -import { - GET_LIST, - GET_ONE, - GET_MANY, - GET_MANY_REFERENCE, - CREATE, - UPDATE, - DELETE -} from 'admin-on-rest' - -import firebase from 'firebase' - -import { RestClient } from '../lib' - -jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000 - -debugger; - -const firebaseConfig = { - apiKey: 'AIzaSyAwoZ5Ph6Hx3-DplWzaouqUOnu4lNKeAFQ', - authDomain: 'aor-firebase-client.firebaseapp.com', - databaseURL: 'https://aor-firebase-client.firebaseio.com', - projectId: 'aor-firebase-client', - storageBucket: 'aor-firebase-client.appspot.com', - messagingSenderId: '1092760245154' -} - -const client = RestClient(['posts', 'profiles'], firebaseConfig) - -test('RestClient is defined', () => { - expect(RestClient).toBeDefined() -}) - -test('RestClient Get Posts', () => { - client(GET_LIST, 'posts', {}).then(data => { - expect(data).toBeDefined() - expect(data.length).toBeDefined() - expect(data.length).toBeGreaterThan(1) - }) -}) - -test('RestClient Get Posts From User 1', () => { - client(GET_LIST, 'posts', { filters: { userId: 1 } }).then(data => { - expect(data).toBeDefined() - expect(data.length).toBeDefined() - expect(data.length).toBe(10) - }) -}) - -test('RestClient Get Posts With Text', () => { - client(GET_LIST, 'posts', { filters: { q: 'vero' } }).then(data => { - expect(data).toBeDefined() - expect(data.length).toBeDefined() - expect(data.length).toBe(10) - }) -}) - -test('RestClient Get Posts With Impossible Text', () => { - client(GET_LIST, 'posts', { filters: { q: 'thisisaveryimpossibletext' } }).then(data => { - expect(data).toBeDefined() - expect(data.length).toBe(0) - }) -}) From 1f852585ecacd0d4a4e6f7c52066a2a39cd00cbd Mon Sep 17 00:00:00 2001 From: Graham Lyus Date: Sun, 20 Aug 2017 13:07:06 -0700 Subject: [PATCH 2/2] gitignore lib --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 491c10d..7de941b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +lib node_modules package-lock.json firebase_env