import {z} from 'zod' export type GetApiOrganizationsProjectsArgs = {org_id?: string | undefined, project_name?: string | undefined, skip_loading_project_resources?: boolean | undefined}; export type GetApiOrganizationsProjectsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const projectResourceDetails = z.object({count: z.number().int()}); export const projectResources = z.object({disks: projectResourceDetails, images: projectResourceDetails, instances: projectResourceDetails, snapshots: projectResourceDetails, vpc_networks: projectResourceDetails}); export const project = z.object({id: z.string(), name: z.string(), organization_id: z.string(), relation: z.string(), resources: projectResources}); export const listProjectsResponseV1 = z.object({items: z.array(project)}); 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 }; /** * Lists all projects the logged-in user belongs to or owns and returns their details. * * GET /organizations/projects */ export const getApiOrganizationsProjects = async (args: GetApiOrganizationsProjectsArgs, options?: GetApiOrganizationsProjectsOptions) => { const {baseUrl = "https://api.cloud.crusoe.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.org_id != null) query.append("org_id", String(args.org_id)) if (args.project_name != null) query.append("project_name", String(args.project_name)) if (args.skip_loading_project_resources != null) query.append("skip_loading_project_resources", String(args.skip_loading_project_resources)) const url = `${baseUrl.replace(/\/+$/, '')}/organizations/projects${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(listProjectsResponseV1, await res.json()) };