Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional axiosConfig parameter for promostandards client options #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/PromoStandards.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"use strict";
import { AxiosRequestConfig } from 'axios';

const axios = require("axios");

import * as templates from "./templates";
Expand All @@ -16,6 +18,7 @@ export namespace PromoStandards {
password?: string;
endpoints?: ServiceEndpointType[];
format?: ResponseFormatType;
axiosConfig?: AxiosRequestConfig;
}

/** Type of service check */
Expand Down Expand Up @@ -73,6 +76,7 @@ export namespace PromoStandards {
public id?: string;
public password?: string;
public endpoints?: ServiceEndpointType[];
public axiosConfig: AxiosRequestConfig;

public format: ResponseFormatType = "json";

Expand All @@ -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;
}

Expand Down Expand Up @@ -137,7 +142,9 @@ export namespace PromoStandards {

axios
.post(endpoint.url, requestXML, {
...this.axiosConfig,
headers: {
...this.axiosConfig.headers,
"Content-Type": "text/xml",
SOAPAction: method,
},
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": "promostandards-sdk-js",
"version": "0.0.9",
"version": "0.0.10",
"description": "PromoStandards JavaScript SDK",
"homepage": "https://github.com/manishrc/promostandards-sdk-js/",
"repository": {
Expand Down
32 changes: 32 additions & 0 deletions test/PromoStandards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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", {
Expand All @@ -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", {
Expand Down