import {z} from 'zod' export type GetApiResourcesPlansV1Args = {offset?: number | undefined, limit?: number | undefined, filter?: string | undefined, orderBy?: ('name' | 'createdAt') | undefined, sortType?: ('ASC' | 'DESC') | undefined, featureIds?: Array | undefined, userIds?: Array | undefined, tenantIds?: Array | undefined, includeTenantEntitlements?: string | undefined}; export type GetApiResourcesPlansV1Options = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const conditionDto = z.object({attribute: z.string(), attributeType: z.enum(["custom", "frontegg"]), negate: z.boolean(), op: z.enum(["in_list", "starts_with", "ends_with", "contains", "matches", "equal", "greater_than", "greater_than_equal", "lower_than", "lower_than_equal", "between_numeric", "is", "on", "between_date", "on_or_after", "on_or_before"]), value: z.object({})}); export const ruleDto = z.object({description: z.string(), conditionLogic: z.literal("and"), conditions: z.array(conditionDto), treatment: z.enum(["true", "false"])}); export const planFeatureDto = z.object({id: z.string(), name: z.string(), key: z.string(), permissions: z.array(z.string())}); export const planDto = z.object({id: z.string(), vendorId: z.string(), name: z.string(), defaultTreatment: z.enum(["true", "false"]).optional(), rules: z.array(ruleDto).optional(), description: z.string().optional(), metadata: z.object({}).optional(), defaultTimeLimitation: z.number().optional(), assignOnSignup: z.boolean(), createdAt: z.string(), updatedAt: z.string().optional(), features: planFeatureDto.optional(), entitlements: planFeatureDto.optional()}); export const getApiResourcesPlansV1Response = z.object({items: z.array(planDto).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 Plan * * GET /resources/plans/v1 */ export const getApiResourcesPlansV1 = async (args: GetApiResourcesPlansV1Args, options?: GetApiResourcesPlansV1Options) => { 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.filter != null) query.append("filter", String(args.filter)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.sortType != null) query.append("sortType", String(args.sortType)) for (const item of args.featureIds ?? []) query.append("featureIds", String(item)) for (const item of args.userIds ?? []) query.append("userIds", String(item)) for (const item of args.tenantIds ?? []) query.append("tenantIds", String(item)) if (args.includeTenantEntitlements != null) query.append("includeTenantEntitlements", String(args.includeTenantEntitlements)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/resources/plans/v1${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(getApiResourcesPlansV1Response, await res.json()) };