Coverage

100%
270
270
0

./src/debug.coffee

100%
9
9
0
LineHitsSource
12(function() {
22 var debug;
3
42 try {
52 debug = require('debug');
6 } catch (_error) {
7
8 }
9
102 module.exports = function(name) {
114 if (debug == null) {
121 debug = function() {
131 return function() {};
14 };
15 }
164 return debug("wishapi:" + name);
17 };
18
19}).call(this);
20

./src/errors.coffee

100%
18
18
0
LineHitsSource
11(function() {
21 var errors;
3
41 module.exports = errors = require('errors');
5
61 errors.create({
7 name: 'ParamInvalidError',
8 code: 1000
9 });
10
111 errors.create({
12 name: 'ParamMissingError',
13 defaultMessage: 'Required Param Missing',
14 code: 1001
15 });
16
171 errors.create({
18 name: 'NotFoundError',
19 code: 1004
20 });
21
221 errors.create({
23 name: 'AuthError',
24 defaultMessage: 'Unauthorized Request',
25 code: 4000
26 });
27
281 errors.create({
29 name: 'ServerError',
30 defaultMessage: 'Server Return Unknown Contest.',
31 defaultExplanation: 'Maybe server is maintaining.',
32 code: 911
33 });
34
351 errors.wish = function(body) {
3613 var FoundError, code;
3713 code = body.code;
3813 FoundError = errors.find(code);
3913 delete body.code;
4013 return new FoundError(body);
41 };
42
431 errors.http = function(code) {
441 var HttpError;
451 HttpError = errors.find(code);
461 return new HttpError;
47 };
48
49}).call(this);
50

./src/merchant.coffee

