import {z} from 'zod' export type DeleteApiPlansPlanIdScheduledTransactionsScheduledTransactionIdArgs = {plan_id: string, scheduled_transaction_id: string}; export type DeleteApiPlansPlanIdScheduledTransactionsScheduledTransactionIdOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionFlagColor = z.enum(["red", "orange", "yellow", "green", "blue", "purple", "", "null"]).nullable(); export const transactionFlagName = z.string().nullable(); export const scheduledSubTransaction = z.object({id: z.string(), scheduled_transaction_id: z.string(), amount: z.number().int(), memo: z.string().nullable().optional(), payee_id: z.string().nullable().optional(), payee_name: z.string().nullable().optional(), category_id: z.string().nullable().optional(), category_name: z.string().nullable().optional(), transfer_account_id: z.string().nullable().optional(), deleted: z.boolean(), amount_formatted: z.string().optional(), amount_currency: z.number().optional()}); export const scheduledTransactionDetail = z.object({id: z.string(), date_first: z.string(), date_next: z.string(), frequency: z.enum(["never", "daily", "weekly", "everyOtherWeek", "twiceAMonth", "every4Weeks", "monthly", "everyOtherMonth", "every3Months", "every4Months", "twiceAYear", "yearly", "everyOtherYear"]), amount: z.number().int(), memo: z.string().nullable().optional(), flag_color: transactionFlagColor.optional(), flag_name: transactionFlagName.optional(), account_id: z.string(), payee_id: z.string().nullable().optional(), category_id: z.string().nullable().optional(), transfer_account_id: z.string().nullable().optional(), deleted: z.boolean(), amount_formatted: z.string().optional(), amount_currency: z.number().optional(), account_name: z.string(), payee_name: z.string().nullable().optional(), category_name: z.string().nullable().optional(), subtransactions: z.array(scheduledSubTransaction)}); export const scheduledTransactionResponse = z.object({data: z.object({scheduled_transaction: scheduledTransactionDetail})}); 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 }; /** * Delete a scheduled transaction * * DELETE /plans/{plan_id}/scheduled_transactions/{scheduled_transaction_id} */ export const deleteApiPlansPlanIdScheduledTransactionsScheduledTransactionId = async (args: DeleteApiPlansPlanIdScheduledTransactionsScheduledTransactionIdArgs, options?: DeleteApiPlansPlanIdScheduledTransactionsScheduledTransactionIdOptions) => { const {baseUrl = "https://api.ynab.com/v1", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/plans/${encodeURIComponent(String(args.plan_id))}/scheduled_transactions/${encodeURIComponent(String(args.scheduled_transaction_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "DELETE", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(scheduledTransactionResponse, await res.json()) };