import {z} from 'zod' export type LicenseValidationRequest = {license_key: string, instance: string, domain?: (string | null) | undefined, installation_path?: (string | null) | undefined, application_version?: (string | null) | undefined}; export type CreateApiLicenseValidateArgs = {body: LicenseValidationRequest}; export type CreateApiLicenseValidateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const licenseStatus = z.enum(["active", "reissued", "suspended", "expired", "revoked"]); export const licenseValidationResponse = z.object({success: z.literal(true), valid: z.boolean(), status: licenseStatus, reason: z.enum(["license_reissued", "license_suspended", "license_expired", "license_revoked", "activation_not_found", "installation_mismatch", "null"]).nullable(), activations_used: z.number().int().gte(0), activation_limit: z.number().int().gte(1).nullable(), offline_token: z.string().nullable(), offline_public_key: z.string().nullable(), offline_expires_at: z.number().int().nullable()}); 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 }; /** * Validate a software license * * POST /license/validate */ export const createApiLicenseValidate = async (args: CreateApiLicenseValidateArgs, options?: CreateApiLicenseValidateOptions) => { const {baseUrl = "https://demo.onlinebillingform.com/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/license/validate${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(licenseValidationResponse, await res.json()) };