Comparing version 2.9.5 to 3.0.0
# Changelog | ||
## 3.0.0 | ||
* Updated message response for sending | ||
* Decoupled and refactored http client part of the library | ||
## 2.9.5 | ||
@@ -4,0 +9,0 @@ |
@@ -29,3 +29,3 @@ "use strict"; | ||
function AccountClient(accountToken, configOptions) { | ||
return _super.call(this, accountToken, models_1.ClientOptions.DefaultHeaderNames.ACCOUNT_TOKEN, configOptions) || this; | ||
return _super.call(this, accountToken, models_1.ClientOptions.AuthHeaderNames.ACCOUNT_TOKEN, configOptions) || this; | ||
} | ||
@@ -32,0 +32,0 @@ /** |
@@ -1,4 +0,3 @@ | ||
import { AxiosInstance } from "axios"; | ||
import { ErrorHandler } from "./errors/ErrorHandler"; | ||
import { Callback, ClientOptions, FilteringParameters } from "./models"; | ||
import { Callback, ClientOptions, FilteringParameters, HttpClient } from "./models"; | ||
/** | ||
@@ -9,12 +8,5 @@ * Base client class from which client classes can be implemented, in our case, AccountClient and ServerClient classes. | ||
export default abstract class BaseClient { | ||
/** | ||
* Client connection configuration options. | ||
* You may modify these values and new clients will use them. | ||
* Any values provided to a Client constructor will override default options. | ||
*/ | ||
static DefaultOptions: ClientOptions.Configuration; | ||
clientVersion: string; | ||
readonly httpClient: AxiosInstance; | ||
httpClient: HttpClient; | ||
protected errorHandler: ErrorHandler; | ||
private clientOptions; | ||
private readonly authHeader; | ||
@@ -26,6 +18,2 @@ private readonly token; | ||
/** | ||
* JSON object with default headers sent by HTTP request. | ||
*/ | ||
getComposedHttpRequestHeaders(): any; | ||
/** | ||
* Process http request with sending body - data. | ||
@@ -43,6 +31,2 @@ * | ||
/** | ||
* Set default values for count and offset when doing filtering with API requests if they are not specified by filter. | ||
*/ | ||
protected setDefaultPaginationValues(filter: FilteringParameters): void; | ||
/** | ||
* Process request for Postmark ClientOptions. | ||
@@ -78,19 +62,6 @@ * | ||
/** | ||
* Process http request. | ||
* | ||
* @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. | ||
* JSON object with default headers sent by HTTP request. | ||
*/ | ||
private httpRequest; | ||
private getComposedHttpRequestHeaders; | ||
/** | ||
* Create http client instance with default settings. | ||
* | ||
* @return {AxiosInstance} | ||
*/ | ||
private buildDefaultHttpClient; | ||
private getRequestTimeoutInSeconds; | ||
private getBaseHttpRequestURL; | ||
/** | ||
* Token can't be empty. | ||
@@ -101,2 +72,6 @@ * | ||
private verifyToken; | ||
/** | ||
* Set default values for count and offset when doing filtering with API requests if they are not specified by filter. | ||
*/ | ||
protected setDefaultPaginationValues(filter: FilteringParameters): void; | ||
} |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var axios_1 = require("axios"); | ||
var ErrorHandler_1 = require("./errors/ErrorHandler"); | ||
var HttpClient_1 = require("./HttpClient"); | ||
var packageJson = require("../../package.json"); | ||
@@ -26,27 +15,14 @@ var CLIENT_VERSION = packageJson.version; | ||
this.verifyToken(token); | ||
this.clientVersion = CLIENT_VERSION; | ||
this.token = token.trim(); | ||
this.authHeader = authHeader; | ||
this.clientOptions = __assign(__assign({}, BaseClient.DefaultOptions), configOptions); | ||
this.httpClient = this.buildDefaultHttpClient(); | ||
this.clientVersion = CLIENT_VERSION; | ||
this.httpClient = new HttpClient_1.AxiosHttpClient(configOptions); | ||
} | ||
BaseClient.prototype.setClientOptions = function (configOptions) { | ||
this.clientOptions = __assign(__assign({}, BaseClient.DefaultOptions), configOptions); | ||
this.buildDefaultHttpClient(); | ||
this.httpClient.initHttpClient(configOptions); | ||
}; | ||
BaseClient.prototype.getClientOptions = function () { | ||
return this.clientOptions; | ||
return this.httpClient.clientOptions; | ||
}; | ||
/** | ||
* JSON object with default headers sent by HTTP request. | ||
*/ | ||
BaseClient.prototype.getComposedHttpRequestHeaders = function () { | ||
var _a; | ||
return _a = {}, | ||
_a[this.authHeader] = this.token, | ||
_a["Accept"] = "application/json", | ||
_a["User-Agent"] = "Postmark.JS - ".concat(this.clientVersion), | ||
_a; | ||
}; | ||
/** | ||
* Process http request with sending body - data. | ||
@@ -69,9 +45,2 @@ * | ||
/** | ||
* Set default values for count and offset when doing filtering with API requests if they are not specified by filter. | ||
*/ | ||
BaseClient.prototype.setDefaultPaginationValues = function (filter) { | ||
filter.count = filter.count || 100; | ||
filter.offset = filter.offset || 0; | ||
}; | ||
/** | ||
* Process request for Postmark ClientOptions. | ||
@@ -104,3 +73,3 @@ * | ||
var _this = this; | ||
return this.httpRequest(method, path, queryParameters, body) | ||
return this.httpClient.httpRequest(method, path, queryParameters, body, this.getComposedHttpRequestHeaders()) | ||
.then(function (response) { return response; }) | ||
@@ -125,45 +94,13 @@ .catch(function (error) { | ||
/** | ||
* Process http request. | ||
* | ||
* @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. | ||
* JSON object with default headers sent by HTTP request. | ||
*/ | ||
BaseClient.prototype.httpRequest = function (method, path, queryParameters, body) { | ||
return this.httpClient.request({ | ||
method: method, | ||
url: path, | ||
data: body, | ||
headers: this.getComposedHttpRequestHeaders(), | ||
params: queryParameters, | ||
}); | ||
BaseClient.prototype.getComposedHttpRequestHeaders = function () { | ||
var _a; | ||
return _a = {}, | ||
_a[this.authHeader] = this.token, | ||
_a["Accept"] = "application/json", | ||
_a["User-Agent"] = "Postmark.JS - ".concat(this.clientVersion), | ||
_a; | ||
}; | ||
/** | ||
* 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(), | ||
responseType: "json", | ||
maxContentLength: Infinity, | ||
maxBodyLength: Infinity, | ||
validateStatus: function (status) { | ||
return status >= 200 && status < 300; | ||
}, | ||
}); | ||
httpClient.interceptors.response.use(function (response) { return (response.data); }); | ||
return httpClient; | ||
}; | ||
BaseClient.prototype.getRequestTimeoutInSeconds = function () { | ||
return (this.clientOptions.timeout || 60) * 1000; | ||
}; | ||
BaseClient.prototype.getBaseHttpRequestURL = function () { | ||
var scheme = this.clientOptions.useHttps ? "https" : "http"; | ||
return "".concat(scheme, "://").concat(this.clientOptions.requestHost); | ||
}; | ||
/** | ||
* Token can't be empty. | ||
@@ -179,10 +116,7 @@ * | ||
/** | ||
* Client connection configuration options. | ||
* You may modify these values and new clients will use them. | ||
* Any values provided to a Client constructor will override default options. | ||
* Set default values for count and offset when doing filtering with API requests if they are not specified by filter. | ||
*/ | ||
BaseClient.DefaultOptions = { | ||
useHttps: true, | ||
requestHost: "api.postmarkapp.com", | ||
timeout: 180, | ||
BaseClient.prototype.setDefaultPaginationValues = function (filter) { | ||
filter.count = filter.count || 100; | ||
filter.offset = filter.offset || 0; | ||
}; | ||
@@ -189,0 +123,0 @@ return BaseClient; |
@@ -1,2 +0,2 @@ | ||
import { AxiosError } from "axios"; | ||
import { HttpClientError } from "../models"; | ||
import * as Errors from "./Errors"; | ||
@@ -15,3 +15,3 @@ /** | ||
*/ | ||
buildRequestError(error: AxiosError): Errors.PostmarkError; | ||
buildRequestError(error: HttpClientError): Errors.PostmarkError; | ||
/** | ||
@@ -18,0 +18,0 @@ * Build general Postmark error. |
@@ -49,4 +49,4 @@ "use strict"; | ||
var data = response.data; | ||
var status = this.retrieveDefaultOrValue(0, response.status); | ||
var errorCode = this.retrieveDefaultOrValue(0, data.ErrorCode); | ||
var status = this.retrieveDefaultOrValue(0, response.status); | ||
var message = this.retrieveDefaultOrValue(errorMessage, data.Message); | ||
@@ -53,0 +53,0 @@ return this.buildRequestErrorByStatus(message, errorCode, status); |
@@ -17,3 +17,3 @@ export declare namespace ClientOptions { | ||
} | ||
enum DefaultHeaderNames { | ||
enum AuthHeaderNames { | ||
SERVER_TOKEN = "X-Postmark-Server-Token", | ||
@@ -20,0 +20,0 @@ ACCOUNT_TOKEN = "X-Postmark-Account-Token" |
@@ -25,8 +25,8 @@ "use strict"; | ||
})(HttpMethod = ClientOptions.HttpMethod || (ClientOptions.HttpMethod = {})); | ||
var DefaultHeaderNames; | ||
(function (DefaultHeaderNames) { | ||
DefaultHeaderNames["SERVER_TOKEN"] = "X-Postmark-Server-Token"; | ||
DefaultHeaderNames["ACCOUNT_TOKEN"] = "X-Postmark-Account-Token"; | ||
})(DefaultHeaderNames = ClientOptions.DefaultHeaderNames || (ClientOptions.DefaultHeaderNames = {})); | ||
var AuthHeaderNames; | ||
(function (AuthHeaderNames) { | ||
AuthHeaderNames["SERVER_TOKEN"] = "X-Postmark-Server-Token"; | ||
AuthHeaderNames["ACCOUNT_TOKEN"] = "X-Postmark-Account-Token"; | ||
})(AuthHeaderNames = ClientOptions.AuthHeaderNames || (ClientOptions.AuthHeaderNames = {})); | ||
})(ClientOptions = exports.ClientOptions || (exports.ClientOptions = {})); | ||
//# sourceMappingURL=ClientOptions.js.map |
export * from "./client/ClientOptions"; | ||
export * from "./client/SupportingTypes"; | ||
export * from "./client/HttpClient"; | ||
export * from "./client/Callback"; | ||
@@ -4,0 +5,0 @@ export * from "./client/DefaultResponse"; |
@@ -15,2 +15,3 @@ "use strict"; | ||
__exportStar(require("./client/SupportingTypes"), exports); | ||
__exportStar(require("./client/HttpClient"), exports); | ||
__exportStar(require("./client/Callback"), exports); | ||
@@ -17,0 +18,0 @@ __exportStar(require("./client/DefaultResponse"), exports); |
@@ -27,3 +27,3 @@ import { DefaultResponse } from "../client/DefaultResponse"; | ||
SubmittedAt: string; | ||
MessageID?: string; | ||
MessageID: string; | ||
} |
@@ -33,3 +33,3 @@ "use strict"; | ||
function ServerClient(serverToken, configOptions) { | ||
return _super.call(this, serverToken, index_1.ClientOptions.DefaultHeaderNames.SERVER_TOKEN, configOptions) || this; | ||
return _super.call(this, serverToken, index_1.ClientOptions.AuthHeaderNames.SERVER_TOKEN, configOptions) || this; | ||
} | ||
@@ -36,0 +36,0 @@ /** Send a single email message. |
@@ -12,3 +12,3 @@ { | ||
], | ||
"version": "2.9.5", | ||
"version": "3.0.0", | ||
"author": "Igor Balos", | ||
@@ -15,0 +15,0 @@ "contributors": [ |
@@ -56,2 +56,12 @@ import * as postmark from "../../src/index"; | ||
/* | ||
it("getComposedHttpRequestHeaders", () => { | ||
expect(client.httpClient.getComposedHttpRequestHeaders()).to.eql({ | ||
"X-Postmark-Account-Token": accountToken, | ||
"Accept": "application/json", | ||
"User-Agent": `Postmark.JS - ${clientVersion}`, | ||
}); | ||
}); | ||
*/ | ||
it("set clientOptions timeout", () => { | ||
@@ -85,3 +95,3 @@ const timeoutValue: number = 10; | ||
describe("request errors", () => { | ||
describe("httpRequest errors", () => { | ||
const errorType = "InternalServerError"; | ||
@@ -101,3 +111,3 @@ const rejectError = {response: {status: 500, data: "response"}}; | ||
client = new postmark.AccountClient(serverToken); | ||
sandbox.stub(client.httpClient, "request").rejects(rejectError); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects(rejectError); | ||
@@ -113,3 +123,3 @@ return client.getSenderSignatures().then((result) => { | ||
client = new postmark.AccountClient("testToken"); | ||
sandbox.stub(client.httpClient, "request").rejects(rejectError); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects(rejectError); | ||
@@ -116,0 +126,0 @@ client.getSenderSignatures(undefined, (error: any, data) => { |
@@ -38,4 +38,5 @@ import * as postmark from "../../src/index"; | ||
/* | ||
it("getComposedHttpRequestHeaders", () => { | ||
expect(client.getComposedHttpRequestHeaders()).to.eql({ | ||
expect(client.httpClient.getComposedHttpRequestHeaders()).to.eql({ | ||
"X-Postmark-Server-Token": serverToken, | ||
@@ -46,3 +47,3 @@ "Accept": "application/json", | ||
}); | ||
*/ | ||
describe("clientOptions", () => { | ||
@@ -131,3 +132,3 @@ it("clientOptions=", () => { | ||
let callback = sinon.spy(); | ||
sandbox.stub(client.httpClient, "request").returns(Promise.resolve("test")); | ||
sandbox.stub(client.httpClient, "httpRequest").returns(Promise.resolve("test")); | ||
@@ -139,3 +140,3 @@ await client.getServer(callback); | ||
it("process regular response based on request status", () => { | ||
sandbox.stub(client.httpClient, "request").returns(Promise.resolve("test")); | ||
sandbox.stub(client.httpClient, "httpRequest").returns(Promise.resolve("test")); | ||
@@ -150,3 +151,3 @@ return client.getServer().then((result) => { | ||
it("process error response based on request status", () => { | ||
sandbox.stub(client.httpClient, "request").rejects({response: {status: 600, data: "response"}}); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects({response: {status: 600, data: "response"}}); | ||
@@ -153,0 +154,0 @@ return client.getServer().then((result) => { |
@@ -37,3 +37,3 @@ import * as postmark from "../../src/index"; | ||
it("instance", () => { | ||
sandbox.stub(client.httpClient, "request").rejects({ message: "Basic error", response: {data: "Basic error" }}); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects({ message: "Basic error", response: {data: "Basic error" }}); | ||
@@ -48,3 +48,3 @@ return client.getServer().then((result) => { | ||
it("message", () => { | ||
sandbox.stub(client.httpClient, "request").rejects({ message: "Basic error", response: {data: "Basic error" }}); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects({ message: "Basic error", response: {data: "Basic error" }}); | ||
@@ -59,3 +59,3 @@ return client.getServer().then((result) => { | ||
it("name", () => { | ||
sandbox.stub(client.httpClient, "request").rejects({ response: { data: 'response', status: 401} }); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects({ response: { data: 'response', status: 401} }); | ||
@@ -72,3 +72,3 @@ return client.getBounces().then((result) => { | ||
it("name", (done) => { | ||
sandbox.stub(client.httpClient, "request").rejects({ response: {data: "Basic error", status: 404}}); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects({ response: {data: "Basic error", status: 404}}); | ||
@@ -97,3 +97,3 @@ client.getServer((error: any, data) => { | ||
it("401", () => { | ||
sandbox.stub(client.httpClient, "request").rejects(buildError(401)); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects(buildError(401)); | ||
@@ -108,3 +108,3 @@ return client.getServer().then((result) => { | ||
it("404", () => { | ||
sandbox.stub(client.httpClient, "request").rejects(buildError(404)); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects(buildError(404)); | ||
@@ -119,3 +119,3 @@ return client.getServer().then((result) => { | ||
it("422", () => { | ||
sandbox.stub(client.httpClient, "request").rejects(buildError(422)); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects(buildError(422)); | ||
@@ -130,3 +130,3 @@ return client.getServer().then((result) => { | ||
it("429", () => { | ||
sandbox.stub(client.httpClient, "request").rejects(buildError(429)); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects(buildError(429)); | ||
@@ -141,3 +141,3 @@ return client.getServer().then((result) => { | ||
it("500", () => { | ||
sandbox.stub(client.httpClient, "request").rejects(buildError(500)); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects(buildError(500)); | ||
@@ -152,3 +152,3 @@ return client.getServer().then((result) => { | ||
it("503", () => { | ||
sandbox.stub(client.httpClient, "request").rejects(buildError(503)); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects(buildError(503)); | ||
@@ -163,3 +163,3 @@ return client.getServer().then((result) => { | ||
it("505", () => { | ||
sandbox.stub(client.httpClient, "request").rejects(buildError(505)); | ||
sandbox.stub(client.httpClient, "httpRequest").rejects(buildError(505)); | ||
@@ -166,0 +166,0 @@ return client.getServer().then((result) => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
309319
146
5408