import {z} from 'zod' export type EdubankBuyersAddress = {city: string, additional_information?: string | undefined, number: string, state_code: string, street: string, neighborhood: string, zip: string, country_code: 'BR'}; export type EdubankBuyerDto = {first_name: string, last_name: string, email?: string | undefined, taxpayer_id: string, birthdate?: string | undefined, phone_number?: string | undefined, address: EdubankBuyersAddress}; export type EdubankSubscriptionSubscribeDto = {encrypted_card: string, authorized: boolean, buyer: EdubankBuyerDto}; export type CreateApiV1SubscriptionsSubscriptionIdSubscribeArgs = {subscription_id: string, body: EdubankSubscriptionSubscribeDto}; export type CreateApiV1SubscriptionsSubscriptionIdSubscribeOptions = {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 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 }; /** * Authorizes the subscription proposal * * POST /v1/subscriptions/{subscription_id}/subscribe * * Also accepts auth: authorization */ export const createApiV1SubscriptionsSubscriptionIdSubscribe = async (args: CreateApiV1SubscriptionsSubscriptionIdSubscribeArgs, options: CreateApiV1SubscriptionsSubscriptionIdSubscribeOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/subscriptions/${encodeURIComponent(String(args.subscription_id))}/subscribe${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(edubankSubscription, await res.json()) };