import {z} from 'zod' export type GetApiV2SoftwareLicenseLicensesArgs = {after?: string | undefined, before?: string | undefined, pageSize?: number | undefined}; export type GetApiV2SoftwareLicenseLicensesOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2SoftwareLicenseLicensesResponse = z.object({id: z.number().int().optional(), name: z.string().optional(), type: z.enum(["PER_DEVICE", "PER_USER", "CUSTOM"]).optional(), description: z.string().optional(), purchaseDate: z.number().int().optional(), automated: z.boolean().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(), currentUsage: z.number().int().optional(), expired: z.boolean().optional(), daysToExpire: z.number().int().optional(), note: z.string().optional(), automation: z.object({assignmentType: z.enum(["NORMALIZED_SOFTWARE", "SOFTWARE_INVENTORY", "DEVICE_SEARCH_GROUPS", "USER_CONTACT"]).optional(), type: z.string()}).optional(), usageMonitoring: z.array(z.object({osType: z.enum(["WINDOWS", "LINUX", "MAC"]).optional(), binaryNames: z.array(z.string()).optional(), nonMatches: z.boolean().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 }; /** * Get All Software Licenses * * GET /v2/software-license/licenses * * Also accepts auth: sessionKey */ export const getApiV2SoftwareLicenseLicenses = async (args: GetApiV2SoftwareLicenseLicensesArgs, options: GetApiV2SoftwareLicenseLicensesOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/software-license/licenses${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(getApiV2SoftwareLicenseLicensesResponse, await res.json()) };