PageRenderTime 128ms CodeModel.GetById 79ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/jsforce/1.3.1/jsforce-api-tooling.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 496 lines | 225 code | 38 blank | 233 comment | 31 complexity | dfc09b90ccd1c5876cf080cdd45ccf10 MD5 | raw file
  1. !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var o;"undefined"!=typeof window?o=window:"undefined"!=typeof global?o=global:"undefined"!=typeof self&&(o=self);var f=o;f=f.jsforce||(f.jsforce={}),f=f.modules||(f.modules={}),f=f.api||(f.api={}),f.Tooling=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. /**
  3. * @file Manages Tooling APIs
  4. * @author Shinichi Tomita <shinichi.tomita@gmail.com>
  5. */
  6. var util = jsforce.require('util'),
  7. _ = jsforce.require('underscore')._,
  8. Cache = require('../cache');
  9. /**
  10. * API class for Tooling API call
  11. *
  12. * @class
  13. * @param {Connection} conn - Connection
  14. */
  15. var Tooling = function(conn) {
  16. this._conn = conn;
  17. this._logger = conn._logger;
  18. var delegates = [
  19. "query",
  20. "queryMore",
  21. "create",
  22. "insert",
  23. "retrieve",
  24. "update",
  25. "upsert",
  26. "del",
  27. "delete",
  28. "destroy",
  29. "describe",
  30. "describeGlobal",
  31. "sobject"
  32. ];
  33. delegates.forEach(function(method) {
  34. this[method] = conn.constructor.prototype[method];
  35. }, this);
  36. this.cache = new Cache();
  37. var cacheOptions = {
  38. key: function(type) { return type ? "describe." + type : "describe"; }
  39. };
  40. this.describe$ = this.cache.makeCacheable(this.describe, this, cacheOptions);
  41. this.describe = this.cache.makeResponseCacheable(this.describe, this, cacheOptions);
  42. this.describeSObject$ = this.describe$;
  43. this.describeSObject = this.describe;
  44. cacheOptions = { key: 'describeGlobal' };
  45. this.describeGlobal$ = this.cache.makeCacheable(this.describeGlobal, this, cacheOptions);
  46. this.describeGlobal = this.cache.makeResponseCacheable(this.describeGlobal, this, cacheOptions);
  47. this.initialize();
  48. };
  49. /**
  50. * Initialize tooling API
  51. * @protected
  52. */
  53. Tooling.prototype.initialize = function() {
  54. this.sobjects = {};
  55. this.cache.clear();
  56. this.cache.get('describeGlobal').on('value', _.bind(function(res) {
  57. if (res.result) {
  58. var types = _.map(res.result.sobjects, function(so) { return so.name; });
  59. _.each(types, this.sobject, this);
  60. }
  61. }, this));
  62. };
  63. /**
  64. * @private
  65. */
  66. Tooling.prototype._baseUrl = function() {
  67. return this._conn.urls.rest.base + "/tooling";
  68. };
  69. /**
  70. * @private
  71. */
  72. Tooling.prototype._request = function() {
  73. return this._conn._request.apply(this._conn, arguments);
  74. };
  75. /**
  76. * Execute query by using SOQL
  77. *
  78. * @param {String} soql - SOQL string
  79. * @param {Callback.<QueryResult>} [callback] - Callback function
  80. * @returns {Query.<QueryResult>}
  81. */
  82. /**
  83. * Query next record set by using query locator
  84. *
  85. * @method Tooling#query
  86. * @param {String} locator - Next record set locator
  87. * @param {Callback.<QueryResult>} [callback] - Callback function
  88. * @returns {Query.<QueryResult>}
  89. */
  90. /**
  91. * Retrieve specified records
  92. *
  93. * @method Tooling#queryMore
  94. * @param {String} type - SObject Type
  95. * @param {String|Array.<String>} ids - A record ID or array of record IDs
  96. * @param {Callback.<Record|Array.<Record>>} [callback] - Callback function
  97. * @returns {Promise.<Record|Array.<Record>>}
  98. */
  99. /**
  100. * Synonym of Tooling#create()
  101. *
  102. * @method Tooling#insert
  103. * @param {String} type - SObject Type
  104. * @param {Object|Array.<Object>} records - A record or array of records to create
  105. * @param {Callback.<RecordResult|Array.<RecordResult>>} [callback] - Callback function
  106. * @returns {Promise.<RecordResult|Array.<RecordResult>>}
  107. */
  108. /**
  109. * Create records
  110. *
  111. * @method Tooling#create
  112. * @param {String} type - SObject Type
  113. * @param {Record|Array.<Record>} records - A record or array of records to create
  114. * @param {Callback.<RecordResult|Array.<RecordResult>>} [callback] - Callback function
  115. * @returns {Promise.<RecordResult|Array.<RecordResult>>}
  116. */
  117. /**
  118. * Update records
  119. *
  120. * @method Tooling#update
  121. * @param {String} type - SObject Type
  122. * @param {Record|Array.<Record>} records - A record or array of records to update
  123. * @param {Callback.<RecordResult|Array.<RecordResult>>} [callback] - Callback function
  124. * @returns {Promise.<RecordResult|Array.<RecordResult>>}
  125. */
  126. /**
  127. * Upsert records
  128. *
  129. * @method Tooling#upsert
  130. * @param {String} type - SObject Type
  131. * @param {Record|Array.<Record>} records - Record or array of records to upsert
  132. * @param {String} extIdField - External ID field name
  133. * @param {Callback.<RecordResult|Array.<RecordResult>>} [callback] - Callback
  134. * @returns {Promise.<RecordResult|Array.<RecordResult>>}
  135. */
  136. /**
  137. * Synonym of Tooling#destroy()
  138. *
  139. * @method Tooling#delete
  140. * @param {String} type - SObject Type
  141. * @param {String|Array.<String>} ids - A ID or array of IDs to delete
  142. * @param {Callback.<RecordResult|Array.<RecordResult>>} [callback] - Callback
  143. * @returns {Promise.<RecordResult|Array.<RecordResult>>}
  144. */
  145. /**
  146. * Synonym of Tooling#destroy()
  147. *
  148. * @method Tooling#del
  149. * @param {String} type - SObject Type
  150. * @param {String|Array.<String>} ids - A ID or array of IDs to delete
  151. * @param {Callback.<RecordResult|Array.<RecordResult>>} [callback] - Callback
  152. * @returns {Promise.<RecordResult|Array.<RecordResult>>}
  153. */
  154. /**
  155. * Delete records
  156. *
  157. * @method Tooling#destroy
  158. * @param {String} type - SObject Type
  159. * @param {String|Array.<String>} ids - A ID or array of IDs to delete
  160. * @param {Callback.<RecordResult|Array.<RecordResult>>} [callback] - Callback
  161. * @returns {Promise.<RecordResult|Array.<RecordResult>>}
  162. */
  163. /**
  164. * Synonym of Tooling#describe()
  165. *
  166. * @method Tooling#describeSObject
  167. * @param {String} type - SObject Type
  168. * @param {Callback.<DescribeSObjectResult>} [callback] - Callback function
  169. * @returns {Promise.<DescribeSObjectResult>}
  170. */
  171. /**
  172. * Describe SObject metadata
  173. *
  174. * @method Tooling#describe
  175. * @param {String} type - SObject Type
  176. * @param {Callback.<DescribeSObjectResult>} [callback] - Callback function
  177. * @returns {Promise.<DescribeSObjectResult>}
  178. */
  179. /**
  180. * Describe global SObjects
  181. *
  182. * @method Tooling#describeGlobal
  183. * @param {Callback.<DescribeGlobalResult>} [callback] - Callback function
  184. * @returns {Promise.<DescribeGlobalResult>}
  185. */
  186. /**
  187. * Get SObject instance
  188. *
  189. * @method Tooling#sobject
  190. * @param {String} type - SObject Type
  191. * @returns {SObject}
  192. */
  193. /**
  194. * @typedef {Object} Tooling~ExecuteAnonymousResult
  195. * @prop {Boolean} compiled - Flag if the query is compiled successfully
  196. * @prop {String} compileProblem - Error reason in compilation
  197. * @prop {Boolean} success - Flag if the code is executed successfully
  198. * @prop {Number} line - Line number for the error
  199. * @prop {Number} column - Column number for the error
  200. * @prop {String} exceptionMessage - Exception message
  201. * @prop {String} exceptionStackTrace - Exception stack trace
  202. */
  203. /**
  204. * Executes Apex code anonymously
  205. *
  206. * @param {String} body - Anonymous Apex code
  207. * @param {Callback.<Tooling~ExecuteAnonymousResult>} [callback] - Callback function
  208. * @returns {Promise.<Tooling~ExecuteAnonymousResult>}
  209. */
  210. Tooling.prototype.executeAnonymous = function(body, callback) {
  211. var url = this._baseUrl() + "/executeAnonymous?anonymousBody=" + encodeURIComponent(body);
  212. return this._request(url).thenCall(callback);
  213. };
  214. /**
  215. * @typedef {Object} Tooling~CompletionsResult
  216. * @prop {Object} publicDeclarations
  217. */
  218. /**
  219. * Retrieves available code completions of the referenced type
  220. *
  221. * @param {String} [type] - completion type (default 'apex')
  222. * @param {Callback.<Tooling~CompletionsResult>} [callback] - Callback function
  223. * @returns {Promise.<Tooling~CompletionsResult>}
  224. */
  225. Tooling.prototype.completions = function(type, callback) {
  226. if (!_.isString(type)) {
  227. callback = type;
  228. type = 'apex';
  229. }
  230. var url = this._baseUrl() + "/completions?type=" + encodeURIComponent(type);
  231. return this._request(url).thenCall(callback);
  232. };
  233. module.exports = Tooling;
  234. },{"../cache":2}],2:[function(require,module,exports){
  235. /**
  236. * @file Manages asynchronous method response cache
  237. * @author Shinichi Tomita <shinichi.tomita@gmail.com>
  238. */
  239. var events = jsforce.require('events'),
  240. inherits = require('inherits'),
  241. _ = jsforce.require('underscore')._;
  242. /**
  243. * Class for managing cache entry
  244. *
  245. * @private
  246. * @class
  247. * @constructor
  248. * @template T
  249. */
  250. var CacheEntry = function() {
  251. this.fetching = false;
  252. };
  253. inherits(CacheEntry, events.EventEmitter);
  254. /**
  255. * Get value in the cache entry
  256. *
  257. * @param {Callback.<T>} [callback] - Callback function callbacked the cache entry updated
  258. * @returns {T|undefined}
  259. */
  260. CacheEntry.prototype.get = function(callback) {
  261. if (!callback) {
  262. return this._value;
  263. } else {
  264. this.once('value', callback);
  265. if (!_.isUndefined(this._value)) {
  266. this.emit('value', this._value);
  267. }
  268. }
  269. };
  270. /**
  271. * Set value in the cache entry
  272. *
  273. * @param {T} [value] - A value for caching
  274. */
  275. CacheEntry.prototype.set = function(value) {
  276. this._value = value;
  277. this.emit('value', this._value);
  278. };
  279. /**
  280. * Clear cached value
  281. */
  282. CacheEntry.prototype.clear = function() {
  283. this.fetching = false;
  284. delete this._value;
  285. };
  286. /**
  287. * Caching manager for async methods
  288. *
  289. * @class
  290. * @constructor
  291. */
  292. var Cache = function() {
  293. this._entries = {};
  294. };
  295. /**
  296. * retrive cache entry, or create if not exists.
  297. *
  298. * @param {String} [key] - Key of cache entry
  299. * @returns {CacheEntry}
  300. */
  301. Cache.prototype.get = function(key) {
  302. if (key && this._entries[key]) {
  303. return this._entries[key];
  304. } else {
  305. var entry = new CacheEntry();
  306. this._entries[key] = entry;
  307. return entry;
  308. }
  309. };
  310. /**
  311. * clear cache entries prefix matching given key
  312. * @param {String} [key] - Key prefix of cache entry to clear
  313. */
  314. Cache.prototype.clear = function(key) {
  315. for (var k in this._entries) {
  316. if (!key || k.indexOf(key) === 0) {
  317. this._entries[k].clear();
  318. }
  319. }
  320. };
  321. /**
  322. * create and return cache key from namespace and serialized arguments.
  323. * @private
  324. */
  325. function createCacheKey(namespace, args) {
  326. args = Array.prototype.slice.apply(args);
  327. return namespace + '(' + _.map(args, function(a){ return JSON.stringify(a); }).join(',') + ')';
  328. }
  329. /**
  330. * Enable caching for async call fn to intercept the response and store it to cache.
  331. * The original async calll fn is always invoked.
  332. *
  333. * @protected
  334. * @param {Function} fn - Function to covert cacheable
  335. * @param {Object} [scope] - Scope of function call
  336. * @param {Object} [options] - Options
  337. * @return {Function} - Cached version of function
  338. */
  339. Cache.prototype.makeResponseCacheable = function(fn, scope, options) {
  340. var cache = this;
  341. options = options || {};
  342. return function() {
  343. var args = Array.prototype.slice.apply(arguments);
  344. var callback = args.pop();
  345. if (!_.isFunction(callback)) {
  346. args.push(callback);
  347. callback = null;
  348. }
  349. var key = _.isString(options.key) ? options.key :
  350. _.isFunction(options.key) ? options.key.apply(scope, args) :
  351. createCacheKey(options.namespace, args);
  352. var entry = cache.get(key);
  353. entry.fetching = true;
  354. if (callback) {
  355. args.push(function(err, result) {
  356. entry.set({ error: err, result: result });
  357. callback(err, result);
  358. });
  359. }
  360. var ret, error;
  361. try {
  362. ret = fn.apply(scope || this, args);
  363. } catch(e) {
  364. error = e;
  365. }
  366. if (ret && _.isFunction(ret.then)) { // if the returned value is promise
  367. if (!callback) {
  368. return ret.then(function(result) {
  369. entry.set({ error: undefined, result: result });
  370. return result;
  371. }, function(err) {
  372. entry.set({ error: err, result: undefined });
  373. throw err;
  374. });
  375. } else {
  376. return ret;
  377. }
  378. } else {
  379. entry.set({ error: error, result: ret });
  380. if (error) { throw error; }
  381. return ret;
  382. }
  383. };
  384. };
  385. /**
  386. * Enable caching for async call fn to lookup the response cache first, then invoke original if no cached value.
  387. *
  388. * @protected
  389. * @param {Function} fn - Function to covert cacheable
  390. * @param {Object} [scope] - Scope of function call
  391. * @param {Object} [options] - Options
  392. * @return {Function} - Cached version of function
  393. */
  394. Cache.prototype.makeCacheable = function(fn, scope, options) {
  395. var cache = this;
  396. options = options || {};
  397. var $fn = function() {
  398. var args = Array.prototype.slice.apply(arguments);
  399. var callback = args.pop();
  400. if (!_.isFunction(callback)) {
  401. args.push(callback);
  402. }
  403. var key = _.isString(options.key) ? options.key :
  404. _.isFunction(options.key) ? options.key.apply(scope, args) :
  405. createCacheKey(options.namespace, args);
  406. var entry = cache.get(key);
  407. if (!_.isFunction(callback)) { // if callback is not given in last arg, return cached result (immediate).
  408. var value = entry.get();
  409. if (!value) { throw new Error('Function call result is not cached yet.'); }
  410. if (value.error) { throw value.error; }
  411. return value.result;
  412. }
  413. entry.get(function(value) {
  414. callback(value.error, value.result);
  415. });
  416. if (!entry.fetching) { // only when no other client is calling function
  417. entry.fetching = true;
  418. args.push(function(err, result) {
  419. entry.set({ error: err, result: result });
  420. });
  421. fn.apply(scope || this, args);
  422. }
  423. };
  424. $fn.clear = function() {
  425. var key = _.isString(options.key) ? options.key :
  426. _.isFunction(options.key) ? options.key.apply(scope, arguments) :
  427. createCacheKey(options.namespace, arguments);
  428. cache.clear(key);
  429. };
  430. return $fn;
  431. };
  432. module.exports = Cache;
  433. },{"inherits":3}],3:[function(require,module,exports){
  434. if (typeof Object.create === 'function') {
  435. // implementation from standard node.js 'util' module
  436. module.exports = function inherits(ctor, superCtor) {
  437. ctor.super_ = superCtor
  438. ctor.prototype = Object.create(superCtor.prototype, {
  439. constructor: {
  440. value: ctor,
  441. enumerable: false,
  442. writable: true,
  443. configurable: true
  444. }
  445. });
  446. };
  447. } else {
  448. // old school shim for old browsers
  449. module.exports = function inherits(ctor, superCtor) {
  450. ctor.super_ = superCtor
  451. var TempCtor = function () {}
  452. TempCtor.prototype = superCtor.prototype
  453. ctor.prototype = new TempCtor()
  454. ctor.prototype.constructor = ctor
  455. }
  456. }
  457. },{}]},{},[1])(1)
  458. });