import {z} from 'zod' export type AssignableTeamRole = 'admin' | 'member'; export type InviteTeamRoleGrant = {role: AssignableTeamRole, scope: 'team'}; export type AssignableAppRole = 'app_developer' | 'app_viewer'; export type InviteAppRoleGrant = {app_id: string, role: AssignableAppRole, scope: 'app'}; export type CreateInvitationSchema = {email: string, roles: Array}; export type CreateApiApiV3TeamsTeamIdInvitationsArgs = {team_id: string, body: CreateInvitationSchema}; export type CreateApiApiV3TeamsTeamIdInvitationsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const assignableTeamRole = z.enum(["admin", "member"]); export const inviteTeamRoleGrant = z.object({role: assignableTeamRole, scope: z.literal("team")}); export const assignableAppRole = z.enum(["app_developer", "app_viewer"]); export const inviteAppRoleGrant = z.object({app_id: z.string(), role: assignableAppRole, scope: z.literal("app")}); export const invitationStatus = z.enum(["pending", "accepted", "revoked"]); export const invitationSchema = z.object({id: z.string(), email: z.string(), roles: z.array(z.union([inviteTeamRoleGrant, inviteAppRoleGrant])), status: invitationStatus, created_at: z.string(), expires_at: z.string()}); export const invitationSchemaNoPagination = z.object({data: invitationSchema}); 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 a team invitation * * POST /api/v3/teams/{team_id}/invitations */ export const createApiApiV3TeamsTeamIdInvitations = async (args: CreateApiApiV3TeamsTeamIdInvitationsArgs, options?: CreateApiApiV3TeamsTeamIdInvitationsOptions) => { const {baseUrl = "https://codemagic.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-auth-token", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/teams/${encodeURIComponent(String(args.team_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(invitationSchemaNoPagination, await res.json()) };