import {z} from 'zod' export type CreateCheckoutRequest = {credits?: (number | null) | undefined, company_id?: (string | null) | undefined, plan?: ('founder' | 'light') | undefined, attempt_id?: (string | null) | undefined, auth_resume?: boolean | undefined}; export type CreateApiConglomeratesConglomerateIdCheckoutSessionArgs = {conglomerate_id: string, 'X-Posthog-Session-Id'?: (string | null) | undefined, 'X-Client-Mobile'?: (string | null) | undefined, 'X-Meta-Fbc'?: (string | null) | undefined, 'X-Meta-Fbp'?: (string | null) | undefined, 'X-Meta-Source-Url'?: (string | null) | undefined, 'X-Tiktok-Ttclid'?: (string | null) | undefined, 'X-Tiktok-Ttp'?: (string | null) | undefined, 'X-Tiktok-Click-Time'?: (string | null) | undefined, body: CreateCheckoutRequest}; export type CreateApiConglomeratesConglomerateIdCheckoutSessionOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createSubscriptionCheckoutResponse = z.object({checkout_url: z.string(), stripe_session_id: z.string(), checkout_attempt_id: z.string(), checkout_intent_id: z.string().nullable().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 }; /** * Create Checkout Session * * POST /conglomerates/{conglomerate_id}/checkout-session */ export const createApiConglomeratesConglomerateIdCheckoutSession = async (args: CreateApiConglomeratesConglomerateIdCheckoutSessionArgs, options: CreateApiConglomeratesConglomerateIdCheckoutSessionOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Posthog-Session-Id'] != null) headers.set("X-Posthog-Session-Id", String(args['X-Posthog-Session-Id'])) if (args['X-Client-Mobile'] != null) headers.set("X-Client-Mobile", String(args['X-Client-Mobile'])) if (args['X-Meta-Fbc'] != null) headers.set("X-Meta-Fbc", String(args['X-Meta-Fbc'])) if (args['X-Meta-Fbp'] != null) headers.set("X-Meta-Fbp", String(args['X-Meta-Fbp'])) if (args['X-Meta-Source-Url'] != null) headers.set("X-Meta-Source-Url", String(args['X-Meta-Source-Url'])) if (args['X-Tiktok-Ttclid'] != null) headers.set("X-Tiktok-Ttclid", String(args['X-Tiktok-Ttclid'])) if (args['X-Tiktok-Ttp'] != null) headers.set("X-Tiktok-Ttp", String(args['X-Tiktok-Ttp'])) if (args['X-Tiktok-Click-Time'] != null) headers.set("X-Tiktok-Click-Time", String(args['X-Tiktok-Click-Time'])) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/conglomerates/${encodeURIComponent(String(args.conglomerate_id))}/checkout-session${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(createSubscriptionCheckoutResponse, await res.json()) };