{"version":3,"file":"client-CLLEZKit.mjs","names":["items: RelioContact[]","cursor: string | null","createPayload: RelioCreateContactInput"],"sources":["../src/client.ts"],"sourcesContent":["import { TRPCClientError, createTRPCProxyClient, httpBatchLink } from \"@trpc/client\";\n\nimport {\n  type RelioAuthStatus,\n  type RelioClientOptions,\n  type RelioCompany,\n  type RelioContact,\n  type RelioCreateContactInput,\n  type RelioCreateCompanyInput,\n  type RelioCreatePropertyInput,\n  type RelioCursorPage,\n  type RelioListCompaniesInput,\n  type RelioListContactsInput,\n  type RelioListPropertiesInput,\n  type RelioProperty,\n  type RelioUpdateCompanyInput,\n  type RelioUpdateContactInput,\n  type RelioUpdatePropertyInput,\n  RelioApiError,\n  normalizeRelioBaseUrl,\n} from \"./core\";\n\ntype UntypedTRPCClient = Record<string, any>;\n\nfunction getProcedure(client: UntypedTRPCClient, path: string) {\n  return path.split(\".\").reduce<any>((acc, part) => acc?.[part], client);\n}\n\nfunction toRelioApiError(error: unknown): RelioApiError {\n  if (error instanceof RelioApiError) return error;\n\n  if (error instanceof TRPCClientError) {\n    const trpcError = error as any;\n    const code = trpcError?.shape?.data?.code as string | undefined;\n    const status = trpcError?.data?.httpStatus as number | undefined;\n    return new RelioApiError(error.message, {\n      code,\n      status,\n      causeData: trpcError?.shape ?? trpcError?.data,\n    });\n  }\n\n  if (error instanceof Error) {\n    return new RelioApiError(error.message);\n  }\n\n  return new RelioApiError(\"Unknown Relio client error\");\n}\n\nexport class RelioClient {\n  private readonly trpc: UntypedTRPCClient;\n\n  readonly baseUrl: string;\n\n  constructor(options: RelioClientOptions) {\n    this.baseUrl = normalizeRelioBaseUrl(options.baseUrl);\n    const authHeader = options.apiKey ? `Bearer ${options.apiKey}` : undefined;\n\n    this.trpc = createTRPCProxyClient<any>({\n      links: [\n        httpBatchLink({\n          url: `${this.baseUrl}/trpc`,\n          methodOverride: \"POST\",\n          headers: authHeader ? { Authorization: authHeader } : undefined,\n          fetch: options.fetch,\n        }),\n      ],\n    }) as unknown as UntypedTRPCClient;\n  }\n\n  private async query<TOutput, TInput = unknown>(path: string, input?: TInput): Promise<TOutput> {\n    try {\n      const procedure = getProcedure(this.trpc, path);\n      if (!procedure?.query) {\n        throw new RelioApiError(`Unknown query procedure: ${path}`);\n      }\n      return await procedure.query(input);\n    } catch (error) {\n      throw toRelioApiError(error);\n    }\n  }\n\n  private async mutate<TOutput, TInput = unknown>(path: string, input?: TInput): Promise<TOutput> {\n    try {\n      const procedure = getProcedure(this.trpc, path);\n      if (!procedure?.mutate) {\n        throw new RelioApiError(`Unknown mutation procedure: ${path}`);\n      }\n      return await procedure.mutate(input);\n    } catch (error) {\n      throw toRelioApiError(error);\n    }\n  }\n\n  async authStatus(): Promise<RelioAuthStatus> {\n    return this.query<RelioAuthStatus>(\"authStatus\");\n  }\n\n  readonly contacts = {\n    list: async (input: RelioListContactsInput = {}): Promise<RelioCursorPage<RelioContact>> => {\n      return this.query<RelioCursorPage<RelioContact>, RelioListContactsInput>(\"contacts.list\", input);\n    },\n\n    listAll: async (input: Omit<RelioListContactsInput, \"cursor\" | \"limit\"> = {}): Promise<RelioContact[]> => {\n      const items: RelioContact[] = [];\n      let cursor: string | null = null;\n      let safetyCounter = 0;\n\n      do {\n        const page = await this.query<RelioCursorPage<RelioContact>, RelioListContactsInput>(\n          \"contacts.list\",\n          { ...input, limit: 100, cursor },\n        );\n        items.push(...page.items);\n        cursor = page.nextCursor;\n        safetyCounter += 1;\n      } while (cursor && safetyCounter < 5_000);\n\n      return items;\n    },\n\n    getById: async (id: string): Promise<RelioContact> => {\n      return this.query<RelioContact, string>(\"contacts.getById\", id);\n    },\n\n    create: async (input: RelioCreateContactInput): Promise<RelioContact> => {\n      return this.mutate<RelioContact, RelioCreateContactInput>(\"contacts.create\", input);\n    },\n\n    update: async (id: string, input: RelioUpdateContactInput): Promise<RelioContact> => {\n      return this.mutate<RelioContact, RelioUpdateContactInput & { id: string }>(\"contacts.update\", {\n        id,\n        ...input,\n      });\n    },\n\n    delete: async (id: string): Promise<{ id: string }> => {\n      return this.mutate<{ id: string }, string>(\"contacts.delete\", id);\n    },\n\n    bulkCreate: async (rows: RelioCreateContactInput[]): Promise<{ count: number }> => {\n      return this.mutate<{ count: number }, { rows: RelioCreateContactInput[] }>(\"contacts.bulkCreate\", {\n        rows,\n      });\n    },\n\n    exportAll: async (\n      input: Partial<Omit<RelioListContactsInput, \"cursor\" | \"limit\" | \"sort\" | \"listId\">> = {},\n    ): Promise<RelioContact[]> => {\n      return this.query<RelioContact[], any>(\"contacts.exportAll\", input);\n    },\n  };\n\n  readonly contact = {\n    list: this.contacts.list,\n    listAll: this.contacts.listAll,\n    getById: this.contacts.getById,\n    create: this.contacts.create,\n    update: this.contacts.update,\n    delete: this.contacts.delete,\n    bulkCreate: this.contacts.bulkCreate,\n    exportAll: this.contacts.exportAll,\n  };\n\n  readonly properties = {\n    list: async (\n      input: RelioListPropertiesInput = {},\n    ): Promise<RelioCursorPage<RelioProperty>> => {\n      return this.query<RelioCursorPage<RelioProperty>, RelioListPropertiesInput>(\n        \"properties.list\",\n        input,\n      );\n    },\n\n    getById: async (id: string): Promise<RelioProperty> => {\n      return this.query<RelioProperty, string>(\"properties.getById\", id);\n    },\n\n    create: async (input: RelioCreatePropertyInput): Promise<RelioProperty> => {\n      return this.mutate<RelioProperty, RelioCreatePropertyInput>(\"properties.create\", input);\n    },\n\n    update: async (id: string, input: RelioUpdatePropertyInput): Promise<RelioProperty> => {\n      return this.mutate<RelioProperty, RelioUpdatePropertyInput & { id: string }>(\n        \"properties.update\",\n        {\n          id,\n          ...input,\n        },\n      );\n    },\n\n    delete: async (id: string): Promise<{ id: string }> => {\n      return this.mutate<{ id: string }, string>(\"properties.delete\", id);\n    },\n\n    bulkCreate: async (\n      rows: RelioCreatePropertyInput[],\n    ): Promise<{ count: number }> => {\n      return this.mutate<{ count: number }, { rows: RelioCreatePropertyInput[] }>(\n        \"properties.bulkCreate\",\n        {\n          rows,\n        },\n      );\n    },\n\n    exportAll: async (\n      input: Partial<Omit<RelioListPropertiesInput, \"cursor\" | \"limit\" | \"sort\" | \"listId\">> = {},\n    ): Promise<RelioProperty[]> => {\n      return this.query<RelioProperty[], any>(\"properties.exportAll\", input);\n    },\n  };\n\n  readonly property = {\n    list: this.properties.list,\n    getById: this.properties.getById,\n    create: this.properties.create,\n    update: this.properties.update,\n    delete: this.properties.delete,\n    bulkCreate: this.properties.bulkCreate,\n    exportAll: this.properties.exportAll,\n  };\n\n  readonly companies = {\n    list: async (\n      input: RelioListCompaniesInput = {},\n    ): Promise<RelioCursorPage<RelioCompany>> => {\n      return this.query<RelioCursorPage<RelioCompany>, RelioListCompaniesInput>(\n        \"companies.list\",\n        input,\n      );\n    },\n\n    getById: async (id: string): Promise<RelioCompany> => {\n      return this.query<RelioCompany, string>(\"companies.getById\", id);\n    },\n\n    create: async (input: RelioCreateCompanyInput): Promise<RelioCompany> => {\n      return this.mutate<RelioCompany, RelioCreateCompanyInput>(\"companies.create\", input);\n    },\n\n    update: async (id: string, input: RelioUpdateCompanyInput): Promise<RelioCompany> => {\n      return this.mutate<RelioCompany, RelioUpdateCompanyInput & { id: string }>(\n        \"companies.update\",\n        {\n          id,\n          ...input,\n        },\n      );\n    },\n\n    delete: async (id: string): Promise<{ id: string }> => {\n      return this.mutate<{ id: string }, string>(\"companies.delete\", id);\n    },\n\n    bulkCreate: async (\n      rows: RelioCreateCompanyInput[],\n    ): Promise<{ count: number }> => {\n      return this.mutate<{ count: number }, { rows: RelioCreateCompanyInput[] }>(\n        \"companies.bulkCreate\",\n        {\n          rows,\n        },\n      );\n    },\n\n    exportAll: async (\n      input: Partial<Omit<RelioListCompaniesInput, \"cursor\" | \"limit\" | \"sort\" | \"listId\">> = {},\n    ): Promise<RelioCompany[]> => {\n      return this.query<RelioCompany[], any>(\"companies.exportAll\", input);\n    },\n  };\n\n  readonly company = {\n    list: this.companies.list,\n    getById: this.companies.getById,\n    create: this.companies.create,\n    update: this.companies.update,\n    delete: this.companies.delete,\n    bulkCreate: this.companies.bulkCreate,\n    exportAll: this.companies.exportAll,\n  };\n\n  async ingestContacts(\n    rows: RelioCreateContactInput[],\n    options: { chunkSize?: number } = {},\n  ): Promise<{ count: number; chunks: number }> {\n    const chunkSize = Math.max(1, Math.min(options.chunkSize ?? 500, 500));\n    let count = 0;\n    let chunks = 0;\n\n    for (let i = 0; i < rows.length; i += chunkSize) {\n      const chunk = rows.slice(i, i + chunkSize);\n      const result = await this.contacts.bulkCreate(chunk);\n      count += result.count;\n      chunks += 1;\n    }\n\n    return { count, chunks };\n  }\n\n  async upsertContactByPrimaryEmail(\n    primaryEmail: string,\n    payload: RelioCreateContactInput,\n  ): Promise<{ action: \"created\" | \"updated\"; contact: RelioContact }> {\n    const normalized = primaryEmail.trim().toLowerCase();\n    if (!normalized) {\n      throw new RelioApiError(\"Primary email is required.\");\n    }\n\n    const page = await this.contacts.list({\n      search: normalized,\n      limit: 50,\n    });\n\n    const existing = page.items.find((contact) =>\n      (contact.email ?? []).some((entry) => entry?.address?.toLowerCase?.() === normalized),\n    );\n\n    if (!existing) {\n      const createPayload: RelioCreateContactInput = {\n        ...payload,\n        email: payload.email?.length ? payload.email : [{ address: normalized, isPrimary: true }],\n      };\n      const contact = await this.contacts.create(createPayload);\n      return { action: \"created\", contact };\n    }\n\n    const contact = await this.contacts.update(existing.id, payload);\n    return { action: \"updated\", contact };\n  }\n}\n\nexport function createRelioClient(options: RelioClientOptions): RelioClient {\n  return new RelioClient(options);\n}\n"],"mappings":";;;;AAwBA,SAAS,aAAa,QAA2B,MAAc;AAC7D,QAAO,KAAK,MAAM,IAAI,CAAC,QAAa,KAAK,SAAS,MAAM,OAAO,OAAO;;AAGxE,SAAS,gBAAgB,OAA+B;AACtD,KAAI,iBAAiB,cAAe,QAAO;AAE3C,KAAI,iBAAiB,iBAAiB;EACpC,MAAM,YAAY;EAClB,MAAM,OAAO,WAAW,OAAO,MAAM;EACrC,MAAM,SAAS,WAAW,MAAM;AAChC,SAAO,IAAI,cAAc,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