import {z} from 'zod' export type UpdateProductRequest = {config?: ({deliveryMethod?: string | undefined, fulfillmentType?: string | undefined, validityPeriod?: number | undefined}) | undefined, description?: string | undefined, imageUrl?: string | undefined, metadata?: {[key: string]: unknown} | undefined, name?: string | undefined, productType?: string | undefined, taxBehavior?: ('auto' | 'inclusive' | 'exclusive') | undefined}; export type UpdateApiV1SdkProductsProductRefArgs = {productRef: string, body: UpdateProductRequest}; export type UpdateApiV1SdkProductsProductRefOptions = {baseUrl?: string | undefined, SecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const productConfigDto = z.object({deliveryMethod: z.string().optional(), fulfillmentType: z.string().optional(), validityPeriod: z.number().optional()}); export const sdkPlanResponse = z.object({createdAt: z.string(), currency: z.string(), currencySymbol: z.string().optional(), description: z.string().optional(), features: z.record(z.string(), z.unknown()).optional(), isActive: z.boolean(), name: z.string().optional(), options: z.array(z.record(z.string(), z.unknown())), price: z.number().optional(), reference: z.string(), requiresPayment: z.boolean(), status: z.string(), type: z.enum(["recurring", "one-time", "usage-based", "hybrid"]), updatedAt: z.string()}); export const sdkProductResponse = z.object({balance: z.number(), config: productConfigDto.optional(), createdAt: z.string(), description: z.string().optional(), imageUrl: z.string().optional(), isManagedMcp: z.boolean(), isMcpPay: z.boolean(), mcp: z.record(z.string(), z.unknown()).optional(), metadata: z.record(z.string(), z.unknown()).optional(), name: z.string(), plans: z.array(sdkPlanResponse).optional(), productType: z.string().optional(), reference: z.string(), status: z.string(), totalTransactions: z.number(), updatedAt: z.string()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Update a product * * PUT /v1/sdk/products/{productRef} */ export const updateApiV1SdkProductsProductRef = async (args: UpdateApiV1SdkProductsProductRefArgs, options?: UpdateApiV1SdkProductsProductRefOptions) => { const {baseUrl = "https://api.solvapay.com", fetch: fetchFn = fetch, SecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (SecretKey !== undefined) headers.set('authorization', `Bearer ${SecretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/sdk/products/${encodeURIComponent(String(args.productRef))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(sdkProductResponse, await res.json()) };