-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (35 loc) · 1.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var util = require('util');
var Easypost = require('@easypost/api');
var buyShippingLabel = require('./buy_shipping_label.js');
function labeler(settings, reallyPayMoney) {
this.settings = settings
if(reallyPayMoney) {
if(!settings.easypost.apiKey) {
throw new Error("You must set settings.easypost.apiKey");
}
this.easypost = new Easypost(settings.easypost.apiKey);
} else {
if(!settings.easypost.apiKeyTesting) {
throw new Error("You must set settings.easypost.apiKeyTesting");
}
this.easypost = new Easypost(settings.easypost.apiKeyTesting);
}
// verify address using easypost API
this.verifyAddress = function(address, callback) {
if(!this.easypost) return callback(new Error("easypost API not initialized"));
var address = new this.easypost.Address(address);
address.save().then(function(addr) {
callback(null, addr);
}).catch(callback);
}
this.buyLabel = function(address, pkg, opts, cb) {
if(typeof opts === 'function') {
cb = opts;
opts = {};
}
opts.outDir = opts.outDir || this.settings.outDir;
opts.customsInfo = opts.customsInfo || this.settings.customsInfo;
buyShippingLabel(this.easypost, address, pkg, opts.fromAddress || this.settings.fromAddress, opts.outDir, opts, cb);
};
}
module.exports = labeler;