100%
243
243
0
LineHitsSource
11(function() {
21 var Merchant, Promise, debug, errors, request, url, util;
3
41 util = require('util');
5
61 url = require('url');
7
81 Promise = require('bluebird');
9
101 request = Promise.promisifyAll(require('request'));
11
121 errors = require('./errors');
13
141 debug = (require('./debug'))('merchant');
15
161 module.exports = Merchant = (function() {
171 function Merchant(options) {
1845 var _ref;
1945 _ref = options || {}, this.key = _ref.key, this.sandbox = _ref.sandbox, this.timeout = _ref.timeout;
2045 if (this.sandbox == null) {
212 this.sandbox = false;
22 }
2345 if (this.timeout == null) {
2444 this.timeout = 20 * 1000;
25 }
2645 this.baseUrl = this.sandbox ? 'https://sandbox.merchant.wish.com/api/v1' : 'https://merchant.wish.com/api/v1';
27 }
28
291 Merchant.prototype.format = function(json) {
30124 var key, value, _ref;
31124 if (typeof json.success === 'string') {
322 json.success = json.success === 'True';
332 return json;
34 }
35122 if (util.isArray(json)) {
3633 return json.map((function(_this) {
3733 return function(item) {
3862 return _this.format(item);
39 };
40 })(this));
41 }
4289 if (json.Product) {
4313 json = json.Product;
4413 if (json.is_promoted) {
459 json.is_promoted = json.is_promoted === 'True';
46 }
4713 ['number_saves', 'number_sold'].map(function(key) {
4826 if (json[key]) {
4916 return json[key] = Number(json[key]);
50 }
51 });
52 }
5389 if (json.Variant) {
5430 json = json.Variant;
5530 if (json.enabled) {
5627 json.enabled = json.enabled === 'True';
57 }
5830 ['msrp', 'inventory', 'price', 'shipping'].map(function(key) {
59120 if (json[key]) {
60104 return json[key] = Number(json[key]);
61 }
62 });
63 }
6489 if (json.Order) {
6513 json = json.Order;
6613 if (json.ShippingDetail) {
6710 _ref = json.ShippingDetail;
6810 for (key in _ref) {
6978 value = _ref[key];
7078 json[key] = value;
71 }
7210 delete json.ShippingDetail;
73 }
7413 ['order_total', 'quantity', 'price', 'cost', 'shipping', 'shipping_cost', 'days_to_fulfill'].map(function(key) {
7591 if (json[key]) {
7666 return json[key] = Number(json[key]);
77 }
78 });
7913 ['last_updated', 'order_time'].map(function(key) {
8026 if (json[key]) {
8120 return json[key] = new Date(json[key]);
82 }
83 });
84 }
8589 json = json.Tag || json;
8689 if (json.tags) {
878 json.tags = this.format(json.tags);
88 }
8989 if (json.auto_tags) {
908 json.auto_tags = this.format(json.auto_tags);
91 }
9289 if (json.variants) {
938 json.variants = this.format(json.variants);
94 }
9589 return json;
96 };
97
981 Merchant.prototype.url = function(path, query) {
9942 var key, uri, value;
10042 uri = url.parse(this.baseUrl);
10142 uri.pathname += path;
10242 uri.path = '';
10342 if (uri.query == null) {
10442 uri.query = {};
105 }
10642 for (key in query) {
10750 value = query[key];
10850 uri.query[key] = value;
109 }
11042 return url.format(uri);
111 };
112
1131 Merchant.prototype.handle = function(promise) {
11440 return promise.then(function(_arg) {
11539 var body, code, res;
11639 res = _arg[0], body = _arg[1];
11739 debug(body);
11839 code = body.code;
11939 if (code === 0) {
12026 return body;
12113 } else if (code) {
12212 throw errors.wish(body);
123 } else {
1241 throw new errors.ServerError({
125 response: body
126 });
127 }
128 });
129 };
130
1311 Merchant.prototype.get = function(path, query) {
13222 var uri;
13322 if (query == null) {
1346 query = {};
135 }
13622 if (!this.key) {
1371 return Promise.reject(new errors.ParamMissingError);
138 }
13921 query.key = this.key;
14021 uri = this.url(path, query);
14121 debug('GET', uri);
14221 return this.handle(request.getAsync(uri, {
143 json: true,
144 timeout: this.timeout
145 }));
146 };
147
1481 Merchant.prototype.post = function(path, form) {
14920 var uri;
15020 if (form == null) {
1512 form = {};
152 }
15320 if (!this.key) {
1541 return Promise.reject(new errors.ParamMissingError);
155 }
15619 uri = this.url(path);
15719 form.key = this.key;
15819 debug('POST', uri, form);
15919 return this.handle(request.postAsync(uri, {
160 json: true,
161 timeout: this.timeout,
162 form: form
163 }));
164 };
165
1661 Merchant.prototype.authTest = function(callback) {
1676 return this.authTestJSON().then((function(_this) {
1686 return function(json) {
1692 return _this.format(json.data);
170 };
171 })(this)).nodeify(callback);
172 };
173
1741 Merchant.prototype.authTestJSON = function(callback) {
1756 return this.get('/auth_test').nodeify(callback);
176 };
177
1781 Merchant.prototype.product = function(id, callback) {
1793 return this.productJSON(id).then((function(_this) {
1803 return function(json) {
1812 return _this.format(json.data);
182 };
183 })(this)).nodeify(callback);
184 };
185
1861 Merchant.prototype.productJSON = function(id, callback) {
1873 if (id.id) {
1881 id = id.id;
189 }
1903 return this.get('/product', {
191 id: id
192 }).nodeify(callback);
193 };
194
1951 Merchant.prototype.products = function(start, limit, callback) {
1962 return this.productsJSON(start, limit).then((function(_this) {
1972 return function(json) {
1982 return _this.format(json.data);
199 };
200 })(this)).nodeify(callback);
201 };
202
2031 Merchant.prototype.productsJSON = function(start, limit, callback) {
2042 if (start == null) {
2051 start = 0;
206 }
2072 if (limit == null) {
2081 limit = 50;
209 }
2102 return this.get('/product/multi-get', {
211 start: start,
212 limit: limit
213 }).nodeify(callback);
214 };
215
2161 Merchant.prototype.createProduct = function(product, callback) {
2173 return this.createProductJSON(product).then((function(_this) {
2183 return function(json) {
2191 return _this.format(json.data);
220 };
221 })(this)).nodeify(callback);
222 };
223
2241 Merchant.prototype.createProductJSON = function(product, callback) {
2253 return this.post('/product/add', product).nodeify(callback);
226 };
227
2281 Merchant.prototype.updateProduct = function(product, callback) {
2295 return this.updateProductJSON(product).then((function(_this) {
2305 return function(json) {
2311 return json.data;
232 };
233 })(this)).nodeify(callback);
234 };
235
2361 Merchant.prototype.updateProductJSON = function(product, callback) {
2375 return this.post('/product/update', product).nodeify(callback);
238 };
239
2401 Merchant.prototype.enableProduct = function(args, callback) {
2411 return this.enableProductJSON(args).then((function(_this) {
2421 return function(json) {
2431 return json.data;
244 };
245 })(this)).nodeify(callback);
246 };
247
2481 Merchant.prototype.enableProductJSON = function(args, callback) {
2491 return this.post('/product/enable', args).nodeify(callback);
250 };
251
2521 Merchant.prototype.disableProduct = function(args, callback) {
2531 return this.disableProductJSON(args).then((function(_this) {
2541 return function(json) {
2551 return json.data;
256 };
257 })(this)).nodeify(callback);
258 };
259
2601 Merchant.prototype.disableProductJSON = function(args, callback) {
2611 return this.post('/product/disable', args).nodeify(callback);
262 };
263
2641 Merchant.prototype.variant = function(sku, callback) {
2652 return this.variantJSON(sku).then((function(_this) {
2662 return function(json) {
2671 return _this.format(json.data);
268 };
269 })(this)).nodeify(callback);
270 };
271
2721 Merchant.prototype.variantJSON = function(sku, callback) {
2732 return this.get('/variant', {
274 sku: sku
275 }).nodeify(callback);
276 };
277
2781 Merchant.prototype.variants = function(start, limit, callback) {
2792 return this.variantsJSON(start, limit).then((function(_this) {
2802 return function(json) {
2812 return _this.format(json.data);
282 };
283 })(this)).nodeify(callback);
284 };
285
2861 Merchant.prototype.variantsJSON = function(start, limit, callback) {
2872 if (start == null) {
2881 start = 0;
289 }
2902 if (limit == null) {
2911 limit = 50;
292 }
2932 return this.get('/variant/multi-get', {
294 start: start,
295 limit: limit
296 }).nodeify(callback);
297 };
298
2991 Merchant.prototype.createVariant = function(variant, callback) {
3002 return this.createVariantJSON(variant).then((function(_this) {
3012 return function(json) {
3021 return _this.format(json.data);
303 };
304 })(this)).nodeify(callback);
305 };
306
3071 Merchant.prototype.createVariantJSON = function(variant, callback) {
3082 return this.post('/variant/add', variant).nodeify(callback);
309 };
310
3111 Merchant.prototype.updateVariant = function(variant, callback) {
3121 return this.updateVariantJSON(variant).then((function(_this) {
3131 return function(json) {
3141 return json.data;
315 };
316 })(this)).nodeify(callback);
317 };
318
3191 Merchant.prototype.updateVariantJSON = function(variant, callback) {
3201 return this.post('/variant/update', variant).nodeify(callback);
321 };
322
3231 Merchant.prototype.enableVariant = function(sku, callback) {
3241 return this.enableVariantJSON(sku).then((function(_this) {
3251 return function(json) {
3261 return json.data;
327 };
328 })(this)).nodeify(callback);
329 };
330
3311 Merchant.prototype.enableVariantJSON = function(sku, callback) {
3321 return this.post('/variant/enable', {
333 sku: sku
334 }).nodeify(callback);
335 };
336
3371 Merchant.prototype.disableVariant = function(sku, callback) {
3381 return this.disableVariantJSON(sku).then((function(_this) {
3391 return function(json) {
3401 return json.data;
341 };
342 })(this)).nodeify(callback);
343 };
344
3451 Merchant.prototype.disableVariantJSON = function(sku, callback) {
3461 return this.post('/variant/disable', {
347 sku: sku
348 }).nodeify(callback);
349 };
350
3511 Merchant.prototype.order = function(id, callback) {
3523 return this.orderJSON(id).then((function(_this) {
3533 return function(json) {
3541 return _this.format(json.data);
355 };
356 })(this)).nodeify(callback);
357 };
358
3591 Merchant.prototype.orderJSON = function(id, callback) {
3603 return this.get('/order', {
361 id: id
362 }).nodeify(callback);
363 };
364
3651 Merchant.prototype.orders = function(start, limit, since, callback) {
3662 return this.ordersJSON(start, limit, since).then((function(_this) {
3672 return function(json) {
3682 return _this.format(json.data);
369 };
370 })(this)).nodeify(callback);
371 };
372
3731 Merchant.prototype.ordersJSON = function(start, limit, since, callback) {
3742 if (start == null) {
3751 start = 0;
376 }
3772 if (limit == null) {
3781 limit = 50;
379 }
3802 if (since == null) {
3812 since = '';
382 }
3832 return this.get('/order/multi-get', {
384 start: start,
385 limit: limit,
386 since: since
387 }).nodeify(callback);
388 };
389
3901 Merchant.prototype.unfullfiledOrders = function(start, limit, since, callback) {
3912 return this.unfullfiledOrdersJSON(start, limit, since).then((function(_this) {
3922 return function(json) {
3932 return _this.format(json.data);
394 };
395 })(this)).nodeify(callback);
396 };
397
3981 Merchant.prototype.unfullfiledOrdersJSON = function(start, limit, since, callback) {
3992 if (start == null) {
4001 start = 0;
401 }
4022 if (limit == null) {
4031 limit = 50;
404 }
4052 if (since == null) {
4062 since = '';
407 }
4082 return this.get('/order/get-fulfill', {
409 start: start,
410 limit: limit,
411 since: since
412 }).nodeify(callback);
413 };
414
4151 Merchant.prototype.fulfillOrder = function(order, callback) {
4161 return this.fulfillOrderJSON(order).then((function(_this) {
4171 return function(json) {
4181 return _this.format(json.data);
419 };
420 })(this)).nodeify(callback);
421 };
422
4231 Merchant.prototype.fulfillOrderJSON = function(order, callback) {
4241 return this.post('/order/fulfill-one', order).nodeify(callback);
425 };
426
4271 Merchant.prototype.refundOrder = function(args, callback) {
4282 return this.refundOrderJSON(args).then((function(_this) {
4292 return function(json) {
4302 return _this.format(json.data);
431 };
432 })(this)).nodeify(callback);
433 };
434
4351 Merchant.prototype.cancelOrder = function(args, callback) {
4361 return this.refundOrder(args, callback);
437 };
438
4391 Merchant.prototype.refundOrderJSON = function(args, callback) {
4402 return this.post('/order/refund', args).nodeify(callback);
441 };
442
4431 Merchant.prototype.modifyOrder = function(args, callback) {
4441 return this.modifyOrderJSON(args).then((function(_this) {
4451 return function(json) {
4461 return _this.format(json.data);
447 };
448 })(this)).nodeify(callback);
449 };
450
4511 Merchant.prototype.modifyOrderJSON = function(args, callback) {
4521 return this.post('/order/modify-tracking', args).nodeify(callback);
453 };
454
4551 return Merchant;
456
457 })();
458
459}).call(this);
460