import {z} from 'zod' export type GetApiRevenuecatV1ProjectsProjectIdSubscribersAppUserIdArgs = {project_id: string, app_user_id: string}; export type GetApiRevenuecatV1ProjectsProjectIdSubscribersAppUserIdOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiRevenuecatV1ProjectsProjectIdSubscribersAppUserIdResponse = z.object({request_date: z.string(), request_date_ms: z.number(), subscriber: z.object({original_app_user_id: z.string(), original_application_version: z.unknown(), original_purchase_date: z.string().nullable(), management_url: z.string().nullable(), first_seen: z.string(), last_seen: z.string(), entitlements: z.record(z.string(), z.object({expires_date: z.string().nullable(), grace_period_expires_date: z.string().nullable(), product_identifier: z.string(), product_plan_identifier: z.string().nullable(), purchase_date: z.string().nullable()})), subscriptions: z.record(z.string(), z.object({auto_resume_date: z.string().nullable(), billing_issues_detected_at: z.string().nullable(), expires_date: z.string().nullable(), grace_period_expires_date: z.string().nullable(), is_sandbox: z.boolean(), original_purchase_date: z.string().nullable(), ownership_type: z.enum(["PURCHASED", "FAMILY_SHARED"]), period_type: z.enum(["normal", "trial", "intro"]), product_plan_identifier: z.string().nullable(), purchase_date: z.string().nullable(), refunded_at: z.string().nullable(), store: z.string(), store_transaction_id: z.string(), unsubscribe_detected_at: z.string().nullable()})), non_subscriptions: z.record(z.string(), z.array(z.object({id: z.string(), is_sandbox: z.boolean(), original_purchase_date: z.string().nullable(), purchase_date: z.string().nullable(), store: z.string(), store_transaction_id: z.string()}))), other_purchases: z.object({}), subscriber_attributes: z.record(z.string(), z.object({value: z.string(), updated_at_ms: z.number()}))})}); 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 }; /** * Get Subscriber * * GET /revenuecat/v1/projects/{project_id}/subscribers/{app_user_id} * * Also accepts auth: cookie, oauth */ export const getApiRevenuecatV1ProjectsProjectIdSubscribersAppUserId = async (args: GetApiRevenuecatV1ProjectsProjectIdSubscribersAppUserIdArgs, options?: GetApiRevenuecatV1ProjectsProjectIdSubscribersAppUserIdOptions) => { const {baseUrl = "https://api.superwall.com", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/revenuecat/v1/projects/${encodeURIComponent(String(args.project_id))}/subscribers/${encodeURIComponent(String(args.app_user_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(getApiRevenuecatV1ProjectsProjectIdSubscribersAppUserIdResponse, await res.json()) };