Methods
-
addInterceptor(interceptorFn){LuCI.rpc~interceptorFn}
-
Registers a new interceptor function.
Name Type Description interceptorFnLuCI.rpc~interceptorFn The interceptor function to register.
Returns:
Type Description LuCI.rpc~interceptorFn Returns the given function value. -
declare(options){LuCI.rpc~invokeFn}
-
Describes a remote RPC call procedure and returns a function implementing it.
Name Type Description optionsLuCI.rpc.DeclareOptions If any object names are given, this function will return the method signatures of each given object.
Returns:
Type Description LuCI.rpc~invokeFn Returns a new function implementing the method call described in options. -
Returns the current RPC base URL.
Returns:
Type Description string Returns the RPC URL endpoint to issue requests against. -
Returns the current RPC session id.
Returns:
Type Description string Returns the 32 byte session ID string used for authenticating remote requests. -
Translates a numeric
ubuserror code into a human readable description.Name Type Description statusCodenumber The numeric status code.
Returns:
Type Description string Returns the textual description of the code. -
Lists available remote ubus objects or the method signatures of specific objects.
This function has two signatures and is sensitive to the number of arguments passed to it:
list()- Returns an array containing the names of all remoteubusobjectslist("objname", ...)Returns method signatures for each givenubusobject name.
Name Type Description objectNamesstring optional repeatable If any object names are given, this function will return the method signatures of each given object.
Returns:
Type Description Promise.<(Array.<string>|Object.<string, Object.<string, Object.<string, string>>>)> When invoked without arguments, this function will return a promise resolving to an array of ubusobject names. When invoked with one or more arguments, a promise resolving to an object describing the method signatures of each requestedubusobject name will be returned. -
Removes a registered interceptor function.
Name Type Description interceptorFnLuCI.rpc~interceptorFn The interceptor function to remove.
Returns:
Type Description boolean Returns trueif the given function has been removed orfalseif it has not been found. -
Set the RPC base URL to use.
Name Type Description urlstring Sets the RPC URL endpoint to issue requests against.
-
Set the RPC session id to use.
Name Type Description sidstring Sets the 32 byte session ID string used for authenticating remote requests.
Type Definitions
-
LuCI.rpc.DeclareOptionsObject
-
params: [ "foo", "bar" ]- When the resulting call function is invoked withfn(true, false), the corresponding args object sent to the remote procedure will be{ foo: true, bar: false }.params: [ "test" ], filter: function(reply, args, extra) { ... }- When the resulting generated function is invoked withfn("foo", "bar", "baz")then{ "test": "foo" }will be sent as argument to the remote procedure and the filter function will be invoked withfilterFn(reply, [ "foo" ], "bar", "baz")expect: { '': { error: 'Invalid response' } }- This requires the entireubusreply to be a plain JavaScript object. If the reply isn't an object but e.g. an array or a numeric error code instead, it will get replaced with{ error: 'Invalid response' }instead.expect: { results: [] }- This requires the receivedubusreply to be an object containing a keyresultswith an array as value. If the received reply does not contain such a key, or ifreply.resultspoints to a non-array value, the empty array ([]) will be used instead.expect: { success: false }- This requires the receivedubusreply to be an object containing a keysuccesswith a boolean value. If the reply does not containsuccessor ifreply.successis not a boolean value,falsewill be returned as default instead.
Properties:
Name Type Argument Default Description objectstring The name of the remote
ubusobject to invoke.methodstring The name of the remote
ubusmethod to invoke.paramsArray.<string> <optional>
Lists the named parameters expected by the remote
ubusRPC method. The arguments passed to the resulting generated method call function will be mapped to named parameters in the order they appear in this array.Extraneous parameters passed to the generated function will not be sent to the remote procedure but are passed to the
filter functionif one is specified.Examples:
expectObject.<string, *> <optional>
Describes the expected return data structure. The given object is supposed to contain a single key selecting the value to use from the returned
ubusreply object. The value of the sole key within theexpectobject is used to infer the expected type of the receivedubusreply data.If the received data does not contain
expect's key, or if the type of the data differs from the type of the value in the expect object, the expect object's value is returned as default instead.The key in the
expectobject may be an empty string ('') in which case the entire reply object is selected instead of one of its subkeys.If the
expectoption is omitted, the received reply will be returned as-is, regardless of its format or type.Examples:
filterLuCI.rpc~filterFn <optional>
Specifies an optional filter function which is invoked to transform the received reply data before it is returned to the caller.
rejectboolean <optional>
false If set to
true, non-zero ubus call status codes are treated as fatal error and lead to the rejection of the call promise. The default behaviour is to resolve with the call return code value instead. -
The filter function is invoked to transform a received
ubusRPC call reply before returning it to the caller.Name Type Description data* The received
ubusreply data or a subset of it as described in theexpectoption of the RPC call declaration. In case of remote call errors,datais numericubuserror code instead.argsArray.<*> The arguments the RPC method has been invoked with.
extraArgs* repeatable All extraneous arguments passed to the RPC method exceeding the number of arguments describes in the RPC call declaration.
Returns:
Type Description * The return value of the filter function will be returned to the caller of the RPC method as-is. -
Registered interceptor functions are invoked before the standard reply parsing and handling logic.
By returning rejected promises, interceptor functions can cause the invocation function to fail, regardless of the received reply.
Interceptors may also modify their message argument in-place to rewrite received replies before they're processed by the standard response handling code.
A common use case for such functions is to detect failing RPC replies due to expired authentication in order to trigger a new login.
Name Type Description msg* The unprocessed, JSON decoded remote RPC method call reply.
Since interceptors run before the standard parsing logic, the reply data is not verified for correctness or filtered according to
expectandfilterspecifications in the declarations.reqObject The related request object which is an extended variant of the declaration object, allowing access to internals of the invocation function such as
filter,expectorparamsvalues.Returns:
Type Description Promise.<*> | * Interceptor functions may return a promise to defer response processing until some delayed work completed. Any values the returned promise resolves to are ignored. When the returned promise rejects with an error, the invocation function will fail too, forwarding the error to the caller. -
The generated invocation function is returned by
rpc.declare()and encapsulates a single RPC method call.Calling this function will execute a remote
ubusHTTP call request using the arguments passed to it as arguments and return a promise resolving to the received reply values.Name Type Description params* repeatable The parameters to pass to the remote procedure call. The given positional arguments will be named to named RPC parameters according to the names specified in the
paramsarray of the method declaration.Any additional parameters exceeding the amount of arguments in the
paramsdeclaration are passed as private extra arguments to the declared filter function.Returns:
Type Description Promise.<*> Returns a promise resolving to the result data of the remote ubusRPC method invocation, optionally substituted and filtered according to theexpectandfilterdeclarations.