import {z} from 'zod' export type OrganizationBrandFont = {origin: string | null, type: string, name: string}; export type OrganizationBrandColor = {brightness: number | null, type: string, hex: string}; export type OrganizationBrandLogo = {format: string | null, url: string, theme: string | null, type: string}; export type SaveOrganizationBrandRequest = {fonts: Array, colors: Array, logos: Array, description: string | null, name: string | null, domain: string}; export type UpdateApiApiV1OrgBrandArgs = {body: SaveOrganizationBrandRequest}; export type UpdateApiApiV1OrgBrandOptions = {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 }; /** * Save organization brand * * PUT /api/v1/org/brand */ export const updateApiApiV1OrgBrand = async (args: UpdateApiApiV1OrgBrandArgs, options: UpdateApiApiV1OrgBrandOptions) => { 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${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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()) };