import {z} from 'zod' export type PatchApiSubscriptionsIdArgs = {id: string, body: {name?: string | undefined, description?: string | undefined, account_id?: string | undefined, merchant_reference?: string | undefined, soft_descriptor?: string | undefined, country?: string | undefined, amount?: {currency: string, value: number} | undefined, frequency?: ({type: 'DAY' | 'WEEK' | 'MONTH', value?: number | undefined, monthly_billing_day?: number | undefined}) | undefined, billing_cycles?: ({total?: number | undefined}) | undefined, customer_payer?: {id: string} | undefined, payment_method?: ({type?: 'CARD' | undefined, vaulted_token?: string | undefined, card?: ({verify?: boolean | undefined, card_data?: ({number?: string | undefined, expiration_month?: number | undefined, expiration_year?: number | undefined, security_code?: number | undefined, holder_name?: string | undefined}) | undefined, store_credentials?: ({usage?: ('FIRST' | 'USED') | undefined}) | undefined}) | undefined, ''?: string | undefined}) | undefined, availability?: ({start_at?: string | undefined, finish_at?: string | undefined}) | undefined, retries?: ({retry_on_decline?: boolean | undefined, amount?: number | undefined}) | undefined, metadata?: ({key?: string | undefined, value?: string | undefined}) | undefined}}; export type PatchApiSubscriptionsIdOptions = {baseUrl?: string | undefined, publicApiKey?: string | undefined, privateSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiSubscriptionsIdResponse = z.object({id: z.string().optional(), name: z.string().optional(), account_id: z.string().optional(), country: z.string().optional(), description: z.string().optional(), merchant_reference: z.string().optional(), soft_descriptor: z.string().optional(), status: z.string().optional(), amount: z.object({currency: z.string().optional(), value: z.number().int().optional()}).optional(), frequency: z.object({type: z.string().optional(), value: z.number().int().optional()}).optional(), billing_cycles: z.object({total: z.number().int().optional(), current: z.number().int().optional(), next_at: z.string().optional()}).optional(), customer_payer: z.object({id: z.string().optional()}).optional(), payment_method: z.object({type: z.string().optional(), vaulted_token: z.string().optional()}).optional(), availability: z.object({start_at: z.string().optional(), finish_at: z.unknown()}).optional(), metadata: z.unknown(), created_at: z.string().optional(), updated_at: z.string().optional()}); 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 Subscription * * PATCH /subscriptions/{id} */ export const patchApiSubscriptionsId = async (args: PatchApiSubscriptionsIdArgs, options?: PatchApiSubscriptionsIdOptions) => { const {baseUrl = "https://api-sandbox.y.uno/v1", fetch: fetchFn = fetch, publicApiKey, privateSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (publicApiKey !== undefined) headers.set("PUBLIC-API-KEY", publicApiKey) if (privateSecretKey !== undefined) headers.set("PRIVATE-SECRET-KEY", privateSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/subscriptions/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiSubscriptionsIdResponse, await res.json()) };