-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjsonrpc.js
158 lines (144 loc) · 4.71 KB
/
jsonrpc.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
/**!
* angular-jsonrpc v0.1.4 [build 2015-10-04]
* @copyright 2015 Arunjit Singh <[email protected]>. All Rights Reserved.
* @license MIT; see LICENCE.
* [https://github.com/ajsd/angular-jsonrpc.git]
*/
'use strict';
/**
* Provides and configures the jsonrpc service.
*/
angular.module('jsonrpc', ['uuid']).provider('jsonrpc', function() {
var defaults = this.defaults = {};
// defaults
defaults.basePath = '/rpc';
// provider.$get
this.$get = ['$http', '$q', 'uuid4', function($http, $q, uuid4) {
/**
* Makes a JSON-RPC request to `method` with `data`.
*
* @param {{path:string=, method:string, data:*)}} options Call options.
* @param {angular.$http.Config} config HTTP config.
* @return {angular.$http.HttpPromise}
*/
function jsonrpc(options, config) {
var id = uuid4.generate();
var payload = {
jsonrpc: '2.0',
method: options.method,
id: id
};
if (angular.isDefined(options.data)) {
payload.params = options.data;
}
// Transformers to extract the response data.
// TODO(arunjit): Use response interceptors when the API is stable.
// REMOVED (jaap): lijkt onnodig
/*
var transforms = [];
angular.forEach($http.defaults.transformResponse, function(t) {
transforms.push(t);
});
transforms.push(function(data) {
return data.id === id ? data.result || data.error : null;
});
config = config || {};
var configTransforms = config.transformResponse;
if (angular.isArray(configTransforms)) {
[].push.apply(transforms, configTransforms);
} else if (angular.isFunction(configTransforms)) {
transforms.push(configTransforms);
}
config.transformResponse = transforms;
*/
// TODO(arunjit): Use $q to resolve the result.
// ADD(jaap): return response data
return $http.post(options.path || defaults.basePath, payload, config)
.then( function(response){
if( response.data.hasOwnProperty('error') ){
return $q.reject(response.data.error);
}
return response.data.result;
});
}
/**
* Shorthand for making a request.
*
* @param {string} path The call path.
* @param {string} method The method to call.
* @param {?*} data The data for the call.
* @param {angular.$http.Config} config HTTP config.
* @return {angular.$http.HttpPromise}
*/
jsonrpc.request = function(path, method, data, config) {
if (arguments.length < 4) {
config = data;
data = method;
method = path;
path = null;
}
return jsonrpc({path: path, method: method, data: data}, config);
};
/**
* Helper to create services.
*
* Usage:
* module.service('locationService', function(jsonrpc) {
* var service = jsonrpc.newService('locationsvc');
* this.get = service.createMethod('Get');
* });
* ...
* module.controller(..., function(locationService) {
* locationService.get({max: 10}).success(function(d) {...});
* // GET /rpc
* // {"method": "locationsvc.Get", "params": {"max": 10}, ...}
* });
*
* @param {string} name The name of the service. This is the prefix used for
* all methods created through this service.
* @param {string} path Optional path for this service.
* @constructor
*/
function Service(name, path) {
this.serviceName = name;
this.path = path;
}
/**
* Creates a new service method.
*
* @param {string} name Method name.
* @param {angular.$http.Config=} config HTTP config.
* @return {function(*):angular.$http.HttpPromise} An implementation for the
* service method.
*/
Service.prototype.createMethod = function(name, config) {
var path = this.path;
var method = name;
if (this.serviceName) {
method = this.serviceName + '.' + method;
}
return function(data) {
return jsonrpc.request(path, method, data, config);
};
};
/** Creates a new Service with the given `name` and optional `path`. */
jsonrpc.newService = function(name, path) {
return new Service(name, path);
};
// ADDED (jaap): set basepath after runtime
jsonrpc.setBasePath = function(path) {
defaults.basePath = path;
return this;
};
// ADDED (jaap): get basepath after runtime
jsonrpc.getBasePath = function() {
return defaults.basePath;
};
return jsonrpc;
}];
/** Set the base path for all JSON-RPC calls to |path|. */
this.setBasePath = function(path) {
defaults.basePath = path;
return this;
};
});