import {z} from 'zod' export type RegisterUserData = {password: string, token?: (string | null) | undefined, username?: (string | null) | undefined, lastName: string, email: string, firstName: string, acceptMailingList: boolean, acceptedEULA: boolean}; export type CreateApiApiAuthRegisterArgs = {body: RegisterUserData}; export type CreateApiApiAuthRegisterOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const quantumUserStatus = z.enum(["Active", "Blocked", "Deleted", "Locked", "Pending", "Reset"]); export const quantumObjectType = z.enum(["Adjacency", "AllowedQuanta", "Analysis", "Animation", "ApiKey", "Artifact", "Asset", "AuthGroup", "Behavior", "Binding", "Building", "Campus", "CampusDirectory", "CompatibleQuanta", "ConnectionNet", "ConnectionNode", "ConstructionCategory", "ControlKnot", "Device", "DisplayRule", "EmailVerification", "Equation", "Equipment", "EquipmentComponent", "EquipmentComponentPropertyRouter", "EscalationGroup", "Event", "EventClass", "EventHistory", "EventRelation", "EventType", "Floor", "GraphVersion", "History", "Image", "ImportedModel", "InterfaceAlternate", "Invite", "Layer", "LayerSet", "LayerSetJoin", "Library", "Location", "LocationProperty", "Manufacturer", "Media", "MediaComponent", "Model", "MultifactorAuthentication", "NodeSet", "NodeSetConnectionNodeJoin", "Organization", "OrganizationAuthGroupPermission", "Prediction", "PredictionGroup", "Preference", "PreferenceGroup", "PrivilegeJoin", "Procedure", "Property", "PropertyTagRouter", "Quanta", "RefreshToken", "Relationship", "SelectedModel", "SessionBinding", "SessionEvent", "Shape", "Site", "Subsystem", "SubsystemCapability", "SubsystemSet", "Surface", "System", "Tag", "TagFamily", "TypicalConstruction", "UnitLookup", "UnitPreference", "User", "UserSession", "UserSessionHistory", "Vertex", "View", "WeatherSource", "Zone", "ZoneGroup", "ZoneSubsystem", "ZoneSubsystemSet"]); export const identifier = z.object({type: quantumObjectType, id: z.string()}); export const registerUserResponse = z.object({userStatus: quantumUserStatus.optional(), token: z.string().nullable().optional(), message: z.string(), newUserID: identifier.optional(), refresh: z.string().nullable().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 }; /** * Registers a new user * * POST /api/auth/register */ export const createApiApiAuthRegister = async (args: CreateApiApiAuthRegisterArgs, options: CreateApiApiAuthRegisterOptions) => { 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(/\/+$/, '')}/api/auth/register${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(registerUserResponse, await res.json()) };