import {z} from 'zod' export type GetApiApiV1AccountsAccountIdSyncArgs = {chunk_size?: number | undefined, partial?: boolean | undefined, linkedin_product?: ('classic' | 'recruiter' | 'sales_navigator') | undefined, before?: number | undefined, after?: number | undefined, account_id: string}; export type GetApiApiV1AccountsAccountIdSyncOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1AccountsAccountIdSyncResponse = z.object({object: z.literal("AccountResync"), status: z.enum(["SYNC_STARTED", "SYNC_RUNNING", "SYNC_DONE", "SYNC_ERROR", "CHUNK_DONE"])}); 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 }; /** * Resynchronize account messaging data * * GET /api/v1/accounts/{account_id}/sync */ export const getApiApiV1AccountsAccountIdSync = async (args: GetApiApiV1AccountsAccountIdSyncArgs, options?: GetApiApiV1AccountsAccountIdSyncOptions) => { const {baseUrl = "https://api1.unipile.com:13111", fetch: fetchFn = fetch, AccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.chunk_size != null) query.append("chunk_size", String(args.chunk_size)) if (args.partial != null) query.append("partial", String(args.partial)) if (args.linkedin_product != null) query.append("linkedin_product", String(args.linkedin_product)) if (args.before != null) query.append("before", String(args.before)) if (args.after != null) query.append("after", String(args.after)) if (AccessToken !== undefined) headers.set("X-API-KEY", AccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/accounts/${encodeURIComponent(String(args.account_id))}/sync${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(getApiApiV1AccountsAccountIdSyncResponse, await res.json()) };