import {z} from 'zod' export type GetApiResourcesFeaturesV1Args = {offset?: number | undefined, limit?: number | undefined, filter?: string | undefined, orderBy?: ('name' | 'key' | 'createdAt') | undefined, sortType?: ('ASC' | 'DESC') | undefined, featureIds?: Array | undefined, permissionKeys?: Array | undefined, hasFeatureFlag?: boolean | undefined}; export type GetApiResourcesFeaturesV1Options = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const featureFlagDtoThin = z.object({id: z.string().optional(), name: z.string().optional(), on: z.boolean().optional(), offTreatment: z.enum(["true", "false"]).optional(), defaultTreatment: z.enum(["true", "false"]).optional(), description: z.string().optional(), updatedAt: z.string().optional(), createdAt: z.string().optional()}); export const featureDto = z.object({id: z.string(), vendorId: z.string(), name: z.string(), key: z.string(), metadata: z.object({}), description: z.string().optional(), createdAt: z.string(), updatedAt: z.string().optional(), permissions: z.array(z.string()).optional(), featureFlag: featureFlagDtoThin.optional()}); export const getApiResourcesFeaturesV1Response = z.object({items: z.array(featureDto).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 Features * * GET /resources/features/v1 */ export const getApiResourcesFeaturesV1 = async (args: GetApiResourcesFeaturesV1Args, options?: GetApiResourcesFeaturesV1Options) => { 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.permissionKeys ?? []) query.append("permissionKeys", String(item)) if (args.hasFeatureFlag != null) query.append("hasFeatureFlag", String(args.hasFeatureFlag)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/resources/features/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(getApiResourcesFeaturesV1Response, await res.json()) };