import {z} from 'zod' export type CreateApiV2SoftwareLicenseArgs = {body: {name: string, description: string, type: 'PER_DEVICE' | 'PER_USER' | 'CUSTOM', purchaseDate: number, publisherId: number, vendorId: number, scope: {global?: boolean | undefined, organizationIds?: Array | undefined, locationIds?: Array | undefined}, quantity: number, currentUsage: number, term: {renewalUnit?: ('MONTH' | 'YEAR') | undefined, value?: number | undefined, expirationDate?: number | undefined, autoRenewal?: boolean | undefined, generateActivityAlert?: boolean | undefined, daysBeforeExpiration?: number | undefined, hasNotifiedExpirationDate?: boolean | undefined, hasNotifiedUpToRenewal?: boolean | undefined}, notificationChannelInfo: {email?: string | undefined, sms?: string | undefined, pushNotification?: string | undefined}, assigmentAutomationSettings?: ({assignmentType?: ('NORMALIZED_SOFTWARE' | 'SOFTWARE_INVENTORY' | 'DEVICE_SEARCH_GROUPS' | 'USER_CONTACT') | undefined, type: string}) | undefined, assignedLicenses?: ({[key: string]: Array | null} | null) | undefined, unassignedLicenses?: ({[key: string]: Array | null} | null) | undefined, note?: (string | null) | undefined, costMode?: ('TOTAL' | 'PER_LICENSE' | null) | undefined, cost?: (number | null) | undefined, usageMonitoring?: ({binaries?: (Array<{osType?: ('WINDOWS' | 'LINUX' | 'MAC') | undefined, binaryNames?: Array | undefined, nonMatches?: boolean | undefined}>) | undefined} | null) | undefined}}; export type CreateApiV2SoftwareLicenseOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2SoftwareLicenseResponse = z.object({id: z.number().int().optional(), name: z.string().optional(), description: z.string().optional(), type: z.enum(["PER_DEVICE", "PER_USER", "CUSTOM"]).optional(), automated: z.boolean().optional(), purchaseDate: z.number().int().optional(), publisherId: z.number().int().optional(), vendorId: z.number().int().optional(), publisher: z.object({id: z.number().int().optional(), name: z.string().optional(), description: z.string().optional()}).optional(), vendor: z.object({id: z.number().int().optional(), name: z.string().optional(), description: z.string().optional()}).optional(), scope: z.object({global: z.boolean().optional(), organizations: z.array(z.object({id: z.number().int().optional(), name: z.string().optional()})).optional(), locations: z.array(z.object({id: z.number().int().optional(), name: z.string().optional(), organizationId: z.number().int().optional()})).optional()}).optional(), quantity: z.number().int().optional(), term: z.object({renewalUnit: z.enum(["MONTH", "YEAR"]).optional(), value: z.number().int().optional(), expirationDate: z.number().int().optional(), autoRenewal: z.boolean().optional(), generateActivityAlert: z.boolean().optional(), daysBeforeExpiration: z.number().int().optional(), hasNotifiedExpirationDate: z.boolean().optional(), hasNotifiedUpToRenewal: z.boolean().optional()}).optional(), notificationChannelInfo: z.object({email: z.string().optional(), sms: z.string().optional(), pushNotification: z.string().optional()}).optional(), assigmentAutomationSettings: z.object({assignmentType: z.enum(["NORMALIZED_SOFTWARE", "SOFTWARE_INVENTORY", "DEVICE_SEARCH_GROUPS", "USER_CONTACT"]).optional(), type: z.string()}).optional(), licenseAssignmentList: z.array(z.object({licenseeId: z.number().int().optional(), licenseeName: z.string().optional(), licenseeType: z.enum(["NODE", "APP_USER", "CONTACT"]).optional(), licenseeUserType: z.enum(["TECHNICIAN", "END_USER", "CONTACT"]).optional(), licenseeOrganizationName: z.string().optional(), licenseeOrganizationId: z.number().int().optional(), licenseeEmail: z.string().optional(), firstFoundAt: z.number().int().optional(), lastUsedAt: z.number().int().optional(), hasViewAccess: z.boolean().optional()})).optional(), currentUsage: z.number().int().optional(), expired: z.boolean().optional(), daysToExpire: z.number().int().optional(), note: z.string().optional(), perLicenseCost: z.number().optional(), totalCost: z.number().optional(), costMode: z.enum(["TOTAL", "PER_LICENSE"]).optional(), currency: z.enum(["USD", "EUR", "CAD", "GBP", "AUD", "JPY", "SEK", "NOK", "DKK", "CHF", "AED", "AFN", "ALL", "AMD", "AOA", "ARS", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTN", "BWP", "BYN", "BZD", "CDF", "CHE", "CHW", "CLF", "CLP", "CNY", "COP", "CRC", "CVE", "CZK", "DJF", "DOP", "DZD", "EGP", "ERN", "ETB", "FJD", "FKP", "GEL", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HTG", "HUF", "IDR", "ILS", "INR", "IRR", "ISK", "JMD", "JOD", "KES", "KGS", "KHR", "KMF", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRU", "MUR", "MVR", "MWK", "MXN", "MXV", "MYR", "MZN", "NAD", "NGN", "NIO", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SGD", "SHP", "SLE", "SOS", "SRD", "SSP", "STN", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "UYI", "UYU", "UZS", "VES", "VND", "VUV", "WST", "XAD", "XAF", "XCD", "XPF", "YER", "ZAR", "ZMW", "ZWG"]).optional(), usageMonitoring: z.array(z.object({osType: z.enum(["WINDOWS", "LINUX", "MAC"]).optional(), binaryNames: z.array(z.string()).optional(), nonMatches: z.boolean().optional()})).optional(), firstFoundAt: z.number().int().optional(), lastUsageAt: z.number().int().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 a Software License * * POST /v2/software-license * * Also accepts auth: sessionKey */ export const createApiV2SoftwareLicense = async (args: CreateApiV2SoftwareLicenseArgs, options: CreateApiV2SoftwareLicenseOptions) => { 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/software-license${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(createApiV2SoftwareLicenseResponse, await res.json()) };