OpenRouter now rejects management keys from the browser on both /api/v1/keys and /api/v1/analytics/query, so the management-key flow can no longer work in the extension. Read the configured key's own usage from GET /api/v1/key instead, which returns UTC daily, weekly, and monthly buckets and works from the browser. Drop the key-type selection, key listing/selection, pagination, and the management/analytics endpoints. Validate the key against /key before persisting, and write a fresh record so legacy keyType/keyIds fields are dropped. Keep the browser-block hint for anyone still storing a management key.
118 lines
3.7 KiB
JavaScript
118 lines
3.7 KiB
JavaScript
import Service from '@ember/service';
|
|
import config from 'dashtab/config/environment';
|
|
import { canUseExtensionAuth } from '../utils/extension';
|
|
|
|
export const CORS_PROXY_HEADER = 'X-Dashtab-Proxy';
|
|
|
|
export class HttpError extends Error {
|
|
constructor(status, errorType = null, body = null) {
|
|
super(`Request failed with status ${status}`);
|
|
this.name = 'HttpError';
|
|
this.status = status;
|
|
this.errorType = errorType;
|
|
this.body = body;
|
|
}
|
|
}
|
|
|
|
export class NetworkError extends Error {
|
|
constructor(cause) {
|
|
super('Request failed before a response was received');
|
|
this.name = 'NetworkError';
|
|
this.cause = cause;
|
|
}
|
|
}
|
|
|
|
// The request was cancelled because the document was navigated away from (or
|
|
// frozen/restored), not because a target was actually unreachable. Callers
|
|
// should treat this as a no-op rather than a user-facing failure.
|
|
export class RequestAbortedError extends Error {
|
|
constructor(cause) {
|
|
super('Request was cancelled');
|
|
this.name = 'RequestAbortedError';
|
|
this.cause = cause;
|
|
}
|
|
}
|
|
|
|
// True for both the browser's own `AbortError` and the wrapper above, so
|
|
// consumers can ignore cancellations without knowing which layer raised them.
|
|
export function isCancellation(err) {
|
|
return err?.name === 'AbortError' || err?.name === 'RequestAbortedError';
|
|
}
|
|
|
|
// Rewrites an absolute http(s) URL to go through the local development CORS
|
|
// proxy. Relative URLs and URLs already pointing at the proxy are untouched.
|
|
export function proxiedUrl(url, base) {
|
|
if (!base || !/^https?:\/\//i.test(url) || url.startsWith(base)) {
|
|
return url;
|
|
}
|
|
return `${base}${url}`;
|
|
}
|
|
|
|
// Single seam for outbound requests. As an extension page the app fetches
|
|
// cross-origin directly using the extension's host permissions; elsewhere the
|
|
// development CORS proxy stands in for them.
|
|
export default class HttpService extends Service {
|
|
// In an extension the development CORS proxy (tied to the localhost origin)
|
|
// must not be used; host permissions take over.
|
|
corsProxyBase = canUseExtensionAuth() ? null : (config.corsProxy ?? null);
|
|
|
|
async request(url, options = {}) {
|
|
const target = proxiedUrl(url, this.corsProxyBase);
|
|
const headers = { ...(options.headers ?? {}) };
|
|
|
|
if (target !== url) {
|
|
headers[CORS_PROXY_HEADER] = '1';
|
|
}
|
|
|
|
let response;
|
|
|
|
try {
|
|
// Extension pages with host permissions are treated as same-origin, so
|
|
// the target origin's cookies would ride along. Cookie-first APIs can
|
|
// then try to read our Authorization key as a session JWT and reject it.
|
|
// We authenticate with headers, never cookies.
|
|
response = await fetch(target, {
|
|
credentials: 'omit',
|
|
...options,
|
|
headers,
|
|
});
|
|
} catch (err) {
|
|
// A navigation (or bfcache entry) cancels in-flight requests; that is not
|
|
// a real network failure and must not surface as an error state.
|
|
if (err?.name === 'AbortError') {
|
|
throw new RequestAbortedError(err);
|
|
}
|
|
throw new NetworkError(err);
|
|
}
|
|
|
|
if (!response.ok) {
|
|
throw new HttpError(response.status, ...(await errorDetails(response)));
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
async requestJson(url, options = {}) {
|
|
return (await this.request(url, options)).json();
|
|
}
|
|
|
|
async requestText(url, options = {}) {
|
|
return (await this.request(url, options)).text();
|
|
}
|
|
|
|
async requestBlob(url, options = {}) {
|
|
return (await this.request(url, options)).blob();
|
|
}
|
|
}
|
|
|
|
// Best-effort extraction of an API error body (e.g. Discourse's
|
|
// `{"error_type":"invalid_access"}`) so callers can classify the failure.
|
|
async function errorDetails(response) {
|
|
try {
|
|
const body = await response.json();
|
|
return [body?.error_type ?? null, body];
|
|
} catch {
|
|
return [null, null];
|
|
}
|
|
}
|