@simpleapps-com/augur-api
Version:
TypeScript client library for Augur microservices API endpoints
92 lines • 4.06 kB
JavaScript
import { z } from 'zod';
// Helper to log normalization warnings in development
const logNormalizationWarning = (message) => {
if (process.env.NODE_ENV !== 'production') {
console.warn(`[augur-api] Response normalization: ${message}`);
}
};
// Helper to normalize count field
const normalizeCount = (count, actualDataLength) => {
if (count === 0 && actualDataLength > 0) {
logNormalizationWarning(`'count' was 0 but data has ${actualDataLength} items`);
return actualDataLength;
}
return count;
};
// Helper to normalize total/totalResults bidirectional sync
const normalizeTotals = (total, totalResults) => {
if (total === 0 && totalResults > 0) {
logNormalizationWarning(`'total' was 0 but 'totalResults' is ${totalResults}`);
return { total: totalResults, totalResults };
}
if (totalResults === 0 && total > 0) {
logNormalizationWarning(`'totalResults' was 0 but 'total' is ${total}`);
return { total, totalResults: total };
}
return { total, totalResults };
};
export const BaseResponseSchema = (dataSchema) => z
.object({
count: z.number(), // Required - Number of items in current response (NOTE: may be inconsistent during API refactoring)
data: dataSchema, // Required - The actual response data
message: z.string(), // Required - Response message
// oneOf format to handle PHP JSON encoding quirk: empty arrays become objects {}
options: z.union([
z.array(z.unknown()), // Preferred new format - always arrays
z.record(z.unknown()), // Legacy PHP format - empty arrays become objects
]), // Required - Additional options (supports both array and object formats during transition)
params: z.union([
z.array(z.unknown()), // Preferred new format - always arrays
z.record(z.unknown()), // Legacy PHP format - empty arrays become objects
]), // Required - Parameters used in request (supports both array and object formats during transition)
status: z.number(), // Required - HTTP status code
total: z.number(), // Required - Synced bidirectionally with totalResults
totalResults: z.number(), // Required - Synced bidirectionally with total
})
.transform(data => {
// During API refactoring, metadata fields may be inconsistent
// We normalize them based on the actual data and bidirectional sync
if (!Array.isArray(data.data)) {
return data;
}
const count = normalizeCount(data.count, data.data.length);
const { total, totalResults } = normalizeTotals(data.total, data.totalResults);
return { ...data, count, total, totalResults };
});
// Standard health check data schema - same across all microservices
export const HealthCheckDataSchema = z.object({
siteHash: z.string(),
siteId: z.string(),
});
// Standard ping data schema - same across all microservices
export const PingDataSchema = z.literal('pong');
// Common pagination parameters
// Note: HTTP query parameters are always strings, so we use coercion for numbers
export const PaginationParamsSchema = z.object({
limit: z.coerce.number().optional(),
offset: z.coerce.number().optional(),
});
// Edge cache duration parameter - supported values for Cloudflare cache rules
// Note: HTTP query parameters are always strings, but we also accept numbers for JavaScript usage
export const EdgeCacheParamsSchema = z.object({
edgeCache: z
.union([
z.literal(1),
z.literal(2),
z.literal(3),
z.literal(4),
z.literal(5),
z.literal(8), // numbers
z.literal('1'),
z.literal('2'),
z.literal('3'),
z.literal('4'),
z.literal('5'),
z.literal('8'), // strings
])
.transform(val => (typeof val === 'string' ? parseInt(val) : val))
.optional(),
});
// Combined base parameters for GET requests (includes pagination and caching)
export const BaseGetParamsSchema = PaginationParamsSchema.merge(EdgeCacheParamsSchema);
//# sourceMappingURL=schemas.js.map