import {z} from 'zod' export type CreateApiV2ItamAssetRelationshipArgs = {body: Array<{sourceId?: number | undefined, sourceType?: ('DEVICE' | 'CHECKLIST' | 'CONTACT' | 'CREDENTIAL' | 'APPS_SERVICES_DOCUMENT' | 'TECHNICIAN' | 'END_USER' | 'KB_ARTICLE' | 'LOCATION' | 'ORGANIZATION') | undefined, targetId?: number | undefined, targetType?: ('DEVICE' | 'CHECKLIST' | 'CONTACT' | 'CREDENTIAL' | 'APPS_SERVICES_DOCUMENT' | 'TECHNICIAN' | 'END_USER' | 'KB_ARTICLE' | 'LOCATION' | 'ORGANIZATION') | undefined, relationshipTypeId?: number | undefined}>}; export type CreateApiV2ItamAssetRelationshipOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2ItamAssetRelationshipResponse = z.object({successfulRelationships: z.array(z.object({relationId: z.number().int().optional(), source: z.object({id: z.number().int().optional(), entityType: z.enum(["DEVICE", "CHECKLIST", "CONTACT", "CREDENTIAL", "APPS_SERVICES_DOCUMENT", "TECHNICIAN", "END_USER", "KB_ARTICLE", "LOCATION", "ORGANIZATION"]).optional(), name: z.string().optional(), clientId: z.number().int().optional()}).optional(), target: z.object({id: z.number().int().optional(), entityType: z.enum(["DEVICE", "CHECKLIST", "CONTACT", "CREDENTIAL", "APPS_SERVICES_DOCUMENT", "TECHNICIAN", "END_USER", "KB_ARTICLE", "LOCATION", "ORGANIZATION"]).optional(), name: z.string().optional(), clientId: z.number().int().optional()}).optional(), relation: z.object({id: z.number().int().optional(), forwardLabel: z.string().optional(), reverseLabel: z.string().optional(), systemDefault: z.boolean().optional()}).optional()})).optional(), errors: z.array(z.object({requestIndex: z.number().int().optional(), request: z.object({sourceId: z.number().int().optional(), sourceType: z.enum(["DEVICE", "CHECKLIST", "CONTACT", "CREDENTIAL", "APPS_SERVICES_DOCUMENT", "TECHNICIAN", "END_USER", "KB_ARTICLE", "LOCATION", "ORGANIZATION"]).optional(), targetId: z.number().int().optional(), targetType: z.enum(["DEVICE", "CHECKLIST", "CONTACT", "CREDENTIAL", "APPS_SERVICES_DOCUMENT", "TECHNICIAN", "END_USER", "KB_ARTICLE", "LOCATION", "ORGANIZATION"]).optional(), relationshipTypeId: z.number().int().optional()}).optional(), errorCode: z.string().optional(), errorMessage: z.string().optional()})).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 multiple asset relationships * * POST /v2/itam/asset-relationship * * Also accepts auth: sessionKey */ export const createApiV2ItamAssetRelationship = async (args: CreateApiV2ItamAssetRelationshipArgs, options: CreateApiV2ItamAssetRelationshipOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/itam/asset-relationship${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(createApiV2ItamAssetRelationshipResponse, await res.json()) };