import {z} from 'zod' export type GetApiAppelementsDDidWvmWvmidEEidReferencesRidArgs = {did: string, eid: string, wvm: string, wvmid: string, rid: string, transactionId?: string | undefined, parentChangeId?: string | undefined, includeInternal?: boolean | undefined, linkDocumentId?: string | undefined}; export type GetApiAppelementsDDidWvmWvmidEEidReferencesRidOptions = {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 bTAppElementReferenceResolveInfo = z.object({changeId: z.string().optional(), errorCode: z.number().int().optional(), errorDescription: z.string().optional(), errorValue: bTAppElementErrorCode.optional(), idTag: z.string().optional(), idTagIsValid: z.boolean().optional(), isConfigurable: z.boolean().optional(), isFlattenedPart: z.boolean().optional(), isLocked: z.boolean().optional(), isSketchOnly: z.boolean().optional(), isSurface: z.boolean().optional(), latestElementMicroversionId: z.string().optional(), partIdentity: z.string().optional(), partNumber: z.string().optional(), referenceId: z.string().optional(), referenceType: z.number().int().optional(), resolvedConfiguration: z.string().optional(), resolvedDocumentMicroversionId: z.string().optional(), resolvedElementMicroversionId: z.string().optional(), revision: z.string().optional(), sketchIds: z.array(z.string()).optional(), sourceElementId: z.string().optional(), targetConfiguration: z.string().optional(), targetDocumentId: z.string().optional(), targetDocumentMicroversionId: z.string().optional(), targetElementId: z.string().optional(), targetElementMicroversionId: z.string().optional(), targetVersionId: z.string().optional(), trackNewVersions: z.boolean().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 }; /** * Resolves a single reference to an app element. * * GET /appelements/d/{did}/{wvm}/{wvmid}/e/{eid}/references/{rid} */ export const getApiAppelementsDDidWvmWvmidEEidReferencesRid = async (args: GetApiAppelementsDDidWvmWvmidEEidReferencesRidArgs, options?: GetApiAppelementsDDidWvmWvmidEEidReferencesRidOptions) => { const {baseUrl = "https://api.onshape.com/api/v17", fetch: fetchFn = fetch, OAuth2, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json;charset=UTF-8; qs=0.09") if (args.transactionId != null) query.append("transactionId", String(args.transactionId)) if (args.parentChangeId != null) query.append("parentChangeId", String(args.parentChangeId)) if (args.includeInternal != null) query.append("includeInternal", String(args.includeInternal)) if (args.linkDocumentId != null) query.append("linkDocumentId", String(args.linkDocumentId)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(bTAppElementReferenceResolveInfo, await res.json()) };