import {z} from 'zod' export type UpdateOrganizationBrandRequest = {domain: string}; export type CreateApiApiV1OrgBrandFetchArgs = {body: UpdateOrganizationBrandRequest}; export type CreateApiApiV1OrgBrandFetchOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const organizationBrandFont = z.object({origin: z.string().nullable(), type: z.string(), name: z.string()}); export const organizationBrandColor = z.object({brightness: z.number().nullable(), type: z.string(), hex: z.string()}); export const organizationBrandLogo = z.object({format: z.string().nullable(), url: z.string(), theme: z.string().nullable(), type: z.string()}); export const organizationBrand = z.object({updatedAt: z.string(), fonts: z.array(organizationBrandFont), colors: z.array(organizationBrandColor), logos: z.array(organizationBrandLogo), description: z.string().nullable(), name: z.string().nullable(), domain: z.string(), organizationUuid: z.string()}); export const apiOrganizationBrandResponse = z.object({results: organizationBrand.nullable(), status: z.literal("ok")}); 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 }; /** * Fetch organization brand * * POST /api/v1/org/brand/fetch */ export const createApiApiV1OrgBrandFetch = async (args: CreateApiApiV1OrgBrandFetchArgs, options: CreateApiApiV1OrgBrandFetchOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/org/brand/fetch${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(apiOrganizationBrandResponse, await res.json()) };