import {z} from 'zod' export type IAgencyCreate = {org_id: string | null, branding?: {[key: string]: unknown} | undefined, domain?: {[key: string]: unknown} | undefined, billing_plan?: {[key: string]: unknown} | undefined, user_auth?: {[key: string]: unknown} | undefined}; export type CreateApiApiV1AgencyArgs = {body: IAgencyCreate}; export type CreateApiApiV1AgencyOptions = {baseUrl: string, YourAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const iAgencyCreate = z.object({org_id: z.string().nullable(), branding: z.record(z.string(), z.unknown()).optional(), domain: z.record(z.string(), z.unknown()).optional(), billing_plan: z.record(z.string(), z.unknown()).optional(), user_auth: z.record(z.string(), z.unknown()).optional()}); export const iPostResponseBaseIAgencyCreate = z.object({message: z.string().nullable().optional(), meta: z.union([z.record(z.string(), z.unknown()), z.unknown()]).nullable().optional(), data: iAgencyCreate.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 }; /** * Create Agency * * POST /api/v1/agency * * Also accepts auth: HTTPBearer */ export const createApiApiV1Agency = async (args: CreateApiApiV1AgencyArgs, options: CreateApiApiV1AgencyOptions) => { const {baseUrl, fetch: fetchFn = fetch, YourAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (YourAPIKey !== undefined) query.set("api_key", YourAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/agency${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(iPostResponseBaseIAgencyCreate, await res.json()) };