import {z} from 'zod' export type GetApiApiV1ProjectsProjectUuidSpacesArgs = {projectUuid: string}; export type GetApiApiV1ProjectsProjectUuidSpacesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const spaceMemberRole = z.enum(["viewer", "editor", "admin"]); export const organizationMemberRole = z.enum(["member", "viewer", "interactive_viewer", "editor", "developer", "admin"]); export const projectMemberRole = z.enum(["viewer", "interactive_viewer", "editor", "developer", "admin"]); export const spaceAccess = z.object({inheritedFrom: z.enum(["organization", "project", "group", "space_group", "parent_space"]).optional(), inheritedRole: z.union([organizationMemberRole, projectMemberRole]).optional(), projectRole: projectMemberRole.optional(), hasDirectAccess: z.boolean(), role: spaceMemberRole, userUuid: z.string()}); export const spaceSummary = z.object({name: z.string(), slug: z.string(), uuid: z.string(), organizationUuid: z.string(), projectUuid: z.string(), inheritParentPermissions: z.boolean(), projectMemberAccessRole: spaceMemberRole.nullable(), pinnedListUuid: z.string().nullable(), pinnedListOrder: z.number().nullable(), parentSpaceUuid: z.string().nullable(), path: z.string(), deletedBy: z.object({lastName: z.string(), firstName: z.string(), userUuid: z.string()}).optional(), deletedAt: z.string().optional(), appCount: z.number(), childSpaceCount: z.number(), dashboardCount: z.number(), chartCount: z.number(), access: z.array(z.string()), userAccess: spaceAccess.optional(), inheritsFromOrgOrProject: z.boolean()}); export const apiSpaceSummaryListResponse = z.object({results: z.array(spaceSummary), status: z.literal("ok")}); 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 spaces in project * * GET /api/v1/projects/{projectUuid}/spaces */ export const getApiApiV1ProjectsProjectUuidSpaces = async (args: GetApiApiV1ProjectsProjectUuidSpacesArgs, options: GetApiApiV1ProjectsProjectUuidSpacesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/projects/${encodeURIComponent(String(args.projectUuid))}/spaces${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(apiSpaceSummaryListResponse, await res.json()) };