UNPKG

@smartsamurai/krapi-sdk

Version:

KRAPI TypeScript SDK - Easy-to-use client SDK for connecting to self-hosted KRAPI servers (like Appwrite SDK)

181 lines (170 loc) 7.04 kB
/** * Changelog Adapter * * Unifies ChangelogHttpClient and ChangelogService behind a common interface. */ import { ChangelogService, ChangelogQueryOptions } from "../../changelog-service"; import { ChangelogHttpClient } from "../../http-clients/changelog-http-client"; import { createAdapterInitError } from "./error-handler"; type Mode = "client" | "server"; export class ChangelogAdapter { private mode: Mode; private httpClient: ChangelogHttpClient | undefined; private service: ChangelogService | undefined; constructor(mode: Mode, httpClient?: ChangelogHttpClient, service?: ChangelogService) { this.mode = mode; this.httpClient = httpClient; this.service = service; } async getProjectChangelog(projectId: string, options?: { limit?: number; offset?: number; action_type?: string; user_id?: string; start_date?: string; end_date?: string; collection_name?: string; document_id?: string; }): Promise<unknown[]> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.getProjectChangelog(projectId, options); return (response.data || []) as unknown[]; } else { if (!this.service) { throw createAdapterInitError("Changelog service", this.mode); } const queryOptions: ChangelogQueryOptions = { entity_type: "project", entity_id: projectId, }; if (options?.user_id) queryOptions.user_id = options.user_id; if (options?.action_type) queryOptions.action = options.action_type; if (options?.start_date) queryOptions.start_date = new Date(options.start_date); if (options?.end_date) queryOptions.end_date = new Date(options.end_date); if (options?.limit) queryOptions.limit = options.limit; if (options?.offset) queryOptions.offset = options.offset; return this.service.getAll(queryOptions); } } async getCollectionChangelog(projectId: string, collectionName: string, options?: { limit?: number; offset?: number; action_type?: string; user_id?: string; start_date?: string; end_date?: string; document_id?: string; }): Promise<unknown[]> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.getCollectionChangelog(projectId, collectionName, options); return (response.data || []) as unknown[]; } else { if (!this.service) { throw createAdapterInitError("Changelog service", this.mode); } const queryOptions: ChangelogQueryOptions = {}; if (options?.user_id) queryOptions.user_id = options.user_id; if (options?.action_type) queryOptions.action = options.action_type; if (options?.start_date) queryOptions.start_date = new Date(options.start_date); if (options?.end_date) queryOptions.end_date = new Date(options.end_date); if (options?.limit) queryOptions.limit = options.limit; if (options?.offset) queryOptions.offset = options.offset; return this.service.getByEntity("collection", `${projectId}:${collectionName}`, queryOptions); } } async getDocumentChangelog(projectId: string, collectionName: string, documentId: string, options?: { limit?: number; offset?: number; action_type?: string; user_id?: string; start_date?: string; end_date?: string; }): Promise<unknown[]> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.getDocumentChangelog(projectId, collectionName, documentId, options); return (response.data || []) as unknown[]; } else { if (!this.service) { throw createAdapterInitError("Changelog service", this.mode); } const queryOptions: ChangelogQueryOptions = {}; if (options?.user_id) queryOptions.user_id = options.user_id; if (options?.action_type) queryOptions.action = options.action_type; if (options?.start_date) queryOptions.start_date = new Date(options.start_date); if (options?.end_date) queryOptions.end_date = new Date(options.end_date); if (options?.limit) queryOptions.limit = options.limit; if (options?.offset) queryOptions.offset = options.offset; return this.service.getByEntity("document", documentId, queryOptions); } } async getUserActivity(projectId: string, userId: string, options?: { limit?: number; offset?: number; action_type?: string; start_date?: string; end_date?: string; entity_type?: string; }): Promise<unknown[]> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.getUserActivity(projectId, userId, options); return (response.data || []) as unknown[]; } else { if (!this.service) { throw createAdapterInitError("Changelog service", this.mode); } const queryOptions: ChangelogQueryOptions = { user_id: userId, }; if (options?.entity_type) queryOptions.entity_type = options.entity_type; if (options?.action_type) queryOptions.action = options.action_type; if (options?.start_date) queryOptions.start_date = new Date(options.start_date); if (options?.end_date) queryOptions.end_date = new Date(options.end_date); if (options?.limit) queryOptions.limit = options.limit; if (options?.offset) queryOptions.offset = options.offset; return this.service.getAll(queryOptions); } } async getSystemChangelog(options?: { limit?: number; offset?: number; action_type?: string; user_id?: string; project_id?: string; entity_type?: string; start_date?: string; end_date?: string; }): Promise<unknown[]> { if (this.mode === "client") { if (!this.httpClient) { throw createAdapterInitError("HTTP client", this.mode); } const response = await this.httpClient.getSystemChangelog(options); return (response.data || []) as unknown[]; } else { if (!this.service) { throw createAdapterInitError("Changelog service", this.mode); } const queryOptions: ChangelogQueryOptions = {}; if (options?.entity_type) queryOptions.entity_type = options.entity_type; if (options?.user_id) queryOptions.user_id = options.user_id; if (options?.action_type) queryOptions.action = options.action_type; if (options?.start_date) queryOptions.start_date = new Date(options.start_date); if (options?.end_date) queryOptions.end_date = new Date(options.end_date); if (options?.limit) queryOptions.limit = options.limit; if (options?.offset) queryOptions.offset = options.offset; return this.service.getAll(queryOptions); } } }