import {z} from 'zod' export type GetApiWorkspaceOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiWorkspaceResponse = z.object({slug: z.string(), counts: z.object({campaigns: z.number(), audiences: z.number(), dataStores: z.number(), brands: z.number()}), recentCampaigns: z.array(z.object({id: z.string(), name: z.string(), status: z.string(), objective: z.string(), brandId: z.string().nullable(), brand: z.object({name: z.string(), website: z.string(), description: z.string()}), industry: z.string(), language: z.string(), locations: z.array(z.string()), successMetrics: z.array(z.string()), campaignType: z.string().nullable(), budget: z.number().nullable(), budgetType: z.enum(["daily", "total"]).nullable(), endDate: z.string().nullable(), biddingStrategy: z.string().nullable(), targetCpa: z.number().nullable(), targetRoas: z.number().nullable(), audiences: z.array(z.object({id: z.string(), name: z.string(), audienceId: z.string().nullable(), dataStoreIds: z.array(z.string())})), googleAds: z.object({customerId: z.string().nullable(), accountName: z.string().nullable(), campaignId: z.string().nullable(), status: z.string().nullable()}), url: z.string(), createdAt: z.string().nullable(), updatedAt: z.string().nullable()}))}); 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 }; /** * Get the workspace * * GET /workspace */ export const getApiWorkspace = async (options?: GetApiWorkspaceOptions) => { const {baseUrl = "https://www.fandom.ai/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/workspace${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(getApiWorkspaceResponse, await res.json()) };