Comparing version 2.3.5 to 2.5.0
@@ -0,1 +1,2 @@ | ||
import { AxiosInstance } from "axios"; | ||
import { ErrorHandler } from "./ErrorHandler"; | ||
@@ -16,2 +17,3 @@ import { Callback, ClientOptions, FilteringParameters } from "./models"; | ||
clientVersion: string; | ||
readonly httpClient: AxiosInstance; | ||
protected errorHandler: ErrorHandler; | ||
@@ -65,13 +67,2 @@ private readonly authHeader; | ||
/** | ||
* Handle http request to return it as Promise. | ||
* | ||
* @param method - Which type of http request will be executed. | ||
* @param path - API URL endpoint. | ||
* @param queryParameters - Querystring parameters used for http request. | ||
* @param body - Data sent with http request. | ||
* | ||
* @returns A promise that will complete when the API responds. | ||
*/ | ||
private promisifiedHttpRequest; | ||
/** | ||
* Process callback function for HTTP request. | ||
@@ -92,4 +83,10 @@ * | ||
private httpRequest; | ||
/** | ||
* Create http client instance with default settings. | ||
* | ||
* @return {AxiosInstance} | ||
*/ | ||
private buildDefaultHttpClient; | ||
private getRequestTimeoutInSeconds; | ||
private getHttpRequestURL; | ||
private getBaseHttpRequestURL; | ||
/** | ||
@@ -96,0 +93,0 @@ * Token can't be empty. |
@@ -11,3 +11,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var request = require("request"); | ||
var axios_1 = require("axios"); | ||
var ErrorHandler_1 = require("./ErrorHandler"); | ||
@@ -27,2 +27,3 @@ var packageJson = require("../../package.json"); | ||
this.errorHandler = new ErrorHandler_1.ErrorHandler(); | ||
this.httpClient = this.buildDefaultHttpClient(); | ||
this.verifyToken(token); | ||
@@ -93,39 +94,9 @@ } | ||
var _this = this; | ||
return this.promisifiedHttpRequest(method, path, queryParameters, body) | ||
.then(function (response) { | ||
return response.body; | ||
}) | ||
return this.httpRequest(method, path, queryParameters, body) | ||
.then(function (response) { return response; }) | ||
.catch(function (error) { | ||
throw _this.errorHandler.generateError(error); | ||
throw _this.errorHandler.buildRequestError(error); | ||
}); | ||
}; | ||
/** | ||
* Handle http request to return it as Promise. | ||
* | ||
* @param method - Which type of http request will be executed. | ||
* @param path - API URL endpoint. | ||
* @param queryParameters - Querystring parameters used for http request. | ||
* @param body - Data sent with http request. | ||
* | ||
* @returns A promise that will complete when the API responds. | ||
*/ | ||
BaseClient.prototype.promisifiedHttpRequest = function (method, path, queryParameters, body) { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
_this.httpRequest(method, path, queryParameters, body, function (error, response) { | ||
if (error) { | ||
reject(error); | ||
} | ||
else { | ||
if (response.statusCode !== 200) { | ||
reject(response); | ||
} | ||
else { | ||
resolve(response); | ||
} | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Process callback function for HTTP request. | ||
@@ -139,5 +110,3 @@ * | ||
httpRequest | ||
.then(function (response) { | ||
callback(null, response); | ||
}) | ||
.then(function (response) { return callback(null, response); }) | ||
.catch(function (error) { return callback(error, null); }); | ||
@@ -154,12 +123,27 @@ } | ||
*/ | ||
BaseClient.prototype.httpRequest = function (method, path, queryParameters, body, callback) { | ||
request(this.getHttpRequestURL(path), { | ||
method: method.toString(), | ||
BaseClient.prototype.httpRequest = function (method, path, queryParameters, body) { | ||
return this.httpClient.request({ | ||
method: method, | ||
url: path, | ||
data: body, | ||
headers: this.getComposedHttpRequestHeaders(), | ||
qs: queryParameters, | ||
body: body, | ||
params: queryParameters, | ||
}); | ||
}; | ||
/** | ||
* Create http client instance with default settings. | ||
* | ||
* @return {AxiosInstance} | ||
*/ | ||
BaseClient.prototype.buildDefaultHttpClient = function () { | ||
var httpClient = axios_1.default.create({ | ||
baseURL: this.getBaseHttpRequestURL(), | ||
timeout: this.getRequestTimeoutInSeconds(), | ||
json: true, | ||
gzip: true, | ||
}, callback); | ||
responseType: "json", | ||
validateStatus: function (status) { | ||
return status >= 200 && status < 300; | ||
}, | ||
}); | ||
httpClient.interceptors.response.use(function (response) { return (response.data); }); | ||
return httpClient; | ||
}; | ||
@@ -169,5 +153,5 @@ BaseClient.prototype.getRequestTimeoutInSeconds = function () { | ||
}; | ||
BaseClient.prototype.getHttpRequestURL = function (path) { | ||
BaseClient.prototype.getBaseHttpRequestURL = function () { | ||
var scheme = this.clientOptions.useHttps ? "https" : "http"; | ||
return scheme + "://" + this.clientOptions.requestHost + path; | ||
return scheme + "://" + this.clientOptions.requestHost; | ||
}; | ||
@@ -181,3 +165,3 @@ /** | ||
if (!token || token.trim() === "") { | ||
throw this.errorHandler.generateError(new Error("A valid API token must be provided.")); | ||
throw this.errorHandler.buildGeneralError("A valid API token must be provided."); | ||
} | ||
@@ -184,0 +168,0 @@ }; |
@@ -0,5 +1,6 @@ | ||
import { AxiosError } from "axios"; | ||
import * as Errors from "./models/client/Errors"; | ||
/** | ||
* This class handles all client request errors. Client response error is classified so that proper response error is generated. | ||
* | ||
* Handles general errors and all client request errors. | ||
* Client response errors are classified so that proper response error is generated. | ||
*/ | ||
@@ -10,7 +11,7 @@ export declare class ErrorHandler { | ||
* | ||
* @param error - error that needs to be identified and transformed to proper Postmark error. | ||
* @param error - request error that needs to be transformed to proper Postmark error. | ||
* | ||
* @returns properly formatted Postmark error. | ||
* @return {PostmarkError} - formatted Postmark error | ||
*/ | ||
generateError(error: any): Errors.PostmarkError; | ||
buildRequestError(error: AxiosError): Errors.PostmarkError; | ||
/** | ||
@@ -23,4 +24,12 @@ * Build general Postmark error. | ||
*/ | ||
private buildGeneralError; | ||
buildGeneralError(errorMessage: string): Errors.PostmarkError; | ||
/** | ||
* Build Postmark error based on response from http client. | ||
* | ||
* @param {AxiosResponse} response - request response used to transform to Postmark error. | ||
* @return {PostmarkError} - formatted Postmark error | ||
*/ | ||
private buildErrorForResponse; | ||
private retrieveDefaultOrValue; | ||
/** | ||
* Build Postmark error based on HTTP request status. | ||
@@ -32,3 +41,3 @@ * | ||
*/ | ||
private buildStatusError; | ||
private buildRequestErrorByStatus; | ||
} |
@@ -5,4 +5,4 @@ "use strict"; | ||
/** | ||
* This class handles all client request errors. Client response error is classified so that proper response error is generated. | ||
* | ||
* Handles general errors and all client request errors. | ||
* Client response errors are classified so that proper response error is generated. | ||
*/ | ||
@@ -15,16 +15,16 @@ var ErrorHandler = /** @class */ (function () { | ||
* | ||
* @param error - error that needs to be identified and transformed to proper Postmark error. | ||
* @param error - request error that needs to be transformed to proper Postmark error. | ||
* | ||
* @returns properly formatted Postmark error. | ||
* @return {PostmarkError} - formatted Postmark error | ||
*/ | ||
ErrorHandler.prototype.generateError = function (error) { | ||
if (error.body !== undefined && error.body.Message !== undefined && error.statusCode !== undefined) { | ||
return this.buildStatusError(error.body.Message, error.body.ErrorCode, error.statusCode); | ||
ErrorHandler.prototype.buildRequestError = function (error) { | ||
var response = error.response; | ||
if (response !== undefined) { | ||
return this.buildErrorForResponse(response, error.message); | ||
} | ||
else if (error.statusCode !== undefined) { | ||
var errorMessage = (error.message === undefined) ? error.statusMessage : error.message; | ||
return this.buildStatusError(errorMessage, 0, error.statusCode); | ||
else if (error.message !== undefined) { | ||
return this.buildGeneralError(error.message); | ||
} | ||
else { | ||
return this.buildGeneralError(error.message); | ||
return this.buildGeneralError(error.toJSON.toString()); | ||
} | ||
@@ -43,2 +43,18 @@ }; | ||
/** | ||
* Build Postmark error based on response from http client. | ||
* | ||
* @param {AxiosResponse} response - request response used to transform to Postmark error. | ||
* @return {PostmarkError} - formatted Postmark error | ||
*/ | ||
ErrorHandler.prototype.buildErrorForResponse = function (response, errorMessage) { | ||
var data = response.data; | ||
var errorCode = this.retrieveDefaultOrValue(0, data.ErrorCode); | ||
var status = this.retrieveDefaultOrValue(0, response.status); | ||
var message = this.retrieveDefaultOrValue(errorMessage, data.Message); | ||
return this.buildRequestErrorByStatus(message, errorCode, status); | ||
}; | ||
ErrorHandler.prototype.retrieveDefaultOrValue = function (defaultValue, data) { | ||
return (data === undefined) ? defaultValue : data; | ||
}; | ||
/** | ||
* Build Postmark error based on HTTP request status. | ||
@@ -50,3 +66,3 @@ * | ||
*/ | ||
ErrorHandler.prototype.buildStatusError = function (errorMessage, errorCode, errorStatusCode) { | ||
ErrorHandler.prototype.buildRequestErrorByStatus = function (errorMessage, errorCode, errorStatusCode) { | ||
switch (errorStatusCode) { | ||
@@ -53,0 +69,0 @@ case 401: |
@@ -19,5 +19,5 @@ export interface Suppression { | ||
interface SuppressionEntries { | ||
Suppressions: { | ||
Suppressions: Array<{ | ||
EmailAddress: string; | ||
}[]; | ||
}>; | ||
} | ||
@@ -24,0 +24,0 @@ export declare type CreateSuppressionsRequest = SuppressionEntries; |
import BaseClient from "./BaseClient"; | ||
import { Callback, ClientOptions, DefaultResponse, FilteringParameters } from "./models/index"; | ||
import { Bounce, BounceActivationResponse, BounceCounts, BounceDump, BounceFilteringParameters, Bounces, BrowserUsageCounts, ClickCounts, ClickLocationCounts, ClickPlaformUsageCounts, CreateInboundRuleRequest, CreateTemplateRequest, CreateWebhookRequest, DeliveryStatistics, EmailClientUsageCounts, EmailPlaformUsageCounts, EmailReadTimesCounts, InboundMessageDetails, InboundMessages, InboundMessagesFilteringParameters, InboundRule, InboundRules, Message, MessageSendingResponse, OpenCounts, OutboundMessageClicks, OutboundMessageClicksFilteringParameters, OutboundMessageDetails, OutboundMessageDump, OutboundMessageOpens, OutboundMessageOpensFilteringParameters, OutboundMessages, OutboundMessagesFilteringParameters, OutboundStatistics, SentCounts, Server, SpamCounts, StatisticsFilteringParameters, Template, TemplatedMessage, TemplateFilteringParameters, Templates, TemplateValidation, TemplateValidationOptions, TrackedEmailCounts, UpdateServerRequest, UpdateTemplateRequest, UpdateWebhookRequest, Webhook, WebhookFilteringParameters, Webhooks, Suppressions, SuppressionStatuses, CreateSuppressionsRequest, DeleteSuppressionsRequest } from "./models/index"; | ||
import { Bounce, BounceActivationResponse, BounceCounts, BounceDump, BounceFilteringParameters, Bounces, BrowserUsageCounts, ClickCounts, ClickLocationCounts, ClickPlaformUsageCounts, CreateInboundRuleRequest, CreateSuppressionsRequest, CreateTemplateRequest, CreateWebhookRequest, DeleteSuppressionsRequest, DeliveryStatistics, EmailClientUsageCounts, EmailPlaformUsageCounts, EmailReadTimesCounts, InboundMessageDetails, InboundMessages, InboundMessagesFilteringParameters, InboundRule, InboundRules, Message, MessageSendingResponse, OpenCounts, OutboundMessageClicks, OutboundMessageClicksFilteringParameters, OutboundMessageDetails, OutboundMessageDump, OutboundMessageOpens, OutboundMessageOpensFilteringParameters, OutboundMessages, OutboundMessagesFilteringParameters, OutboundStatistics, SentCounts, Server, SpamCounts, StatisticsFilteringParameters, Suppressions, SuppressionStatuses, Template, TemplatedMessage, TemplateFilteringParameters, Templates, TemplateValidation, TemplateValidationOptions, TrackedEmailCounts, UpdateServerRequest, UpdateTemplateRequest, UpdateWebhookRequest, Webhook, WebhookFilteringParameters, Webhooks } from "./models/index"; | ||
/** | ||
@@ -5,0 +5,0 @@ * Server client class that can be used to interact with an individual Postmark Server. |
@@ -12,3 +12,3 @@ { | ||
], | ||
"version": "2.3.5", | ||
"version": "2.5.0", | ||
"author": "Igor Balos", | ||
@@ -84,4 +84,4 @@ "contributors": [ | ||
"dependencies": { | ||
"request": "^2.88.0" | ||
"axios": "^0.19.2" | ||
} | ||
} |
@@ -8,3 +8,2 @@ import * as postmark from "../../src/index"; | ||
import * as nconf from "nconf"; | ||
import BaseClient from "../../src/client/BaseClient"; | ||
const testingKeys = nconf.env().file({ file: __dirname + "/../../testing_keys.json" }); | ||
@@ -18,2 +17,3 @@ | ||
const accountToken: string = testingKeys.get("ACCOUNT_TOKEN"); | ||
const serverToken: string = testingKeys.get("SERVER_TOKEN"); | ||
@@ -68,3 +68,4 @@ beforeEach(() => { | ||
describe("request errors", () => { | ||
const invalidTokenError = "InvalidAPIKeyError"; | ||
const errorType = "InternalServerError"; | ||
const rejectError = {response: {status: 500, data: 'response'}} | ||
let sandbox: sinon.SinonSandbox; | ||
@@ -81,4 +82,4 @@ | ||
it("promise error", () => { | ||
client = new postmark.AccountClient("testToken"); | ||
sandbox.stub(BaseClient.prototype, <any> "httpRequest").yields(undefined, {statusCode: 401, body: 'response'}); | ||
client = new postmark.AccountClient(serverToken); | ||
sandbox.stub(client.httpClient, "request").rejects(rejectError); | ||
@@ -88,3 +89,3 @@ return client.getSenderSignatures().then((result) => { | ||
}, (error) => { | ||
expect(error.name).to.equal(invalidTokenError); | ||
expect(error.name).to.equal(errorType); | ||
}); | ||
@@ -95,7 +96,7 @@ }); | ||
client = new postmark.AccountClient("testToken"); | ||
sandbox.stub(BaseClient.prototype, <any> "httpRequest").yields(undefined, {statusCode: 401, body: 'response'}); | ||
sandbox.stub(client.httpClient, "request").rejects(rejectError); | ||
client.getSenderSignatures(undefined, (error: any, data) => { | ||
expect(data).to.equal(null); | ||
expect(error.name).to.equal(invalidTokenError); | ||
expect(error.name).to.equal(errorType); | ||
done(); | ||
@@ -102,0 +103,0 @@ }); |
@@ -8,3 +8,3 @@ import { Errors } from "../../src"; | ||
describe("ErrorHandler", () => { | ||
it("generateError", () => { | ||
it("buildGeneralError", () => { | ||
const errorHandler = new ErrorHandler(); | ||
@@ -16,3 +16,3 @@ | ||
const postmarkError = errorHandler.generateError(error); | ||
const postmarkError = errorHandler.buildGeneralError("Test message"); | ||
expect(postmarkError.message).to.equal(error.message); | ||
@@ -27,14 +27,15 @@ expect(postmarkError.name).to.equal("PostmarkError"); | ||
const error: any = { | ||
name: "Test name", | ||
body: { | ||
Message: "Test message", | ||
ErrorCode: 401, | ||
}, | ||
statusCode: 401, | ||
response: { | ||
data: { | ||
Message: "Test message", | ||
ErrorCode: 401, | ||
}, | ||
status: 401, | ||
} | ||
}; | ||
const postmarkError = errorHandler.generateError(error); | ||
const postmarkError = errorHandler.buildRequestError(error); | ||
expect(postmarkError).to.be.an.instanceof(Errors.InvalidAPIKeyError); | ||
expect(postmarkError.name).to.equal("InvalidAPIKeyError"); | ||
expect(postmarkError.message).to.equal(error.body.Message); | ||
expect(postmarkError.message).to.equal(error.response.data.Message); | ||
}); | ||
@@ -46,14 +47,15 @@ | ||
const error: any = { | ||
name: "Test name", | ||
body: { | ||
Message: "Test message", | ||
ErrorCode: 422, | ||
}, | ||
statusCode: 422, | ||
response: { | ||
data: { | ||
Message: "Test message", | ||
ErrorCode: 422, | ||
}, | ||
status: 422, | ||
} | ||
}; | ||
const postmarkError = errorHandler.generateError(error); | ||
const postmarkError = errorHandler.buildRequestError(error); | ||
expect(postmarkError).to.be.an.instanceof(Errors.ApiInputError); | ||
expect(postmarkError.name).to.equal("ApiInputError"); | ||
expect(postmarkError.message).to.equal(error.body.Message); | ||
expect(postmarkError.message).to.equal(error.response.data.Message); | ||
}); | ||
@@ -65,14 +67,15 @@ | ||
const error: any = { | ||
name: "Test name", | ||
body: { | ||
Message: "Test message", | ||
ErrorCode: 500, | ||
}, | ||
statusCode: 500, | ||
response: { | ||
data: { | ||
Message: "Test message", | ||
ErrorCode: 500, | ||
}, | ||
status: 500, | ||
} | ||
}; | ||
const postmarkError = errorHandler.generateError(error); | ||
const postmarkError = errorHandler.buildRequestError(error); | ||
expect(postmarkError).to.be.an.instanceof(Errors.InternalServerError); | ||
expect(postmarkError.name).to.equal("InternalServerError"); | ||
expect(postmarkError.message).to.equal(error.body.Message); | ||
expect(postmarkError.message).to.equal(error.response.data.Message); | ||
}); | ||
@@ -84,27 +87,43 @@ | ||
const error: any = { | ||
name: "Test name", | ||
message: "test message", | ||
statusCode: 600, | ||
response: { | ||
data: { | ||
Message: "Test message", | ||
ErrorCode: 600, | ||
}, | ||
status: 600, | ||
} | ||
}; | ||
const postmarkError = errorHandler.generateError(error); | ||
const postmarkError = errorHandler.buildRequestError(error); | ||
expect(postmarkError).to.be.an.instanceof(Errors.PostmarkError); | ||
expect(postmarkError.name).to.equal("UnknownError"); | ||
expect(postmarkError.message).to.equal(error.message); | ||
expect(postmarkError.message).to.equal(error.response.data.Message); | ||
}); | ||
it("postmark error", () => { | ||
it("no status", () => { | ||
const errorHandler = new ErrorHandler(); | ||
const error: any = { | ||
name: "Test name", | ||
message: "test message" | ||
response: { | ||
data: { | ||
Message: "Test message" | ||
} | ||
} | ||
}; | ||
const postmarkError = errorHandler.generateError(error); | ||
const postmarkError = errorHandler.buildRequestError(error); | ||
expect(postmarkError).to.be.an.instanceof(Errors.PostmarkError); | ||
expect(postmarkError.name).to.equal("UnknownError"); | ||
expect(postmarkError.message).to.equal(error.response.data.Message); | ||
}); | ||
it("postmark default error", () => { | ||
const errorHandler = new ErrorHandler(); | ||
const postmarkError = errorHandler.buildGeneralError("Test message"); | ||
expect(postmarkError).to.be.an.instanceof(Errors.PostmarkError); | ||
expect(postmarkError.name).to.equal("PostmarkError"); | ||
expect(postmarkError.message).to.equal(error.message); | ||
expect(postmarkError.message).to.equal("Test message"); | ||
}); | ||
}); | ||
}); |
@@ -12,3 +12,2 @@ import * as postmark from "../../src/index"; | ||
import * as sinon from 'sinon'; | ||
import BaseClient from "../../src/client/BaseClient"; | ||
@@ -25,7 +24,4 @@ describe("ServerClient", () => { | ||
it("default clientOptions", () => { | ||
expect(client.clientOptions).to.eql({ | ||
useHttps: true, | ||
requestHost: "api.postmarkapp.com", | ||
timeout: 30, | ||
}); | ||
const defaultClientOptions = { useHttps: true, requestHost: "api.postmarkapp.com", timeout: 30 }; | ||
expect(client.clientOptions).to.eql(defaultClientOptions); | ||
}); | ||
@@ -114,18 +110,8 @@ | ||
it('processRequest - without body called', () => { | ||
sandbox.stub(BaseClient.prototype, <any> "processRequest").returns("called") | ||
expect(client.getServer()).to.eq("called") | ||
}); | ||
it('processRequest - with body called', () => { | ||
sandbox.stub(BaseClient.prototype, <any> "processRequest").returns("called"); | ||
expect(client.editServer({Name: 'Test'})).to.eq("called") | ||
}); | ||
describe("callback", () => { | ||
it('process it when there are no errors', async() => { | ||
let callback = sinon.spy(); | ||
sandbox.stub(BaseClient.prototype, <any> "processHttpRequest").returns(new Promise( function(resolve) { resolve("test"); })); | ||
sandbox.stub(client.httpClient, "request").returns(Promise.resolve("test")); | ||
await client.getServer(callback); | ||
expect(callback.calledOnce).to.be.true | ||
@@ -135,6 +121,8 @@ }); | ||
it('process regular response based on request status', () => { | ||
sandbox.stub(BaseClient.prototype, <any> "httpRequest").yields(undefined, {statusCode: 200, body: 'response'}); | ||
sandbox.stub(client.httpClient, "request").returns(Promise.resolve("test")); | ||
return client.getServer( (error, result) => { | ||
expect(result).to.eq('response'); | ||
return client.getServer().then((result) => { | ||
expect(result).to.eq('test'); | ||
}, (error) => { | ||
throw Error(`Should not be here with error: ${error}`); | ||
}); | ||
@@ -144,7 +132,9 @@ }); | ||
it('process error response based on request status', () => { | ||
sandbox.stub(BaseClient.prototype, <any> "httpRequest").yields(undefined, {statusCode: 201, body: 'response'}); | ||
sandbox.stub(client.httpClient, "request").rejects({response: {status: 600, data: 'response'}}); | ||
return client.getServer( (error: any, result) => { | ||
return client.getServer().then((result) => { | ||
throw Error(`Should not be here with result: ${result}`); | ||
}, (error) => { | ||
expect(error.name).to.eq('UnknownError'); | ||
}).catch( error => {}); | ||
}); | ||
}); | ||
@@ -151,0 +141,0 @@ }); |
@@ -7,3 +7,2 @@ import * as postmark from "../../src/index"; | ||
import * as nconf from "nconf"; | ||
import BaseClient from "../../src/client/BaseClient"; | ||
import * as sinon from 'sinon'; | ||
@@ -34,39 +33,64 @@ | ||
describe("handling errors", () => { | ||
it("throw basic error - promise", () => { | ||
sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(new Error("Basic error")); | ||
const serverToken: string = testingKeys.get("SERVER_TOKEN"); | ||
const client: postmark.ServerClient = new postmark.ServerClient(serverToken); | ||
const serverToken: string = testingKeys.get("SERVER_TOKEN"); | ||
let client: postmark.ServerClient = new postmark.ServerClient(serverToken); | ||
describe("promise error", () => { | ||
it("instance", () => { | ||
sandbox.stub(client.httpClient, "request").rejects({ message: "Basic error", response: {data: "Basic error" }}); | ||
return client.getServer().then((result) => { | ||
return result; | ||
}, (error) => { | ||
expect(error).to.be.instanceOf(postmark.Errors.PostmarkError); | ||
expect(error.message).to.equal("Basic error"); | ||
return client.getServer().then((result) => { | ||
return result; | ||
}, (error) => { | ||
expect(error).to.be.instanceOf(postmark.Errors.PostmarkError); | ||
}); | ||
}); | ||
}); | ||
it("throw api key error - promise", () => { | ||
let error: any = new Error("Basic error"); | ||
error.statusCode = 401; | ||
sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(error); | ||
it("message", () => { | ||
sandbox.stub(client.httpClient, "request").rejects({ message: "Basic error", response: {data: "Basic error" }}); | ||
const serverToken: string = testingKeys.get("SERVER_TOKEN"); | ||
let client: postmark.ServerClient = new postmark.ServerClient(serverToken); | ||
return client.getServer().then((result) => { | ||
return result; | ||
}, (error) => { | ||
expect(error.message).to.equal("Basic error"); | ||
}); | ||
}); | ||
return client.getServer().then((result) => { | ||
return result; | ||
}, (error) => { | ||
expect(error).to.be.instanceOf(postmark.Errors.InvalidAPIKeyError); | ||
it("name", () => { | ||
sandbox.stub(client.httpClient, "request").rejects({ response: { data: 'response', status: 401} }); | ||
return client.getBounces().then((result) => { | ||
return result; | ||
}, (error) => { | ||
expect(error.name).to.equal(invalidTokenError); | ||
}); | ||
}); | ||
}); | ||
describe("callback error", () => { | ||
it("name", (done) => { | ||
sandbox.stub(client.httpClient, "request").rejects({ response: {data: "Basic error", status: 404}}); | ||
client.getServer((error: any, data) => { | ||
expect(data).to.equal(null); | ||
expect(error.name).to.equal('PostmarkError'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it("empty token", () => { | ||
expect(() => new postmark.ServerClient("")) | ||
.to.throw("A valid API token must be provided."); | ||
}); | ||
describe("http status code errors", () => { | ||
it("404", () => { | ||
let error: any = new Error("Basic error"); | ||
error.statusCode = 404; | ||
sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(error); | ||
const buildError = (statusNumber: number) => ({ | ||
response: { | ||
data: "Basic error", | ||
status: statusNumber | ||
} | ||
}); | ||
const serverToken: string = testingKeys.get("SERVER_TOKEN"); | ||
let client: postmark.ServerClient = new postmark.ServerClient(serverToken); | ||
it("401", () => { | ||
sandbox.stub(client.httpClient, "request").rejects(buildError(401)); | ||
@@ -76,2 +100,12 @@ return client.getServer().then((result) => { | ||
}, (error) => { | ||
expect(error).to.be.instanceOf(postmark.Errors.InvalidAPIKeyError); | ||
}); | ||
}); | ||
it("404", () => { | ||
sandbox.stub(client.httpClient, "request").rejects(buildError(404)); | ||
return client.getServer().then((result) => { | ||
return result; | ||
}, (error) => { | ||
expect(error).to.be.instanceOf(postmark.Errors.PostmarkError); | ||
@@ -82,9 +116,4 @@ }); | ||
it("422", () => { | ||
let error: any = new Error("Basic error"); | ||
error.statusCode = 422; | ||
sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(error) | ||
sandbox.stub(client.httpClient, "request").rejects(buildError(422)); | ||
const serverToken: string = testingKeys.get("SERVER_TOKEN"); | ||
let client: postmark.ServerClient = new postmark.ServerClient(serverToken); | ||
return client.getServer().then((result) => { | ||
@@ -98,9 +127,4 @@ return result; | ||
it("500", () => { | ||
let error: any = new Error("Basic error"); | ||
error.statusCode = 500; | ||
sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(error); | ||
sandbox.stub(client.httpClient, "request").rejects(buildError(500)); | ||
const serverToken: string = testingKeys.get("SERVER_TOKEN"); | ||
let client: postmark.ServerClient = new postmark.ServerClient(serverToken); | ||
return client.getServer().then((result) => { | ||
@@ -114,9 +138,4 @@ return result; | ||
it("503", () => { | ||
let error: any = new Error("Basic error"); | ||
error.statusCode = 503; | ||
sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(error); | ||
sandbox.stub(client.httpClient, "request").rejects(buildError(503)); | ||
const serverToken: string = testingKeys.get("SERVER_TOKEN"); | ||
let client: postmark.ServerClient = new postmark.ServerClient(serverToken); | ||
return client.getServer().then((result) => { | ||
@@ -130,9 +149,4 @@ return result; | ||
it("505", () => { | ||
let error: any = new Error("Basic error"); | ||
error.statusCode = 505; | ||
sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(error); | ||
sandbox.stub(client.httpClient, "request").rejects(buildError(505)); | ||
const serverToken: string = testingKeys.get("SERVER_TOKEN"); | ||
let client: postmark.ServerClient = new postmark.ServerClient(serverToken); | ||
return client.getServer().then((result) => { | ||
@@ -145,42 +159,3 @@ return result; | ||
}); | ||
it("throw basic error - callback", (done) => { | ||
sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(new Error("Basic error")); | ||
const serverToken: string = testingKeys.get("SERVER_TOKEN"); | ||
let client: postmark.ServerClient = new postmark.ServerClient(serverToken); | ||
client.getServer((error: any, data) => { | ||
expect(data).to.equal(null); | ||
expect(error.name).to.equal('PostmarkError'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it("empty token", () => { | ||
expect(() => new postmark.ServerClient("")) | ||
.to.throw("A valid API token must be provided."); | ||
}); | ||
it("promise error", () => { | ||
sandbox.stub(BaseClient.prototype, <any> "httpRequest").yields(undefined, {statusCode: 401, body: 'response'}); | ||
return client.getBounces().then((result) => { | ||
return result; | ||
}, (error) => { | ||
expect(error.name).to.equal(invalidTokenError); | ||
}); | ||
}); | ||
it("callback error", (done) => { | ||
client = new postmark.ServerClient("testToken"); | ||
sandbox.stub(BaseClient.prototype, <any> "httpRequest").yields(undefined, {statusCode: 401, body: 'response'}); | ||
client.getBounces(undefined, (error: any, data) => { | ||
expect(data).to.equal(null); | ||
expect(error.name).to.equal(invalidTokenError); | ||
done(); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
262951
4673
+ Addedaxios@^0.19.2
+ Addedaxios@0.19.2(transitive)
+ Addedfollow-redirects@1.5.10(transitive)
- Removedrequest@^2.88.0
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.13.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)