import {z} from 'zod' export type BTAppElementReferenceParams = {hasDocumentMicroversions?: boolean | undefined, idTag?: string | undefined, idTagMicroversionId?: string | undefined, isLocked?: boolean | undefined, isSketchOnly?: boolean | undefined, parentChangeId?: string | undefined, parentViewId?: string | undefined, partNumber?: string | undefined, pureSketch?: boolean | undefined, referenceType?: number | undefined, returnError?: boolean | undefined, revision?: string | undefined, sketchIds?: Array | undefined, targetConfiguration?: string | undefined, targetDocumentId?: string | undefined, targetElementId?: string | undefined, targetMicroversionId?: string | undefined, targetVersionId?: string | undefined, trackNewVersions?: boolean | undefined, transactionId?: string | undefined, updateSketchInfo?: boolean | undefined}; export type CreateApiAppelementsDDidWvmWvmidEEidReferencesRidArgs = {did: string, eid: string, wvm: string, wvmid: string, rid: string, body: BTAppElementReferenceParams}; export type CreateApiAppelementsDDidWvmWvmidEEidReferencesRidOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const bTAppElementErrorCode = z.enum(["OK", "TRANSACTION_CONFLICT", "NOT_FOUND", "INCONSISTENT_CHANGES"]); export const bTAppElementReferenceInfo = z.object({changeId: z.string(), errorCode: z.number().int().optional(), errorDescription: z.string().optional(), errorValue: bTAppElementErrorCode.optional(), parentChangeId: z.string().optional(), referenceId: z.string().optional(), transactionId: z.string().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 }; /** * Update an app element reference. * * POST /appelements/d/{did}/{wvm}/{wvmid}/e/{eid}/references/{rid} */ export const createApiAppelementsDDidWvmWvmidEEidReferencesRid = async (args: CreateApiAppelementsDDidWvmWvmidEEidReferencesRidArgs, options?: CreateApiAppelementsDDidWvmWvmidEEidReferencesRidOptions) => { 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(/\/+$/, '')}/appelements/d/${encodeURIComponent(String(args.did))}/${encodeURIComponent(String(args.wvm))}/${encodeURIComponent(String(args.wvmid))}/e/${encodeURIComponent(String(args.eid))}/references/${encodeURIComponent(String(args.rid))}${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(bTAppElementReferenceInfo, await res.json()) };