import {z} from 'zod' export type GetApiV1ProjectsProjectIdBrandsBrandIdArgs = {projectId: string, brandId: string}; export type GetApiV1ProjectsProjectIdBrandsBrandIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const brandStatus = z.enum(["DRAFT", "NEW", "PENDING", "PENDING_REVIEW", "REJECTED", "INCOMPLETE", "COMPLETED", "ARCHIVED"]); export const brandCategory = z.record(z.string(), z.union([z.string(), z.number().int(), z.boolean()])); export const brandLog = z.object({status: brandStatus.optional(), createTime: z.string().optional(), comment: z.string().optional(), author: z.enum(["CUSTOMER", "AGENT"]).optional()}); export const brand = z.object({brandId: z.string().optional(), brandName: z.string().optional(), brandMetadataId: z.string().optional(), status: brandStatus.optional(), data: z.array(brandCategory).optional(), logs: z.array(brandLog).optional(), callbackUrl: z.string().optional(), createTime: z.string().optional(), updateTime: z.string().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 a brand by project and brand id. * * GET /v1/projects/{projectId}/brands/{brandId} */ export const getApiV1ProjectsProjectIdBrandsBrandId = async (args: GetApiV1ProjectsProjectIdBrandsBrandIdArgs, options?: GetApiV1ProjectsProjectIdBrandsBrandIdOptions) => { const {baseUrl = "https://brand.registrations.api.sinch.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.projectId))}/brands/${encodeURIComponent(String(args.brandId))}${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(brand, await res.json()) };