Skip to content

Commit

Permalink
Merge pull request goshippo#39 from goshippo/update-promises
Browse files Browse the repository at this point in the history
Update promises, fixes goshippo#38
  • Loading branch information
mootrichard authored Sep 30, 2017
2 parents f000c5c + e2d6291 commit 933b7fa
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 67 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
language: node_js
node_js:
- "0.8"
- "0.12.18"

sudo: false

cache:
directories:
- node_modules
- node_modules
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@ Shippo is a shipping API that connects you with [multiple shipping carriers](htt

Print a shipping label in 10 mins using our default USPS and DHL Express accounts. No need to register for a carrier account to get started.

You will need to [register for a Shippo account](https://goshippo.com/) to use the Shippo API. It's free to sign up, free to use the API. Only pay to print a live label, test labels are free.
You will need to [register for a Shippo account](https://goshippo.com/) to use the Shippo API. It's free to sign up, free to use the API. Only pay to print a live label, test labels are free.

## Installation:
You can install this package by running the follwing command:
```shell
npm install shippo
```
This means, you don't actually have to download this repository. If you wish to make modifications to the wrapper, you can clone this repository into your project.
This means, you don't actually have to download this repository. If you wish to make modifications to the wrapper, you can clone this repository into your project.

### Requirements:
The shippo Node.js has the following dependencies:
```js
npm install when
```
The shippo Node.js has no additional dependencies.

## Usage:

Expand All @@ -32,7 +29,7 @@ The snippet below demonstrates how to create an address object (a Shippo `Resour

```js
var shippo = require('shippo')('<YOUR_PRIVATE_KEY>');

shippo.address.create({
'name' : 'Mr Hippo',
'company' : 'SF Zoo',
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.0
1.3.1
8 changes: 4 additions & 4 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ shippo.customsdeclaration.create({
rate = rates.results[0];
// Purchase the desired rate
return shippo.transaction.create({"rate": rate.object_id, "async": false})
}, function(err) {
}).catch(function(err) {
// Deal with an error
console.log("There was an error retrieving rates : %s", err);
})
Expand All @@ -104,14 +104,14 @@ shippo.customsdeclaration.create({
console.log("Tracking Number: %s", transaction.tracking_number);
}else{
//Deal with an error with the transaction
console.log("Message: %s", transaction.messages);
console.log("Message: %s", JSON.stringify(transaction.messages, null, 2));
}

}, function(err) {
}).catch(function(err) {
// Deal with an error
console.log("There was an error creating transaction : %s", err);
});
},function(err) {
}).catch(function(err) {
// Deal with an error
console.log("There was an error creating shipment: %s", err);
});
77 changes: 40 additions & 37 deletions lib/Method.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var utils = require('./utils');
* Create an API method from the declared spec.
*
* @param [spec.method='GET'] Request Method (POST, GET, DELETE, PUT)
* @param [spec.path=''] Path to be appended to the API BASE_PATH, joined with
* @param [spec.path=''] Path to be appended to the API BASE_PATH, joined with
* the instance's path (e.g. "addresses" or "shipments")
* @param [spec.required=[]] Array of required arguments in the order that they
* must be passed by the consumer of the API. Subsequent optional arguments are
Expand Down Expand Up @@ -43,47 +43,50 @@ module.exports = function Method(spec) {
var data = utils.isObject(args[args.length - 1]) || utils.isArrayObject(args[args.length - 1]) ? args.pop() : {};
var urlData = this.createUrlData();

var deferred = this.createDeferred(callback);
return this.wrapTimeout(new Promise((function(resolve, reject){
for (var i = 0, l = urlParams.length; i < l; ++i) {
var arg = args[0];
if (urlParams[i] && !arg && !optional) {
var err = new Error('Shippo: I require argument "' + urlParams[i] + '", but I got: ' + arg);
reject(err);
return;
} //added handling for urlParams that are optional
if (optional && urlParams[i] && !arg) {
optParam = optParam - 1;
continue;
}

for (var i = 0, l = urlParams.length; i < l; ++i) {
var arg = args[0];
if (urlParams[i] && !arg && !optional) {
throw new Error('Shippo: I require argument "' + urlParams[i] + '", but I got: ' + arg);
} //added handling for urlParams that are optional
if (optional && urlParams[i] && !arg) {
optParam = optParam - 1;
continue;
}

urlData[urlParams[i]] = args.shift();
urlData[urlParams[i]] = args.shift();

if (data.hasOwnProperty(urlParams[i])) {
delete data[urlParams[i]];
if (data.hasOwnProperty(urlParams[i])) {
delete data[urlParams[i]];
}
}
}

// commandPath depending on the number of optional parameters
switch(optParam) {
case 1:
commandPath = optPath;
break;
case 2:
commandPath = optPath2;
break;
default:
break;
}
var requestPath = this.createFullPath(commandPath, urlData);

self._request(requestMethod, requestPath, data, auth, function(err, response) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(spec.transformResponseData ? spec.transformResponseData(response) : response);
// commandPath depending on the number of optional parameters
switch(optParam) {
case 1:
commandPath = optPath;
break;
case 2:
commandPath = optPath2;
break;
default:
break;
}
});

return deferred.promise;
var requestPath = this.createFullPath(commandPath, urlData);

self._request(requestMethod, requestPath, data, auth, function(err, response) {
if (err) {
reject(err);
} else {
resolve(
spec.transformResponseData ?
spec.transformResponseData(response) :
response
);
}
});
}).bind(this)), callback);
};
};
11 changes: 4 additions & 7 deletions lib/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var http = require('http');
var https = require('https');
var path = require('path');
var querystring = require('querystring');
var when = require('when');
var utils = require('./utils');
var Error = require('./Error');

Expand Down Expand Up @@ -58,13 +57,11 @@ Resource.prototype = {
return urlData;
},

createDeferred: function(callback) {
var deferred = when.defer();

wrapTimeout: function(promise, callback) {
if (callback) {
// Callback, if provided, is a simply translated to Promise'esque:
// (Ensure callback is called outside of promise stack)
deferred.promise.then(function(res) {
promise.then(function(res) {
setTimeout(function() {
callback(null, res)
}, 0);
Expand All @@ -75,7 +72,7 @@ Resource.prototype = {
});
}

return deferred;
return promise;
},

_timeoutHandler: function(timeout, req, callback) {
Expand Down Expand Up @@ -221,4 +218,4 @@ Resource.prototype = {

};

module.exports = Resource;
module.exports = Resource;
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@
},
"bugs:": "https://github.com/goshippo/shippo-node-client",
"engines": {
"node": ">= v0.8.0"
"node": ">= v0.12.18"
},
"main": "lib/shippo.js",
"devDependencies": {
"chai": "~1.10.0",
"chai-as-promised": "~4.1.1",
"mocha": "~2.1.0"
},
"dependencies": {
"when": "~2.4.0"
},
"dependencies": {},
"scripts": {
"test": "mocha"
}
Expand Down
5 changes: 0 additions & 5 deletions test/testUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

require('chai');

var when = require('when');

var utils = module.exports = {

getUserShippoKey: function() {
Expand Down Expand Up @@ -108,6 +106,3 @@ var utils = module.exports = {
}())

};



0 comments on commit 933b7fa

Please sign in to comment.