1 | 'use strict';
|
2 |
|
3 | const isString = require('lodash/isString');
|
4 | const isUndefined = require('lodash/isUndefined');
|
5 | const isObject = require('lodash/isObject');
|
6 | const isArray = require('lodash/isArray');
|
7 | const isFunction = require('lodash/isFunction');
|
8 | const uuid = require('uuid/v4');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | const generateRequest = function(method, params, id, options) {
|
23 | if(!isString(method)) {
|
24 | throw new TypeError(method + ' must be a string');
|
25 | }
|
26 |
|
27 | options = options || {};
|
28 |
|
29 | const request = {
|
30 | method: method
|
31 | };
|
32 |
|
33 |
|
34 | if(isUndefined(options.version) || options.version !== 1) {
|
35 | request.jsonrpc = '2.0';
|
36 | }
|
37 |
|
38 | if(params) {
|
39 |
|
40 |
|
41 | if(!isObject(params) && !isArray(params)) {
|
42 | throw new TypeError(params + ' must be an object, array or omitted');
|
43 | }
|
44 |
|
45 | request.params = params;
|
46 |
|
47 | }
|
48 |
|
49 |
|
50 | if(typeof(id) === 'undefined') {
|
51 | const generator = isFunction(options.generator) ? options.generator : function() { return uuid(); };
|
52 | request.id = generator(request, options);
|
53 | } else {
|
54 | request.id = id;
|
55 | }
|
56 |
|
57 | return request;
|
58 | };
|
59 |
|
60 | module.exports = generateRequest;
|