/* * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT. */ import { output, ZodEffects, ZodError, ZodObject, ZodRawShape, ZodTypeAny, } from "zod/v3"; import { SDKValidationError } from "../models/errors/sdkvalidationerror.js"; import { ERR, OK, Result } from "../types/fp.js"; export function lazySchema(factory: () => T): T { let schema: T | undefined; const getSchema = (): T => { if (schema === undefined) { schema = factory(); } return schema; }; const target = Object.create(null); return new Proxy(target, { get(_target, property) { const descriptor = Reflect.getOwnPropertyDescriptor(target, property); if ( descriptor && !descriptor.configurable && "value" in descriptor && !descriptor.writable ) { return descriptor.value; } const value = Reflect.get(getSchema(), property, getSchema()); return property !== "constructor" && typeof value === "function" ? value.bind(getSchema()) : value; }, getOwnPropertyDescriptor(_target, property) { const descriptor = Reflect.getOwnPropertyDescriptor( getSchema(), property, ); return descriptor && { ...descriptor, configurable: Reflect.getOwnPropertyDescriptor(target, property)?.configurable ?? true, }; }, getPrototypeOf() { return Reflect.getPrototypeOf(getSchema()); }, has(_target, property) { return Reflect.has(getSchema(), property); }, ownKeys() { return Reflect.ownKeys(getSchema()); }, defineProperty(_target, property, descriptor) { if (!Reflect.defineProperty(getSchema(), property, descriptor)) { return false; } const defined = Reflect.getOwnPropertyDescriptor(getSchema(), property)!; if (!defined.configurable || !Reflect.isExtensible(target)) { return Reflect.defineProperty(target, property, defined); } return true; }, deleteProperty(_target, property) { if (!Reflect.deleteProperty(getSchema(), property)) { return false; } return Reflect.deleteProperty(target, property); }, preventExtensions() { const concrete = getSchema(); if (!Reflect.preventExtensions(concrete)) { return false; } Reflect.setPrototypeOf(target, Reflect.getPrototypeOf(concrete)); for (const property of Reflect.ownKeys(concrete)) { Reflect.defineProperty( target, property, Reflect.getOwnPropertyDescriptor(concrete, property)!, ); } return Reflect.preventExtensions(target); }, set(_target, property, value) { return Reflect.set(getSchema(), property, value, getSchema()); }, }); } /** * Utility function that executes some code which may throw a ZodError. It * intercepts this error and converts it to an SDKValidationError so as to not * leak Zod implementation details to user code. */ export function parse( rawValue: Inp, fn: (value: Inp) => Out, errorMessage: string, ): Out { try { return fn(rawValue); } catch (err) { if (err instanceof ZodError) { throw new SDKValidationError(errorMessage, err, rawValue); } throw err; } } /** * Utility function that executes some code which may result in a ZodError. It * intercepts this error and converts it to an SDKValidationError so as to not * leak Zod implementation details to user code. */ export function safeParse( rawValue: Inp, fn: (value: Inp) => Out, errorMessage: string, ): Result { try { return OK(fn(rawValue)); } catch (err) { return ERR(new SDKValidationError(errorMessage, err, rawValue)); } } export function collectExtraKeys< Shape extends ZodRawShape, Catchall extends ZodTypeAny, K extends string, >( obj: ZodObject, extrasKey: K, optional: boolean, ): ZodEffects< typeof obj, & output> & { [k in K]: Record>; } > { return obj.transform((val) => { const extras: Record> = {}; const { shape } = obj; for (const [key] of Object.entries(val)) { if (key in shape) { continue; } const v = val[key]; if (typeof v === "undefined") { continue; } extras[key] = v; delete val[key]; } if (optional && Object.keys(extras).length === 0) { return val; } return { ...val, [extrasKey]: extras }; }); }