@heroku-cli/command
Version:
base class for Heroku CLI commands
58 lines (57 loc) • 2.3 kB
JavaScript
import { HTTP } from '@heroku/http-call';
import * as url from 'node:url';
import { RequestId, requestIdHeader } from './request-id.js';
import { vars } from './vars.js';
export class ParticleboardClient {
config;
http;
_auth;
constructor(config) {
this.config = config;
this.config = config;
const particleboardUrl = new url.URL(vars.particleboardUrl);
const self = this;
const envParticleboardHeaders = JSON.parse(process.env.HEROKU_PARTICLEBOARD_HEADERS || '{}');
const particleboardOpts = {
headers: {
accept: 'application/vnd.heroku+json; version=3',
'user-agent': `heroku-cli/${self.config.version} ${self.config.platform}`,
...envParticleboardHeaders,
},
host: particleboardUrl.hostname,
port: particleboardUrl.port,
protocol: particleboardUrl.protocol,
};
this.http = class ParticleboardHTTPClient extends HTTP.create(particleboardOpts) {
static async request(url, opts = {}) {
opts.headers = opts.headers || {};
opts.headers[requestIdHeader] = RequestId.create() && RequestId.headerValue;
if (!Object.keys(opts.headers).some(h => h.toLowerCase() === 'authorization')) {
opts.headers.authorization = `Bearer ${self.auth}`;
}
const response = await super.request(url, opts);
this.trackRequestIds(response);
return response;
}
static trackRequestIds(response) {
const responseRequestIdHeader = response.headers[requestIdHeader] || response.headers[requestIdHeader.toLocaleLowerCase()];
if (responseRequestIdHeader) {
const requestIds = Array.isArray(responseRequestIdHeader) ? responseRequestIdHeader : responseRequestIdHeader.split(',');
RequestId.track(...requestIds);
}
}
};
}
get auth() {
return this._auth;
}
set auth(token) {
this._auth = token;
}
get defaults() {
return this.http.defaults;
}
get(url, options = {}) {
return this.http.get(url, options);
}
}