-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathremote-objects.js
839 lines (737 loc) · 23.3 KB
/
remote-objects.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
// Node module: strong-remoting
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
var g = require('strong-globalize')();
/*!
* Expose `RemoteObjects`.
*/
module.exports = RemoteObjects;
/*!
* Module dependencies.
*/
var EventEmitter = require('eventemitter2').EventEmitter2;
var debug = require('debug')('strong-remoting:remotes');
var deprecated = require('depd')('strong-remoting');
var util = require('util');
var urlUtil = require('url');
var inherits = util.inherits;
var assert = require('assert');
var SharedClass = require('./shared-class');
var SharedMethod = require('./shared-method');
var ExportsHelper = require('./exports-helper');
var PhaseList = require('loopback-phase').PhaseList;
var TypeRegistry = require('./type-registry');
// require the rest adapter for browserification
// TODO(ritch) remove this somehow...?
require('./rest-adapter');
/**
* Create a new `RemoteObjects` with the given `options`.
*
* ```js
* var remoteObjects = require('strong-remoting').create();
* ```
*
* @param {Object} options
* @return {RemoteObjects}
* @class
* @property {Object} auth Authentication options used by underlying adapters
* to set authorization metadata. **The `rest` adapter supports:**
*
* - **basic** - `username` and `password` are required.
* - **digest** - `username` and `password` required and `sendImmediately` must
* be false.
* - **bearer** - `bearer` must be set as the bearer token
*
* @property {String} auth.username
* @property {String} auth.password
* @property {String} auth.bearer The **bearer token**.
* @property {Boolean} auth.sendImmediately Defaults to `false`.
*/
function RemoteObjects(options) {
EventEmitter.call(this, {wildcard: true});
// Avoid warning: possible EventEmitter memory leak detected
this.setMaxListeners(16);
this.options = options || {};
this.exports = this.options.exports || {};
this._typeRegistry = new TypeRegistry(this.options.types);
this._classes = {};
this._setupPhases();
}
/*!
* Inherit from `EventEmitter`.
*/
inherits(RemoteObjects, EventEmitter);
/*!
* Simplified APIs
*/
RemoteObjects.create = function(options) {
return new RemoteObjects(options);
};
RemoteObjects.extend = function(exports) {
return new ExportsHelper(exports);
};
/**
* Create a handler from the given adapter.
*
* @param {String} nameOrClass Adapter name or provided adapter class
* @param {Object} options Adapter options
* @return {Function}
*/
RemoteObjects.prototype.handler = function(nameOrClass, options) {
var Adapter = this.adapter(nameOrClass);
var adapter = new Adapter(this, options);
var handler = adapter.createHandler();
if (handler) {
// allow adapter reference from handler
handler.adapter = adapter;
}
return handler;
};
/**
* Create a connection to a remoting server.
*
* @param {String} url Server root
* @param {String} name Name of the adapter (eg. "rest")
*/
RemoteObjects.prototype.connect = function(url, name) {
// parse URL for auth
var urlWithoutAuth = url;
var auth;
var parsedUrl = urlUtil.parse(url);
// If base parsedUrl contains auth, extract it so we can set it separately
if (parsedUrl.auth) {
auth = this.auth = {};
auth.username = parsedUrl.auth.split(':')[0];
auth.password = parsedUrl.auth.split(':')[1];
// set base without auth so request honours our auth options
delete parsedUrl.auth;
urlWithoutAuth = urlUtil.format(parsedUrl);
// ensure a "/" hasn't been appended where there wasn't one before
if (url[url.length - 1] !== urlWithoutAuth[urlWithoutAuth.length - 1]) {
urlWithoutAuth = urlWithoutAuth.slice(0, -1);
}
}
var Adapter = this.adapter(name);
var adapter = new Adapter(this);
this.serverAdapter = adapter;
return adapter.connect(urlWithoutAuth);
};
/**
* Invoke a method on a remote server using the connected adapter.
*
* @param {String} method The remote method string
* @param {String} [ctorArgs] Constructor arguments (for prototype methods)
* @param {String} [args] Method arguments
* @callback {Function} [callback] callback
* @param {Error} err
* @param {Any} arg...
* @end
*/
RemoteObjects.prototype.invoke = function(method, ctorArgs, args, callback) {
assert(this.serverAdapter,
g.f('Cannot invoke method without an adapter. See {{RemoteObjects#connect().}}'));
return this.serverAdapter.invoke.apply(this.serverAdapter, arguments, callback);
};
/**
* Get an adapter by name.
* @param {String} nameOrClass The adapter name or provided class
* @return {Adapter}
*/
RemoteObjects.prototype.adapter = function(nameOrClass) {
if (typeof nameOrClass === 'function') {
// Throws an error if the provided adapter doesn't meets the requirements
this.validateAdapter(nameOrClass);
return nameOrClass;
} else {
return require('./' + nameOrClass + '-adapter');
}
};
RemoteObjects.prototype.getRequiredAdapterMethods = () => ['createHandler'];
RemoteObjects.prototype.validateAdapter = function(Adapter) {
const requiredMethods = this.getRequiredAdapterMethods();
const notImplementedMethods = requiredMethods.filter(
methodString => typeof Adapter.prototype[methodString] !== 'function'
);
if (notImplementedMethods.length > 0) {
throw new Error(g.f(`
Invalid adapter class.
The provided adapter class does not implement
the following required methods: %s`,
notImplementedMethods.join(', ')
));
}
return Adapter;
};
/**
* Get all classes.
*/
RemoteObjects.prototype.classes = function(options) {
options = options || {};
var exports = this.exports;
var result = [];
var sharedClasses = this._classes;
Object
.keys(exports)
.forEach(function(name) {
result.push(new SharedClass(name, exports[name], options));
});
Object
.keys(sharedClasses)
.forEach(function(name) {
result.push(sharedClasses[name]);
});
return result;
};
/**
* Add a shared class.
*
* @param {SharedClass} sharedClass
*/
RemoteObjects.prototype.addClass = function(sharedClass) {
assert(sharedClass && sharedClass.constructor.name === 'SharedClass',
g.f('must provide a valid {{SharedClass}}'));
this._classes[sharedClass.name] = sharedClass;
};
/**
* Find a method by its string name.
*
* @param {String} methodString String specifying the method. For example:
*
* - `MyClass.prototype.myMethod`
* - `MyClass.staticMethod`
* - `obj.method`
*/
RemoteObjects.prototype.findMethod = function(methodString) {
var methods = this.methods();
for (var i = 0; i < methods.length; i++) {
if (methods[i].stringName === methodString) return methods[i];
}
};
/**
* List all methods.
*/
RemoteObjects.prototype.methods = function() {
var methods = [];
this
.classes()
.forEach(function(sc) {
methods = sc.methods().concat(methods);
});
return methods;
};
/**
* Get as JSON.
*/
RemoteObjects.prototype.toJSON = function() {
var result = {};
var methods = this.methods();
methods.forEach(function(sharedMethod) {
result[sharedMethod.stringName] = {
http: sharedMethod.fn && sharedMethod.fn.http,
accepts: sharedMethod.accepts,
returns: sharedMethod.returns,
errors: sharedMethod.errors,
};
});
return result;
};
/**
* Execute the given function before the matched method string.
*
* @example
*
* Do something before the `user.greet` method is called:
* ```js
* remotes.before('user.greet', function(ctx, next) {
* if ((ctx.req.param('password') || '').toString() !== '1234') {
* next(new Error('Bad password!'));
* } else {
* next();
* }
* });
* ```
*
* Do something before any `user` method:
* ```js
* remotes.before('user.*', function(ctx, next) {
* console.log('Calling a user method.');
* next();
* });
* ```
*
* Do something before a `dog` instance method:
* ```js
* remotes.before('dog.prototype.*', function(ctx, next) {
* var dog = this;
* console.log('Calling a method on "%s".', dog.name);
* next();
* });
* ```
*
* @param {String} methodMatch The glob to match a method string
* @callback {Function} hook
* @param {Context} ctx The adapter specific context
* @param {Function} next Call with an optional error object
* @param {SharedMethod} method The SharedMethod object
*/
RemoteObjects.prototype.before = function(methodMatch, fn) {
this.on('before.' + methodMatch, fn);
};
/**
* Execute the given `hook` function after the matched method string.
*
* @example
*
* Do something after the `speak` instance method.
* NOTE: you cannot cancel a method after it has been called.
* ```js
* remotes.after('dog.prototype.speak', function(ctx, next) {
* console.log('After speak!');
* next();
* });
*```
*
* Do something before all methods.
```js
* remotes.before('**', function(ctx, next, method) {
* console.log('Calling:', method.name);
* next();
* });
* ```
*
* Modify all returned values named `result`.
* ```js
* remotes.after('**', function(ctx, next) {
* ctx.result += '!!!';
* next();
* });
* ```
*
* @param {String} methodMatch The glob to match a method string
* @callback {Function} hook
* @param {Context} ctx The adapter specific context
* @param {Function} next Call with an optional error object
* @param {SharedMethod} method The SharedMethod object
*/
RemoteObjects.prototype.after = function(methodMatch, fn) {
this.on('after.' + methodMatch, fn);
};
/**
* Execute the given `hook` function after the method matched by the method
* string failed.
*
* @example
* Do something after the `speak` instance method failed.
*
* ```js
* remotes.afterError('dog.prototype.speak', function(ctx, next) {
* console.log('Cannot speak!', ctx.error);
* next();
* });
* ```
*
* Do something before all methods:
* ```js
* remotes.afterError('**', function(ctx, next, method) {
* console.log('Failed', method.name, ctx.error);
* next();
* });
* ```
*
* Modify all returned errors:
* ```js
* remotes.after('**', function(ctx, next) {
* if (!ctx.error.details) ctx.result.details = {};
* ctx.error.details.info = 'intercepted by a hook';
* next();
* });
* ```
*
* Report a different error:
* ```js
* remotes.after('dog.prototype.speak', function(ctx, next) {
* console.error(ctx.error);
* next(new Error('See server console log for details.'));
* });
* ```
*
* @param {String} methodMatch The glob to match a method string
* @callback {Function} hook
* @param {Context} ctx The adapter specific context
* @param {Function} next Call with an optional error object
* @param {SharedMethod} method The SharedMethod object
*/
RemoteObjects.prototype.afterError = function(methodMatch, fn) {
this.on('afterError.' + methodMatch, fn);
};
RemoteObjects.prototype.registerPhaseHandler = function(phaseName,
methodNameWildcard,
handler) {
var pattern = methodNameWildcard.replace(/[.+?^${}()|[\]\\]/g, '\\$&')
// single star matches one segment only
.replace(/(^|\.)\*($|\.)/g, '$1[^.]*$2')
// double-start match one or more segments
.replace(/(^|\.)\*\*($|\.)/g, '$1.*$2');
var matcher = new RegExp('^' + pattern + '$');
debug('registerPhaseHandler(%j) -> pattern %j',
methodNameWildcard,
pattern);
this.phases.registerHandler(phaseName, function matchHandler(ctx, next) {
if (matcher.test(ctx.method.stringName)) {
handler(ctx, next);
} else {
next();
}
});
};
/*!
* Create a middleware style emit that supports wildcards.
*/
RemoteObjects.prototype.execHooks = function(when, method, scope, ctx, next) {
var stack = [];
var ee = this;
var isStatic = method.isStatic ||
method.sharedMethod && method.sharedMethod.isStatic;
var type, handler;
// Commented-out by bajtos: init is not defined.
// this._events || init.call(this);
// context
this.objectName = method.sharedClass && method.sharedClass.name ||
method.restClass && method.restClass.name;
this.methodName = method.name;
if (method.fullName) {
type = when + '.' + method.fullName;
} else {
type = when + '.' + this.objectName +
(isStatic ? '.' : '.prototype.') + this.methodName;
}
if (this.wildcard) {
handler = [];
var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
searchListenerTree.call(this, handler, ns, this.listenerTree, 0);
} else {
handler = this._events[type];
}
if (typeof handler === 'function') {
this.event = type;
addToStack(handler);
return execStack();
} else if (handler) {
var l = arguments.length; // eslint-disable-line one-var
var i; // eslint-disable-line one-var
var args = new Array(l - 1);
for (i = 1; i < l; i++) {
args[i - 1] = arguments[i];
}
var listeners = handler.slice();
for (i = 0, l = listeners.length; i < l; i++) {
addToStack(listeners[i]);
}
}
function addToStack(fn) {
stack.push(fn);
}
function execStack(err) {
if (err) return next(err);
var cur = stack.shift();
if (cur) {
try {
var result = cur.call(scope, ctx, execStack, method);
if (result && typeof result.then === 'function') {
result.then(function() { execStack(); }, next);
}
} catch (err) {
next(err);
}
} else {
next();
}
}
return execStack();
};
// from EventEmitter2
function searchListenerTree(handlers, type, tree, i) {
if (!tree) {
return [];
}
var listeners = [];
var leaf, len, branch, xTree, xxTree, isolatedBranch, endReached;
var typeLength = type.length;
var currentType = type[i];
var nextType = type[i + 1];
if (i === typeLength && tree._listeners) {
//
// If at the end of the event(s) list and the tree has listeners
// invoke those listeners.
//
if (typeof tree._listeners === 'function') {
if (handlers) handlers.push(tree._listeners);
return [tree];
} else {
for (leaf = 0, len = tree._listeners.length; leaf < len; leaf++) {
if (handlers) handlers.push(tree._listeners[leaf]);
}
return [tree];
}
}
if ((currentType === '*' || currentType === '**') || tree[currentType]) {
//
// If the event emitted is '*' at this part
// or there is a concrete match at this patch
//
if (currentType === '*') {
for (branch in tree) {
if (branch !== '_listeners' && tree.hasOwnProperty(branch)) {
listeners = listeners.concat(
searchListenerTree(handlers, type, tree[branch], i + 1));
}
}
return listeners;
} else if (currentType === '**') {
endReached = (i + 1 === typeLength || (i + 2 === typeLength && nextType === '*'));
if (endReached && tree._listeners) {
// The next element has a _listeners, add it to the handlers.
listeners = listeners.concat(
searchListenerTree(handlers, type, tree, typeLength));
}
for (branch in tree) {
if (branch !== '_listeners' && tree.hasOwnProperty(branch)) {
if (branch === '*' || branch === '**') {
if (tree[branch]._listeners && !endReached) {
listeners = listeners.concat(
searchListenerTree(handlers, type, tree[branch], typeLength));
}
listeners = listeners.concat(
searchListenerTree(handlers, type, tree[branch], i));
} else if (branch === nextType) {
listeners = listeners.concat(
searchListenerTree(handlers, type, tree[branch], i + 2));
} else {
// No match on this one, shift into the tree but not in the type array.
listeners = listeners.concat(
searchListenerTree(handlers, type, tree[branch], i));
}
}
}
return listeners;
}
listeners = listeners.concat(
searchListenerTree(handlers, type, tree[currentType], i + 1));
}
xTree = tree['*'];
if (xTree) {
//
// If the listener tree will allow any match for this part,
// then recursively explore all branches of the tree
//
searchListenerTree(handlers, type, xTree, i + 1);
}
xxTree = tree['**'];
if (xxTree) {
if (i < typeLength) {
if (xxTree._listeners) {
// If we have a listener on a '**', it will catch all, so add its handler.
searchListenerTree(handlers, type, xxTree, typeLength);
}
// Build arrays of matching next branches and others.
for (branch in xxTree) {
if (branch !== '_listeners' && xxTree.hasOwnProperty(branch)) {
if (branch === nextType) {
// We know the next element will match, so jump twice.
searchListenerTree(handlers, type, xxTree[branch], i + 2);
} else if (branch === currentType) {
// Current node matches, move into the tree.
searchListenerTree(handlers, type, xxTree[branch], i + 1);
} else {
isolatedBranch = {};
isolatedBranch[branch] = xxTree[branch];
searchListenerTree(handlers, type, {'**': isolatedBranch}, i + 1);
}
}
}
} else if (xxTree._listeners) {
// We have reached the end and still on a '**'
searchListenerTree(handlers, type, xxTree, typeLength);
} else if (xxTree['*'] && xxTree['*']._listeners) {
searchListenerTree(handlers, type, xxTree['*'], typeLength);
}
}
return listeners;
}
RemoteObjects.prototype._executeAuthorizationHook = function(ctx, cb) {
if (typeof this.authorization === 'function') {
this.authorization(ctx, cb);
} else {
process.nextTick(function() {
cb();
});
}
};
RemoteObjects.prototype._setupPhases = function() {
var self = this;
self.phases = new PhaseList();
var auth = self.phases.add('auth');
var invoke = self.phases.add('invoke');
auth.use(function phaseAuthorization(ctx, next) {
self._executeAuthorizationHook(ctx, next);
});
invoke.before(function reportSharedCtorError(ctx, next) {
next(ctx.sharedCtorError);
});
invoke.before(function phaseBeforeInvoke(ctx, next) {
self.execHooks('before', ctx.method, ctx.getScope(), ctx, next);
});
invoke.use(function phaseInvoke(ctx, next) {
ctx.invoke(ctx.getScope(), ctx.method, function(err, result) {
if (!err) ctx.result = result;
next(err);
});
});
invoke.after(function phaseAfterInvoke(ctx, next) {
self.execHooks('after', ctx.method, ctx.getScope(), ctx, next);
});
};
/**
* Invoke the given shared method using the supplied context.
* Execute registered before/after hooks.
*
* @param {Object} ctx
* @param {Object} method
* @param {function(Error=)} cb
*/
RemoteObjects.prototype.invokeMethodInContext = function(ctx, method, cb) {
var self = this;
var scope = ctx.getScope();
if (cb === undefined && typeof method === 'function') {
// the new API with two arguments
cb = method;
method = ctx.method;
} else {
// backwards compatibility: invokeMethodInContext(ctx, method, cb)
// TODO remove in v3.0
assert.equal(method, ctx.method);
deprecated('invokeMethodInContext(ctx, method, cb) is deprecated.' +
'Pass the method as ctx.method instead.');
}
self.phases.run(ctx, function interceptInvocationErrors(err) {
if (!err) return cb();
ctx.error = err;
self.execHooks('afterError', method, scope, ctx, function(hookErr) {
cb(hookErr || err);
});
});
};
/**
* Determine what scope object to use when invoking the given remote method in
* the given context.
* @private
*/
RemoteObjects.prototype.getScope = function(ctx, method) {
deprecated('remoteObjects.getScope(ctx, method) is deprecated, ' +
'use ctx.getScope() instead');
assert.equal(ctx.method, method);
return ctx.getScope();
};
/**
* Define a named type conversion. The conversion is used when a
* `SharedMethod` argument defines a type with the given `name`.
*
* See also `remotes.defineObjectType`.
*
* @example
*
* ```js
* remotes.defineType('MyType', {
* // Convert the raw "value" coming typically from JSON.
* // Use the remoting context in "ctx" to access the type registry and
* // other request-related information.
* fromTypedValue: function(ctx, value) {
* if (value === undefined || value === null)
* return { value: value };
*
* value = new MyType(value);
* var error = this.validate(ctx, value);
* return error ? { error: error } : { value: value };
* },
*
* // Apply any coercion needed to convert values coming from
* // string-only sources like HTTP headers and query string.
* //
* // A sloppy value is one of:
* // - a string
* // - an array containing sloppy values
* // - an object where property values are sloppy
* fromSloppyValue: function(ctx, value) {
* var objectConverter = ctx.typeRegistry.getConverter('object');
* var result = objectConverter.fromSloppyString(ctx, value);
* return result.error ? result : this.fromTypedValue(ctx, result.value);
* },
*
* // Perform basic validation of the value. Validations are required to be
* // synchronous.
* validate: function(ctx, value) {
* if (value === undefined || value === null)
* return null;
* if (typeof value !== 'object' || !(value instanceof MyType) {
* return new Error('Value is not an instance of MyType.');
* }
* return null;
* },
* });
* ```
*
* @param {String} name The type name
* @param {Object} converter A converter implementing the following methods:
*
* - `fromTypedValue(ctx, value) -> { value } or { error }`
* - `fromSloppyValue(ctx, value) -> { value } or { error }`
* - `validate(ctx, value) -> error or undefined/null`
*/
RemoteObjects.prototype.defineType = function(name, converter) {
if (typeof converter === 'function') {
throw new Error(g.f(
'%s is no longer supported. Use one of the new APIs instead: %s or %s',
'remoteObjects.defineType(name, fn)',
'remoteObjects.defineObjectType(name, factoryFn)',
'remoteObjects.defineType(name, converter)'));
}
this._typeRegistry.defineType(name, converter);
};
/**
* Define a named type conversion for an object-like type.
* The conversion is used when a `SharedMethod` argument
* defines a type with the given `name`.
*
* Under the hood, a converter is created that ensures the input data
* is an object (or sloppy value is coerced to an object) and calls
* the provided factory function to convert plain data object to
* a class instance.
*
* @example
*
* ```js
* remotes.defineObjectType('MyClass', function(data) {
* return new MyClass(data);
* });
* ```
*
* @param {String} name The type name
* @param {Function(Object)} factoryFn Factory function creating object
* instance from a plain-data object.
*/
RemoteObjects.prototype.defineObjectType = function(name, factoryFn) {
this._typeRegistry.registerObjectType(name, factoryFn);
};
RemoteObjects.convert =
RemoteObjects.prototype.convert = function(name, fn) {
throw new Error(g.f(
'RemoteObjects.convert(name, fn) is no longer supported. ' +
'Use remoteObjects.defineType(name, converter) instead.'));
};
RemoteObjects.defineType = function(name, fn) {
throw new Error(g.f(
'RemoteObjects.defineType(name, fn) is no longer supported. ' +
'Use remoteObjects.defineType(name, converter) instead.'));
};