import {z} from 'zod' export type GetApiResourcesEntitlementsV2Args = {offset?: number | undefined, limit?: number | undefined, sortType?: ('ASC' | 'DESC') | undefined, planId?: string | undefined, planIds?: Array | undefined, assignLevel?: ('USER' | 'TENANT') | undefined, orderBy?: ('expirationDate' | 'createdAt') | undefined, userIds?: Array | undefined, tenantIds?: Array | undefined, withRelations?: boolean | undefined}; export type GetApiResourcesEntitlementsV2Options = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const entitlementPlan = z.object({id: z.string(), vendorId: z.string(), name: z.string(), defaultTimeLimitation: z.number().optional(), description: z.string().optional(), createdAt: z.string(), updatedAt: z.string().optional()}); export const entitlementDto = z.object({id: z.string(), planId: z.string(), tenantId: z.string(), userId: z.string().optional(), expirationDate: z.string().optional(), createdAt: z.string(), updatedAt: z.string().optional(), plan: entitlementPlan}); export const getApiResourcesEntitlementsV2Response = z.object({items: z.array(entitlementDto).optional(), hasNext: z.boolean().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 Entitlements * * GET /resources/entitlements/v2 */ export const getApiResourcesEntitlementsV2 = async (args: GetApiResourcesEntitlementsV2Args, options?: GetApiResourcesEntitlementsV2Options) => { const {baseUrl = "http://localhost:8181", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sortType != null) query.append("sortType", String(args.sortType)) if (args.planId != null) query.append("planId", String(args.planId)) for (const item of args.planIds ?? []) query.append("planIds", String(item)) if (args.assignLevel != null) query.append("assignLevel", String(args.assignLevel)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) for (const item of args.userIds ?? []) query.append("userIds", String(item)) for (const item of args.tenantIds ?? []) query.append("tenantIds", String(item)) if (args.withRelations != null) query.append("withRelations", String(args.withRelations)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/resources/entitlements/v2${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(getApiResourcesEntitlementsV2Response, await res.json()) };