import {z} from 'zod' export type GetApiV4OrganizationsOrganizationIdProjectsProjectIdReplicationsArgs = {page?: number | undefined, perPage?: number | undefined, sortBy?: Array | undefined, sortDirection?: ('asc' | 'desc') | undefined, organizationId: string, projectId: string}; export type GetApiV4OrganizationsOrganizationIdProjectsProjectIdReplicationsOptions = {baseUrl?: string | undefined, token?: string | undefined, fetch?: typeof fetch | undefined}; export const replicationAuditData = z.object({createdBy: z.string(), createdAt: z.string()}); export const replicationSummary = z.object({id: z.string(), sourceCluster: z.string(), targetCluster: z.string(), status: z.enum(["pending", "pausing", "failed", "paused", "running"]), direction: z.enum(["oneWay", "twoWay"]).optional(), audit: replicationAuditData}); export const pages = z.object({page: z.number().int(), next: z.number().int().optional(), previous: z.number().int().optional(), last: z.number().int(), perPage: z.number().int(), totalItems: z.number().int()}); export const hrefs = z.object({first: z.string(), last: z.string(), previous: z.string(), next: z.string()}); export const cursor = z.object({pages: pages, hrefs: hrefs}); export const listProjectReplicationsResponse = z.object({data: z.array(replicationSummary), cursor: cursor}); 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 }; /** * List replications for a given project * * GET /v4/organizations/{organizationId}/projects/{projectId}/replications */ export const getApiV4OrganizationsOrganizationIdProjectsProjectIdReplications = async (args: GetApiV4OrganizationsOrganizationIdProjectsProjectIdReplicationsArgs, options?: GetApiV4OrganizationsOrganizationIdProjectsProjectIdReplicationsOptions) => { const {baseUrl = "https://cloudapi.cloud.couchbase.com", fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.perPage != null) query.append("perPage", String(args.perPage)) for (const item of args.sortBy ?? []) query.append("sortBy", String(item)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (token !== undefined) headers.set('authorization', `Bearer ${token}`) const url = `${baseUrl.replace(/\/+$/, '')}/v4/organizations/${encodeURIComponent(String(args.organizationId))}/projects/${encodeURIComponent(String(args.projectId))}/replications${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(listProjectReplicationsResponse, await res.json()) };