UNPKG

forma-embedded-view-sdk

Version:

The Forma Embedded View SDK is a JavaScript library for creating custom extensions in Autodesk Forma (previously Spacemaker).

171 lines (170 loc) 5.57 kB
// eslint-disable-next-line @typescript-eslint/no-unused-vars import { EmbeddedViewSdk } from "./embedded-view.js"; /** * Create and upload elements to the integrate element system. * * @remarks * Available via {@link auto.Forma | Forma}.{@link index.EmbeddedViewSdk.integrateElements | integrateElements}. */ export class IntegrateApi { #iframeMessenger; /** @hidden */ constructor(iframeMessenger) { this.#iframeMessenger = iframeMessenger; } /** * Create a hierarchy of elements in the integrate element system. * * More information about the integrate element system can be seen * at https://aps.autodesk.com/en/docs/forma/v1/http-specification/integrate-api/ * * Requires edit access. See {@link EmbeddedViewSdk.getCanEdit | getCanEdit} for more info. * * @deprecated Prefer using {@link IntegrateApi.createElementV2} or {@link IntegrateApi.batchIngestElementsV2} instead. * * @returns ID, URN, and revision of the root element created. * * @example * const upload = await Forma.integrateElements.uploadFile({ * data: "glb data here...", * }) * * const { urn } = await Forma.integrateElements.createElementHierarchy({ * data: { * rootElement: "root", * elements: { * root: { * id: "root", * properties: { * geometry: { * type: "File", * format: "glb", * s3Id: upload.fileId, * }, * }, * }, * }, * }, * }) * * console.log(`Created element: ${urn}`) */ async createElementHierarchy(request) { return await this.#iframeMessenger.sendRequest("integrate/create-element-hierarchy", request); } /** * Create a new element. * * Requires edit access. See {@link EmbeddedViewSdk.getCanEdit | getCanEdit} for more info. * * @example * const upload = await Forma.integrateElements.uploadFile({ * data: "glb data here...", * }) * * const { urn } = await Forma.integrateElements.createElementV2({ * representations: { * volumeMesh: { * blobId: upload.blobId, * }, * }, * }) * * @returns URN of the created element. */ async createElementV2(request) { return await this.#iframeMessenger.sendRequest("integrate/v2/create-element", { data: request, }); } /** * Create a new element based on the existing element. The element must * belong to integrate system. * * The update will be merged onto the existing element. The merge happens * for each individual property and representation, but not deeper. * If you want to remove a field you must set it to \`null\`. * * Requires edit access. See {@link EmbeddedViewSdk.getCanEdit | getCanEdit} for more info. * * @example * const { urn } = await Forma.integrateElements.updateElementV2({ * urn: existingUrn, * properties: { * foo: "bar", * }, * }) * * @returns URN of the created element. */ async updateElementV2(request) { const { urn, ...rest } = request; return await this.#iframeMessenger.sendRequest("integrate/v2/update-element", { urn, data: rest, }); } /** * Create and/or update multiple elements in a batch. * * As part of the same batch elements can reference each other to form * a hierarchy in a single request. You need to specify the element URN * that should be used to store the new element for this. * * The response can be partial including failed items. * * Requires edit access. See {@link EmbeddedViewSdk.getCanEdit | getCanEdit} for more info. * * @example * const upload = await Forma.integrateElements.uploadFile({ * data: "glb data here...", * }) * * const { items } = await Forma.integrateElements.batchIngestElementsV2({ * items: [ * { * operation: "create", * representations: { * volumeMesh: { * blobId: upload.blobId, * }, * }, * }, * { * operation: "update", * urn: existingUrn, * properties: { * foo: "bar", * }, * }, * ], * }) * // Handle failures. */ async batchIngestElementsV2(request) { return await this.#iframeMessenger.sendRequest("integrate/v2/batch-ingest-elements", { data: request, }); } /** * Upload a file to the integrate file storage. Files stored here are can only * be retrieved in relation to an element. An example of this is uploading a * geometric file - e.g. GLB or GeoJSON files. * * Requires edit access. See {@link EmbeddedViewSdk.getCanEdit | getCanEdit} for more info. * * @returns Unique identifier for the uploaded file. */ async uploadFile(request) { return await this.#iframeMessenger.sendRequest("integrate/upload-file", request); } /** * Create a new URN to be used for an element in Integrate API. * * @example * const urn = createUrn("pro_japoqu38nx") */ createUrn(authcontext) { return `urn:adsk-forma-elements:integrate:${authcontext}:${crypto.randomUUID()}:${Date.now()}`; } }