import {z} from 'zod' export type GetApiMyeduzzV1SubscriptionsIdArgs = {id: string}; export type GetApiMyeduzzV1SubscriptionsIdOptions = {baseUrl?: string | undefined, eduzzAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiMyeduzzV1SubscriptionsIdResponse = 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 de uma assinatura por id * * GET /myeduzz/v1/subscriptions/:id */ export const getApiMyeduzzV1SubscriptionsId = async (args: GetApiMyeduzzV1SubscriptionsIdArgs, options?: GetApiMyeduzzV1SubscriptionsIdOptions) => { 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 (eduzzAuth !== undefined) headers.set('authorization', `Bearer ${eduzzAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/myeduzz/v1/subscriptions/:id${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(getApiMyeduzzV1SubscriptionsIdResponse, await res.json()) };