import {z} from 'zod' export type UpdatePlanDto = {name?: string | undefined, description?: string | undefined, billingInterval?: ('HOURLY' | 'DAILY' | 'WEEKLY' | 'MONTHLY' | 'QUARTERLY' | 'YEARLY') | undefined, billingTiming?: ('IN_ADVANCE' | 'IN_ARREARS') | undefined, features?: Array | undefined, isActive?: boolean | undefined, netPaymentTerms?: number | undefined, invoiceGracePeriodDays?: number | undefined, progressiveBillingThreshold?: number | undefined}; export type PatchApiApiPlansIdArgs = {id: string, body: UpdatePlanDto}; export type PatchApiApiPlansIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const planPriceResponse = z.object({id: z.string(), planId: z.string(), currency: z.string(), amount: z.string(), isActive: z.boolean(), createdAt: z.string(), updatedAt: z.string()}); export const planResponse = z.object({id: z.string(), name: z.string(), code: z.string(), description: z.string().optional(), billingInterval: z.enum(["HOURLY", "DAILY", "WEEKLY", "MONTHLY", "QUARTERLY", "YEARLY"]), features: z.array(z.string()).optional(), isActive: z.boolean(), billingTiming: z.enum(["IN_ADVANCE", "IN_ARREARS"]), minimumCommitment: z.string().optional(), prices: z.array(planPriceResponse), createdAt: z.string(), 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 plan * * PATCH /api/plans/{id} */ export const patchApiApiPlansId = async (args: PatchApiApiPlansIdArgs, options?: PatchApiApiPlansIdOptions) => { const {baseUrl = "https://api.novabilling.one", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/plans/${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(planResponse, await res.json()) };