spaps
Version:
Sweet Potato Authentication & Payment Service CLI - Docker Compose orchestrator for local Python/FastAPI SPAPS server with built-in admin middleware
75 lines (65 loc) • 1.85 kB
JavaScript
function normalizeServerUrl(serverUrl) {
return String(serverUrl || '').trim().replace(/\/+$/, '');
}
function buildApiUrl(serverUrl, path) {
const normalizedPath = path.startsWith('/api/')
? path
: `/api${path.startsWith('/') ? path : `/${path}`}`;
return `${normalizeServerUrl(serverUrl)}${normalizedPath}`;
}
function unwrapApiData(payload) {
if (
payload &&
typeof payload === 'object' &&
Object.prototype.hasOwnProperty.call(payload, 'success')
) {
if (payload.success === true && Object.prototype.hasOwnProperty.call(payload, 'data')) {
return payload.data;
}
if (payload.success === false && Object.prototype.hasOwnProperty.call(payload, 'error')) {
return payload.error;
}
}
return payload;
}
function extractApiError(payload, status) {
const source = unwrapApiData(payload);
if (source && typeof source === 'object') {
if (typeof source.code === 'string' || typeof source.message === 'string') {
return {
code: source.code || `HTTP_${status}`,
message: source.message || `Request failed with status ${status}`,
};
}
if (
typeof source.error === 'string' ||
typeof source.error_description === 'string' ||
typeof source.detail === 'string'
) {
return {
code: source.error || `HTTP_${status}`,
message:
source.error_description ||
source.detail ||
source.message ||
`Request failed with status ${status}`,
};
}
}
if (typeof source === 'string' && source.trim()) {
return {
code: `HTTP_${status}`,
message: source,
};
}
return {
code: `HTTP_${status}`,
message: `Request failed with status ${status}`,
};
}
module.exports = {
buildApiUrl,
extractApiError,
normalizeServerUrl,
unwrapApiData,
};