@smartsamurai/krapi-sdk
Version:
KRAPI TypeScript SDK - Easy-to-use client SDK for connecting to self-hosted KRAPI servers (like Appwrite SDK)
208 lines (193 loc) • 6.39 kB
text/typescript
/**
* Users Adapter
*
* Unifies UsersHttpClient and UsersService behind a common interface.
*/
import { UsersHttpClient } from "../../http-clients/users-http-client";
import { ProjectUser } from "../../types";
import { UsersService } from "../../users-service";
import { createAdapterInitError } from "./error-handler";
type Mode = "client" | "server";
export class UsersAdapter {
private mode: Mode;
private httpClient: UsersHttpClient | undefined;
private service: UsersService | undefined;
constructor(mode: Mode, httpClient?: UsersHttpClient, service?: UsersService) {
this.mode = mode;
this.httpClient = httpClient;
this.service = service;
}
async getAll(projectId: string, options?: {
search?: string;
limit?: number;
page?: number;
}): Promise<ProjectUser[]> {
if (this.mode === "client") {
if (!this.httpClient) {
throw createAdapterInitError("HTTP client", this.mode);
}
return await this.httpClient.getAllUsers(projectId, options);
} else {
if (!this.service) {
throw createAdapterInitError("Users service", this.mode);
}
const users = await this.service.getAllUsers(projectId, options);
return (users as unknown as ProjectUser[]) || [];
}
}
async get(projectId: string, userId: string): Promise<ProjectUser> {
if (this.mode === "client") {
if (!this.httpClient) {
throw createAdapterInitError("HTTP client", this.mode);
}
return await this.httpClient.getUser(projectId, userId);
} else {
if (!this.service) {
throw createAdapterInitError("Users service", this.mode);
}
const user = await this.service.getUserById(projectId, userId);
return (user as unknown as ProjectUser) || ({} as ProjectUser);
}
}
async create(projectId: string, userData: {
username: string;
email: string;
password: string;
role?: string;
permissions?: string[];
first_name?: string;
last_name?: string;
phone?: string;
}): Promise<ProjectUser> {
if (this.mode === "client") {
if (!this.httpClient) {
throw createAdapterInitError("HTTP client", this.mode);
}
return await this.httpClient.createUser(projectId, userData);
} else {
if (!this.service) {
throw createAdapterInitError("Users service", this.mode);
}
const user = await this.service.createUser(projectId, userData);
return (user as unknown as ProjectUser) || ({} as ProjectUser);
}
}
async update(projectId: string, userId: string, updates: {
username?: string;
email?: string;
external_id?: string;
first_name?: string;
last_name?: string;
display_name?: string;
avatar_url?: string;
role?: string;
permissions?: string[];
metadata?: Record<string, unknown>;
is_active?: boolean;
}): Promise<ProjectUser> {
if (this.mode === "client") {
if (!this.httpClient) {
throw createAdapterInitError("HTTP client", this.mode);
}
return await this.httpClient.updateUser(projectId, userId, updates);
} else {
if (!this.service) {
throw createAdapterInitError("Users service", this.mode);
}
const user = await this.service.updateUser(projectId, userId, updates);
return (user as unknown as ProjectUser) || ({} as ProjectUser);
}
}
async delete(projectId: string, userId: string): Promise<{ success: boolean }> {
if (this.mode === "client") {
if (!this.httpClient) {
throw createAdapterInitError("HTTP client", this.mode);
}
await this.httpClient.deleteUser(projectId, userId);
return { success: true };
} else {
if (!this.service) {
throw createAdapterInitError("Users service", this.mode);
}
await this.service.deleteUser(projectId, userId);
return { success: true };
}
}
async updateRole(projectId: string, userId: string, role: string): Promise<ProjectUser> {
return this.update(projectId, userId, { role });
}
async updatePermissions(
projectId: string,
userId: string,
permissions: string[]
): Promise<ProjectUser> {
return this.update(projectId, userId, { permissions });
}
async getActivity(projectId: string, userId: string, options?: {
limit?: number;
offset?: number;
start_date?: string;
end_date?: string;
}): Promise<Array<{
id: string;
action: string;
timestamp: string;
details: Record<string, unknown>;
}>> {
if (this.mode === "client") {
if (!this.httpClient) {
throw createAdapterInitError("HTTP client", this.mode);
}
return await this.httpClient.getUserActivity(projectId, userId, options);
} else {
if (!this.service) {
throw createAdapterInitError("Users service", this.mode);
}
const activityOptions: {
limit?: number;
offset?: number;
action_filter?: string;
entity_type_filter?: string;
} = {};
if (options?.limit !== undefined) {
activityOptions.limit = options.limit;
}
if (options?.offset !== undefined) {
activityOptions.offset = options.offset;
}
const activities = await this.service.getUserActivity(projectId, userId, activityOptions);
// Map UserActivity to the expected format
return activities.map((activity) => ({
id: activity.id,
action: activity.action,
timestamp: activity.created_at,
details: activity.details,
}));
}
}
async getStatistics(projectId: string): Promise<{
total_users: number;
active_users: number;
users_by_role: Record<string, number>;
recent_logins: number;
}> {
if (this.mode === "client") {
if (!this.httpClient) {
throw createAdapterInitError("HTTP client", this.mode);
}
return await this.httpClient.getUserStatistics(projectId);
} else {
if (!this.service) {
throw createAdapterInitError("Users service", this.mode);
}
const stats = await this.service.getUserStatistics(projectId);
// Map UserStatistics to the expected format
return {
total_users: stats.total_users,
active_users: stats.active_users,
users_by_role: stats.users_by_role,
recent_logins: stats.recent_logins,
};
}
}
}