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