From 74cf441cb6d0ef6bd3f808d69c0519d1b9dcf923 Mon Sep 17 00:00:00 2001 From: Stuart Corbishley Date: Thu, 30 Jun 2016 10:31:52 +0200 Subject: [PATCH] Final State can be serialized into JSON. #5 --- CHANGELOG.md | 11 +++++++++++ lib/Adaptor.js | 29 +++++++++++++++++++---------- lib/FakeAdaptor.js | 11 +++++++---- package.json | 2 +- src/Adaptor.js | 19 ++++++++++++------- src/FakeAdaptor.js | 8 +++++++- 6 files changed, 57 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1254cd0..d5179ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,14 @@ +v0.1.6 +====== + +* State gets cleaned up after the operations are finished. + This means that the final state is serializable. + + The JSForce connection object is provided by `createConnection`, and in turn + `execute` ensures it is run before the user's operations. + + The `cleanupState` reducer simply deletes the connection key from state. + v0.1.3 ====== diff --git a/lib/Adaptor.js b/lib/Adaptor.js index 094d736..c9e6b91 100644 --- a/lib/Adaptor.js +++ b/lib/Adaptor.js @@ -1,11 +1,12 @@ 'use strict'; -var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; - Object.defineProperty(exports, "__esModule", { value: true }); exports.arrayToString = exports.toArray = exports.beta = exports.index = exports.lastReferenceValue = exports.referencePath = exports.dataValue = exports.dataPath = exports.merge = exports.combine = exports.map = exports.sourceValue = exports.source = exports.field = exports.fields = exports.join = exports.each = exports.relationship = exports.lookup = exports.reference = exports.upsert = exports.create = exports.describe = undefined; + +var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; + exports.execute = execute; exports.steps = steps; @@ -137,7 +138,7 @@ var _lodashFp = require('lodash-fp'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } -function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } +function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } /** @module Adaptor */ @@ -162,6 +163,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr var describe = exports.describe = (0, _lodashFp.curry)(function (sObject, state) { var connection = state.connection; + return connection.sobject(sObject).describe().then(function (meta) { console.log('Label : ' + meta.label); console.log('Num of Fields : ' + meta.fields.length); @@ -218,6 +220,7 @@ var reference = exports.reference = (0, _lodashFp.curry)(function (position, _re function createConnection(state) { var loginUrl = state.configuration.loginUrl; + if (!loginUrl) { throw new Error("loginUrl missing from configuration."); } @@ -234,7 +237,9 @@ function login(state) { console.info('Logging in as ' + username + '.'); - return connection.login(username, password + securityToken).then(injectState(state)); + return connection.login(username, password + securityToken).then(function () { + return state; + }); } function execute() { @@ -255,15 +260,19 @@ function execute() { return function (state) { // Note: we no longer need `steps` anymore since `commonExecute` // takes each operation as an argument. - return _languageCommon.execute.apply(undefined, [createConnection, login].concat(_toConsumableArray((0, _lodashFp.flatten)(operations))))(_extends({}, initialState, state)); + return _languageCommon.execute.apply(undefined, [createConnection, login].concat(_toConsumableArray((0, _lodashFp.flatten)(operations)), [cleanupState]))(_extends({}, initialState, state)); }; } -// Utils -function injectState(state) { - return function () { - return state; - }; +/** + * Removes unserializable keys from the state. + * @constructor + * @param {State} state + * @returns {State} + */ +function cleanupState(state) { + delete state.connection; + return state; } function steps() { diff --git a/lib/FakeAdaptor.js b/lib/FakeAdaptor.js index 4686905..9c93b5d 100644 --- a/lib/FakeAdaptor.js +++ b/lib/FakeAdaptor.js @@ -1,12 +1,12 @@ 'use strict'; -var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; - Object.defineProperty(exports, "__esModule", { value: true }); exports.arrayToString = exports.toArray = exports.beta = exports.index = exports.lastReferenceValue = exports.referencePath = exports.dataValue = exports.dataPath = exports.merge = exports.combine = exports.map = exports.sourceValue = exports.source = exports.field = exports.fields = exports.join = exports.each = exports.relationship = exports.lookup = exports.upsert = exports.steps = exports.reference = exports.execute = exports.create = undefined; +var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; + var _sourceHelpers = require('./sourceHelpers'); Object.defineProperty(exports, 'lookup', { @@ -129,7 +129,7 @@ Object.defineProperty(exports, 'arrayToString', { var _lodashFp = require('lodash-fp'); -function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } +function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } /** @module FakeAdaptor */ @@ -204,7 +204,10 @@ function execute() { return function (state) { // Note: we no longer need `steps` anymore since `commonExecute` - return _languageCommon.execute.apply(undefined, _toConsumableArray((0, _lodashFp.flatten)(operations)))(_extends({}, initialState, state)).then(function (state) { + return _languageCommon.execute.apply(undefined, _toConsumableArray((0, _lodashFp.flatten)(operations)).concat([function (state) { + delete state.connection; + return state; + }]))(_extends({}, initialState, state)).then(function (state) { state.logger.info(JSON.stringify(state.references, null, 2)); console.info("Finished Successfully"); return state; diff --git a/package.json b/package.json index 73dc388..cb6a1b8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "language-salesforce", - "version": "0.1.5", + "version": "0.1.6", "description": "Salesforce Language Pack for OpenFn", "main": "lib/index.js", "scripts": { diff --git a/src/Adaptor.js b/src/Adaptor.js index ffb128b..183ec38 100644 --- a/src/Adaptor.js +++ b/src/Adaptor.js @@ -101,7 +101,7 @@ function login(state) { console.info(`Logging in as ${username}.`); return connection.login( username, password + securityToken ) - .then(injectState(state)) + .then(() => state) } @@ -123,7 +123,8 @@ export function execute(...operations) { return commonExecute( createConnection, login, - ...flatten(operations) + ...flatten(operations), + cleanupState )({ ...initialState, ...state }) }; @@ -131,11 +132,15 @@ export function execute(...operations) { } -// Utils -function injectState(state) { - return function() { - return state; - }; +/** + * Removes unserializable keys from the state. + * @constructor + * @param {State} state + * @returns {State} + */ +function cleanupState(state) { + delete state.connection; + return state; } export function steps(...operations) { diff --git a/src/FakeAdaptor.js b/src/FakeAdaptor.js index d58c935..11b05cd 100644 --- a/src/FakeAdaptor.js +++ b/src/FakeAdaptor.js @@ -71,7 +71,13 @@ function execute(...operations) { return state => { // Note: we no longer need `steps` anymore since `commonExecute` - return commonExecute(...flatten(operations))({ ...initialState, ...state }) + return commonExecute( + ...flatten(operations), + function(state) { + delete state.connection; + return state; + } + )({ ...initialState, ...state }) .then(function(state) { state.logger.info( JSON.stringify(state.references, null, 2)