import {z} from 'zod' export type CreateUserDto = {email: string, name?: string | undefined, profilePictureUrl?: string | undefined, password?: string | undefined, phoneNumber?: string | undefined, provider?: ('local' | 'saml' | 'google' | 'github' | 'facebook' | 'microsoft' | 'scim2' | 'slack' | 'apple') | undefined, metadata?: string | undefined, skipInviteEmail?: boolean | undefined, roleIds?: Array | undefined, emailMetadata?: Record | undefined, expirationInSeconds?: number | undefined}; export type CreateApiResourcesUsersV2Args = {'frontegg-tenant-id': string, body: CreateUserDto}; export type CreateApiResourcesUsersV2Options = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const userCreatedResponse = z.object({id: z.string().optional(), email: z.string().optional(), verified: z.boolean().optional(), name: z.string().optional(), profilePictureUrl: z.string().optional(), roles: z.array(z.string()).optional(), permissions: z.array(z.string()).optional(), metadata: z.string().optional(), createdAt: z.string().optional(), lastLogin: z.string().optional(), activatedForTenant: z.boolean().optional(), temporaryExpirationDate: z.string().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 * * POST /resources/users/v2 */ export const createApiResourcesUsersV2 = async (args: CreateApiResourcesUsersV2Args, options?: CreateApiResourcesUsersV2Options) => { const {baseUrl = "http://localhost:8181", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['frontegg-tenant-id'] != null) headers.set("frontegg-tenant-id", String(args['frontegg-tenant-id'])) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/resources/users/v2${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(userCreatedResponse, await res.json()) };