import {z} from 'zod' export type ProjectTypeEnum = 'public'; export type Object = {id?: string | undefined, name?: string | undefined}; export type Creator = {id?: string | undefined, email?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, organization?: Object | undefined}; export type CreatedAt = string; export type ProjectCreate = {id?: string | undefined, name: string, description?: string | undefined, type?: ProjectTypeEnum | undefined, members?: number | undefined, networks?: number | undefined, creator?: Creator | undefined, created_at?: CreatedAt | undefined}; export type CreateApiV1ProjectsArgs = {body: ProjectCreate}; export type CreateApiV1ProjectsOptions = {baseUrl?: string | undefined, APIKeyAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const projectTypeEnum = z.literal("public"); export const object = z.object({id: z.string().optional(), name: z.string().optional()}); export const creator = z.object({id: z.string().optional(), email: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), organization: object.optional()}); export const createdAt = z.string(); export const project = z.object({id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), type: projectTypeEnum.optional(), members: z.number().int().optional(), networks: z.number().int().optional(), creator: creator.optional(), created_at: createdAt.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 }; /** * Create Project * * POST /v1/projects/ */ export const createApiV1Projects = async (args: CreateApiV1ProjectsArgs, options?: CreateApiV1ProjectsOptions) => { const {baseUrl = "https://api.chainstack.com", fetch: fetchFn = fetch, APIKeyAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyAuthentication !== undefined) headers.set('authorization', `Bearer ${APIKeyAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(project, await res.json()) };