import {z} from 'zod' export type CreateApiProjectsProjectIdInvitationsArgs = {body: {role?: ('admin' | 'viewer') | undefined, email: string, permissions?: (Array<{type?: ('production' | 'staging' | 'development') | undefined, role?: ('admin' | 'viewer' | 'contributor') | undefined}>) | undefined, environments?: (Array<{id?: string | undefined, role?: ('admin' | 'viewer' | 'contributor') | undefined}>) | undefined, force?: boolean | undefined}}; export type CreateApiProjectsProjectIdInvitationsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const projectInvitation = z.object({id: z.string().optional(), state: z.enum(["pending", "processing", "accepted", "cancelled", "error"]).optional(), project_id: z.string().optional(), role: z.enum(["admin", "viewer"]).optional(), email: z.string().optional(), owner: z.object({id: z.string().optional(), display_name: z.string().optional()}).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), finished_at: z.string().nullable().optional(), environments: z.array(z.object({id: z.string().optional(), type: z.string().optional(), role: z.enum(["admin", "viewer", "contributor"]).optional(), title: z.string().optional()})).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 }; /** * Invite user to a project by email * * POST /projects/{project_id}/invitations */ export const createApiProjectsProjectIdInvitations = async (args: CreateApiProjectsProjectIdInvitationsArgs, options?: CreateApiProjectsProjectIdInvitationsOptions) => { const {baseUrl = "https://api.upsun.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.project_id))}/invitations${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(projectInvitation, await res.json()) };