Skip to content
Browse files

added some checks

  • Loading branch information...
1 parent 583946c commit d25927642c42504d5201afb86bcc71ad237877e8 @pdfcrowd committed
Showing with 21 additions and 14 deletions.
  1. +21 −14 lib/pdfcrowd.js
View
35 lib/pdfcrowd.js
@@ -5,16 +5,15 @@ var fs = require('fs');
var Pdfcrowd = function(username, apikey) {
- // TBD: check username & apikey not null
+ if (!username)
+ throw new Error('Missing username.');
+
+ if (!apikey)
+ throw new Error('Missing apikey.');
+
this.username = username;
this.apikey = apikey;
- this.httpOptions = {
- host: 'pdfcrowd.com',
- port: 80,
- method: 'POST',
- };
-
this.callbacks = {
error: function(status, err) {
console.warn("WARNING: [Pdfcrowd] Unhandled error: %d - %s", status, err);
@@ -29,18 +28,24 @@ var Pdfcrowd = function(username, apikey) {
Pdfcrowd.prototype.on = function(event, callback) {
- // TBD: verify event
+
+ if (event !== 'error' && event !== 'pdf')
+ throw new Error("Supported events: pdf, error")
+
this.callbacks[event] = callback;
};
+
Pdfcrowd.prototype.convertHtml = function(html, options) {
convertInternal(this, html, options, '/api/pdf/convert/html/');
}
+
Pdfcrowd.prototype.convertURI = function(uri, options) {
convertInternal(this, uri, options, '/api/pdf/convert/uri/');
}
+
Pdfcrowd.prototype.convertFile = function(fname, options) {
var that = this;
fs.readFile(fname, function(err, data) {
@@ -50,8 +55,6 @@ Pdfcrowd.prototype.convertFile = function(fname, options) {
}
-
-
module.exports = Pdfcrowd;
@@ -70,10 +73,14 @@ var convertInternal = function(that, src, options, endpoint) {
//console.log("%d - %s", postData.length, postData);
// http options
- var httpOptions = clone_object(that.httpOptions);
- httpOptions['path'] = endpoint;
- httpOptions['headers'] = { 'content-length': postData.length,
- 'content-type': 'application/x-www-form-urlencoded' };
+ httpOptions = {
+ host: 'pdfcrowd.com',
+ port: 80,
+ method: 'POST',
+ path: endpoint,
+ headers: { 'content-length': postData.length,
+ 'content-type': 'application/x-www-form-urlencoded' },
+ };
var req = http.request(httpOptions, function(res) {
if (res.statusCode < 300) {

0 comments on commit d259276

Please sign in to comment.
Something went wrong with that request. Please try again.