import {z} from 'zod' export type CreateApiApiV1LinksArgs = {body: Array<{fb_pixel_id?: string | undefined, tiktok_pixel_id?: string | undefined, hide_referrer?: boolean | undefined, expiry_datetime?: string | undefined, expiry_destination?: string | undefined, rules?: (Array<{matches?: string | undefined, percentage?: string | undefined, rule_url?: string | undefined, url?: string | undefined, what?: string | undefined}>) | undefined, cloaking?: boolean | undefined, linkify_words?: string | undefined, og_description?: string | undefined, body_tags?: string | undefined, spam?: boolean | undefined, og_title?: string | undefined, note?: string | undefined, name?: string | undefined, id?: number | undefined, gtm_id?: string | undefined, og_image?: string | undefined, block_bots?: boolean | undefined, utm_content?: string | undefined, enabled?: boolean | undefined, url?: string | undefined, replacements?: string | undefined, deleted?: boolean | undefined, expiry_clicks?: number | undefined, workspace_id?: number | undefined, utm_source?: string | undefined, slug?: string | undefined, domain?: string | undefined, forward_params?: boolean | undefined, utm_medium?: string | undefined, head_tags?: string | undefined, ga4_tag_id?: string | undefined, utm_term?: string | undefined, utm_campaign?: string | undefined}>}; export type CreateApiApiV1LinksOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1LinksResponse = z.array(z.object({error: z.object({}).optional(), index: z.number().int().optional(), link: z.object({}).optional(), status: z.enum(["success", "error"]).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 }; /** * Create or update multiple links * * POST /api/v1/links * * Also accepts auth: bearerAuth */ export const createApiApiV1Links = async (args: CreateApiApiV1LinksArgs, options?: CreateApiApiV1LinksOptions) => { const {baseUrl = "https://api.linklyhq.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) query.set("api_key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/links${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(createApiApiV1LinksResponse, await res.json()) };