import {z} from 'zod' export type GetApiLinkedinLeadsProfileIdPostsArgs = {profileId: string, startDate?: string | undefined, endDate?: string | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiLinkedinLeadsProfileIdPostsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const linkedInLeadPost = z.object({id: z.string().optional(), url: z.string().optional(), commentary: z.string().nullable().optional(), publishedAt: z.string().nullable().optional(), likeCount: z.number().int().nullable().optional(), commentCount: z.number().int().nullable().optional(), shareCount: z.number().int().nullable().optional(), scrapedLeadsCount: z.object({reactions: z.number().int().optional(), comments: z.number().int().optional(), reshares: z.number().int().optional()}).optional()}); export const getApiLinkedinLeadsProfileIdPostsResponse = z.object({posts: z.array(linkedInLeadPost).optional(), nextCursor: z.string().nullable().optional(), hasMore: z.boolean().optional()}); 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 }; /** * List posts * * GET /linkedin/leads/{profileId}/posts */ export const getApiLinkedinLeadsProfileIdPosts = async (args: GetApiLinkedinLeadsProfileIdPostsArgs, options?: GetApiLinkedinLeadsProfileIdPostsOptions) => { const {baseUrl = "https://app.tryordinal.com/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/linkedin/leads/${encodeURIComponent(String(args.profileId))}/posts${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(getApiLinkedinLeadsProfileIdPostsResponse, await res.json()) };