diff --git a/bin/now-alias b/bin/now-alias index 8eb68cb..b90449c 100755 --- a/bin/now-alias +++ b/bin/now-alias @@ -106,13 +106,13 @@ async function run (token) { switch (subcommand) { case 'ls': - alias.ls(url); + await alias.ls(url); break; case 'rm': - alias.rm(url, aliases); + await alias.rm(url, aliases); break; case 'set': - alias.set(url, aliases); + await alias.set(url, aliases); break; default: error('Invalid subcommand'); diff --git a/lib/alias.js b/lib/alias.js index 3d2359e..fe17698 100644 --- a/lib/alias.js +++ b/lib/alias.js @@ -1,37 +1,72 @@ -import EventEmitter from 'events'; -import Agent from './agent'; - -export default class Now extends EventEmitter { - constructor (url, token, { forceNew = false, debug = false }) { - super(); - this._token = token; - this._debug = debug; - this._forceNew = forceNew; - this._agent = new Agent(url, { debug }); - this._onRetry = this._onRetry.bind(this); - } +import retry from './retry'; +import Now from '../lib'; - ls (url) { +export default class Alias extends Now { + async ls (url) { console.log('list', url); + const deploymentId = url; // TODO get from API + return retry(async (bail) => { + const res = await this._fetch(`/now/aliases/${deploymentId}/`); + + 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)); + } + + return await res.json(); + }, { retries: 3, minTimeout: 2500, onRetry: this._onRetry }); } - rm (url, aliases) { + async rm (url, aliases) { console.log('rm', url, aliases); + const deploymentId = url; // TODO get from API + return await Promise.all(aliases.map(async (alias) => { + retry(async (bail) => { + const res = await this._fetch(`/now/aliases/${deploymentId}/${alias}`, { + method: 'DELETE' + }); + + 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)); + } + + return await res.json(); + }, { retries: 3, minTimeout: 2500, onRetry: this._onRetry }); + })); } - set (url, aliases) { + 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 + } + }); + + 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)); + } + + return await res.json(); + }, { retries: 3, minTimeout: 2500, onRetry: this._onRetry }); } +} - _onRetry (err) { - if (this._debug) { - console.log(`> [debug] Retrying: ${err.stack}`); +function responseError (res) { + const err = new Error('Response error'); + err.status = res.status; + + if (429 === res.status) { + const retryAfter = res.headers.get('Retry-After'); + if (retryAfter) { + err.retryAfter = parseInt(retryAfter, 10); } } - async _fetch (_url, opts = {}) { - opts.headers = opts.headers || {}; - opts.headers.authorization = `Bearer ${this._token}`; - return await this._agent.fetch(_url, opts); - } + return err; }