import {z} from 'zod' export type GetApiResourcesPlansV1IdArgs = {id: string}; export type GetApiResourcesPlansV1IdOptions = {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 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 Single Plan * * GET /resources/plans/v1/{id} */ export const getApiResourcesPlansV1Id = async (args: GetApiResourcesPlansV1IdArgs, options?: GetApiResourcesPlansV1IdOptions) => { const {baseUrl = "http://localhost:8181", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/resources/plans/v1/${encodeURIComponent(String(args.id))}${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(planDto, await res.json()) };