import type { ResponseType } from 'axios'; import { z } from 'zod'; import type { Attachment } from '../../forms/FormDataHelpers.js'; import { createViaForm } from '../../operators/createViaForm.js'; import { get } from '../../operators/get.js'; import { getById } from '../../operators/getById.js'; import { healthcheck } from '../../operators/healthCheck.js'; import { request } from '../../operators/request.js'; import { Caching, Configuration, EntityMetadata, getDefaultResponseType, Metadata, NestedMetadata, Pagination, type RequestResponseReturnType, type RequestResponseType } from '../../shared.js'; import type { ThreekitAuthProps } from '../../ThreekitAuthProps.js'; import { Route } from '../Route.js'; export const ShortId = z.string().regex(/^[a-zA-Z0-9_-]{7,14}$/); export type ShortId = z.infer; export const SavedConfigurationId = z.union([z.string().uuid(), ShortId]); export const SavedConfiguration = EntityMetadata.merge( z.object({ id: z.string().uuid(), shortId: ShortId, orgId: z.string().uuid(), variant: Configuration.nullable(), sceneGraphState: z.string().nullable(), customerId: z.string().nullable(), identifier: z.string().nullable(), metadata: NestedMetadata.nullable(), productId: z.string().uuid(), productVersion: z.string().nullable(), scope: z.string().nullable(), thumbnail: z.string().nullable(), attachments: z.record(z.string(), z.string()), sessionId: z.string().nullable(), publishFileId: z.string().nullable(), creator: z.string().nullable() }) ); export type SavedConfiguration = z.infer; export const SavedConfigurationListing = Pagination.merge( z.object({ configurations: SavedConfiguration.array() }) ); export type SavedConfigurationListing = z.infer< typeof SavedConfigurationListing >; export const CreateSavedConfigurationProps = SavedConfiguration.pick({ productId: true, productVersion: true, variant: true, sceneGraphState: true, metadata: true, shortId: true, customerId: true, attachments: true, sessionId: true }).partial(); export type CreateSavedConfigurationProps = z.infer< typeof CreateSavedConfigurationProps > & { files?: Attachment[]; }; export const QuerySavedConfigurationProps = z.object({ productId: z.string().optional(), productVersion: z.string().optional(), shortId: ShortId.optional(), sku: z.string().optional(), metadata: Metadata.optional(), fullConfiguration: z.boolean().optional() }); export type QuerySavedConfigurationProps = z.infer< typeof QuerySavedConfigurationProps >; const API_ROUTE = `/api/configurations`; export class SavedConfigurations extends Route { constructor(auth: ThreekitAuthProps) { super(auth, API_ROUTE); } healthcheck() { return healthcheck(this.context); } create(createProps: CreateSavedConfigurationProps) { return createViaForm( this.context, createProps ); } get( queryProps?: QuerySavedConfigurationProps, pagination: Pagination = {}, caching: Caching = {} ) { return get( this.context, { ...queryProps, ...pagination, ...caching } ); } getById(idOrShortId: string, caching: Caching = {}) { const configurationId = z .union([ShortId, z.string().uuid()]) .parse(idOrShortId); return getById(this.context, configurationId, caching); } getFileById( idOrShortId: string, attachmentKey: string, caching: Caching = {}, responseType = getDefaultResponseType() ) { const configurationId = z .union([ShortId, z.string().uuid()]) .parse(idOrShortId); return request>(this.context, { url: `${configurationId}/files/${attachmentKey}`, responseType: responseType as ResponseType, params: caching }); } getThumbnailById( idOrShortId: string, responseType = getDefaultResponseType() ) { const configurationId = z .union([ShortId, z.string().uuid()]) .parse(idOrShortId); return request>(this.context, { url: `${configurationId}/thumbnail`, responseType: responseType as ResponseType }); } }