UNPKG

studiocms

Version:

Astro Native CMS for AstroDB. Built from the ground up by the Astro community.

92 lines (91 loc) 2.37 kB
import { z } from "astro/zod"; import { defaultCacheLifeTime } from "../../consts.js"; const TimeUnitSchema = z.union([z.literal("m"), z.literal("h")]); const TimeStringSchema = z.string().regex(/^\d+(m|h)$/, { message: "Invalid time string format. Must be a number followed by 'm' or 'h'." }).transform((value) => { const timeUnits = { m: 60 * 1e3, // Minutes to milliseconds h: 60 * 60 * 1e3 // Hours to milliseconds }; const match = value.match(/^(\d+)([mh])$/); if (!match) { throw new Error("Invalid time format. Use values like '5m', '1h', etc."); } const val = Number.parseInt(match[1], 10); const unit = match[2]; return val * timeUnits[unit]; }); const CacheConfigSchema = z.object({ /** * Cache Lifetime * * `{number}{unit}` - e.g. '5m' for 5 minutes or '1h' for 1 hour * @default '5m' */ lifetime: TimeStringSchema.optional().default(defaultCacheLifeTime) }); const ProcessedCacheConfigSchema = z.object({ /** * Cache Enabled * * @default true */ enabled: z.boolean().default(true), /** * Cache Lifetime * * `{number}{unit}` - e.g. '5m' for 5 minutes or '1h' for 1 hour * @default '5m' */ lifetime: TimeStringSchema.default(defaultCacheLifeTime) }); const SDKCacheSchema = z.union([z.boolean(), CacheConfigSchema]).optional().default(true).transform((cacheConfig) => { if (typeof cacheConfig === "boolean") { return { enabled: cacheConfig, lifetime: TimeStringSchema.parse(defaultCacheLifeTime) }; } return { enabled: true, lifetime: cacheConfig.lifetime }; }); const ProcessedSDKSchema = z.object({ /** * Cache Configuration * * @default cacheConfig: { lifetime: '5m' } */ cacheConfig: ProcessedCacheConfigSchema }); const SDKSchema = z.union([ z.boolean(), z.object({ /** * Cache Configuration * * @default cacheConfig: { lifetime: '5m' } */ cacheConfig: SDKCacheSchema }) ]).optional().default(true).transform((sdkConfig) => { if (typeof sdkConfig === "boolean") { return { cacheConfig: { enabled: sdkConfig, lifetime: TimeStringSchema.parse(defaultCacheLifeTime) } }; } return sdkConfig; }); export { CacheConfigSchema, ProcessedCacheConfigSchema, ProcessedSDKSchema, SDKCacheSchema, SDKSchema, TimeStringSchema, TimeUnitSchema };