1 | 1 | (function() { |
2 | 1 | var Merchant, Promise, debug, errors, request, url, util; |
3 | | |
4 | 1 | util = require('util'); |
5 | | |
6 | 1 | url = require('url'); |
7 | | |
8 | 1 | Promise = require('bluebird'); |
9 | | |
10 | 1 | request = Promise.promisifyAll(require('request')); |
11 | | |
12 | 1 | errors = require('./errors'); |
13 | | |
14 | 1 | debug = (require('./debug'))('merchant'); |
15 | | |
16 | 1 | module.exports = Merchant = (function() { |
17 | 1 | function Merchant(options) { |
18 | 45 | var _ref; |
19 | 45 | _ref = options || {}, this.key = _ref.key, this.sandbox = _ref.sandbox, this.timeout = _ref.timeout; |
20 | 45 | if (this.sandbox == null) { |
21 | 2 | this.sandbox = false; |
22 | | } |
23 | 45 | if (this.timeout == null) { |
24 | 44 | this.timeout = 20 * 1000; |
25 | | } |
26 | 45 | this.baseUrl = this.sandbox ? 'https://sandbox.merchant.wish.com/api/v1' : 'https://merchant.wish.com/api/v1'; |
27 | | } |
28 | | |
29 | 1 | Merchant.prototype.format = function(json) { |
30 | 124 | var key, value, _ref; |
31 | 124 | if (typeof json.success === 'string') { |
32 | 2 | json.success = json.success === 'True'; |
33 | 2 | return json; |
34 | | } |
35 | 122 | if (util.isArray(json)) { |
36 | 33 | return json.map((function(_this) { |
37 | 33 | return function(item) { |
38 | 62 | return _this.format(item); |
39 | | }; |
40 | | })(this)); |
41 | | } |
42 | 89 | if (json.Product) { |
43 | 13 | json = json.Product; |
44 | 13 | if (json.is_promoted) { |
45 | 9 | json.is_promoted = json.is_promoted === 'True'; |
46 | | } |
47 | 13 | ['number_saves', 'number_sold'].map(function(key) { |
48 | 26 | if (json[key]) { |
49 | 16 | return json[key] = Number(json[key]); |
50 | | } |
51 | | }); |
52 | | } |
53 | 89 | if (json.Variant) { |
54 | 30 | json = json.Variant; |
55 | 30 | if (json.enabled) { |
56 | 27 | json.enabled = json.enabled === 'True'; |
57 | | } |
58 | 30 | ['msrp', 'inventory', 'price', 'shipping'].map(function(key) { |
59 | 120 | if (json[key]) { |
60 | 104 | return json[key] = Number(json[key]); |
61 | | } |
62 | | }); |
63 | | } |
64 | 89 | if (json.Order) { |
65 | 13 | json = json.Order; |
66 | 13 | if (json.ShippingDetail) { |
67 | 10 | _ref = json.ShippingDetail; |
68 | 10 | for (key in _ref) { |
69 | 78 | value = _ref[key]; |
70 | 78 | json[key] = value; |
71 | | } |
72 | 10 | delete json.ShippingDetail; |
73 | | } |
74 | 13 | ['order_total', 'quantity', 'price', 'cost', 'shipping', 'shipping_cost', 'days_to_fulfill'].map(function(key) { |
75 | 91 | if (json[key]) { |
76 | 66 | return json[key] = Number(json[key]); |
77 | | } |
78 | | }); |
79 | 13 | ['last_updated', 'order_time'].map(function(key) { |
80 | 26 | if (json[key]) { |
81 | 20 | return json[key] = new Date(json[key]); |
82 | | } |
83 | | }); |
84 | | } |
85 | 89 | json = json.Tag || json; |
86 | 89 | if (json.tags) { |
87 | 8 | json.tags = this.format(json.tags); |
88 | | } |
89 | 89 | if (json.auto_tags) { |
90 | 8 | json.auto_tags = this.format(json.auto_tags); |
91 | | } |
92 | 89 | if (json.variants) { |
93 | 8 | json.variants = this.format(json.variants); |
94 | | } |
95 | 89 | return json; |
96 | | }; |
97 | | |
98 | 1 | Merchant.prototype.url = function(path, query) { |
99 | 42 | var key, uri, value; |
100 | 42 | uri = url.parse(this.baseUrl); |
101 | 42 | uri.pathname += path; |
102 | 42 | uri.path = ''; |
103 | 42 | if (uri.query == null) { |
104 | 42 | uri.query = {}; |
105 | | } |
106 | 42 | for (key in query) { |
107 | 50 | value = query[key]; |
108 | 50 | uri.query[key] = value; |
109 | | } |
110 | 42 | return url.format(uri); |
111 | | }; |
112 | | |
113 | 1 | Merchant.prototype.handle = function(promise) { |
114 | 40 | return promise.then(function(_arg) { |
115 | 39 | var body, code, res; |
116 | 39 | res = _arg[0], body = _arg[1]; |
117 | 39 | debug(body); |
118 | 39 | code = body.code; |
119 | 39 | if (code === 0) { |
120 | 26 | return body; |
121 | 13 | } else if (code) { |
122 | 12 | throw errors.wish(body); |
123 | | } else { |
124 | 1 | throw new errors.ServerError({ |
125 | | response: body |
126 | | }); |
127 | | } |
128 | | }); |
129 | | }; |
130 | | |
131 | 1 | Merchant.prototype.get = function(path, query) { |
132 | 22 | var uri; |
133 | 22 | if (query == null) { |
134 | 6 | query = {}; |
135 | | } |
136 | 22 | if (!this.key) { |
137 | 1 | return Promise.reject(new errors.ParamMissingError); |
138 | | } |
139 | 21 | query.key = this.key; |
140 | 21 | uri = this.url(path, query); |
141 | 21 | debug('GET', uri); |
142 | 21 | return this.handle(request.getAsync(uri, { |
143 | | json: true, |
144 | | timeout: this.timeout |
145 | | })); |
146 | | }; |
147 | | |
148 | 1 | Merchant.prototype.post = function(path, form) { |
149 | 20 | var uri; |
150 | 20 | if (form == null) { |
151 | 2 | form = {}; |
152 | | } |
153 | 20 | if (!this.key) { |
154 | 1 | return Promise.reject(new errors.ParamMissingError); |
155 | | } |
156 | 19 | uri = this.url(path); |
157 | 19 | form.key = this.key; |
158 | 19 | debug('POST', uri, form); |
159 | 19 | return this.handle(request.postAsync(uri, { |
160 | | json: true, |
161 | | timeout: this.timeout, |
162 | | form: form |
163 | | })); |
164 | | }; |
165 | | |
166 | 1 | Merchant.prototype.authTest = function(callback) { |
167 | 6 | return this.authTestJSON().then((function(_this) { |
168 | 6 | return function(json) { |
169 | 2 | return _this.format(json.data); |
170 | | }; |
171 | | })(this)).nodeify(callback); |
172 | | }; |
173 | | |
174 | 1 | Merchant.prototype.authTestJSON = function(callback) { |
175 | 6 | return this.get('/auth_test').nodeify(callback); |
176 | | }; |
177 | | |
178 | 1 | Merchant.prototype.product = function(id, callback) { |
179 | 3 | return this.productJSON(id).then((function(_this) { |
180 | 3 | return function(json) { |
181 | 2 | return _this.format(json.data); |
182 | | }; |
183 | | })(this)).nodeify(callback); |
184 | | }; |
185 | | |
186 | 1 | Merchant.prototype.productJSON = function(id, callback) { |
187 | 3 | if (id.id) { |
188 | 1 | id = id.id; |
189 | | } |
190 | 3 | return this.get('/product', { |
191 | | id: id |
192 | | }).nodeify(callback); |
193 | | }; |
194 | | |
195 | 1 | Merchant.prototype.products = function(start, limit, callback) { |
196 | 2 | return this.productsJSON(start, limit).then((function(_this) { |
197 | 2 | return function(json) { |
198 | 2 | return _this.format(json.data); |
199 | | }; |
200 | | })(this)).nodeify(callback); |
201 | | }; |
202 | | |
203 | 1 | Merchant.prototype.productsJSON = function(start, limit, callback) { |
204 | 2 | if (start == null) { |
205 | 1 | start = 0; |
206 | | } |
207 | 2 | if (limit == null) { |
208 | 1 | limit = 50; |
209 | | } |
210 | 2 | return this.get('/product/multi-get', { |
211 | | start: start, |
212 | | limit: limit |
213 | | }).nodeify(callback); |
214 | | }; |
215 | | |
216 | 1 | Merchant.prototype.createProduct = function(product, callback) { |
217 | 3 | return this.createProductJSON(product).then((function(_this) { |
218 | 3 | return function(json) { |
219 | 1 | return _this.format(json.data); |
220 | | }; |
221 | | })(this)).nodeify(callback); |
222 | | }; |
223 | | |
224 | 1 | Merchant.prototype.createProductJSON = function(product, callback) { |
225 | 3 | return this.post('/product/add', product).nodeify(callback); |
226 | | }; |
227 | | |
228 | 1 | Merchant.prototype.updateProduct = function(product, callback) { |
229 | 5 | return this.updateProductJSON(product).then((function(_this) { |
230 | 5 | return function(json) { |
231 | 1 | return json.data; |
232 | | }; |
233 | | })(this)).nodeify(callback); |
234 | | }; |
235 | | |
236 | 1 | Merchant.prototype.updateProductJSON = function(product, callback) { |
237 | 5 | return this.post('/product/update', product).nodeify(callback); |
238 | | }; |
239 | | |
240 | 1 | Merchant.prototype.enableProduct = function(args, callback) { |
241 | 1 | return this.enableProductJSON(args).then((function(_this) { |
242 | 1 | return function(json) { |
243 | 1 | return json.data; |
244 | | }; |
245 | | })(this)).nodeify(callback); |
246 | | }; |
247 | | |
248 | 1 | Merchant.prototype.enableProductJSON = function(args, callback) { |
249 | 1 | return this.post('/product/enable', args).nodeify(callback); |
250 | | }; |
251 | | |
252 | 1 | Merchant.prototype.disableProduct = function(args, callback) { |
253 | 1 | return this.disableProductJSON(args).then((function(_this) { |
254 | 1 | return function(json) { |
255 | 1 | return json.data; |
256 | | }; |
257 | | })(this)).nodeify(callback); |
258 | | }; |
259 | | |
260 | 1 | Merchant.prototype.disableProductJSON = function(args, callback) { |
261 | 1 | return this.post('/product/disable', args).nodeify(callback); |
262 | | }; |
263 | | |
264 | 1 | Merchant.prototype.variant = function(sku, callback) { |
265 | 2 | return this.variantJSON(sku).then((function(_this) { |
266 | 2 | return function(json) { |
267 | 1 | return _this.format(json.data); |
268 | | }; |
269 | | })(this)).nodeify(callback); |
270 | | }; |
271 | | |
272 | 1 | Merchant.prototype.variantJSON = function(sku, callback) { |
273 | 2 | return this.get('/variant', { |
274 | | sku: sku |
275 | | }).nodeify(callback); |
276 | | }; |
277 | | |
278 | 1 | Merchant.prototype.variants = function(start, limit, callback) { |
279 | 2 | return this.variantsJSON(start, limit).then((function(_this) { |
280 | 2 | return function(json) { |
281 | 2 | return _this.format(json.data); |
282 | | }; |
283 | | })(this)).nodeify(callback); |
284 | | }; |
285 | | |
286 | 1 | Merchant.prototype.variantsJSON = function(start, limit, callback) { |
287 | 2 | if (start == null) { |
288 | 1 | start = 0; |
289 | | } |
290 | 2 | if (limit == null) { |
291 | 1 | limit = 50; |
292 | | } |
293 | 2 | return this.get('/variant/multi-get', { |
294 | | start: start, |
295 | | limit: limit |
296 | | }).nodeify(callback); |
297 | | }; |
298 | | |
299 | 1 | Merchant.prototype.createVariant = function(variant, callback) { |
300 | 2 | return this.createVariantJSON(variant).then((function(_this) { |
301 | 2 | return function(json) { |
302 | 1 | return _this.format(json.data); |
303 | | }; |
304 | | })(this)).nodeify(callback); |
305 | | }; |
306 | | |
307 | 1 | Merchant.prototype.createVariantJSON = function(variant, callback) { |
308 | 2 | return this.post('/variant/add', variant).nodeify(callback); |
309 | | }; |
310 | | |
311 | 1 | Merchant.prototype.updateVariant = function(variant, callback) { |
312 | 1 | return this.updateVariantJSON(variant).then((function(_this) { |
313 | 1 | return function(json) { |
314 | 1 | return json.data; |
315 | | }; |
316 | | })(this)).nodeify(callback); |
317 | | }; |
318 | | |
319 | 1 | Merchant.prototype.updateVariantJSON = function(variant, callback) { |
320 | 1 | return this.post('/variant/update', variant).nodeify(callback); |
321 | | }; |
322 | | |
323 | 1 | Merchant.prototype.enableVariant = function(sku, callback) { |
324 | 1 | return this.enableVariantJSON(sku).then((function(_this) { |
325 | 1 | return function(json) { |
326 | 1 | return json.data; |
327 | | }; |
328 | | })(this)).nodeify(callback); |
329 | | }; |
330 | | |
331 | 1 | Merchant.prototype.enableVariantJSON = function(sku, callback) { |
332 | 1 | return this.post('/variant/enable', { |
333 | | sku: sku |
334 | | }).nodeify(callback); |
335 | | }; |
336 | | |
337 | 1 | Merchant.prototype.disableVariant = function(sku, callback) { |
338 | 1 | return this.disableVariantJSON(sku).then((function(_this) { |
339 | 1 | return function(json) { |
340 | 1 | return json.data; |
341 | | }; |
342 | | })(this)).nodeify(callback); |
343 | | }; |
344 | | |
345 | 1 | Merchant.prototype.disableVariantJSON = function(sku, callback) { |
346 | 1 | return this.post('/variant/disable', { |
347 | | sku: sku |
348 | | }).nodeify(callback); |
349 | | }; |
350 | | |
351 | 1 | Merchant.prototype.order = function(id, callback) { |
352 | 3 | return this.orderJSON(id).then((function(_this) { |
353 | 3 | return function(json) { |
354 | 1 | return _this.format(json.data); |
355 | | }; |
356 | | })(this)).nodeify(callback); |
357 | | }; |
358 | | |
359 | 1 | Merchant.prototype.orderJSON = function(id, callback) { |
360 | 3 | return this.get('/order', { |
361 | | id: id |
362 | | }).nodeify(callback); |
363 | | }; |
364 | | |
365 | 1 | Merchant.prototype.orders = function(start, limit, since, callback) { |
366 | 2 | return this.ordersJSON(start, limit, since).then((function(_this) { |
367 | 2 | return function(json) { |
368 | 2 | return _this.format(json.data); |
369 | | }; |
370 | | })(this)).nodeify(callback); |
371 | | }; |
372 | | |
373 | 1 | Merchant.prototype.ordersJSON = function(start, limit, since, callback) { |
374 | 2 | if (start == null) { |
375 | 1 | start = 0; |
376 | | } |
377 | 2 | if (limit == null) { |
378 | 1 | limit = 50; |
379 | | } |
380 | 2 | if (since == null) { |
381 | 2 | since = ''; |
382 | | } |
383 | 2 | return this.get('/order/multi-get', { |
384 | | start: start, |
385 | | limit: limit, |
386 | | since: since |
387 | | }).nodeify(callback); |
388 | | }; |
389 | | |
390 | 1 | Merchant.prototype.unfullfiledOrders = function(start, limit, since, callback) { |
391 | 2 | return this.unfullfiledOrdersJSON(start, limit, since).then((function(_this) { |
392 | 2 | return function(json) { |
393 | 2 | return _this.format(json.data); |
394 | | }; |
395 | | })(this)).nodeify(callback); |
396 | | }; |
397 | | |
398 | 1 | Merchant.prototype.unfullfiledOrdersJSON = function(start, limit, since, callback) { |
399 | 2 | if (start == null) { |
400 | 1 | start = 0; |
401 | | } |
402 | 2 | if (limit == null) { |
403 | 1 | limit = 50; |
404 | | } |
405 | 2 | if (since == null) { |
406 | 2 | since = ''; |
407 | | } |
408 | 2 | return this.get('/order/get-fulfill', { |
409 | | start: start, |
410 | | limit: limit, |
411 | | since: since |
412 | | }).nodeify(callback); |
413 | | }; |
414 | | |
415 | 1 | Merchant.prototype.fulfillOrder = function(order, callback) { |
416 | 1 | return this.fulfillOrderJSON(order).then((function(_this) { |
417 | 1 | return function(json) { |
418 | 1 | return _this.format(json.data); |
419 | | }; |
420 | | })(this)).nodeify(callback); |
421 | | }; |
422 | | |
423 | 1 | Merchant.prototype.fulfillOrderJSON = function(order, callback) { |
424 | 1 | return this.post('/order/fulfill-one', order).nodeify(callback); |
425 | | }; |
426 | | |
427 | 1 | Merchant.prototype.refundOrder = function(args, callback) { |
428 | 2 | return this.refundOrderJSON(args).then((function(_this) { |
429 | 2 | return function(json) { |
430 | 2 | return _this.format(json.data); |
431 | | }; |
432 | | })(this)).nodeify(callback); |
433 | | }; |
434 | | |
435 | 1 | Merchant.prototype.cancelOrder = function(args, callback) { |
436 | 1 | return this.refundOrder(args, callback); |
437 | | }; |
438 | | |
439 | 1 | Merchant.prototype.refundOrderJSON = function(args, callback) { |
440 | 2 | return this.post('/order/refund', args).nodeify(callback); |
441 | | }; |
442 | | |
443 | 1 | Merchant.prototype.modifyOrder = function(args, callback) { |
444 | 1 | return this.modifyOrderJSON(args).then((function(_this) { |
445 | 1 | return function(json) { |
446 | 1 | return _this.format(json.data); |
447 | | }; |
448 | | })(this)).nodeify(callback); |
449 | | }; |
450 | | |
451 | 1 | Merchant.prototype.modifyOrderJSON = function(args, callback) { |
452 | 1 | return this.post('/order/modify-tracking', args).nodeify(callback); |
453 | | }; |
454 | | |
455 | 1 | return Merchant; |
456 | | |
457 | | })(); |
458 | | |
459 | | }).call(this); |
460 | | |