Tony Kovanen
9 years ago
2 changed files with 61 additions and 26 deletions
@ -1,37 +1,72 @@ |
|||
import EventEmitter from 'events'; |
|||
import Agent from './agent'; |
|||
import retry from './retry'; |
|||
import Now from '../lib'; |
|||
|
|||
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); |
|||
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)); |
|||
} |
|||
|
|||
ls (url) { |
|||
console.log('list', url); |
|||
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 |
|||
} |
|||
}); |
|||
|
|||
_onRetry (err) { |
|||
if (this._debug) { |
|||
console.log(`> [debug] Retrying: ${err.stack}`); |
|||
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 }); |
|||
} |
|||
} |
|||
|
|||
async _fetch (_url, opts = {}) { |
|||
opts.headers = opts.headers || {}; |
|||
opts.headers.authorization = `Bearer ${this._token}`; |
|||
return await this._agent.fetch(_url, opts); |
|||
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); |
|||
} |
|||
} |
|||
|
|||
return err; |
|||
} |
|||
|
Loading…
Reference in new issue