diff --git a/src/client.model.js b/src/client.model.js index 9ed2682..aa5680b 100644 --- a/src/client.model.js +++ b/src/client.model.js @@ -771,7 +771,12 @@ class Client extends Document { }, params: sanitizeParameters( Object.assign( - { script: name, 'script.param': stringify(parameters) }, + { + script: name, + 'script.param': isJson(parameters) + ? stringify(parameters) + : parameters.toString() + }, namespace({ limit: 1 }) ) ) diff --git a/src/utilities/conversion.utilities.js b/src/utilities/conversion.utilities.js index 72f3174..607c525 100644 --- a/src/utilities/conversion.utilities.js +++ b/src/utilities/conversion.utilities.js @@ -47,12 +47,19 @@ const toArray = data => (Array.isArray(data) ? data : [data]); */ const isJson = data => { + data = typeof data !== 'string' ? JSON.stringify(data) : data; + try { - JSON.parse(data); + data = JSON.parse(data); } catch (e) { return false; } - return true; + + if (typeof data === 'object' && data !== null) { + return true; + } + + return false; }; /** diff --git a/src/utilities/filemaker.utilities.js b/src/utilities/filemaker.utilities.js index b3e09cd..65ed9ab 100644 --- a/src/utilities/filemaker.utilities.js +++ b/src/utilities/filemaker.utilities.js @@ -1,7 +1,7 @@ 'use strict'; const _ = require('lodash'); -const { stringify, isJson, parse } = require('./conversion.utilities'); +const { stringify, parse } = require('./conversion.utilities'); /** * @module Filemaker Utilities diff --git a/tests/script.test.js b/tests/script.test.js index 7d2489b..ab885c5 100644 --- a/tests/script.test.js +++ b/tests/script.test.js @@ -1,4 +1,4 @@ -/* global describe before beforeEach it */ +/* global describe before after it */ /* eslint-disable */ @@ -62,6 +62,30 @@ describe('Script Capabilities', () => { .that.has.all.keys('result'); }); + it('should allow you to trigger a script specifying a string as a parameter', () => { + return expect( + client.script(process.env.LAYOUT, 'FMS Triggered Script', 'string-here') + ) + .to.eventually.be.a('object') + .that.has.all.keys('result'); + }); + + it('should allow you to trigger a script specifying a number as a parameter', () => { + return expect(client.script(process.env.LAYOUT, 'FMS Triggered Script', 1)) + .to.eventually.be.a('object') + .that.has.all.keys('result'); + }); + + it('should allow you to trigger a script specifying an object as a parameter', () => { + return expect( + client.script(process.env.LAYOUT, 'FMS Triggered Script', { + object: true + }) + ) + .to.eventually.be.a('object') + .that.has.all.keys('result'); + }); + it('should allow you to trigger a script in a find', () => { return expect( client.find( @@ -85,7 +109,7 @@ describe('Script Capabilities', () => { .that.has.all.keys('scriptResult', 'scriptError', 'data'); }); - it('should allow reject a script that does not exist', () => { + it('should reject a script that does not exist', () => { return expect( client .script(process.env.LAYOUT, { @@ -125,7 +149,7 @@ describe('Script Capabilities', () => { }); it('should not parse script results if the results are not json', () => { - return expect(client.script(process.env.LAYOUT,'Non JSON Script')) + return expect(client.script(process.env.LAYOUT, 'Non JSON Script')) .to.eventually.be.a('object') .that.has.all.keys('result') .and.property('result') diff --git a/tests/utilities.test.js b/tests/utilities.test.js index 21a7194..7ac6112 100644 --- a/tests/utilities.test.js +++ b/tests/utilities.test.js @@ -1,4 +1,4 @@ -/* global describe before after it */ +/* global describe it */ /* eslint-disable */ @@ -9,9 +9,11 @@ const { expect, should } = require('chai'); const chai = require('chai'); const chaiAsPromised = require('chai-as-promised'); -const environment = require('dotenv'); -const varium = require('varium'); -const { omit, parse } = require('../src/utilities/conversion.utilities'); +const { + omit, + parse, + isJson +} = require('../src/utilities/conversion.utilities'); chai.use(chaiAsPromised); @@ -51,4 +53,27 @@ describe('Utility Capabilities', () => { .and.to.include.keys('name'); }); }); + describe('isJson Utility', () => { + it('it should return true for an object', () => { + return expect(isJson({ object: true })).to.equal(true); + }); + it('it should return true for an empty object', () => { + return expect(isJson({ object: true })).to.equal(true); + }); + it('it should return true for a stringified object', () => { + return expect(isJson({})).to.equal(true); + }); + it('it should return false for a number', () => { + return expect(isJson(1)).to.equal(false); + }); + it('it should return false for undefined', () => { + return expect(isJson()).to.equal(false); + }); + it('it should return false for a string', () => { + return expect(isJson('string')).to.equal(false); + }); + it('it should return false for null', () => { + return expect(isJson(null)).to.equal(false); + }); + }); });