Skip to content

Commit

Permalink
Merge bf2a3bc into 985dae6
Browse files Browse the repository at this point in the history
  • Loading branch information
javamatte committed Jan 10, 2019
2 parents 985dae6 + bf2a3bc commit 742a636
Show file tree
Hide file tree
Showing 4 changed files with 375 additions and 44 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,16 @@ The Newgistics Shipping API is designed to be low latency and scalable. It offer
newgisticsClient.ping(function(err, pong) {
console.log(pong);
});
```

### newgisticsClient.voidTracking(trackingNumber, callback)

Void label previously created by tracking number.

**Example**

```javascript
newgisticsClient.voidTracking('myTrackingNumber', function(err) {
console.log(err);
});
```
93 changes: 70 additions & 23 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const cache = require('memory-cache');
const createError = require('http-errors');
const request = require('request');

function NewgisticsClient(args) {
Expand Down Expand Up @@ -41,12 +42,8 @@ function NewgisticsClient(args) {
return callback(err);
}

if (body && body.error) {
return callback(new Error(body.error.message));
}

if (res.statusCode !== 200) {
return callback(new Error(`${res.statusCode} ${res.request.method} ${res.request.href} ${res.body}`));
return callback(createError(res.statusCode, body && body.error && body.error.message));
}

callback();
Expand All @@ -69,20 +66,16 @@ function NewgisticsClient(args) {
url: `${opts.shippingapi_url}/v1/packages`
};

request(req, function(err, res, package) {
request(req, function(err, res, body) {
if (err) {
return callback(err);
}

if (package && package.error) {
return callback(new Error(package.error.message));
}

if (res.statusCode !== 200) {
return callback(new Error(`${res.statusCode} ${res.request.method} ${res.request.href} ${res.body}`));
return callback(createError(res.statusCode, body && body.error && body.error.message));
}

callback(null, package.data);
callback(null, body.data);
});
});
};
Expand All @@ -107,23 +100,19 @@ function NewgisticsClient(args) {
url: `${opts.authapi_url}/connect/token`
};

request(req, function(err, res, token) {
request(req, function(err, res, body) {
if (err) {
return callback(err);
}

if (token && token.error) {
return callback(new Error(token.error));
}

if (res.statusCode !== 200) {
return callback(new Error(`${res.statusCode} ${res.request.method} ${res.request.href} ${res.body}`));
return callback(createError(res.statusCode, body && body.error));
}

// Put the token in memory cache
cache.put('newgistics-client-token', token, token.expires_in / 2);
cache.put('newgistics-client-token', body, body.expires_in / 2);

callback(null, token);
callback(null, body);
});
};

Expand All @@ -134,16 +123,74 @@ function NewgisticsClient(args) {
url: `${opts.shippingapi_url}/ping`
};

request(req, function(err, res, pong) {
request(req, function(err, res, body) {
if (err) {
return callback(err);
}

if (res.statusCode !== 200) {
return callback(new Error(`${res.statusCode} ${res.request.method} ${res.request.href} ${res.body}`));
return callback(createError(res.statusCode, res.body));
}

callback(null, body);
});
};

this.voidPackage = function(packageId, callback) {
this.getToken(function(err, token) {
if (err) {
return callback(err);
}

callback(null, pong);
const req = {
auth: {
bearer: token.access_token
},
json: {},
method: 'POST',
url: `${opts.shippingapi_url}/v1/packages/${packageId}/void`
};

request(req, function(err, res, body) {
if (err) {
return callback(err);
}

if (res.statusCode !== 200) {
return callback(createError(res.statusCode, body && body.error && body.error.message));
}

callback();
});
});
};

this.voidTracking = function(trackingNumber, callback) {
this.getToken(function(err, token) {
if (err) {
return callback(err);
}

const req = {
auth: {
bearer: token.access_token
},
json: {},
method: 'POST',
url: `${opts.shippingapi_url}/v1/packages/trackingId/${trackingNumber}/void`
};

request(req, function(err, res, body) {
if (err) {
return callback(err);
}

if (res.statusCode !== 200) {
return callback(createError(res.statusCode, body && body.error && body.error.message));
}

callback();
});
});
};
}
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"dependencies": {
"http-errors": "~1.7.1",
"memory-cache": "~0.2.0",
"request": "~2.88.0"
},
Expand All @@ -20,12 +21,12 @@
"main": "client.js",
"name": "newgistics-client",
"repository": {
"type" : "git",
"url" : "https://github.com/mediocre/newgistics-client.git"
"type": "git",
"url": "https://github.com/mediocre/newgistics-client.git"
},
"scripts": {
"coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec test/* && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
"test": "mocha --exit --reporter spec test"
},
"version": "1.0.0"
}
}
Loading

0 comments on commit 742a636

Please sign in to comment.