Some checks are pending
Bidi Control Character Guard / bidi-control-guard (push) Waiting to run
Circular Dependency Check / Check for new circular dependencies (push) Waiting to run
Citus Migration Smoke / Combined migrations on single-node Citus (push) Waiting to run
E2E Fresh Install Tests / fresh-install-e2e (push) Waiting to run
ext-v2 guardrails / Run ext-v2 guard and ESLint (push) Waiting to run
Integration Tests / Check for relevant changes (push) Waiting to run
Integration Tests / ${{ (github.event_name == 'schedule' || github.event.inputs.suite == 'full') && 'Full integration suite' || 'Tier-1 integration subset' }} (push) Blocked by required conditions
Mobile checks / Mobile lint + typecheck (push) Waiting to run
Mobile checks / Mobile unit tests (push) Waiting to run
Mobile checks / Mobile dependency audit (report) (push) Waiting to run
Mobile checks / Mobile reproducibility checks (push) Waiting to run
Secrets guard (env backups) / Ensure no tracked env backup files (push) Waiting to run
Temporal Readiness / fast-readiness (push) Waiting to run
Temporal Readiness / docker-parity (push) Waiting to run
TypeScript Type Check / Nx affected typecheck (push) Waiting to run
Unit Tests / Skipped-test budget (push) Waiting to run
Unit Tests / Nx affected unit tests (push) Waiting to run
Unit Tests / Server unit coverage (informational) (push) Waiting to run
Validate Tenant Management Schema / Check for relevant changes (push) Waiting to run
Validate Tenant Management Schema / Validate Tenant Management Schema (push) Blocked by required conditions
EE Workflows Build Guard / ee-workflows-build-guard (push) Waiting to run
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz Source: /opt/alga-psa on psa.joliet.tech
76 lines
2.0 KiB
JavaScript
76 lines
2.0 KiB
JavaScript
class HttpError extends Error {
|
|
constructor(message, { status, details } = {}) {
|
|
super(message);
|
|
this.name = 'HttpError';
|
|
this.status = status;
|
|
this.details = details;
|
|
}
|
|
}
|
|
|
|
function normalizeBaseUrl(baseUrl) {
|
|
return String(baseUrl || '').replace(/\/$/, '');
|
|
}
|
|
|
|
function tryParseJson(text) {
|
|
try {
|
|
return JSON.parse(text);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function createHttpClient({ baseUrl, tenantId, cookie, debug = false, fetchImpl = fetch }) {
|
|
const base = normalizeBaseUrl(baseUrl);
|
|
if (!base) throw new Error('createHttpClient requires baseUrl');
|
|
|
|
async function request(path, opts = {}) {
|
|
const url = path.startsWith('http') ? path : `${base}${path.startsWith('/') ? '' : '/'}${path}`;
|
|
const method = (opts.method ?? 'GET').toUpperCase();
|
|
const headers = { ...(opts.headers ?? {}) };
|
|
if (cookie) headers.Cookie = cookie;
|
|
if (tenantId) headers['x-tenant-id'] = tenantId;
|
|
const envApiKey = process.env.WORKFLOW_HARNESS_API_KEY || process.env.ALGA_API_KEY || '';
|
|
if (envApiKey) {
|
|
const hasApiKeyHeader = Object.keys(headers).some((k) => String(k).toLowerCase() === 'x-api-key');
|
|
if (!hasApiKeyHeader) headers['x-api-key'] = envApiKey;
|
|
}
|
|
|
|
let body = opts.body;
|
|
if (opts.json !== undefined) {
|
|
headers['Content-Type'] = 'application/json';
|
|
body = JSON.stringify(opts.json);
|
|
}
|
|
|
|
if (debug) {
|
|
// eslint-disable-next-line no-console
|
|
console.error(`[http] ${method} ${url}`);
|
|
}
|
|
|
|
const res = await fetchImpl(url, { method, headers, body });
|
|
const text = await res.text();
|
|
|
|
if (!res.ok) {
|
|
const parsed = tryParseJson(text);
|
|
const details = parsed ?? { error: text };
|
|
throw new HttpError(`HTTP ${res.status}: ${details?.error ?? 'Request failed'}`, {
|
|
status: res.status,
|
|
details
|
|
});
|
|
}
|
|
|
|
return {
|
|
status: res.status,
|
|
headers: res.headers,
|
|
text,
|
|
json: tryParseJson(text)
|
|
};
|
|
}
|
|
|
|
return { request };
|
|
}
|
|
|
|
module.exports = {
|
|
HttpError,
|
|
createHttpClient
|
|
};
|