import {z} from 'zod' export type ProjectsPostRequest = {name: string, organization_id: string}; export type CreateApiOrganizationsProjectsArgs = {body: ProjectsPostRequest}; export type CreateApiOrganizationsProjectsOptions = {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 projectsPutPostResponseV1 = z.object({project: 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 }; /** * Creates a project and returns the created project. * * POST /organizations/projects */ export const createApiOrganizationsProjects = async (args: CreateApiOrganizationsProjectsArgs, options?: CreateApiOrganizationsProjectsOptions) => { const {baseUrl = "https://api.cloud.crusoe.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/organizations/projects${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(projectsPutPostResponseV1, await res.json()) };