Skip to content

Commit

Permalink
Merge pull request #48 from frenautvh/master
Browse files Browse the repository at this point in the history
feat: allow /v1 or /v2 prefixes in path
  • Loading branch information
rbeuque74 authored Oct 9, 2023
2 parents 224296c + 380d1ac commit 06d61af
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
26 changes: 20 additions & 6 deletions lib/ovh.es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,36 @@ var Ovh = function () {
}

/**
* Recursively loads the schemas of the specified used APIs.
*
* @param {String} path
* @param {Function} callback
* Returns the endpoint's full path, if the endpoint's path starts
* with /v1 or /v2, remove the trailing '/1.0' from the basePath
*/


_createClass(Ovh, [{
key: 'getFullPath',
value: function getFullPath(path) {
if ((this.basePath || '').endsWith('/1.0') && /^\/v(1|2)/.test(path)) {
return '' + this.basePath.slice(0, -4) + path;
}
return '' + this.basePath + path;
}

/**
* Recursively loads the schemas of the specified used APIs.
*
* @param {String} path
* @param {Function} callback
*/

}, {
key: 'loadSchemas',
value: function loadSchemas(path, callback) {
var _this = this;

var request = {
host: this.host,
port: this.port,
path: this.basePath + path
path: this.getFullPath(path)
};

// Fetch only selected APIs
Expand Down Expand Up @@ -334,7 +348,7 @@ var Ovh = function () {
host: this.host,
port: this.port,
method: httpMethod,
path: this.basePath + path
path: this.getFullPath(path)
};

// Headers
Expand Down
15 changes: 13 additions & 2 deletions lib/ovh.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ class Ovh {
}
}

/**
* Returns the endpoint's full path, if the endpoint's path starts
* with /v1 or /v2, remove the trailing '/1.0' from the basePath
*/
getFullPath(path) {
if ((this.basePath || '').endsWith('/1.0') && /^\/v(1|2)/.test(path)) {
return `${this.basePath.slice(0, -4)}${path}`;
}
return `${this.basePath}${path}`;
}

/**
* Recursively loads the schemas of the specified used APIs.
*
Expand All @@ -96,7 +107,7 @@ class Ovh {
let request = {
host: this.host,
port: this.port,
path: this.basePath + path
path: this.getFullPath(path)
};

// Fetch only selected APIs
Expand Down Expand Up @@ -326,7 +337,7 @@ class Ovh {
host: this.host,
port: this.port,
method: httpMethod,
path: this.basePath + path
path: this.getFullPath(path)
};

// Headers
Expand Down
40 changes: 39 additions & 1 deletion tests/02_REST_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,5 +474,43 @@ exports.REST_check = {
assert.equal(err.error, '[OVH] Unable to fetch OVH API time');
})
.finally(done);
}
},
'Fetch v1 endpoint': function (done) {
'use strict';
nock('https://ca.api.ovh.com')
.intercept('/v1/call', 'GET')
.reply(200, {});

var rest = ovh({
appKey: APP_KEY,
appSecret: APP_SECRET,
endpoint: 'ovh-ca'
});

assert.equal(rest.basePath, '/1.0');
rest.requestPromised('GET', '/v1/call')
.then(function (resp) {
assert.ok(!resp);
})
.finally(done);
},
'Fetch v2 endpoint': function (done) {
'use strict';
nock('https://ca.api.ovh.com')
.intercept('/v2/call', 'GET')
.reply(200, {});

var rest = ovh({
appKey: APP_KEY,
appSecret: APP_SECRET,
endpoint: 'ovh-ca'
});

assert.equal(rest.basePath, '/1.0');
rest.requestPromised('GET', '/v2/call')
.then(function (resp) {
assert.ok(!resp);
})
.finally(done);
},
};

0 comments on commit 06d61af

Please sign in to comment.