import {z} from 'zod' export type GetApiV1OrgPersonasArgs = {organization_ids?: (Array | null) | undefined}; export type GetApiV1OrgPersonasOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const personaProfileBehavior = z.object({painPoints: z.string().nullable().optional(), motivations: z.string().nullable().optional()}); export const personaProfileEmployment = z.object({industry: z.array(z.string()).optional(), jobTitle: z.array(z.string()).optional(), companySize: z.array(z.string()).optional(), roleSeniority: z.array(z.string()).optional()}); export const personaProfileDemographics = z.object({ageRange: z.array(z.string()).optional()}); export const personaProfile = z.object({behavior: personaProfileBehavior, employment: personaProfileEmployment, demographics: personaProfileDemographics}); export const category = z.object({id: z.string(), name: z.string()}); export const organization = z.object({id: z.string(), name: z.string().nullable()}); export const organizationPersonasWithCategory = z.object({id: z.string(), name: z.string(), persona: personaProfile, category: category, organization: organization}); export const organizationPersonasWithCategoryResponse = z.object({data: z.array(organizationPersonasWithCategory)}); 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 Personas * * GET /v1/org/personas * * Also accepts auth: BearerAuth */ export const getApiV1OrgPersonas = async (args: GetApiV1OrgPersonasArgs, options?: GetApiV1OrgPersonasOptions) => { const {baseUrl = "https://api.tryprofound.com", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.organization_ids ?? []) query.append("organization_ids", String(item)) if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/org/personas${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(organizationPersonasWithCategoryResponse, await res.json()) };