@cantera/viewer-typestypes
Typed surface for the Autodesk Viewer global runtime — Autodesk's official @types/forge-viewer definitions re-exported under stable APS* names, plus domain types for cameras, properties, extensions, and promise-based token callbacks.
npx shadcn@latest add @cantera/viewer-typesThe Autodesk Viewer ships as a browser global rather than an ESM package. These types re-export Autodesk's official @types/forge-viewer definitions (a dev dependency — the full Autodesk.Viewing namespace, typed) under stable APS* names, plus cantera's domain types for cameras, properties, and token callbacks.
import type { GetAccessToken } from '@/lib/viewer-types'
const getAccessToken: GetAccessToken = async () => {
const response = await fetch('/api/viewer-token')
if (!response.ok) throw new Error('Viewer token unavailable')
return response.json()
}Types come from Autodesk's official @types/forge-viewer package (installed as a dev dependency), which declares the global Autodesk namespace plus a bundled minimal THREE namespace. lib/forge-viewer.d.ts adds the few members the official definitions miss (Profile, ProfileSettings, setProfile). If your tsconfig sets an explicit "types" array, add "forge-viewer" to it.
| Export | Type | Description |
|---|---|---|
| GetAccessToken | () => Promise<{ accessToken: string; expiresInSeconds: number }> | Promise-based backend token supplier adapted by APSViewer to the Autodesk callback contract. |
| APSViewer3D / APSModel / APSDocument | interface | Structural subsets of the Viewer global objects used by the component and hooks. |
| APSViewerExtension / APSViewingNamespace / AutodeskGlobal | interface | Public extension and global-runtime surfaces, including the extension manager and toolbar lifecycle. |
| APSCameraState / APSPropertyResult / APSContextMenuItem | interface | Typed values returned by the camera, property, and context-menu hooks. |
This is the exact code the CLI installs into your project — you own it from there.
// The SDK's types come from `@types/forge-viewer` (a dev dependency of this
// item), re-exported here under stable APS* names; the sibling
// `forge-viewer.d.ts` declares the few members the official definitions miss.
export type Vec3 = { x: number; y: number; z: number }
export interface APSCameraState {
position: Vec3
target: Vec3
up: Vec3
isPerspective: boolean
}
export interface APSProperty {
displayName: string
displayValue: string | number | boolean
displayCategory: string
units: string | null
hidden: boolean
type: number
}
export interface APSPropertyResult {
dbId: number
name: string
externalId: string
properties: APSProperty[]
}
export interface APSContextMenuItem {
title: string
target: (() => void) | APSContextMenuItem[]
icon?: string
divider?: boolean
}
export interface APSContextMenuStatus {
/** dbIds under the cursor when the menu opened (empty over empty space) */
numSelected: number
hasSelected: boolean
canvasX: number
canvasY: number
[key: string]: unknown
}
// `Viewer3D` is the base class, so a `GuiViewer3D` is an `APSViewer3D` too.
export type APSViewer3D = Autodesk.Viewing.Viewer3D
export type APSModel = Autodesk.Viewing.Model
export type APSDocument = Autodesk.Viewing.Document
export type APSDocumentNode = Autodesk.Viewing.BubbleNode
export type APSViewerExtension = Autodesk.Viewing.Extension
export type APSViewerExtensionConstructor = new (
viewer: Autodesk.Viewing.GuiViewer3D,
options?: Record<string, unknown>,
) => Autodesk.Viewing.Extension
export type APSViewingNamespace = typeof Autodesk.Viewing
export type AutodeskGlobal = typeof Autodesk
/** Fetch the token from YOUR backend — never embed APS credentials in the
* browser. The runtime re-calls the supplier before expiry. */
export type GetAccessToken = () => Promise<{
accessToken: string
/** seconds until expiry */
expiresInSeconds: number
}>
export type APSViewerStatus = 'idle' | 'loading-runtime' | 'ready' | 'error'
/** Deliberately accepts any string, so consumer-registered extensions need
* nothing extra; the `viewer-extension-types` item types the public ids. */
export type APSExtensionRequest = string | { id: string; options?: Record<string, unknown> }
export type APSExtensionStatus = 'loading' | 'ready' | 'error'
export type APSViewerProfile = 'aec' | 'default' | 'fluent' | 'navis'
// Augments `@types/forge-viewer` with the members it misses. A .d.ts because
// `namespace` merging — the only way to extend an ambient global namespace —
// would trip `@typescript-eslint/no-namespace` in a .ts file.
declare global {
/** The CDN script defines `window.Autodesk`; it is absent until the viewer
* loader has injected and awaited that script. */
interface Window {
Autodesk?: typeof Autodesk
}
namespace Autodesk {
namespace Viewing {
/** Missing from `@types/forge-viewer`: LMV 7.x settings profiles —
* `viewer.setProfile(new Profile(ProfileSettings.AEC))`. */
class Profile {
constructor(settings: unknown, name?: string)
}
const ProfileSettings: {
AEC: unknown
Default: unknown
Fluent: unknown
Navis: unknown
} & Record<string, unknown>
interface Viewer3D {
setProfile(profile: Profile, override?: boolean): boolean
}
interface ExtensionManager {
getExtensionClass?(extensionId: string): unknown
}
}
}
}
export {}