import {z} from 'zod' export type GetApiGroupsGroupIdAppsAppIdServiceServiceIdConfigOptions = {baseUrl?: string | undefined, tokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const flexibleSync = z.object({state: z.string(), database_name: z.string().optional(), queryable_fields_names: z.array(z.string()).optional(), indexed_queryable_fields_names: z.array(z.string()).optional(), collection_queryable_fields_names: z.record(z.string(), z.record(z.string(), z.string())).optional(), permissions: z.object({}).optional(), last_disabled: z.string().optional(), client_max_offline_days: z.number().int().optional(), is_recovery_mode_disabled: z.boolean().optional(), asymmetric_tables: z.array(z.string()).optional()}); export const syncDataResponse = z.object({clusterId: z.string().optional(), clusterName: z.string().optional(), clusterType: z.enum(["atlas", "serverless"]).optional(), flexible_sync: flexibleSync.optional(), groupName: z.string().optional(), orgName: z.string().optional(), readPreference: z.enum(["primary", "primaryPreferred", "secondary", "secondaryPreferred", "nearest"]).optional(), wireProtocolEnabled: 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 Flexible Sync information * * GET /groups/{groupId}/apps/{appId}/service/{serviceId}/config */ export const getApiGroupsGroupIdAppsAppIdServiceServiceIdConfig = async (options?: GetApiGroupsGroupIdAppsAppIdServiceServiceIdConfigOptions) => { const {baseUrl = "https://services.cloud.mongodb.com/api/admin/v3.0", fetch: fetchFn = fetch, tokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (tokenAuth !== undefined) headers.set('authorization', `Bearer ${tokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/groups/${encodeURIComponent(String(args.groupId))}/apps/${encodeURIComponent(String(args.appId))}/service/${encodeURIComponent(String(args.serviceId))}/config${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(syncDataResponse, await res.json()) };