heroku-cli-addons
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -5,11 +5,42 @@ 'use strict' | ||
const addonHeaders = function () { | ||
return { | ||
'Accept': 'application/vnd.heroku+json; version=3.actions', | ||
'Accept-Expansion': 'addon_service,plan' | ||
} | ||
} | ||
const attachmentHeaders = function () { | ||
return { | ||
'Accept': 'application/vnd.heroku+json; version=3.actions', | ||
'Accept-Inclusion': 'addon:plan,config_vars' | ||
} | ||
} | ||
const appAddon = function (heroku, app, id, options = {}) { | ||
const headers = addonHeaders() | ||
return heroku.post('/actions/addons/resolve', { | ||
'headers': headers, | ||
'body': {'app': app, 'addon': id, 'addon_service': options.addon_service} | ||
}) | ||
.then(singularize('addon')) | ||
} | ||
exports.appAddon = appAddon | ||
const addonResolver = function (heroku, app, id, options = {}) { | ||
const headers = {'Accept-Expansion': 'addon_service,plan'} | ||
const headers = addonHeaders() | ||
let getAddon = function (id) { | ||
return heroku.get(`/addons/${encodeURIComponent(id)}`, {headers}) | ||
return heroku.post('/actions/addons/resolve', { | ||
'headers': headers, | ||
'body': {'app': null, 'addon': id, 'addon_service': options.addon_service} | ||
}) | ||
.then(singularize('addon')) | ||
} | ||
if (!app || id.indexOf('::') !== -1) return getAddon(id) | ||
return heroku.get(`/apps/${app}/addons/${encodeURIComponent(id)}`, {headers}) | ||
.catch(function (err) { if (err.statusCode === 404) return getAddon(id); else throw err }) | ||
if (!app || id.includes('::')) return getAddon(id) | ||
return appAddon(heroku, app, id, options) | ||
.catch(function (err) { if (err.statusCode === 404) return getAddon(id); else throw err }) | ||
} | ||
@@ -44,3 +75,3 @@ | ||
exports.addon = memoizePromise(addonResolver, (_, app, id) => `${app}|${id}`) | ||
exports.addon = memoizePromise(addonResolver, (_, app, id, options = {}) => `${app}|${id}|${options.addon_service}`) | ||
@@ -56,3 +87,3 @@ function NotFound () { | ||
function AmbiguousError (objects) { | ||
function AmbiguousError (matches, type) { | ||
Error.call(this) | ||
@@ -63,40 +94,46 @@ Error.captureStackTrace(this, this.constructor) | ||
this.statusCode = 422 | ||
this.message = `Ambiguous identifier; multiple matching add-ons found: ${objects.map((object) => object.name).join(', ')}.` | ||
this.message = `Ambiguous identifier; multiple matching add-ons found: ${matches.map((match) => match.name).join(', ')}.` | ||
this.body = {'id': 'multiple_matches', 'message': this.message} | ||
this.matches = matches | ||
this.type = type | ||
} | ||
const singularize = function (matches) { | ||
switch (matches.length) { | ||
case 0: | ||
throw new NotFound() | ||
case 1: | ||
return matches[0] | ||
default: | ||
throw new AmbiguousError(matches) | ||
const singularize = function (type) { | ||
return (matches) => { | ||
switch (matches.length) { | ||
case 0: | ||
throw new NotFound() | ||
case 1: | ||
return matches[0] | ||
default: | ||
throw new AmbiguousError(matches, type) | ||
} | ||
} | ||
} | ||
exports.attachment = function (heroku, app, id, options = {}) { | ||
const headers = {'Accept-Inclusion': 'addon:plan,config_vars'} | ||
const headers = attachmentHeaders() | ||
function getAttachment (id) { | ||
return heroku.get(`/addon-attachments/${encodeURIComponent(id)}`, {headers}) | ||
return heroku.post('/actions/addon-attachments/resolve', { | ||
'headers': headers, 'body': {'app': null, 'addon_attachment': id, 'addon_service': options.addon_service} | ||
}).then(singularize('addon_attachment')) | ||
.catch(function (err) { if (err.statusCode !== 404) throw err }) | ||
} | ||
function getAppAttachment (app, id) { | ||
if (!app || id.indexOf('::') !== -1) return getAttachment(id) | ||
return heroku.get(`/apps/${app}/addon-attachments/${encodeURIComponent(id)}`, {headers}) | ||
.catch(function (err) { if (err.statusCode !== 404) throw err }) | ||
} | ||
function getAppAddonAttachment (addon, app) { | ||
return heroku.get(`/addons/${encodeURIComponent(addon.id)}/addon-attachments`, {headers}) | ||
.then(function (attachments) { | ||
return singularize(attachments.filter((att) => att.app.name === app)) | ||
}) | ||
.then(filter(app, options.addon_service)) | ||
.then(singularize('addon_attachment')) | ||
} | ||
let promise | ||
if (!app || id.includes('::')) { | ||
promise = getAttachment(id) | ||
} else { | ||
promise = appAttachment(heroku, app, id, options) | ||
.catch(function (err) { if (err.statusCode !== 404) throw err }) | ||
} | ||
// first check to see if there is an attachment matching this app/id combo | ||
return getAppAttachment(app, id) | ||
return promise | ||
// if no attachment, look up an add-on that matches the id | ||
@@ -109,3 +146,3 @@ .then((attachment) => { | ||
else if (app) { | ||
return exports.addon(heroku, app, id) | ||
return exports.addon(heroku, app, id, options) | ||
.then((addon) => getAppAddonAttachment(addon, app)) | ||
@@ -117,1 +154,26 @@ } else { | ||
} | ||
const appAttachment = function (heroku, app, id, options = {}) { | ||
const headers = attachmentHeaders() | ||
return heroku.post('/actions/addon-attachments/resolve', { | ||
'headers': headers, 'body': {'app': app, 'addon_attachment': id, 'addon_service': options.addon_service} | ||
}).then(singularize('addon_attachment')) | ||
} | ||
exports.appAttachment = appAttachment | ||
const filter = function (app, addonService) { | ||
return attachments => { | ||
return attachments.filter(attachment => { | ||
if (attachment.app.name !== app) { | ||
return false | ||
} | ||
if (addonService && attachment.addon_service.name !== addonService) { | ||
return false | ||
} | ||
return true | ||
}) | ||
} | ||
} |
{ | ||
"name": "heroku-cli-addons", | ||
"description": "`heroku addons:*` commands", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"author": "Bo Jeanes @bjeanes", | ||
@@ -25,2 +25,3 @@ "bugs": { | ||
"chai": "^3.2.0", | ||
"lolex": "1.5.1", | ||
"mocha": "3.0.2", | ||
@@ -27,0 +28,0 @@ "nock": "8.0.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44185
1121
8