import type { Stream } from 'stream'; import { z } from 'zod'; export const EntityMetadata = z.object({ createdAt: z.string(), createdBy: z.string().optional(), deletedAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional() }); export const Pagination = z.object({ page: z.number().optional(), perPage: z.number().optional(), //count: z.number(), // what is this? sort: z.string().optional() }); export type Pagination = z.infer; export const Caching = z.object({ cacheScope: z.string().optional(), cacheMaxAge: z.number().optional() }); export type Caching = z.infer; export const Metadata = z.record(z.string(), z.union([z.string(), z.number()])); export type Metadata = z.infer; export const NestedMetadata = z.record( z.union([ z.string(), z.number(), z.object({}).catchall(z.unknown()) // This allows for any object shape with any values ]) ); export type NestedMetadata = z.infer; export const ConfigurationBase = z.union([ z.string(), z.number(), z.boolean(), z.null(), z.object({ r: z.number(), g: z.number(), b: z.number() }) ]); export type ConfigurationBase = z.infer; export const Configuration: z.ZodType = z.record( z.string(), z.union([ z.string(), z.number(), z.boolean(), z.null(), z.object({ r: z.number(), g: z.number(), b: z.number() }), z.lazy(() => z.object({ assetId: z.union([z.string(), z.literal('')]), configuration: z.union([Configuration, z.string()]).optional(), type: z.string().optional() }) ), z.lazy(() => z.array( z.object({ assetId: z.union([z.string(), z.literal('')]), configuration: z.union([Configuration, z.string()]).optional(), type: z.string().optional() }) ) ) ]) ); // can not infer from configurationScheme since it is recursive export type Configuration = { [attributeName: string]: | z.infer | { type?: string; assetId: string; configuration?: Configuration | string; } | { type?: string; assetId: string; configuration?: Configuration | string; }[]; }; export const VariantConfiguration = z.lazy(() => z.array( z.object({ attribute: z.object({ id: z.string() }), option: z.object({ id: z.string() }) }) ) ); export type VariantConfiguration = z.infer; export type RequestResponseType = 'arraybuffer' | 'stream' | 'blob' | 'text'; export type RequestResponseReturnType = T extends 'arraybuffer' ? Buffer : T extends 'stream' ? Stream : T extends 'text' ? string : Blob; export const getDefaultResponseType = (): T => (typeof window === 'undefined' ? 'stream' : 'blob') as T; export const threekitHostToEnv = (host: string): string => { const environment = host.replace('.threekit.com', ''); if (environment === 'admin-fts') return 'fts'; return environment; }; export const threekitEnvToHost = (env: string): string => { const subdomain = env === 'fts' ? 'admin-fts' : env; const topLevelDomain = ['preview', 'fts', 'admin'].includes(env) ? 'com' : 'dev'; return `${subdomain}.threekit.${topLevelDomain}`; };