import {z} from 'zod' export type BulkCreatePlatformUsersRequest = {emails: Array, organizationId: string, groupId?: (string | null) | undefined, groupIds?: Array | undefined}; export type CreateApiApiPlatformUsersBulkArgs = {body: BulkCreatePlatformUsersRequest}; export type CreateApiApiPlatformUsersBulkOptions = {baseUrl: string, Auth0AuthorizationCode?: string | undefined, fetch?: typeof fetch | undefined}; export const inviteEmailStatus = z.enum(["sent", "unconfirmed", "failed"]); export const user = z.object({userId: z.string(), email: z.string().nullable().optional(), name: z.string().nullable().optional(), picture: z.string().nullable().optional(), loginCount: z.number().int().optional(), lastLogin: z.string().nullable().optional(), createdAt: z.string().nullable().optional(), emailVerified: z.boolean().nullable().optional(), needsInvitation: z.boolean().nullable().optional(), inviteEmailStatus: inviteEmailStatus.nullable().optional()}); export const bulkUserCreateFailure = z.object({email: z.string(), error: z.string()}); export const rejectedTupleKey = z.object({user: z.string(), relation: z.string(), object: z.string()}); export const rejectedTuple = z.object({operation: z.enum(["write", "delete"]), tuple: rejectedTupleKey, reason: z.string()}); export const bulkCreatePlatformUsersResponseWithFgaWriteReport = z.object({created: z.array(user).optional(), failed: z.array(bulkUserCreateFailure).optional(), total: z.number().int().optional(), rejectedTuples: z.array(rejectedTuple).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 Users * * POST /api/platform/users/bulk */ export const createApiApiPlatformUsersBulk = async (args: CreateApiApiPlatformUsersBulkArgs, options: CreateApiApiPlatformUsersBulkOptions) => { const {baseUrl, fetch: fetchFn = fetch, Auth0AuthorizationCode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Auth0AuthorizationCode !== undefined) headers.set('authorization', `Bearer ${Auth0AuthorizationCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/platform/users/bulk${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(bulkCreatePlatformUsersResponseWithFgaWriteReport, await res.json()) };