import {z} from 'zod' export type GetApiWorkspaceSpacesSpaceArgs = {workspace: string, space: string}; export type GetApiWorkspaceSpacesSpaceOptions = {baseUrl?: string | undefined, accessTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({href: z.string(), templated: z.boolean().optional()}); export const spaceLinks = z.object({self: link, detail: link, space_report_pins: link.optional(), web: link.optional(), reports: link, creator: link.optional(), user_space_membership: link.optional(), space_memberships: link.optional(), preview_space_memberships: link.optional(), search_space_permissions: link, viewed: link.optional()}); export const spaceEmbeds = z.unknown(); export const space = z.object({token: z.string(), id: z.number().int(), space_type: z.string(), name: z.string(), description: z.string(), state: z.string(), restricted: z.boolean(), free_default: z.string(), "viewable?": z.string(), "viewed?": z.string().optional(), default_access_level: z.string().optional(), _links: spaceLinks, _embedded: spaceEmbeds.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 a Collection * * GET /{workspace}/spaces/{space} */ export const getApiWorkspaceSpacesSpace = async (args: GetApiWorkspaceSpacesSpaceArgs, options?: GetApiWorkspaceSpacesSpaceOptions) => { const {baseUrl = "https://modeanalytics.com/api", fetch: fetchFn = fetch, accessTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/hal+json") if (accessTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${accessTokenAuth.username}:${accessTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.workspace))}/spaces/${encodeURIComponent(String(args.space))}${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(space, await res.json()) };