export type GetApiContactsExportArgs = {segment_id?: string | undefined}; export type GetApiContactsExportOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; /** * Export contacts as CSV * * GET /contacts/export * * Also accepts auth: bearerOAuth */ export const getApiContactsExport = async (args: GetApiContactsExportArgs, options?: GetApiContactsExportOptions) => { const {baseUrl = "https://api.poststack.dev", fetch: fetchFn = fetch, bearerApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "text/csv") if (args.segment_id != null) query.append("segment_id", String(args.segment_id)) if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/contacts/export${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 res.text() };