diff --git a/lib/PromoStandards.ts b/lib/PromoStandards.ts index 487e235..ff9f13a 100644 --- a/lib/PromoStandards.ts +++ b/lib/PromoStandards.ts @@ -1,4 +1,6 @@ "use strict"; +import { AxiosRequestConfig } from 'axios'; + const axios = require("axios"); import * as templates from "./templates"; @@ -16,6 +18,7 @@ export namespace PromoStandards { password?: string; endpoints?: ServiceEndpointType[]; format?: ResponseFormatType; + axiosConfig?: AxiosRequestConfig; } /** Type of service check */ @@ -73,6 +76,7 @@ export namespace PromoStandards { public id?: string; public password?: string; public endpoints?: ServiceEndpointType[]; + public axiosConfig: AxiosRequestConfig; public format: ResponseFormatType = "json"; @@ -86,6 +90,7 @@ export namespace PromoStandards { this.id = options.id; this.password = options.password; this.endpoints = options.endpoints; + this.axiosConfig = options.axiosConfig || {}; this.format = options.format || this.format; } @@ -137,7 +142,9 @@ export namespace PromoStandards { axios .post(endpoint.url, requestXML, { + ...this.axiosConfig, headers: { + ...this.axiosConfig.headers, "Content-Type": "text/xml", SOAPAction: method, }, diff --git a/package.json b/package.json index 7d8674a..30b124c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "promostandards-sdk-js", - "version": "0.0.9", + "version": "0.0.10", "description": "PromoStandards JavaScript SDK", "homepage": "https://github.com/manishrc/promostandards-sdk-js/", "repository": { diff --git a/test/PromoStandards.test.ts b/test/PromoStandards.test.ts index 74e7ea7..303770c 100644 --- a/test/PromoStandards.test.ts +++ b/test/PromoStandards.test.ts @@ -76,6 +76,12 @@ describe("PromoStandardsClient", () => { }) .options("/ProductData") .reply(200); + nock("https://test2.dev") + .persist() + .options("/ProductData") + .reply((uri: any, requestBody: any, cb: (arg0: null, arg1: (string | number)[]) => void) => { + setTimeout(() => cb(null, [200, '']), 2500) + }); nock("https://test.dev") .persist() @@ -113,6 +119,22 @@ describe("PromoStandardsClient", () => { } ] }); + + const supplierWithTimeoutClient = new PromoStandards.Client({ + id: "aliquid", + password: "vitae", + axiosConfig: { + timeout: 1000, + }, + endpoints: [ + { + type: "ProductData", + version: "1.0.0", + url: "https://test2.dev/ProductData" + } + ] + }); + it("should return a Promise", () => { return expect( supplierClient.promoStandardsAPIRequest("ProductData.getProduct", { @@ -123,6 +145,16 @@ describe("PromoStandardsClient", () => { ).toBeInstanceOf(Promise); }); + it("should return a timeout error", () => { + return expect( + supplierWithTimeoutClient.promoStandardsAPIRequest("ProductData.getProduct", { + productId: "5790", + localizationCountry: "US", + localizationLanguage: "en" + }) + ).rejects.toThrow('timeout of 1000ms exceeded'); + }); + it("should return a JSON by default", async () => { await expect( supplierClient.promoStandardsAPIRequest("ProductData.getProduct", {