import { type ResponseType } from 'axios'; import { z } from 'zod'; import { getUri } from '../../../operators/getUri.js'; import { healthcheck } from '../../../operators/healthCheck.js'; import { type ThreekitAxiosContext } from '../../../operators/HttpContext.js'; import { request } from '../../../operators/request.js'; import { Configuration, getDefaultResponseType, type RequestResponseReturnType, type RequestResponseType, threekitHostToEnv } from '../../../shared.js'; import { type ThreekitAuthProps } from '../../../ThreekitAuthProps.js'; import { createLocalAxios } from '../utils/createLocalAxios.js'; export enum MeshCompression { Draco = 'draco', Meshopt = 'meshopt', Quantize = 'quantize', None = 'none' } export enum ImageFormat { Ktx2 = 'ktx2', Webp = 'webp', Original = 'original' } export const GlbExportProps = z.object({ assetId: z.string(), configuration: Configuration.optional(), cache: z.boolean().optional(), cacheScope: z.string().optional(), cacheMaxAge: z.number().optional(), version: z .union([z.literal('platform'), z.literal('experimental')]) .optional(), settings: z .union([ z.record(z.string(), z.any()), z.object({ arExport: z.boolean().optional(), prune: z .object({ invisible: z.boolean().optional(), // default true. removes nodes that are invisible in the hierarchy childless: z.boolean().optional() // default true. recursively remove nodes that have no PolyMesh or Connector grandchildren }) .optional() }) ]) .optional() }); export type GlbExportProps = z.infer; export const GlbExportResult = z.object({ fileId: z.string(), url: z.string() }); export type GlbExportResult = z.infer; export const GlbOptimizeProps = z.object({ sourceUrl: z.string(), dedup: z.boolean().optional(), prune: z.boolean().optional(), instance: z.boolean().optional(), instanceMinimum: z.number().optional(), simplify: z.boolean().optional(), simplifyError: z.number().optional(), meshCompression: z.nativeEnum(MeshCompression).optional(), textureFormat: z.nativeEnum(ImageFormat).optional(), textureSize: z.number().optional(), cacheScope: z.string().optional(), cacheMaxAge: z.number().optional() }); export type GlbOptimizeProps = z.infer; const DEBUG = false; const API_ROUTE = `api/glb`; const API_HOST = DEBUG ? 'localhost:4002' : 'glb-service.alpha.3kit.com'; const API_PROTOCOL = DEBUG ? 'http://' : 'https://'; export class Glb { context: ThreekitAxiosContext; constructor(auth: ThreekitAuthProps) { this.context = { axios: createLocalAxios({ protocol: API_PROTOCOL, host: API_HOST }), auth, urlPrefix: API_ROUTE }; } healthcheck() { return healthcheck(this.context); } exportToFile(props: GlbExportProps) { const auth = this.context.auth; const token = 'publicToken' in auth ? auth.publicToken : 'privateToken' in auth ? auth.privateToken : undefined; if (token === undefined) throw new Error('Only public and private tokens supported'); const { assetId, configuration = {}, cache = true, cacheScope, cacheMaxAge, version } = GlbExportProps.parse(props); return request(this.context, { url: `export`, params: { environment: threekitHostToEnv(this.context.auth.host), orgId: this.context.auth.orgId, bearer_token: token, assetId, configuration, cache, cacheScope, cacheMaxAge, response: 'json', version } }); } exportGlb(props: GlbExportProps) { const auth = this.context.auth; const token = 'publicToken' in auth ? auth.publicToken : 'privateToken' in auth ? auth.privateToken : undefined; if (token === undefined) throw new Error('Only public and private tokens supported'); const { assetId, configuration = {}, cache = true, cacheScope, cacheMaxAge, version } = GlbExportProps.parse(props); return request(this.context, { url: `export`, params: { environment: threekitHostToEnv(this.context.auth.host), orgId: this.context.auth.orgId, bearer_token: token, assetId, configuration, cache, cacheScope, cacheMaxAge, response: 'glb', version } }); } exportGlbAsUrl(props: GlbExportProps) { const auth = this.context.auth; const token = 'publicToken' in auth ? auth.publicToken : 'privateToken' in auth ? auth.privateToken : undefined; if (token === undefined) throw new Error('Only public and private tokens supported'); const { assetId, configuration = {}, cache = true, cacheScope, cacheMaxAge, version, settings } = GlbExportProps.parse(props); return getUri(this.context, { url: `export`, params: { environment: threekitHostToEnv(this.context.auth.host), orgId: this.context.auth.orgId, bearer_token: token, assetId, configuration, cache, cacheScope, cacheMaxAge, response: 'glb', version, settings } }); } optimize( props: GlbOptimizeProps, responseType = getDefaultResponseType() ) { return request>(this.context, { url: `optimize`, params: { environment: threekitHostToEnv(this.context.auth.host), orgId: this.context.auth.orgId, ...props }, responseType: responseType as ResponseType }); } getOptimizeUrl(props: GlbOptimizeProps) { return getUri(this.context, { url: `optimize`, params: { environment: threekitHostToEnv(this.context.auth.host), orgId: this.context.auth.orgId, ...props } }); } }