Skip to content

Commit

Permalink
Allow overwriting of intermediate parts of paths
Browse files Browse the repository at this point in the history
makeURI (and associated functions) will take an additional parameter
that will overrwrite middle parts of the path.
  • Loading branch information
Anson Wayman authored and Anson Wayman committed Mar 22, 2017
1 parent 1976a07 commit ad70c17
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
44 changes: 35 additions & 9 deletions src/jira.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,52 +118,78 @@ export default class JiraApi {
* @name makeUri
* @function
* Creates a URI object for a given pathname
* @param {string} pathname - The url after the /rest/api/version
* @param {object} [options] - an object containing path information
*/
makeUri({ pathname, query }) {
makeUri({ pathname, query, intermediatePath }) {
const tempPath = intermediatePath || `/rest/api/${this.apiVersion}`;
const uri = url.format({
protocol: this.protocol,
hostname: this.host,
port: this.port,
pathname: `${this.base}/rest/api/${this.apiVersion}${pathname}`,
pathname: `${this.base}${tempPath}${pathname}`,
query,
});
return decodeURIComponent(uri);
}

/**
* @typedef makeUriOptions
* @type {object}
* @property {string} pathname - The url after the /rest/api/version
* @property {object} query - a query object
* @property {string} intermediatePath - If specified will overwrite the /rest/api/version section
*/

/**
* @name makeWebhookUri
* @function
* Creates a URI object for a given pathName
* @param {string} pathname - The url after the /rest/
* @param {object} [options] - An options object specifying uri information
*/
makeWebhookUri({ pathname }) {
makeWebhookUri({ pathname, intermediatePath }) {
const tempPath = intermediatePath || `/rest/webhooks/${this.webhookVersion}`;
const uri = url.format({
protocol: this.protocol,
hostname: this.host,
port: this.port,
pathname: `${this.base}/rest/webhooks/${this.webhookVersion}${pathname}`,
pathname: `${this.base}${tempPath}${pathname}`,
});
return decodeURIComponent(uri);
}

/**
* @typedef makeWebhookUriOptions
* @type {object}
* @property {string} pathname - The url after the /rest/webhooks
* @property {string} intermediatePath - If specified will overwrite the /rest/webhooks section
*/

/**
* @name makeSprintQueryUri
* @function
* Creates a URI object for a given pathName
* @param {string} pathname - The url after the /rest/
* @param {object} [options] - The url after the /rest/
*/
makeSprintQueryUri({ pathname, query }) {
makeSprintQueryUri({ pathname, query, intermediatePath }) {
const tempPath = intermediatePath || `/rest/greenhopper/${this.greenhopperVersion}`;
const uri = url.format({
protocol: this.protocol,
hostname: this.host,
port: this.port,
pathname: `${this.base}/rest/greenhopper/${this.greenhopperVersion}${pathname}`,
pathname: `${this.base}${tempPath}${pathname}`,
query,
});
return decodeURIComponent(uri);
}

/**
* @typedef makeSprintQueryUriOptions
* @type {object}
* @property {string} pathname - The url after the /rest/api/version
* @property {object} query - a query object
* @property {string} intermediatePath - will overwrite the /rest/greenhopper/version section
*/

/**
* @name doRequest
* @function
Expand Down
27 changes: 27 additions & 0 deletions test/jira-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ describe('Jira API Tests', () => {
.to.eql('http://jira.somehost.com:8080/rest/api/2.0/somePathName');
});

it('builds url with intermediatePath', () => {
const jira = new JiraApi(getOptions());

expect(jira.makeUri({ pathname: '/somePathName', intermediatePath: 'intermediatePath' }))
.to.eql('http://jira.somehost.com:8080/intermediatePath/somePathName');
});

it('builds url with query string parameters', () => {
const jira = new JiraApi(getOptions());

Expand Down Expand Up @@ -145,6 +152,16 @@ describe('Jira API Tests', () => {
.to.eql('http://jira.somehost.com:8080/rest/webhooks/1.0/somePathName');
});

it('makeWebhookUri functions with intermediate path', () => {
const jira = new JiraApi(getOptions());

expect(jira.makeWebhookUri({
pathname: '/somePathName',
intermediatePath: '/someIntermediatePath',
}))
.to.eql('http://jira.somehost.com:8080/someIntermediatePath/somePathName');
});

it('makeSprintQueryUri functions properly in the average case', () => {
const jira = new JiraApi(getOptions());

Expand All @@ -154,6 +171,16 @@ describe('Jira API Tests', () => {
.to.eql('http://jira.somehost.com:8080/rest/greenhopper/1.0/somePathName');
});

it('makeSprintQueryUri functions properly in the average case', () => {
const jira = new JiraApi(getOptions());

expect(jira.makeSprintQueryUri({
pathname: '/somePathName',
intermediatePath: '/someIntermediatePath',
}))
.to.eql('http://jira.somehost.com:8080/someIntermediatePath/somePathName');
});

it('makeUri functions properly no port http', () => {
const { port, ...options } = getOptions();

Expand Down

0 comments on commit ad70c17

Please sign in to comment.