import { QelosSDKOptions } from '../types'; import BaseSDK from '../base-sdk'; import { IUser } from '../authentication'; export interface IManagedUser extends IUser { internalMetadata?: T; } export interface IManagedUserRequest extends IManagedUser { password?: string; } export default class QlUsers extends BaseSDK { private relativePath = '/api/users'; constructor(private options: QelosSDKOptions) { super(options) } getUser(userId: string) { return this.callJsonApi>(`${this.relativePath}/${userId}`) } // Method getList accepts filters including roles getList(filters?: { username?: string; exact?: boolean; roles?: string[] }) { const queryParams = filters ? `?${Object.entries(filters) .map(([key, value]) => Array.isArray(value) ? `${key}=${value.join(',')}` : `${key}=${value}` ).join('&')}` : ''; return this.callJsonApi(`${this.relativePath}${queryParams}`); } remove(userId: string): Promise { return this.callApi(`${this.relativePath}/${userId}`, { method: 'delete' }); } update(userId: string, changes: Partial>): Promise { return this.callJsonApi( `${this.relativePath}/${userId}`, { method: 'put', headers: { 'content-type': 'application/json' }, body: JSON.stringify(changes) } ) } create(user: Omit, '_id' | 'fullName' | 'birthDate'>): Promise> { return this.callJsonApi(this.relativePath, { method: 'post', headers: { 'content-type': 'application/json' }, body: JSON.stringify(user) }) } getEncryptedData(userId: string, encryptedId: string = '') { return this.callJsonApi(`${this.relativePath}/${userId}/encrypted`, { headers: { 'x-encrypted-id': encryptedId } }) } async setEncryptedData(userId: string, encryptedId: string = '', data: Z): Promise { const res = await this.callApi(`${this.relativePath}/${userId}/encrypted`, { method: 'post', headers: { 'content-type': 'application/json', 'x-encrypted-id': encryptedId }, body: JSON.stringify(data) }) if (res.status >= 300) { throw new Error('could not set encrypted data') } } }