import {z} from 'zod' export type CreateApiV2ContactsArgs = {page?: number | undefined, limit?: number | undefined, createdBefore?: string | undefined, createdAfter?: string | undefined, orgSlug?: string | undefined, body: {email?: string | undefined, text?: string | undefined, ids?: Array | undefined, firstName?: string | undefined, lastName?: string | undefined, authProvider?: string | undefined, status?: ('pending' | 'validated' | 'deactivated') | undefined, platformRole?: string | undefined, groups?: Array | undefined, includeOrgMembership?: string | undefined}}; export type CreateApiV2ContactsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const contact = z.object({firstName: z.string(), lastName: z.string().optional(), language: z.string().optional(), photo: z.string().optional(), status: z.enum(["pending", "validated", "deactivated"]).optional(), meta: z.object({}).optional(), groups: z.array(z.string()).optional(), membership: z.object({roleSlug: z.string().optional(), status: z.enum(["active", "invited", "suspended"]).optional(), joinedAt: z.string().optional(), joinedVia: z.enum(["direct", "invite-code", "invite-email", "auto-join", "join-rule"]).optional(), invitedAt: z.string().optional(), invitedBy: z.string().optional()}).optional(), id: z.string().optional()}); export const createApiV2ContactsResponse = z.object({size: z.number().optional(), contacts: z.array(contact)}); 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 }; /** * Find Contacts * * POST /v2/contacts * * Also accepts auth: OrgApiKeyAuth, BearerAuth + OrgApiKeyAuth */ export const createApiV2Contacts = async (args: CreateApiV2ContactsArgs, options?: CreateApiV2ContactsOptions) => { const {baseUrl = "https://api.studio.prisme.ai", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.createdBefore != null) query.append("createdBefore", String(args.createdBefore)) if (args.createdAfter != null) query.append("createdAfter", String(args.createdAfter)) if (args.orgSlug != null) query.append("orgSlug", String(args.orgSlug)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/contacts${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(createApiV2ContactsResponse, await res.json()) };