import {z} from 'zod' export type Status = 'ACTIVE' | 'PENDING' | 'DISABLED'; export type Permissions = 'all' | 'iam.tenant.create' | 'iam.tenant.read' | 'iam.tenant.update' | 'iam.tenant.delete' | 'iam.secret.create' | 'iam.secret.read' | 'iam.key.create' | 'iam.key.read' | 'iam.key.delete' | 'iam.account.create' | 'iam.account.read' | 'iam.account.update' | 'iam.account.delete' | 'iam.token.create' | 'iam.token.update' | 'iam.token.delete' | 'iam.token.introspect' | 'tenant.all' | 'tenant.membership.create' | 'tenant.membership.update' | 'tenant.membership.delete' | 'tenant.profile.read' | 'tenant.profile.update' | 'tenant.profile.delete' | 'tenant.roles.read' | 'tenant.roles.create' | 'tenant.roles.update' | 'tenant.roles.delete' | 'tenant.flows.read' | 'tenant.flows.create' | 'tenant.flows.update' | 'tenant.flows.delete'; export type Role = {id?: string | undefined, name?: string | undefined, permissions?: Array | undefined, scope?: string | undefined, createdAt?: string | undefined, updatedAt?: string | undefined, isGlobal?: boolean | undefined, description?: string | undefined}; export type UserObject = {id?: string | undefined, username: string, firstname: string, lastname: string, status?: Status | undefined, tenant?: string | undefined, roles?: Array | undefined, permissions?: Array | undefined, confirmed?: boolean | undefined, img?: string | undefined}; export type CreateApiApiV1UsersArgs = {body: UserObject}; export type CreateApiApiV1UsersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const status = z.enum(["ACTIVE", "PENDING", "DISABLED"]); export const permissions = z.enum(["all", "iam.tenant.create", "iam.tenant.read", "iam.tenant.update", "iam.tenant.delete", "iam.secret.create", "iam.secret.read", "iam.key.create", "iam.key.read", "iam.key.delete", "iam.account.create", "iam.account.read", "iam.account.update", "iam.account.delete", "iam.token.create", "iam.token.update", "iam.token.delete", "iam.token.introspect", "tenant.all", "tenant.membership.create", "tenant.membership.update", "tenant.membership.delete", "tenant.profile.read", "tenant.profile.update", "tenant.profile.delete", "tenant.roles.read", "tenant.roles.create", "tenant.roles.update", "tenant.roles.delete", "tenant.flows.read", "tenant.flows.create", "tenant.flows.update", "tenant.flows.delete"]); export const role = z.object({id: z.string().optional(), name: z.string().optional(), permissions: z.array(permissions).optional(), scope: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), isGlobal: z.boolean().optional(), description: z.string().optional()}); export const userObject = z.object({id: z.string().optional(), username: z.string(), firstname: z.string(), lastname: z.string(), status: status.optional(), tenant: z.string().optional(), roles: z.array(role).optional(), permissions: z.array(permissions).optional(), confirmed: z.boolean().optional(), img: 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 }; /** * Create user * * POST /api/v1/users */ export const createApiApiV1Users = async (args: CreateApiApiV1UsersArgs, options?: CreateApiApiV1UsersOptions) => { const {baseUrl = "https://iam.example.com", 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(/\/+$/, '')}/api/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(userObject, await res.json()) };