import {z} from 'zod' export type GetApiMyeduzzV1SubscriptionsArgs = {page?: number | undefined, itemsPerPage?: number | undefined, startDate?: string | undefined, endDate?: string | undefined, filterBy?: ('creation' | 'update') | undefined}; export type GetApiMyeduzzV1SubscriptionsOptions = {baseUrl?: string | undefined, eduzzAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiMyeduzzV1SubscriptionsResponse = z.object({id: z.string(), createdAt: z.string(), updatedAt: z.string(), status: z.enum(["upToDate", "awaitingPayment", "late", "canceled", "defaulter", "suspended", "trial", "finished", "free"]), firstInvoiceId: z.string(), isFinite: z.boolean(), isUnlimitedInstallments: z.boolean(), hasNegotiation: z.boolean(), interruption: z.object({createdAt: z.string(), type: z.enum(["cancel", "suspend"]), removeAccessDate: z.string(), reason: z.string(), origin: z.string(), previousStatus: z.enum(["upToDate", "awaitingPayment", "late", "canceled", "defaulter", "suspended", "trial", "finished", "free"]), responsible: z.string()}), recurrence: z.object({startsAt: z.string(), nextDueDate: z.string(), currentDueDate: z.string(), finalDueDate: z.string(), finishesAt: z.string(), frequency: z.object({type: z.enum(["daily", "weekly", "monthly", "yearly"]), value: z.number()})}), charges: z.object({total: z.number(), current: z.number(), paid: z.number(), pending: z.number(), negotiated: z.number()}), products: z.array(z.object({id: z.string(), title: z.string(), hasMembershipFee: z.boolean(), membershipFee: z.object({currency: z.string(), value: z.number()}), deliveries: z.array(z.string())})), payment: z.object({price: z.object({currency: z.string(), value: z.number()}), method: z.enum(["bankslip", "creditcard", "pix", "unknown"]), installments: z.number()}), client: z.object({name: z.string(), email: z.string(), phone: z.object({countryCode: z.string(), areaCode: z.string(), number: 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 }; /** * Obtém os detalhes das assinaturas por intervalo de data * * GET /myeduzz/v1/subscriptions */ export const getApiMyeduzzV1Subscriptions = async (args: GetApiMyeduzzV1SubscriptionsArgs, options?: GetApiMyeduzzV1SubscriptionsOptions) => { const {baseUrl = "https://api.eduzz.com", fetch: fetchFn = fetch, eduzzAuth} = 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.itemsPerPage != null) query.append("itemsPerPage", String(args.itemsPerPage)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.filterBy != null) query.append("filterBy", String(args.filterBy)) if (eduzzAuth !== undefined) headers.set('authorization', `Bearer ${eduzzAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/myeduzz/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(getApiMyeduzzV1SubscriptionsResponse, await res.json()) };