diff --git a/.talismanrc b/.talismanrc index afb99a8..3fca841 100644 --- a/.talismanrc +++ b/.talismanrc @@ -6,7 +6,7 @@ fileignoreconfig: - filename: test/unit/mock/execution-mock.js checksum: 89d239d37c9d8d0cdb6ac61553a7d2e2d9115a10207f7c0b387c3565c9cb6564 - filename: package-lock.json - checksum: 0f9b6180aa8b66fdd055ef99f74f269febb07e90bbd64badf1039a4d375850b6 + checksum: bc3cbd474a69321d97be9557ef74a056b7fab4111869d496b0d0403c1c84b971 - filename: docdash-template/fixtures/documents/probe.js checksum: e841ecf889d0e82367c53c48ee0b3be8bd68d7babf4777a87ced769f29686ac4 - filename: docdash-template/.travis.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d6d5f7..6ab4efd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ # Changelog + +## [v1.2.7](https://github.com/contentstack/contentstack-marketplace-sdk/tree/v1.2.6) (2024-05-15) + - Fixed base URL path logic in contentstackClient to handle when region and host not provided + ## [v1.2.6](https://github.com/contentstack/contentstack-marketplace-sdk/tree/v1.2.6) (2024-03-03) - Update sanity tests diff --git a/lib/contentstackClient.js b/lib/contentstackClient.js index 356e725..7947011 100644 --- a/lib/contentstackClient.js +++ b/lib/contentstackClient.js @@ -31,6 +31,10 @@ export default function contentstackClient ({ http }) { if (region && region !== Region.NA) { baseUrlPath = `https://${region}-api.contentstack.com:443/v3/user-session` + } else if (!region && http?.defaults?.host) { + baseUrlPath = `https://${http.defaults.host}:443/v3/user-session` + } else { + baseUrlPath = `https://api.contentstack.io:443/v3/user-session` } function login (requestBody, params = {}) { diff --git a/package-lock.json b/package-lock.json index b7caa3e..f077d0d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "@contentstack/marketplace-sdk", - "version": "1.2.6", + "version": "1.2.7", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@contentstack/marketplace-sdk", - "version": "1.2.6", + "version": "1.2.7", "license": "MIT", "dependencies": { - "axios": "^1.8.2" + "axios": "^1.8.3" }, "devDependencies": { "@babel/cli": "^7.26.4", @@ -4180,9 +4180,9 @@ } }, "node_modules/axios": { - "version": "1.8.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.8.2.tgz", - "integrity": "sha512-ls4GYBm5aig9vWx8AWDSGLpnpDQRtWAfrjU+EuytuODrFBkqesN2RkOQCBzrA1RQNHw1SmRMSDDDSwzNAYQ6Rg==", + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.9.0.tgz", + "integrity": "sha512-re4CqKTJaURpzbLHtIi6XpDv20/CnpXOtjRY5/CU32L8gU8ek9UIivcfvSWvmKEngmVbrUtPpdDwWDWL7DNHvg==", "license": "MIT", "dependencies": { "follow-redirects": "^1.15.6", diff --git a/package.json b/package.json index cedd39c..ae09f59 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/marketplace-sdk", - "version": "1.2.6", + "version": "1.2.7", "description": "The Contentstack Marketplace SDK is used to manage the content of your Contentstack marketplace apps", "main": "./dist/node/contentstack-marketplace.js", "browser": "./dist/web/contentstack-marketplace.js", @@ -92,6 +92,6 @@ "webpack-merge": "4.2.2" }, "dependencies": { - "axios": "^1.8.2" + "axios": "^1.8.3" } } diff --git a/sanity-report-dev11.js b/sanity-report-dev11.js index a726572..0c1ec34 100644 --- a/sanity-report-dev11.js +++ b/sanity-report-dev11.js @@ -10,7 +10,7 @@ const user4 = process.env.USER4; const mochawesomeJsonOutput = fs.readFileSync( "./mochawesome-report/mochawesome.json", - "utf-8" + "utf8" ); const mochawesomeReport = JSON.parse(mochawesomeJsonOutput); diff --git a/sanity-report.mjs b/sanity-report.mjs index 9e73180..9fcd909 100644 --- a/sanity-report.mjs +++ b/sanity-report.mjs @@ -5,7 +5,7 @@ import fs from 'fs' dotenv.config() -const mochawesomeJsonOutput = fs.readFileSync('./mochawesome-report/mochawesome.json', 'utf-8') +const mochawesomeJsonOutput = fs.readFileSync('./mochawesome-report/mochawesome.json', 'utf8') const mochawesomeReport = JSON.parse(mochawesomeJsonOutput) const report = `./mochawesome-report/sanity-report.html` @@ -25,8 +25,16 @@ console.log(`Failed Tests: ${failedTests}`) console.log(`Pending Tests: ${pendingTests}`) console.log(`Total Duration: ${durationInMinutes}m ${durationInSeconds.toFixed(2)}s`) +const host = process.env.DEFAULTHOST || '' +let region = 'NA' + +const match = host.match(/^([^-]+(?:-[^-]+)*)-api/) +if (match && match[1]) { + region = match[1].toUpperCase() +} + const slackMessage = ` -*JavaScript Marketplace SDK Report* +*JavaScript Marketplace SDK Report - ${region}* • Total Suites: *${totalSuites}* • Total Tests: *${totalTests}* • Passed Tests: *${passedTests}* diff --git a/test/unit/ContentstackClient-test.js b/test/unit/ContentstackClient-test.js index 7bd42a1..6212e6b 100644 --- a/test/unit/ContentstackClient-test.js +++ b/test/unit/ContentstackClient-test.js @@ -4,11 +4,31 @@ import { expect } from 'chai' import { describe, it, beforeEach } from 'mocha' import MockAdapter from 'axios-mock-adapter' import Region from '../../lib/core/region' -var host = 'http://localhost/' +var host = 'localhost' + +describe('Region Test ', () => { + it('Contentstack Client login success with region NA', done => { + var mock = new MockAdapter(axios) + axios.defaults.region = Region.NA + mock.onPost('https://api.contentstack.io:443/v3/user-session').reply(200, { + user: { + authtoken: 'Test Auth' + } + }) + + ContentstackClient({ http: axios }) + .login() + .then((response) => { + expect(response.user.authtoken).to.be.equal('Test Auth') + done() + }) + .catch(done) + }) +}) describe('Contentstack Client', () => { beforeEach(function () { - host = 'http://localhost/' + host = 'localhost' axios.defaults.host = host axios.defaults.adapter = 'http' }) @@ -21,7 +41,7 @@ describe('Contentstack Client', () => { it('Contentstack Client login success', done => { var mock = new MockAdapter(axios) - mock.onPost('/user-session').reply(200, { + mock.onPost('https://localhost:443/v3/user-session').reply(200, { user: { authtoken: 'Test Auth' } @@ -37,7 +57,7 @@ describe('Contentstack Client', () => { it('Contentstack Client Logout with Authtoken', done => { var mock = new MockAdapter(axios) - mock.onDelete('/user-session').reply(200, { + mock.onDelete('https://localhost:443/v3/user-session').reply(200, { notice: 'You\'ve logged out successfully' }) ContentstackClient({ http: axios }) @@ -51,7 +71,7 @@ describe('Contentstack Client', () => { it('Contentstack Client Logout', done => { var mock = new MockAdapter(axios) - mock.onDelete('/user-session').reply(200, { + mock.onDelete('https://localhost:443/v3/user-session').reply(200, { notice: 'You\'ve logged out successfully' }) axios.defaults.headers = { @@ -101,20 +121,57 @@ describe('Contentstack Client', () => { done() }) - it('Contentstack Client login success with region', done => { - var mock = new MockAdapter(axios) - axios.defaults.region = Region.AZURE_NA - mock.onPost('https://azure-na-api.contentstack.com:443/v3/user-session').reply(200, { - user: { - authtoken: 'Test Auth' - } + it('Contentstack Client login success with region AZURE-NA', done => { + var mock = new MockAdapter(axios) + axios.defaults.region = Region.AZURE_NA + mock.onPost('https://azure-na-api.contentstack.com:443/v3/user-session').reply(200, { + user: { + authtoken: 'Test Auth' + } + }) + + ContentstackClient({ http: axios }) + .login() + .then((response) => { + expect(response.user.authtoken).to.be.equal('Test Auth') + done() }) - ContentstackClient({ http: axios }) - .login() - .then((response) => { - expect(response.user.authtoken).to.be.equal('Test Auth') - done() - }) - .catch(done) + .catch(done) + }) + + it('Contentstack Client login success with region AZURE-EU', done => { + var mock = new MockAdapter(axios) + axios.defaults.region = Region.AZURE_EU + mock.onPost('https://azure-eu-api.contentstack.com:443/v3/user-session').reply(200, { + user: { + authtoken: 'Test Auth' + } + }) + + ContentstackClient({ http: axios }) + .login() + .then((response) => { + expect(response.user.authtoken).to.be.equal('Test Auth') + done() + }) + .catch(done) + }) + + it('Contentstack Client login success with region GCP-NA', done => { + var mock = new MockAdapter(axios) + axios.defaults.region = Region.GCP_NA + mock.onPost('https://gcp-na-api.contentstack.com:443/v3/user-session').reply(200, { + user: { + authtoken: 'Test Auth' + } + }) + + ContentstackClient({ http: axios }) + .login() + .then((response) => { + expect(response.user.authtoken).to.be.equal('Test Auth') + done() + }) + .catch(done) }) }) diff --git a/test/unit/concurrency-Queue-test.js b/test/unit/concurrency-Queue-test.js index 36550d1..e866e1b 100644 --- a/test/unit/concurrency-Queue-test.js +++ b/test/unit/concurrency-Queue-test.js @@ -130,22 +130,15 @@ describe('Concurrency queue test', () => { }) it('Refresh Token on 401 with 1000 concurrent request', done => { - var mock = new MockAdapter(axios) - mock.onPost('/user-session').reply(200, { - token - }) - const axiosClient = client({ - baseURL: `${host}:${port}`, - authorization: 'Bearer ', + const axiosClient = client({ + baseURL: `${host}:${port}`, + authorization: 'Bearer ', logHandler: logHandlerStub, refreshToken: () => { - return new Promise((resolve, reject) => { - return contentstackClient({ http: axios }).login().then((res) => { - resolve({ authorization: res.token }) - }).catch((error) => { - reject(error) + return Axios.post(`${host}:${port}/user-session`) + .then((res) => { + return { authorization: res.data.token } }) - }) } }) Promise.all(sequence(1003).map(() => axiosClient.axiosInstance.get('/unauthorized')))