import {z} from 'zod' export type GetApiV1LicenseStatusArgs = {license_key: string, instance_id: string}; export type GetApiV1LicenseStatusOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const quotaStatus = z.object({used_chars: z.number().int(), used_seconds: z.number(), quota_chars: z.number().int().nullable().optional(), quota_seconds: z.number().nullable().optional(), exceeded: z.boolean()}); export const legacyLicenseStatusResponse = z.object({active: z.boolean(), quota: quotaStatus}); 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 }; /** * Status * * GET /v1/license/status */ export const getApiV1LicenseStatus = async (args: GetApiV1LicenseStatusArgs, options: GetApiV1LicenseStatusOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.license_key != null) query.append("license_key", String(args.license_key)) if (args.instance_id != null) query.append("instance_id", String(args.instance_id)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/license/status${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(legacyLicenseStatusResponse, await res.json()) };