import {z} from 'zod' export type CreateApiUserProfileV1UsersArgs = {body: {externalId: string, firstName?: string | undefined, lastName?: string | undefined, uid?: string | undefined, displayName?: string | undefined, email?: string | undefined, roles: Array<'authenticatedVisitor' | 'viewer' | 'editor' | 'manager' | 'admin'>}}; export type CreateApiUserProfileV1UsersOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiUserProfileV1UsersResponse = z.object({id: z.string(), externalId: z.string().max(512), firstName: z.string().max(512).optional(), lastName: z.string().max(512).optional(), uid: z.string().max(30).optional(), displayName: z.string().max(512).optional(), email: z.string().max(512).optional(), roles: z.array(z.enum(["authenticatedVisitor", "viewer", "editor", "manager", "admin"])), links: z.object({self: z.object({href: z.string(), methods: z.array(z.enum(["GET", "POST", "PUT", "DELETE"])), name: z.object({}).optional()})}).optional(), lastLogin: z.string().optional(), created: z.string().optional(), creator: z.object({id: z.string()}).optional(), lastModified: z.string().optional(), lastModifier: z.object({id: z.string()}).optional(), privacyNotice: z.object({version: z.string().min(1).max(20).optional(), accepted: z.string().optional(), locale: z.string().min(1).max(20).optional(), acceptRequired: z.boolean()}).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 }; /** * Add a new user. * * POST /user-profile/v1/users */ export const createApiUserProfileV1Users = async (args: CreateApiUserProfileV1UsersArgs, options: CreateApiUserProfileV1UsersOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/user-profile/v1/users${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(createApiUserProfileV1UsersResponse, await res.json()) };