@simpleapps-com/augur-api
Version:
TypeScript client library for Augur microservices API endpoints
138 lines • 7.52 kB
TypeScript
import { z } from 'zod';
/**
* Flexible Schema Utilities for Handling Inconsistent API Outputs
*
* During microservice API refactoring, many endpoints return inconsistent formats.
* These utilities provide standardized patterns for handling common inconsistencies.
*/
/**
* Creates a schema that accepts both array and object formats
* Common pattern: server sometimes returns [] and sometimes {}
*/
export declare const flexibleArrayOrObject: <T extends z.ZodTypeAny>(itemSchema: T) => z.ZodUnion<[z.ZodArray<T, "many">, z.ZodRecord<z.ZodString, z.ZodUnknown>, z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNever>>]>;
/**
* Creates a schema that accepts both array and record formats for collections
* Handles cases where APIs return either array of items or object with key-value pairs
*/
export declare const flexibleCollection: <T extends z.ZodTypeAny>(itemSchema: T) => z.ZodUnion<[z.ZodArray<T, "many">, z.ZodRecord<z.ZodString, T>, z.ZodArray<z.ZodUnknown, "many">, z.ZodRecord<z.ZodString, z.ZodUnknown>]>;
/**
* Creates a schema for fields that might be string, number, or null
* Common during API migrations when data types are being standardized
*/
export declare const flexibleStringOrNumber: () => z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodNull, z.ZodUndefined]>, string | null, string | number | null | undefined>;
/**
* Creates a schema for boolean fields that might come as string, number, or boolean
* Handles legacy APIs that return "1"/"0", true/false, or "true"/"false"
*/
export declare const flexibleBoolean: () => z.ZodEffects<z.ZodUnion<[z.ZodBoolean, z.ZodString, z.ZodNumber, z.ZodNull, z.ZodUndefined]>, boolean, string | number | boolean | null | undefined>;
/**
* Creates a schema for date fields that might be string, Date, or null
* Handles various date format inconsistencies
*/
export declare const flexibleDate: () => z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodDate, z.ZodNull, z.ZodUndefined]>, string | null, string | Date | null | undefined>;
/**
* Creates a flexible schema for profile/metadata fields that vary between endpoints
* This is the pattern we found with profileValues - sometimes array, sometimes object
*/
export declare const flexibleProfileData: () => z.ZodOptional<z.ZodUnion<[z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>, z.ZodArray<z.ZodUnknown, "many">, z.ZodNull, z.ZodUndefined]>>;
/**
* Creates a flexible schema that gracefully handles unexpected extra fields
* Uses passthrough to allow additional properties during API evolution
*/
export declare const flexibleObject: <T extends z.ZodRawShape>(shape: T) => z.ZodObject<T, "passthrough", z.ZodTypeAny, z.objectOutputType<T, z.ZodTypeAny, "passthrough">, z.objectInputType<T, z.ZodTypeAny, "passthrough">>;
/**
* Creates a schema that handles nested data that might be normalized or denormalized
* Common when APIs are being refactored from flat to nested structures
*/
export declare const flexibleNested: <T extends z.ZodTypeAny>(simpleSchema: T, complexSchema: T) => z.ZodUnion<[T, T]>;
/**
* Pre-built schema for common metadata fields that are inconsistent during refactoring
* Handles count normalization and bidirectional sync between total and totalResults
*/
export declare const flexibleMetadataFields: z.ZodEffects<z.ZodObject<{
count: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
total: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
totalResults: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
}, "strip", z.ZodTypeAny, {
count: number;
total: number;
totalResults: number;
}, {
count?: number | undefined;
total?: number | undefined;
totalResults?: number | undefined;
}>, {
count: number;
total: number;
totalResults: number;
}, {
count?: number | undefined;
total?: number | undefined;
totalResults?: number | undefined;
}>;
/**
* Utility for wrapping existing schemas to be more flexible during API refactoring
* Adds .catch() to provide fallback values instead of throwing validation errors
*/
export declare const makeFlexible: <T extends z.ZodTypeAny>(schema: T, fallbackValue: z.infer<T>) => z.ZodCatch<T>;
/**
* Common patterns for user-related data that varies across services
*/
export declare const flexibleUserFields: {
profileValues: z.ZodOptional<z.ZodUnion<[z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>, z.ZodArray<z.ZodUnknown, "many">, z.ZodNull, z.ZodUndefined]>>;
customFields: z.ZodUnion<[z.ZodArray<z.ZodUnknown, "many">, z.ZodRecord<z.ZodString, z.ZodUnknown>, z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNever>>]>;
permissions: z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodRecord<z.ZodString, z.ZodString>, z.ZodArray<z.ZodUnknown, "many">, z.ZodRecord<z.ZodString, z.ZodUnknown>]>;
groups: z.ZodUnion<[z.ZodArray<z.ZodObject<{
id: z.ZodNumber;
title: z.ZodString;
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
id: z.ZodNumber;
title: z.ZodString;
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
id: z.ZodNumber;
title: z.ZodString;
}, z.ZodTypeAny, "passthrough">>, "many">, z.ZodRecord<z.ZodString, z.ZodUnknown>, z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNever>>]>;
};
/**
* Common patterns for product/item data that varies across services
*/
export declare const flexibleProductFields: {
attributes: z.ZodUnion<[z.ZodArray<z.ZodObject<{
attributeId: z.ZodString;
value: z.ZodString;
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
attributeId: z.ZodString;
value: z.ZodString;
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
attributeId: z.ZodString;
value: z.ZodString;
}, z.ZodTypeAny, "passthrough">>, "many">, z.ZodRecord<z.ZodString, z.ZodObject<{
attributeId: z.ZodString;
value: z.ZodString;
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
attributeId: z.ZodString;
value: z.ZodString;
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
attributeId: z.ZodString;
value: z.ZodString;
}, z.ZodTypeAny, "passthrough">>>, z.ZodArray<z.ZodUnknown, "many">, z.ZodRecord<z.ZodString, z.ZodUnknown>]>;
categories: z.ZodUnion<[z.ZodArray<z.ZodObject<{
categoryUid: z.ZodNumber;
categoryDesc: z.ZodString;
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
categoryUid: z.ZodNumber;
categoryDesc: z.ZodString;
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
categoryUid: z.ZodNumber;
categoryDesc: z.ZodString;
}, z.ZodTypeAny, "passthrough">>, "many">, z.ZodRecord<z.ZodString, z.ZodUnknown>, z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNever>>]>;
metadata: z.ZodOptional<z.ZodUnion<[z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>, z.ZodArray<z.ZodUnknown, "many">, z.ZodNull, z.ZodUndefined]>>;
customData: z.ZodUnion<[z.ZodArray<z.ZodUnknown, "many">, z.ZodRecord<z.ZodString, z.ZodUnknown>, z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNever>>]>;
};
/**
* Export type utilities for TypeScript inference
*/
export type FlexibleArray<T> = T[] | Record<string, unknown> | Record<string, never>;
export type FlexibleCollection<T> = T[] | Record<string, T> | unknown[] | Record<string, unknown>;
export type FlexibleProfileData = Record<string, string | string[]> | unknown[] | null | undefined;
//# sourceMappingURL=flexible-schemas.d.ts.map