import {z} from 'zod' export type GetApiAgenciesSlugArgs = {slug: string}; export type GetApiAgenciesSlugOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const meta = z.object({source: z.string().optional(), attribution: z.string().optional(), docs: z.string().optional()}); export const getApiAgenciesSlugResponse = z.object({agency: z.object({slug: z.string().optional(), name: z.string().optional(), tagline: z.string().nullable().optional(), location: z.object({city: z.string().nullable().optional(), country: z.string().nullable().optional()}).optional(), rating: z.number().nullable().optional(), total_reviews: z.number().int().optional(), services: z.array(z.string()).optional(), industries: z.array(z.string()).optional(), team_size: z.enum(["1-10", "11-50", "51-200", "201-500", "500+", "null"]).nullable().optional(), founded_year: z.number().int().nullable().optional(), min_budget_usd: z.number().int().nullable().optional(), website: z.string().nullable().optional(), is_verified: z.boolean().optional(), profile_url: z.string().optional(), description: z.string().nullable().optional(), recent_reviews: z.array(z.object({rating: z.number().nullable().optional(), summary: z.string().nullable().optional(), author: z.string().optional()})).optional()}).optional(), meta: meta.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 }; /** * Get one agency profile with recent reviews * * GET /agencies/{slug} */ export const getApiAgenciesSlug = async (args: GetApiAgenciesSlugArgs, options?: GetApiAgenciesSlugOptions) => { const {baseUrl = "https://www.pickanagency.com/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/agencies/${encodeURIComponent(String(args.slug))}${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(getApiAgenciesSlugResponse, await res.json()) };