Create a new RemoteObjects
with the given options
.
var remoteObjects = require('strong-remoting').create();
Name | Type | Description |
---|---|---|
options |
Object
|
Name | Type | Description |
---|---|---|
auth |
Object
|
Authentication options used by underlying adapters to set authorization metadata. The
|
auth.username |
String
|
|
auth.password |
String
|
|
auth.bearer |
String
|
The bearer token. |
auth.sendImmediately |
Boolean
|
Defaults to |
Define a named type conversion. The conversion is used when a
SharedMethod
argument defines a type with the given name
.
remotes.defineType('MyType', function(val, ctx) {
// use the val and ctx objects to return the concrete value
return new MyType(val);
});
Note: the alias remotes.convert()
is deprecated.
Name | Type | Description |
---|---|---|
name |
String
|
The type name |
converter |
Function
|
Get an adapter by name.
Name | Type | Description |
---|---|---|
name |
String
|
The adapter name |
Add a shared class.
Name | Type | Description |
---|---|---|
sharedClass |
SharedClass
|
Execute the given hook
function after the matched method string.
Name | Type | Description |
---|---|---|
methodMatch |
String
|
The glob to match a method string |
hook |
Function
|
Name | Type | Description |
---|---|---|
ctx |
Context
|
The adapter specific context |
next |
Function
|
Call with an optional error object |
method |
SharedMethod
|
The SharedMethod object |
Do something after the speak
instance method.
NOTE: you cannot cancel a method after it has been called.
remotes.after('dog.prototype.speak', function(ctx, next) {
console.log('After speak!');
next();
});
Do something before all methods.
remotes.before('**', function(ctx, next, method) {
console.log('Calling:', method.name);
next();
});
Modify all returned values named result
.
remotes.after('**', function(ctx, next) {
ctx.result += '!!!';
next();
});
Execute the given hook
function after the method matched by the method
string failed.
Name | Type | Description |
---|---|---|
methodMatch |
String
|
The glob to match a method string |
hook |
Function
|
Name | Type | Description |
---|---|---|
ctx |
Context
|
The adapter specific context |
next |
Function
|
Call with an optional error object |
method |
SharedMethod
|
The SharedMethod object |
Do something after the speak
instance method failed.
remotes.afterError('dog.prototype.speak', function(ctx, next) {
console.log('Cannot speak!', ctx.error);
next();
});
Do something before all methods:
remotes.afterError('**', function(ctx, next, method) {
console.log('Failed', method.name, ctx.error);
next();
});
Modify all returned errors:
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:
remotes.after('dog.prototype.speak', function(ctx, next) {
console.error(ctx.error);
next(new Error('See server console log for details.'));
});
Execute the given function before the matched method string.
Name | Type | Description |
---|---|---|
methodMatch |
String
|
The glob to match a method string |
hook |
Function
|
Name | Type | Description |
---|---|---|
ctx |
Context
|
The adapter specific context |
next |
Function
|
Call with an optional error object |
method |
SharedMethod
|
The SharedMethod object |
Do something before the user.greet
method is called:
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:
remotes.before('user.*', function(ctx, next) {
console.log('Calling a user method.');
next();
});
Do something before a dog
instance method:
remotes.before('dog.prototype.*', function(ctx, next) {
var dog = this;
console.log('Calling a method on "%s".', dog.name);
next();
});
Get all classes.
Create a connection to a remoting server.
Name | Type | Description |
---|---|---|
url |
String
|
Server root |
name |
String
|
Name of the adapter (eg. "rest") |
Delete a named type conversion.
remotes.deleteType('MyType');
Name | Type | Description |
---|---|---|
name |
String
|
The type name |
Find a method by its string name.
Name | Type | Description |
---|---|---|
methodString |
String
|
String specifying the method. For example:
|
Create a handler from the given adapter.
Name | Type | Description |
---|---|---|
name |
String
|
Adapter name |
options |
Object
|
Adapter options |
Invoke a method on a remote server using the connected adapter.
Name | Type | Description |
---|---|---|
method |
String
|
The remote method string |
[ctorArgs] |
String
|
Constructor arguments (for prototype methods) |
[args] |
String
|
Method arguments |
[callback] |
Function
|
callback |
Name | Type | Description |
---|---|---|
err |
Error
|
|
arg... |
Any
|
Invoke the given shared method using the supplied context. Execute registered before/after hooks.
Name | Type | Description |
---|---|---|
ctx |
Object
|
|
method |
Object
|
|
cb |
function(Error|undefined)
|
List all methods.
Get as JSON.
Create a new SharedClass
with the given options
.
Name | Type | Description |
---|---|---|
name |
String
|
The |
constructor |
Function
|
The |
options |
Object
|
Additional options. |
Name | Type | Description |
---|---|---|
ctor |
Function
|
The |
http |
Object
|
The HTTP settings |
Normalize HTTP path.
Define a shared method with the given name.
Name | Type | Description |
---|---|---|
name |
String
|
The method name |
[options] |
Object
|
Set of options used to create a |
Disable a sharedMethod with the given name or function object.
Name | Type | Description |
---|---|---|
fn |
String
|
The function or method name |
isStatic |
Boolean
|
Disable a static or prototype method |
Disable a sharedMethod with the given static or prototype method name.
Name | Type | Description |
---|---|---|
methodName |
String
|
The method name |
Find a sharedMethod with the given name or function object.
Name | Type | Description |
---|---|---|
fn |
String or Function
|
The function or method name |
[isStatic] |
Boolean
|
Required if |
Name | Type | Description |
---|---|---|
result |
SharedMethod
|
Find a sharedMethod with the given static or prototype method name.
Name | Type | Description |
---|---|---|
methodName |
String
|
The method name Find a static or prototype method with the given name. |
Name | Type | Description |
---|---|---|
result |
SharedMethod
|
Get a key for the given method.
Name | Type | Description |
---|---|---|
fn |
String
|
The function or method name. |
isStatic |
Boolean
|
True if the method is static. |
Get all shared methods belonging to this shared class.
Name | Type | Description |
---|---|---|
options |
|
{Object} |
options.includeDisabled |
|
{Boolean} include all methods, even disabled. |
Name | Type | Description |
---|---|---|
result |
Array.<SharedMethod>
|
An array of shared methods |
Define a shared method resolver for dynamically defining methods.
Name | Type | Description |
---|---|---|
resolver |
Function
|
The resolver function. |
// below is a simple example
sharedClass.resolve(function(define) {
define('myMethod', {
accepts: {arg: 'str', type: 'string'},
returns: {arg: 'str', type: 'string'}
errors: [ { code: 404, message: 'Not Found', responseModel: 'Error' } ]
}, myMethod);
});
function myMethod(str, cb) {
cb(null, str);
}
Create a new SharedMethod
(remote method) with the given fn
.
See also Remote methods.
Name | Type | Description |
---|---|---|
fn |
Function
|
The |
name |
String
|
The name of the |
sharedClass |
SharedClass
|
The |
options |
Object
|
See below. |
Name | Type | Description |
---|---|---|
[accepts] |
Array or Object
|
Defines either a single argument as an object or an ordered set of arguments as an array. |
[accepts.arg] |
String
|
The name of the argument. |
[accepts.description] |
String
|
Text description of the argument, used by API documentation generators like Swagger. |
[accepts.http] |
String
|
HTTP mapping for the argument. See argument mapping in the docs. |
[accepts.http.source] |
String
|
The HTTP source for the argument. May be one of the following:
|
[accepts.rest] |
Object
|
The REST mapping / settings for the argument. |
[accepts.type] |
String
|
Argument datatype; must be a Loopback type. |
[aliases] |
Array
|
A list of aliases for the method. |
[errors] |
Array or Object
|
Object or |
[http] |
Array
|
HTTP-only options. |
[http.errorStatus] |
Number
|
Default error status code. |
[http.path] |
String
|
HTTP path (relative to the model) at which the method is exposed. |
[http.status] |
Number
|
Default status code when the callback is called without an error. |
[http.verb] |
String
|
HTTP method (verb) at which the method is available. One of: get, post (default), put, del, or all |
[isStatic] |
Boolean
|
Whether the method is a static method or a prototype method. |
[returns] |
Array or Object
|
Specifies the remote method's callback arguments; either a single argument as an object or an ordered set of arguments as an array.
The Additionally, one of the callback arguments can have |
[shared] |
Boolean
|
Whether the method is shared. Default is |
[status] |
Number
|
The default status code. |
Name | Type | Description |
---|---|---|
name |
String
|
The method name. |
aliases |
Array.<String>
|
An array of method aliases. |
isStatic |
Boolean
|
Whether the method is static; from |
accepts |
Array or Object
|
See |
returns |
Array or Object
|
See |
errors |
Array or Object
|
See |
description |
String
|
Text description of the method. |
notes |
String
|
Additional notes, used by API documentation generators like Swagger. |
http |
String
|
|
rest |
String
|
|
shared |
String
|
|
[documented] |
Boolean
|
Default: true. Set to |
Coerce an 'accepts' value into its final type. If using HTTP, some coercion is already done in http-context.
This should only do very simple coercion.
Name | Type | Description |
---|---|---|
uarg |
|
Argument value. |
desc |
Object
|
Argument description. |
Returns a reformatted Object valid for consumption as remoting function arguments
Returns an appropriate type based on a type specifier from remoting metadata.
Name | Type | Description |
---|---|---|
type |
Object
|
A type specifier from remoting metadata, e.g. "[Number]" or "MyModel" from |
Name | Type | Description |
---|---|---|
result |
String
|
A type name compatible with the values returned by |
Create a new SharedMethod
with the given fn
. The function should include
all the method options.
Name | Type | Description |
---|---|---|
fn |
Function
|
|
name |
Function
|
|
SharedClass |
SharedClass
|
|
isStatic |
Boolean
|
Returns an appropriate type based on val
.
Name | Type | Description |
---|---|---|
val |
|
The value to determine the type for |
Name | Type | Description |
---|---|---|
result |
String
|
The type name |
Returns a reformatted Object valid for consumption as JSON from an Array of
results from a remoting function, based on returns
.
Add an alias
Name | Type | Description |
---|---|---|
alias |
String
|
Alias method name. |
Get the function the SharedMethod
will invoke()
.
Execute the remote method using the given arg data.
Name | Type | Description |
---|---|---|
scope |
Object
|
|
args |
Object
|
containing named argument data |
remotingOptions |
Object
|
remote-objects options |
cb |
Function
|
callback |
Determine if this shared method invokes the given "suspect" function.
Name | Type | Description |
---|---|---|
suspect |
String or Function
|
The name of the suspected function or a |
Name | Type | Description |
---|---|---|
result |
|
Boolean True if the shared method invokes the given function; false otherwise. |
sharedMethod.isDelegateFor(myClass.myMethod); // pass a function
sharedMethod.isDelegateFor(myClass.prototype.myInstMethod);
sharedMethod.isDelegateFor('myMethod', true); // check for a static method by name
sharedMethod.isDelegateFor('myInstMethod', false); // instance method by name
Determine if this shared method invokes the given "suspect" function.
Name | Type | Description |
---|---|---|
result |
|
Boolean True if the shared method invokes the given function; false otherwise. |
sharedMethod.isDelegateForName('myMethod'); // check for a static method by name
sharedMethod.isDelegateForName('prototype.myInstMethod'); // instance method by name
Create a new HttpContext
with the given options
.
Invoking a remote method via HTTP creates HttpContext
object.
Name | Type | Description |
---|---|---|
req |
Object
|
Express Request object. |
res |
Object
|
Express Response object. |
method |
Function
|
|
options |
Object
|
See below. |
Name | Type | Description |
---|---|---|
xml |
Boolean
|
Set to |
Utility functions to send response body
Build args object from the http context's req
and res
.
Finish the request and send the correct response.
Get an arg by name using the given options.
Name | Type | Description |
---|---|---|
name |
String
|
|
options |
Object
|
optional |
Invoke the given shared method using the provided scope against the current context.
Deciding on the operation of response, function is called inside this.done()
Create a new HttpInvocation
.
Name | Type | Description |
---|---|---|
method |
SharedMethod
|
|
[args] |
Array
|
|
base |
String
|
The base URL |
Name | Type | Description |
---|---|---|
base |
String
|
The base URL |
method |
SharedMethod
|
The |
args |
Array
|
The arguments to be used when invoking the |
Inherit from EventEmitter
.
Determine if the value matches the given accept definition.
Build args object from the http context's req
and res
.
Get an argument value by name.
Name | Type | Description |
---|---|---|
name |
String
|
Name | Type | Description |
---|---|---|
result |
|
Value of specified argument. |
Get Response object
Start the invocation.
Transform the response into callback arguments
Name | Type | Description |
---|---|---|
res |
HttpResponse
|
|
callback |
Function
|