import {z} from 'zod' export type SearchBody = {q: string}; export type CreateApiV1InternalOrgsSearchArgs = {body: SearchBody}; export type CreateApiV1InternalOrgsSearchOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionPausedInvoiceData = z.object({invoice_id: z.string(), invoice_number: z.string(), amount: z.number(), timestamp: z.number().int(), pause_reason: z.string(), invoice_name: z.string(), payment_link: z.string().nullable(), severity: z.union([z.literal(1), z.literal(2)])}); export const subscriptionPaused = z.object({invoices: z.array(subscriptionPausedInvoiceData)}); export const internalCompanyData = z.object({id: z.string(), name: z.string(), billing_email: z.string(), org_stripe_id: z.string(), subscription_paused_reason: subscriptionPaused.nullable(), slug: z.string(), hubspot_id: z.string().nullable(), city: z.string().optional(), country: z.string().optional(), creator: z.string(), email: z.string(), subscription_tier: z.enum(["_NONE_", "_TRIAL_", "_INTERNAL_", "STARTUP", "GROWTH", "SCALE", "ENTERPRISE"]).nullable(), subscription_cycle: z.enum(["monthly", "yearly"]).nullable(), trial_end: z.number().int().nullable()}); export const createApiV1InternalOrgsSearchResponse = z.array(internalCompanyData); 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 }; /** * Internally Search Orgs * * POST /v1/internal/orgs/search */ export const createApiV1InternalOrgsSearch = async (args: CreateApiV1InternalOrgsSearchArgs, options: CreateApiV1InternalOrgsSearchOptions) => { 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 (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/internal/orgs/search${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(createApiV1InternalOrgsSearchResponse, await res.json()) };