import Now from '../lib'; export default class Certs extends Now { ls () { return this.retry(async (bail, attempt) => { if (this._debug) console.time(`> [debug] #${attempt} GET now/certs`); const res = await this._fetch('/now/certs'); if (this._debug) console.timeEnd(`> [debug] #${attempt} GET now/certs`); const body = await res.json(); return body.certs; }); } create (cn) { return this.createCert(cn); } renew (cn) { return this.createCert(cn, { renew: true }); } replace (cn, crt, key, ca) { return this.retry(async (bail, attempt) => { if (this._debug) console.time(`> [debug] #${attempt} PUT now/certs`); const res = await this._fetch('/now/certs', { method: 'PUT', body: { domains: [cn], ca: ca, cert: crt, key: key } }); if (this._debug) console.timeEnd(`> [debug] #${attempt} PUT now/certs`); if (403 === res.status) { return bail(new Error('Unauthorized')); } const body = await res.json(); if (res.status !== 200) { if (404 === res.status || 400 === res.status) { const err = new Error(body.error.message); err.userError = true; return bail(err); } else { throw new Error(body.error.message); } } return body; }); } delete (cn) { return this.retry(async (bail, attempt) => { if (this._debug) console.time(`> [debug] #${attempt} DELETE now/certs/${cn}`); const res = await this._fetch(`/now/certs/${cn}`, { method: 'DELETE' }); if (this._debug) console.timeEnd(`> [debug] #${attempt} DELETE now/certs/${cn}`); if (403 === res.status) { return bail(new Error('Unauthorized')); } const body = await res.json(); if (res.status !== 200) { if (404 === res.status || 400 === res.status) { const err = new Error(body.error.message); err.userError = true; return bail(err); } else { throw new Error(body.error.message); } } return body; }); } }