diff --git a/lib/alias.js b/lib/alias.js index e68b1a0..5f510c7 100644 --- a/lib/alias.js +++ b/lib/alias.js @@ -1,7 +1,9 @@ import retry from 'async-retry'; import Now from '../lib'; +import toHost from './to-host'; export default class Alias extends Now { + async ls () { return retry(async (bail) => { const res = await this._fetch('/now/aliases'); @@ -34,25 +36,27 @@ export default class Alias extends Now { })); } - async set (url, aliases) { - console.log('set', url, aliases); - const deploymentId = url; // TODO get from API - return retry(async (bail) => { - const res = await this._fetch(`/now/aliases/${deploymentId}/`, { - method: 'POST', - body: { - aliases: aliases - } - }); + async set (deployment, alias) { + const list = await this.list(); + let key, val; - if (200 !== res.status && (400 <= res.status || 500 > res.status)) { - if (this._debug) console.log('> [debug] bailing on creating due to %s', res.status); - return bail(responseError(res)); - } + if (~deployment.indexOf('.')) { + val = toHost(deployment); + key = 'url'; + } else { + val = deployment; + key = 'uid'; + } - return await res.json(); - }, { retries: 3, minTimeout: 2500, onRetry: this._onRetry }); + const id = list.find((d) => d[key] === val); + + if (!id) { + const err = new Error(`Deployment not found by ${key} "${deployment}"`); + err.userError = true; + throw err; + } } + } function responseError (res) {