Browse Source

Sketch requests

(Mostly we need to decide what the route for DELETE is gonna look like)
master
Tony Kovanen 9 years ago
parent
commit
6d189db9d8
  1. 6
      bin/now-alias
  2. 81
      lib/alias.js

6
bin/now-alias

@ -106,13 +106,13 @@ async function run (token) {
switch (subcommand) { switch (subcommand) {
case 'ls': case 'ls':
alias.ls(url); await alias.ls(url);
break; break;
case 'rm': case 'rm':
alias.rm(url, aliases); await alias.rm(url, aliases);
break; break;
case 'set': case 'set':
alias.set(url, aliases); await alias.set(url, aliases);
break; break;
default: default:
error('Invalid subcommand'); error('Invalid subcommand');

81
lib/alias.js

@ -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…
Cancel
Save