import {z} from 'zod' export type GetApiV1BrandArgs = {include?: string | undefined}; export type GetApiV1BrandOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const brandGetResponse = z.object({brand: z.object({brandId: z.string().min(1), domain: z.string().min(1), status: z.enum(["extracting", "completed", "failed", "deleting"]), ready: z.boolean(), createdAt: z.string().optional(), updatedAt: z.string().optional()}), identity: z.object({brandName: z.string().optional(), description: z.string().optional(), tagline: z.string().optional(), contactEmail: z.string().optional(), contactPhone: z.string().optional(), address: z.string().optional(), socialLinks: z.array(z.unknown()).optional(), contentLinks: z.array(z.unknown()).optional()}).and(z.record(z.string(), z.unknown())).optional(), emailDesign: z.string().optional(), imageStyle: z.string().optional(), logos: z.array(z.object({src: z.string(), type: z.string().optional(), theme: z.string().optional(), format: z.string().optional(), background: z.string().optional(), width: z.number().optional(), height: z.number().optional(), size: z.number().optional()})).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 the brand bound to this API key * * GET /v1/brand * * Also accepts auth: apiKeyAuth */ export const getApiV1Brand = async (args: GetApiV1BrandArgs, options?: GetApiV1BrandOptions) => { const {baseUrl = "https://brew.new/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.include != null) query.append("include", String(args.include)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/brand${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(brandGetResponse, await res.json()) };