import {z} from 'zod' export type GetApiV2WorkspacesWorkspaceIdArgs = {workspaceId: string}; export type GetApiV2WorkspacesWorkspaceIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const workspaceMember = z.object({id: z.string(), name: z.string().nullable(), group: z.enum(["owner", "admin", "maintain", "manager", "view"]), joinedAt: z.string()}); export const workspaceApp = z.object({id: z.string(), name: z.string(), desc: z.string().nullable(), ram: z.number().int(), lang: z.string(), domain: z.string().nullable().optional(), custom: z.string().nullable().optional()}); export const workspace = z.object({id: z.string(), name: z.string(), owner: z.string(), members: z.array(workspaceMember), applications: z.array(workspaceApp), createdAt: z.string()}); export const getApiV2WorkspacesWorkspaceIdResponse = z.object({status: z.literal("success"), response: workspace}); 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 single workspace * * GET /v2/workspaces/{workspaceId} */ export const getApiV2WorkspacesWorkspaceId = async (args: GetApiV2WorkspacesWorkspaceIdArgs, options?: GetApiV2WorkspacesWorkspaceIdOptions) => { const {baseUrl = "https://api.squarecloud.app", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/workspaces/${encodeURIComponent(String(args.workspaceId))}${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(getApiV2WorkspacesWorkspaceIdResponse, await res.json()) };