import {z} from 'zod' export type GetApiApiV1SubscriptionsArgs = {customer?: number | undefined, page?: number | undefined, plan?: number | undefined, record_id?: string | undefined, reference?: string | undefined, status?: ('ACTIVE' | 'CANCELED' | 'COMPLETE' | 'FAILED' | 'PENDING') | undefined, updated_at_after?: string | undefined, updated_at_before?: string | undefined}; export type GetApiApiV1SubscriptionsOptions = {baseUrl?: string | undefined, IntaSendBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const customerSer = z.object({customer_id: z.string(), email: z.string().max(254), first_name: z.string(), last_name: z.string(), reference: z.string().max(45).nullable().optional(), address: z.string().max(50).nullable().optional(), city: z.string().max(50).nullable().optional(), state: z.string().max(50).nullable().optional(), zipcode: z.string().max(10).nullable().optional(), country: z.union([z.enum(["KE", "GH", "NG", "UG", "TZ", "CM", "BF", "CI"]), z.literal(""), z.unknown()]).nullable().optional(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); export const frequencyUnitEnum = z.enum(["D", "W", "M", "Y"]); export const currencyEnum = z.enum(["KES", "GHS", "NGN", "UGX", "TZS", "XAF", "XOF"]); export const planSer = z.object({name: z.string().max(32), plan_id: z.string(), frequency: z.number().int().gte(0).lte(2147483647), frequency_unit: frequencyUnitEnum.optional(), billing_cycles: z.number().int().gte(0).lte(2147483647).optional(), currency: currencyEnum, amount: z.string(), reference: z.string().max(45).nullable().optional(), plan_url: z.string(), redirect_url: z.string().max(400).nullable().optional(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); export const paymentMethodEnum = z.literal("CARD-PAYMENT"); export const cardTypeEnum = z.enum(["Visa", "MasterCard", "AMEX", "Discover", "Unknown"]); export const blankEnum = z.literal(""); export const nullEnum = z.unknown(); export const status3b8Enum = z.enum(["PENDING", "ACTIVE", "CANCELED", "COMPLETE", "FAILED"]); export const subscriptionSer = z.object({subscription_id: z.string(), reference: z.string().max(45).nullable().optional(), customer: customerSer, plan: planSer, payment_method: paymentMethodEnum.optional(), card_mask: z.string().nullable(), card_type: z.union([cardTypeEnum, blankEnum, nullEnum]).nullable().optional(), card_expiry: z.string().nullable(), status: status3b8Enum.optional(), start_date: z.string(), next_date: z.string().nullable().optional(), completed_cycles: z.number().int().gte(0).lte(2147483647).optional(), fail_reason: z.string().nullable().optional(), setup_url: z.string(), redirect_url: z.string().max(400).nullable().optional(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); export const paginatedSubscriptionSerList = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(subscriptionSer)}); 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 }; /** * List Subscriptions * * GET /api/v1/subscriptions/ */ export const getApiApiV1Subscriptions = async (args: GetApiApiV1SubscriptionsArgs, options?: GetApiApiV1SubscriptionsOptions) => { const {baseUrl = "https://api.intasend.com", fetch: fetchFn = fetch, IntaSendBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.customer != null) query.append("customer", String(args.customer)) if (args.page != null) query.append("page", String(args.page)) if (args.plan != null) query.append("plan", String(args.plan)) if (args.record_id != null) query.append("record_id", String(args.record_id)) if (args.reference != null) query.append("reference", String(args.reference)) if (args.status != null) query.append("status", String(args.status)) if (args.updated_at_after != null) query.append("updated_at_after", String(args.updated_at_after)) if (args.updated_at_before != null) query.append("updated_at_before", String(args.updated_at_before)) if (IntaSendBearerAuth !== undefined) headers.set('authorization', `Bearer ${IntaSendBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/subscriptions/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paginatedSubscriptionSerList, await res.json()) };