Last round of prettier: npx prettier --write src/
This commit is contained in:
@@ -41,7 +41,9 @@ export const post_object = async function post_object({
|
||||
retry_count?: number;
|
||||
}) {
|
||||
if (log_lvl) {
|
||||
console.log(`*** post_object() *** Endpoint: ${endpoint} Task ID: ${task_id}`);
|
||||
console.log(
|
||||
`*** post_object() *** Endpoint: ${endpoint} Task ID: ${task_id}`
|
||||
);
|
||||
console.log('Params:', params);
|
||||
if (log_lvl > 1) {
|
||||
console.log('Data:', data);
|
||||
@@ -65,7 +67,9 @@ export const post_object = async function post_object({
|
||||
// Construct the URL with query parameters
|
||||
const url = new URL(endpoint, api_cfg['base_url']);
|
||||
if (params) {
|
||||
Object.keys(params).forEach((key) => url.searchParams.append(key, params[key]));
|
||||
Object.keys(params).forEach((key) =>
|
||||
url.searchParams.append(key, params[key])
|
||||
);
|
||||
}
|
||||
|
||||
// Clean and merge headers
|
||||
@@ -95,14 +99,19 @@ export const post_object = async function post_object({
|
||||
}
|
||||
|
||||
// Handle "Bootstrap Paradox" for unauthenticated requests
|
||||
const bypass_val = merged_headers['x-no-account-id'] || merged_headers['x_no_account_id'];
|
||||
const is_valid_bypass = bypass_val === 'bypass' ||
|
||||
bypass_val === 'Nothing to See Here' ||
|
||||
params['key'] ||
|
||||
bypass_val === 'direct-download';
|
||||
const bypass_val =
|
||||
merged_headers['x-no-account-id'] || merged_headers['x_no_account_id'];
|
||||
const is_valid_bypass =
|
||||
bypass_val === 'bypass' ||
|
||||
bypass_val === 'Nothing to See Here' ||
|
||||
params['key'] ||
|
||||
bypass_val === 'direct-download';
|
||||
|
||||
if (is_valid_bypass) {
|
||||
if (log_lvl > 1) console.log('api_post_object: Valid bypass detected. Stripping account ID context.');
|
||||
if (log_lvl > 1)
|
||||
console.log(
|
||||
'api_post_object: Valid bypass detected. Stripping account ID context.'
|
||||
);
|
||||
delete merged_headers['x-account-id'];
|
||||
delete merged_headers['x_account_id'];
|
||||
} else {
|
||||
@@ -124,11 +133,12 @@ export const post_object = async function post_object({
|
||||
}
|
||||
|
||||
// Auto-inject Authorization header if JWT is present but header is missing
|
||||
let jwt = headers_cleaned['jwt'] ||
|
||||
headers_cleaned['JWT'] ||
|
||||
api_cfg['jwt'] ||
|
||||
api_cfg['headers']?.['jwt'] ||
|
||||
api_cfg['headers']?.['JWT'];
|
||||
let jwt =
|
||||
headers_cleaned['jwt'] ||
|
||||
headers_cleaned['JWT'] ||
|
||||
api_cfg['jwt'] ||
|
||||
api_cfg['headers']?.['jwt'] ||
|
||||
api_cfg['headers']?.['JWT'];
|
||||
|
||||
// Final Fallback: Direct check of primary ae_loc key
|
||||
if (!jwt && typeof localStorage !== 'undefined') {
|
||||
@@ -143,7 +153,11 @@ export const post_object = async function post_object({
|
||||
}
|
||||
}
|
||||
|
||||
if (jwt && !headers_cleaned['Authorization'] && !headers_cleaned['authorization']) {
|
||||
if (
|
||||
jwt &&
|
||||
!headers_cleaned['Authorization'] &&
|
||||
!headers_cleaned['authorization']
|
||||
) {
|
||||
headers_cleaned['Authorization'] = `Bearer ${jwt}`;
|
||||
}
|
||||
|
||||
@@ -186,13 +200,21 @@ export const post_object = async function post_object({
|
||||
console.log('Fetch Options:', fetchOptions);
|
||||
}
|
||||
|
||||
const response = await fetch_method(url.toString(), fetchOptions).catch(function (
|
||||
error: any
|
||||
) {
|
||||
const response = await fetch_method(
|
||||
url.toString(),
|
||||
fetchOptions
|
||||
).catch(function (error: any) {
|
||||
// SILENCE NOISE: Aborted requests shouldn't spam logs at log_lvl 0
|
||||
if (error.name === 'AbortError' || error.message?.includes('aborted') || error.name === 'TypeError') {
|
||||
if (
|
||||
error.name === 'AbortError' ||
|
||||
error.message?.includes('aborted') ||
|
||||
error.name === 'TypeError'
|
||||
) {
|
||||
if (log_lvl > 1) {
|
||||
console.log('API POST: Request was aborted or terminated by browser. Expected during navigation.', error);
|
||||
console.log(
|
||||
'API POST: Request was aborted or terminated by browser. Expected during navigation.',
|
||||
error
|
||||
);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
@@ -206,9 +228,17 @@ export const post_object = async function post_object({
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
// Check if we should stop due to abort or network failure
|
||||
if (response instanceof Error || (response && (response.name === 'TypeError' || response.name === 'AbortError'))) {
|
||||
if (
|
||||
response instanceof Error ||
|
||||
(response &&
|
||||
(response.name === 'TypeError' ||
|
||||
response.name === 'AbortError'))
|
||||
) {
|
||||
if (response.name === 'AbortError') return false;
|
||||
if (log_lvl > 1) console.log('API POST Object: Detected NetworkError or TypeError. Failing fast.');
|
||||
if (log_lvl > 1)
|
||||
console.log(
|
||||
'API POST Object: Detected NetworkError or TypeError. Failing fast.'
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -219,30 +249,53 @@ export const post_object = async function post_object({
|
||||
}
|
||||
|
||||
if (log_lvl) {
|
||||
console.log(`Response: status=${response.status} attempt=${attempt}`);
|
||||
console.log(
|
||||
`Response: status=${response.status} attempt=${attempt}`
|
||||
);
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 404) {
|
||||
if (log_lvl) {
|
||||
console.log('The response was a 404 not found "error". Returning null.');
|
||||
console.log(
|
||||
'The response was a 404 not found "error". Returning null.'
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// FAIL FAST (Section 2D): Do not retry on Auth or Client errors (400, 401, 403, 422)
|
||||
if (response.status === 400 || response.status === 401 || response.status === 403 || response.status === 422) {
|
||||
if (log_lvl) console.error(`API Client Failure (${response.status}). Failing fast.`);
|
||||
if (
|
||||
response.status === 400 ||
|
||||
response.status === 401 ||
|
||||
response.status === 403 ||
|
||||
response.status === 422
|
||||
) {
|
||||
if (log_lvl)
|
||||
console.error(
|
||||
`API Client Failure (${response.status}). Failing fast.`
|
||||
);
|
||||
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
console.warn(`AUTH DIAGNOSTICS (POST): Headers sent for ${endpoint}:`, {
|
||||
has_auth: !!headers_cleaned['Authorization'],
|
||||
has_api_key: !!headers_cleaned['x-aether-api-key'],
|
||||
has_account_id: !!headers_cleaned['x-account-id'],
|
||||
jwt_preview: jwt ? `${jwt.slice(0, 8)}...` : 'MISSING'
|
||||
});
|
||||
console.warn(
|
||||
`AUTH DIAGNOSTICS (POST): Headers sent for ${endpoint}:`,
|
||||
{
|
||||
has_auth: !!headers_cleaned['Authorization'],
|
||||
has_api_key:
|
||||
!!headers_cleaned['x-aether-api-key'],
|
||||
has_account_id:
|
||||
!!headers_cleaned['x-account-id'],
|
||||
jwt_preview: jwt
|
||||
? `${jwt.slice(0, 8)}...`
|
||||
: 'MISSING'
|
||||
}
|
||||
);
|
||||
// Signal the root layout to show the session-expired banner.
|
||||
if (browser) ae_auth_error.set({ type: 'expired', ts: Date.now() });
|
||||
if (browser)
|
||||
ae_auth_error.set({
|
||||
type: 'expired',
|
||||
ts: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
// Structured Error Handling (V3): Attempt to get rich error metadata
|
||||
@@ -253,7 +306,11 @@ export const post_object = async function post_object({
|
||||
// Not JSON
|
||||
}
|
||||
|
||||
if (log_lvl) console.log('The response was not ok. Structured Error Check:', error_json);
|
||||
if (log_lvl)
|
||||
console.log(
|
||||
'The response was not ok. Structured Error Check:',
|
||||
error_json
|
||||
);
|
||||
|
||||
if (error_json?.meta?.details) {
|
||||
return error_json;
|
||||
@@ -267,7 +324,10 @@ export const post_object = async function post_object({
|
||||
status_code: response.status,
|
||||
details: {
|
||||
category: 'validation',
|
||||
message: typeof error_json.detail === 'string' ? error_json.detail : JSON.stringify(error_json.detail),
|
||||
message:
|
||||
typeof error_json.detail === 'string'
|
||||
? error_json.detail
|
||||
: JSON.stringify(error_json.detail),
|
||||
raw: error_json.detail
|
||||
}
|
||||
}
|
||||
@@ -311,7 +371,11 @@ export const post_object = async function post_object({
|
||||
|
||||
// Return the response data or metadata
|
||||
// Robustly handle V3 response envelopes
|
||||
return return_meta ? json : (json.data !== undefined ? json.data : json);
|
||||
return return_meta
|
||||
? json
|
||||
: json.data !== undefined
|
||||
? json.data
|
||||
: json;
|
||||
} else {
|
||||
const blob = await response.blob();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user