import {z} from 'zod' export type VerifyLicenseArgs = {licenseEnvelope: string}; export type CreateApiLicensesVerifyArgs = {body: VerifyLicenseArgs}; export type CreateApiLicensesVerifyOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const longMap = z.record(z.string(), z.number().int()); export const licenseGateRules = z.object({enabled: z.array(z.string()).optional(), disabled: z.array(z.string()).optional()}); export const licenseGatePolicy = z.object({productFeatures: licenseGateRules.optional(), commands: licenseGateRules.optional()}); export const licenseStatusListReference = z.object({uri: z.string(), index: z.number().int(), purpose: z.string(), tokenFormat: z.string(), listId: z.string().optional()}); export const stringMap = z.record(z.string(), z.string()); export const licenseIssuanceRequest = z.object({schema: z.string(), licenseId: z.string(), product: z.string(), edition: z.string(), customerPartyId: z.string(), customerName: z.string(), softwarePartyId: z.string(), installationId: z.string(), installationName: z.string(), features: z.array(z.string()), quotas: longMap, gatePolicy: licenseGatePolicy.optional(), validFrom: z.string(), expiresAt: z.string(), issuedAt: z.string(), issuer: z.string(), keyId: z.string(), profileId: z.string().optional(), recipientKeyId: z.string().optional(), recipientPublicKeyFingerprint: z.string().optional(), licenseRequestId: z.string().optional(), requestArtifactDigest: z.string().optional(), recipientKeyThumbprint: z.string().optional(), installationBaseDomain: z.string().optional(), platformDomainRoots: z.array(z.string()).optional(), statusList: licenseStatusListReference.optional(), checkInterval: z.string().optional(), checkGrace: z.string().optional(), stapledStatusListToken: z.string().optional(), metadata: stringMap}); export const verifyLicenseResult = z.object({valid: z.boolean(), claims: licenseIssuanceRequest.optional(), error: 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 }; /** * Verify a license artifact * * POST /licenses/verify */ export const createApiLicensesVerify = async (args: CreateApiLicensesVerifyArgs, options?: CreateApiLicensesVerifyOptions) => { const {baseUrl = "https://api.example.com/api/license-portal/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/licenses/verify${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(verifyLicenseResult, await res.json()) };