Skip to content

Commit

Permalink
Merge pull request #61 from sendwithus/bugfix/Sendwithus-API-Client_N…
Browse files Browse the repository at this point in the history
…ode_issue_58

Return callback if there is a network error using the node client
  • Loading branch information
brandond-dyspatch authored Feb 16, 2022
2 parents 0a94f48 + 54b5f7f commit de0eee2
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 140 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ npm install sendwithus
All callbacks accept `err` and `response`:

```javascript
var callback = function(err, response) {
const callback = function(err, response) {
if (err) {
console.log(err.statusCode, response);
console.log({message: err.message, status: response.status, statusText: response.statusText});
} else {
console.log(response);
}
Expand Down
185 changes: 49 additions & 136 deletions lib/sendwithus.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,53 @@ Sendwithus.prototype._buildUrl = function (resource, identifier, action) {
};

Sendwithus.prototype._fetchBoilerplate = async function (url, args) {

const response = await fetch(url, args);

try {
const result = await response.json();
return { response, result };
} catch (error) {
}
catch (error) {
return { response, result: error };
}

};

Sendwithus.prototype._fetch = function () {
Sendwithus.prototype._fetch = function (callback) {
var that = this;

return {
postJson: (url, data, options) => {
return this._fetchBoilerplate(url, {
method: "post",
body: JSON.stringify(data),
...options,
});
})
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
})
.catch(({ response, result }) => { that._handleResponse.call(that, result, response, callback); });
},
get: (url, options) => {
return this._fetchBoilerplate(url, {
method: "get",
...options,
});
})
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
})
.catch(({ response, result }) => { that._handleResponse.call(that, result, response, callback); });
},
del: (url, options) => {
return this._fetchBoilerplate(url, {
method: "delete",
...options,
});
})
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
})
.catch(({ response, result }) => { that._handleResponse.call(that, result, response, callback); });
},
};
};
Expand All @@ -102,86 +119,61 @@ Sendwithus.prototype._handleResponse = function (result, response, callback) {
if (typeof callback == "function") {
callback(result, response);
}
} else if (response.status === 200) {
} else if (response.ok) { // the range 200-299 means the response was successful and should not return an error. https://developer.mozilla.org/en-US/docs/Web/API/Response/ok
this.emit("response", response.status, result, response);
this._debug("Response 200: " + JSON.stringify(result));
this._debug(`Response ${response.status}: ` + JSON.stringify(result));
if (typeof callback == "function") {
callback(null, result);
}
} else {
this.emit("response", response.status, result, response);
}
// handle anything which is an error outside of the instaceof Error type
else if (result && result.stack && result.message) {
this._debug("Response " + response.status + ": " + JSON.stringify(result));

var err = new Error("Request failed with " + response.status);
err.statusCode = response.status;

if (typeof callback == "function") {
callback(err, result);
}
}
else {
// handle anything else outside errors level responses which are not errors (100, and 300 response group).
this.emit("response", response.status, result, response);
this._debug(`Response ${response.status}: ` + JSON.stringify(result));
if (typeof callback == "function") {
callback(null, result);
}
}
};

////////////////////////////
// PUBLIC METHODS

Sendwithus.prototype.batch = function (data, callback) {
var url = this._buildUrl("batch");

var options = this._getOptions();

var that = this;
this.emit("request", "POST", url, options.headers, data);

this._fetch()
.postJson(url, data, options)
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
});
this._fetch(callback).postJson(url, data, options)
};

Sendwithus.prototype.send = function (data, callback) {
var url = this._buildUrl("send");

var options = this._getOptions();

var that = this;
this.emit("request", "POST", url, options.headers, data);

this._fetch()
.postJson(url, data, options)
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
});
this._fetch(callback).postJson(url, data, options)
};

Sendwithus.prototype.render = function (data, callback) {
var url = this._buildUrl("render");

var options = this._getOptions();

var that = this;
this.emit("request", "POST", url, options.headers, data);

this._fetch()
.postJson(url, data, options)
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
});
this._fetch(callback).postJson(url, data, options)
};

Sendwithus.prototype.templates = function (callback) {
var url = this._buildUrl("templates");

var options = this._getOptions();

var that = this;
this.emit("request", "GET", url, options.headers, {});

this._fetch()
.get(url, options)
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
});
this._fetch(callback).get(url, options)
};

/* 'emails' is a deprecated method since Sendwithus has changed their language
Expand All @@ -191,65 +183,33 @@ Sendwithus.prototype.emails = Sendwithus.prototype.templates;

Sendwithus.prototype.customersUpdateOrCreate = function (data, callback) {
var url = this._buildUrl("customers");

var options = this._getOptions();

var that = this;
this.emit("request", "POST", url, options.headers, data);

this._fetch()
.postJson(url, data, options)
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
});
this._fetch(callback).postJson(url, data, options)
};

Sendwithus.prototype.customersDelete = function (email, callback) {
var url = this._buildUrl("customers", email);

var options = this._getOptions();

var that = this;
this.emit("request", "DELETE", url, options.headers, {});

this._fetch()
.del(url, options)
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
});
this._fetch(callback).del(url, options)
};

Sendwithus.prototype.dripCampaignList = function (callback) {
var url = this._buildUrl("drip_campaigns");

var options = this._getOptions();

var that = this;
this.emit("request", "GET", url, options.headers, {});

this._fetch()
.get(url, options)
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
});
this._fetch(callback).get(url, options)
};

Sendwithus.prototype.dripCampaignDetails = function (
drip_campaign_id,
callback
) {
var url = this._buildUrl("drip_campaigns", drip_campaign_id);

var options = this._getOptions();

var that = this;
this.emit("request", "GET", url, options.headers, {});

this._fetch()
.get(url, options)
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
});
this._fetch(callback).get(url, options)
};

Sendwithus.prototype.dripCampaignActivate = function (
Expand All @@ -258,17 +218,9 @@ Sendwithus.prototype.dripCampaignActivate = function (
callback
) {
var url = this._buildUrl("drip_campaigns", drip_campaign_id, "activate");

var options = this._getOptions();

var that = this;
this.emit("request", "POST", url, options.headers, data);

this._fetch()
.postJson(url, data, options)
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
});
this._fetch(callback).postJson(url, data, options)
};

Sendwithus.prototype.dripCampaignDeactivate = function (
Expand All @@ -277,47 +229,23 @@ Sendwithus.prototype.dripCampaignDeactivate = function (
callback
) {
var url = this._buildUrl("drip_campaigns", drip_campaign_id, "deactivate");

var options = this._getOptions();

var that = this;
this.emit("request", "POST", url, options.headers, data);

this._fetch()
.del(url, options)
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
});
this._fetch(callback).del(url, options);
};

Sendwithus.prototype.dripCampaignDeactivateAll = function (data, callback) {
var url = this._buildUrl("drip_campaigns", "deactivate");

var options = this._getOptions();

var that = this;
this.emit("request", "POST", url, options.headers, data);

this._fetch()
.postJson(url, data, options)
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
});
this._fetch(callback).postJson(url, data, options)
};

Sendwithus.prototype.createTemplate = function (data, callback) {
var url = this._buildUrl("templates");

var options = this._getOptions();

var that = this;
this.emit("request", "POST", url, options.headers, data);

this._fetch()
.postJson(url, data, options)
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
});
this._fetch(callback).postJson(url, data, options)
};

Sendwithus.prototype.createTemplateVersion = function (
Expand All @@ -326,31 +254,16 @@ Sendwithus.prototype.createTemplateVersion = function (
callback
) {
var url = this._buildUrl("templates", templateId, "versions");

var options = this._getOptions();

var that = this;
this.emit("request", "POST", url, options.headers, data);

this._fetch()
.postJson(url, data, options)
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
});
this._fetch(callback).postJson(url, data, options)
};

Sendwithus.prototype.resend = function (data, callback) {
var url = this._buildUrl("resend");

var options = this._getOptions();

var that = this;
this.emit("request", "POST", url, options.headers, data);
this._fetch()
.postJson(url, data, options)
.then(({ response, result }) => {
that._handleResponse.call(that, result, response, callback);
});
this._fetch(callback).postJson(url, data, options)
};

module.exports = function (apiKey, debug) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sendwithus",
"version": "5.1.0",
"version": "6.0.0",
"author": "Sendwithus <[email protected]>",
"description": "Sendwithus.com Node.js client",
"main": "index.js",
Expand Down
Loading

0 comments on commit de0eee2

Please sign in to comment.