import {z} from 'zod' export type GBTInsertableType = 'PARTS' | 'SKETCHES' | 'SURFACES' | 'FLATTENED_PARTS' | 'COMPOSITE_PARTS' | 'PART_STUDIOS' | 'WIRES' | 'PARAMETRIC_PART_STUDIOS' | 'UNKNOWN'; export type BTAssemblyInstanceDefinitionParams = {configuration?: string | undefined, documentId: string, elementId?: string | undefined, featureId?: string | undefined, includePartTypes?: Array | undefined, isAssembly?: boolean | undefined, isFixed?: boolean | undefined, isHidden?: boolean | undefined, isSuppressed?: boolean | undefined, isWholePartStudio?: boolean | undefined, microversionId?: string | undefined, partId?: string | undefined, partNumber?: string | undefined, revision?: string | undefined, versionId?: string | undefined}; export type TransformGroup = {instances?: Array | undefined, transform?: Array | undefined}; export type BTAssemblyTransformedInstancesDefinitionParams = {transformGroups?: Array | undefined}; export type CreateApiAssembliesDDidWWidEEidTransformedinstancesArgs = {did: string, eid: string, wid: string, body: BTAssemblyTransformedInstancesDefinitionParams}; export type CreateApiAssembliesDDidWWidEEidTransformedinstancesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const gBTNodeStatusType = z.enum(["OK", "INFO", "WARNING", "ERROR", "UNKNOWN"]); export const bTAssemblyOccurrenceInfo = z.object({fixed: z.boolean().optional(), hidden: z.boolean().optional(), mateStatuses: z.record(z.string(), gBTNodeStatusType).optional(), path: z.array(z.string()).optional(), transform: z.array(z.number()).optional()}); export const bTAssemblyInstanceOccurrenceInfo = z.object({occurrences: z.array(bTAssemblyOccurrenceInfo).optional()}); export const bTAssemblyInsertTransformedInstancesResponse = z.object({insertInstanceResponses: z.array(bTAssemblyInstanceOccurrenceInfo).optional(), insertResponses: z.array(bTAssemblyOccurrenceInfo).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 }; /** * Create new instances with transformation. * * POST /assemblies/d/{did}/w/{wid}/e/{eid}/transformedinstances */ export const createApiAssembliesDDidWWidEEidTransformedinstances = async (args: CreateApiAssembliesDDidWWidEEidTransformedinstancesArgs, options?: CreateApiAssembliesDDidWWidEEidTransformedinstancesOptions) => { const {baseUrl = "https://api.onshape.com/api/v17", fetch: fetchFn = fetch, OAuth2, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json;charset=UTF-8; qs=0.09") headers.set('accept', "application/json;charset=UTF-8; qs=0.09") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/assemblies/d/${encodeURIComponent(String(args.did))}/w/${encodeURIComponent(String(args.wid))}/e/${encodeURIComponent(String(args.eid))}/transformedinstances${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(bTAssemblyInsertTransformedInstancesResponse, await res.json()) };