import {z} from 'zod' export type GetApiAutomationsOutreachCapacityOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, ShopId?: string | undefined, fetch?: typeof fetch | undefined}; export const outreachQuotaTier = z.object({creator_levels: z.array(z.number().int()), unlimited: z.boolean(), standard_limit: z.number().int().nullable(), standard_used: z.number().int().nullable(), bonus_limit: z.number().int().nullable(), bonus_used: z.number().int().nullable(), remaining: z.number().int().nullable()}); export const outreachQuota = z.object({quota_type: z.string(), used: z.number().int(), limit: z.number().int().nullable(), remaining: z.number().int().nullable(), unlimited: z.boolean(), resets_at: z.string().nullable(), gmv_tier: z.string().nullable().optional(), gmv_level_rule: z.array(z.unknown()).nullable().optional(), tiered: z.boolean().optional(), tiers: z.array(outreachQuotaTier).nullable().optional()}); export const outreachCapacityResponse = z.object({shop_id: z.number().int(), daily_creators_used: z.number().int(), daily_window_resets_at: z.string(), concurrent_automations_running: z.number().int(), max_concurrent_automations: z.number().int(), daily_limit_throttled: z.boolean(), weekly_limit_throttled: z.boolean(), outreach_quota: outreachQuota.nullable().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 }; /** * Outreach send capacity for this shop * * GET /automations/outreach-capacity */ export const getApiAutomationsOutreachCapacity = async (options?: GetApiAutomationsOutreachCapacityOptions) => { const {baseUrl = "https://api.reacherapp.com/public/v1", fetch: fetchFn = fetch, ApiKeyAuth, ShopId} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) if (ShopId !== undefined) headers.set("x-shop-id", ShopId) const url = `${baseUrl.replace(/\/+$/, '')}/automations/outreach-capacity${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(outreachCapacityResponse, await res.json()) };