1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const EOL = require('os').EOL;
|
17 |
|
18 | const request = require('request');
|
19 |
|
20 | const { Errors, HTTPMethod } = require('./Constants');
|
21 | const KinveyError = require('./KinveyError');
|
22 | const Utils = require('./Utils');
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | class Request {
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | constructor(user, options) {
|
48 | this.options = {};
|
49 | this._init(user, options);
|
50 | }
|
51 |
|
52 | _init(user, options) {
|
53 | this._buildHeaders(user, options);
|
54 | this._buildURL(user, options);
|
55 |
|
56 | this.options.method = options.method || HTTPMethod.GET;
|
57 | if (options.timeout) {
|
58 | this.options.timeout = options.timeout;
|
59 | }
|
60 | this.options.json = true;
|
61 |
|
62 | if (options.data) {
|
63 | this.options.body = options.data;
|
64 | }
|
65 |
|
66 | if (options.formData) {
|
67 | this.options.formData = options.formData;
|
68 | }
|
69 | }
|
70 |
|
71 | _buildHeaders(user, options) {
|
72 | this.options.headers = {};
|
73 | if (!Utils.isNullOrUndefined(options.headers)) {
|
74 | this.options.headers = options.headers;
|
75 | }
|
76 |
|
77 | this.options.headers['X-Kinvey-Device-Information'] = Utils.getDeviceInformation(options.cliVersion);
|
78 | this._buildAuthHeader(user, options);
|
79 | }
|
80 |
|
81 | _buildAuthHeader(user, options) {
|
82 | if (options.skipAuth) {
|
83 | return;
|
84 | }
|
85 |
|
86 | if (!Utils.isNullOrUndefined(this.options.headers.Authorization)) {
|
87 | return;
|
88 | }
|
89 |
|
90 | if (!Utils.isEmpty(user)) {
|
91 | this.options.headers.Authorization = `Kinvey ${user.token}`;
|
92 | }
|
93 | }
|
94 |
|
95 | _buildURL(user, options) {
|
96 | let host;
|
97 | if (user) {
|
98 | host = user.host;
|
99 | } else {
|
100 | host = options.host;
|
101 | }
|
102 |
|
103 | if (options.isBaas) {
|
104 | host = options.baasHost || host.replace('manage', 'baas');
|
105 | }
|
106 |
|
107 | if (!host.endsWith('/')) {
|
108 | host = `${host}/`;
|
109 | }
|
110 |
|
111 | this.options.url = `${host}${options.endpoint}`;
|
112 |
|
113 | if (options.query) {
|
114 | this.options.qs = options.query;
|
115 | }
|
116 | }
|
117 |
|
118 | static _getDebugMessage(debug) {
|
119 | if (Utils.isNullOrUndefined(debug)) {
|
120 | return '';
|
121 | }
|
122 |
|
123 | let debugMsg = '';
|
124 | if (debug.message) {
|
125 | debugMsg = debug.message;
|
126 | } else if (typeof debug === 'string') {
|
127 | debugMsg = debug;
|
128 | } else if (typeof debug === 'object') {
|
129 | debugMsg = JSON.stringify(debug);
|
130 | }
|
131 |
|
132 | return debugMsg;
|
133 | }
|
134 |
|
135 | static _getProcessedError(err, response) {
|
136 | const isSuccess = !err && response && response.statusCode >= 200 && response.statusCode < 300;
|
137 | if (isSuccess) {
|
138 | return null;
|
139 | }
|
140 |
|
141 | if (err) {
|
142 | return Utils.getErrorFromRequestError(err);
|
143 | }
|
144 |
|
145 |
|
146 | if (response.body) {
|
147 | let errMsg = response.body.description || '';
|
148 | const debugMsg = Request._getDebugMessage(response.body.debug);
|
149 | if (debugMsg) {
|
150 | errMsg += ` ${debugMsg}`;
|
151 | }
|
152 |
|
153 | const errors = response.body.errors;
|
154 | if (!Utils.isEmpty(errors) && Array.isArray(errors)) {
|
155 | errors.forEach((x) => {
|
156 | const field = x.field ? `Field: ${x.field}.` : '';
|
157 | errMsg += `${EOL}\t${field} ${x.message}`;
|
158 | });
|
159 | }
|
160 |
|
161 | return new KinveyError(response.body.code, errMsg);
|
162 | }
|
163 |
|
164 | let errMsg = response.statusCode;
|
165 | if (response.statusMessage) {
|
166 | errMsg += ` ${response.statusMessage}`;
|
167 | }
|
168 |
|
169 | return new KinveyError(Errors.RequestError.NAME, errMsg);
|
170 | }
|
171 |
|
172 | _send(done) {
|
173 | return request(this.options, done);
|
174 | }
|
175 |
|
176 | |
177 |
|
178 |
|
179 |
|
180 |
|
181 | send(done) {
|
182 | return this._send((err, response) => {
|
183 | const errResult = Request._getProcessedError(err, response);
|
184 | if (errResult) {
|
185 | return done(errResult, response || {});
|
186 | }
|
187 |
|
188 | done(null, response);
|
189 | });
|
190 | }
|
191 | }
|
192 |
|
193 | module.exports = Request;
|