import {z} from 'zod' export type GetApiV1SubscriptionsArgs = {page: string, limit: string, sort: 'ASC' | 'DESC', reference_id?: string | undefined}; export type GetApiV1SubscriptionsOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const edubankSubscriptionCharge = z.object({start_date: z.string(), frequency: z.enum(["DAILY", "MONTHLY"]), duration: z.number(), amount: z.number()}); export const edubankSubscriptionPermission = z.object({authorized: z.boolean(), authorized_in: z.string(), card_id: z.string()}); export const edubankSubscriptionReferences = z.object({due_date: z.string(), reference_id: z.string()}); export const edubankSubscription = z.object({created_at: z.string().optional(), updated_at: z.string().optional(), seller_id: z.string(), subscription_id: z.string(), description: z.string(), reference_id: z.string(), buyer_id: z.string(), status: z.enum(["PROPOSAL", "ACTIVE", "EXPIRED", "CANCELED", "SUSPENDED", "REACTIVATED"]), expires_in: z.string(), charge: edubankSubscriptionCharge, notification_url: z.string(), subscription_permission: edubankSubscriptionPermission, references: z.array(edubankSubscriptionReferences).optional()}); export const getAllSubscriptionsDto = z.object({page: z.number(), limit: z.number(), sort: z.enum(["ASC", "DESC"]), last_page: z.number(), max_items: z.number(), items: z.array(edubankSubscription)}); 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 }; /** * Gets all seller subscriptions * * GET /v1/subscriptions * * Also accepts auth: authorization */ export const getApiV1Subscriptions = async (args: GetApiV1SubscriptionsArgs, options: GetApiV1SubscriptionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.reference_id != null) query.append("reference_id", String(args.reference_id)) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/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(getAllSubscriptionsDto, await res.json()) };