From 1cfc625442b6411012c0160991e52d2e4338a8e8 Mon Sep 17 00:00:00 2001 From: lborv Date: Tue, 28 Oct 2025 20:59:44 +0200 Subject: [PATCH] feat: implement core API classes for project, database, query, command, functions, and redis management --- src/classes/api.class.ts | 65 ++++++ src/classes/command.class.ts | 75 +++++++ src/classes/database.class.ts | 101 +++++++++ src/classes/functions.class.ts | 27 +++ src/classes/project.class..ts | 73 ++++++ src/classes/query.class.ts | 75 +++++++ src/classes/redis.class.ts | 25 +++ src/index.ts | 399 +-------------------------------- 8 files changed, 443 insertions(+), 397 deletions(-) create mode 100644 src/classes/api.class.ts create mode 100644 src/classes/command.class.ts create mode 100644 src/classes/database.class.ts create mode 100644 src/classes/functions.class.ts create mode 100644 src/classes/project.class..ts create mode 100644 src/classes/query.class.ts create mode 100644 src/classes/redis.class.ts diff --git a/src/classes/api.class.ts b/src/classes/api.class.ts new file mode 100644 index 0000000..d80c318 --- /dev/null +++ b/src/classes/api.class.ts @@ -0,0 +1,65 @@ +import axios from "axios"; +import { GenerateTokenResponse } from "../types/token.type"; +import { Project } from "./project.class."; +import { Database } from "./database.class"; +import { Query } from "./query.class"; +import { Command } from "./command.class"; +import { Redis } from "./redis.class"; + +export class Api { + public project: Project; + public database: Database; + public query: Query; + public command: Command; + public redis: Redis; + + constructor(private url: string, private token: string) { + this.project = new Project(this); + this.database = new Database(this); + this.query = new Query(this); + this.redis = new Redis(this); + this.command = new Command(this); + } + + public getUrl(): string { + return this.url; + } + + public async createRequestWithAuthHeaders( + url: string, + method: string, + data?: any, + headers: any = {} + ): Promise { + return await axios.request({ + url, + method, + data, + headers: { + "x-api-token": this.token, + ...headers, + }, + }); + } + + async generateToken(projectId: string): Promise { + const response = await this.createRequestWithAuthHeaders( + `${this.url}/api/token/generate`, + "POST", + { + id: projectId, + } + ); + + return response.data; + } + + async revokeToken(token: string): Promise { + const response = await this.createRequestWithAuthHeaders( + `${this.url}/api/token/revoke/${token}`, + "DELETE" + ); + + return response.data; + } +} diff --git a/src/classes/command.class.ts b/src/classes/command.class.ts new file mode 100644 index 0000000..16afd54 --- /dev/null +++ b/src/classes/command.class.ts @@ -0,0 +1,75 @@ +import { Api } from "./api.class"; + +export class Command { + constructor(private api: Api) {} + + async create( + projectToken: string, + source: string, + isTypescript: number = 0, + isPublic: number = 0 + ): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/command/create`, + "POST", + { + source, + isTypescript, + isPublic, + projectToken, + } + ); + + return response.data; + } + + async update( + id: string, + updateData: Partial<{ + source: string; + isTypescript?: number; + isPublic?: number; + }> + ): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/command/update/${id}`, + "POST", + updateData + ); + + return response.data; + } + + async run( + id: string, + query: Record, + headers: Record = {}, + isPublic: boolean = false + ): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/command/${isPublic ? "run-public" : "run"}/${id}`, + "POST", + query, + headers + ); + + return response.data; + } + + runPublic( + id: string, + query: Record, + headers: Record = {} + ): Promise { + return this.run(id, query, headers, true); + } + + async delete(id: string): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/command/delete/${id}`, + "DELETE" + ); + + return response.data; + } +} diff --git a/src/classes/database.class.ts b/src/classes/database.class.ts new file mode 100644 index 0000000..3dc6340 --- /dev/null +++ b/src/classes/database.class.ts @@ -0,0 +1,101 @@ +import { Api } from "./api.class"; + +export class Database { + constructor(private api: Api) {} + + async create(projectId: string): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/database/create`, + "POST", + { projectId } + ); + + return response.data; + } + + async createNode( + host: string, + port: number, + username: string, + password: string + ): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/database/node/create`, + "POST", + { + host, + port, + username, + password, + } + ); + + return response.data; + } + + async getTables(databaseId: string): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/database/tables/${databaseId}`, + "GET" + ); + + return response.data; + } + + async getColumns(databaseId: string, tableName: string): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/database/columns/${databaseId}/${tableName}`, + "GET" + ); + + return response.data; + } + + async migrateUp(databaseId: string): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/database/migration/up/${databaseId}`, + "GET" + ); + + return response.data; + } + + async migrateDown(databaseId: string): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/database/migration/down/${databaseId}`, + "GET" + ); + + return response.data; + } + + async createMigration( + up: string, + down: string, + databaseId: string + ): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/database/migration/create`, + "POST", + { + up, + down, + databaseId, + } + ); + + return response.data; + } + + async query(databaseId: string, query: string): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/database/query/${databaseId}`, + "POST", + { + query, + } + ); + + return response.data; + } +} diff --git a/src/classes/functions.class.ts b/src/classes/functions.class.ts new file mode 100644 index 0000000..9dd9350 --- /dev/null +++ b/src/classes/functions.class.ts @@ -0,0 +1,27 @@ +import { Api } from "./api.class"; + +export class Functions { + constructor(private api: Api) {} + + async create(name: string, source: string): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/functions/create`, + "POST", + { + name, + source, + } + ); + + return response.data; + } + + async delete(name: string): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/functions/delete/${name}`, + "DELETE" + ); + + return response.data; + } +} diff --git a/src/classes/project.class..ts b/src/classes/project.class..ts new file mode 100644 index 0000000..a39d747 --- /dev/null +++ b/src/classes/project.class..ts @@ -0,0 +1,73 @@ +import { Api } from "./api.class"; + +export class Project { + constructor(private api: Api) {} + + async create(name: string, withDb: boolean = true): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/project/${withDb ? "create" : "create-without-db"}`, + "PUT", + { + name, + } + ); + + return response.data; + } + + async createSettings(setting: { key: string; value: string }): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/project/settings/create`, + "PUT", + setting + ); + + return response.data; + } + + async deleteSetting(key: string): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/project/settings/delete/${key}`, + "DELETE" + ); + + return response.data; + } + + async getAllSettings(): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/project/settings`, + "GET" + ); + + return response.data; + } + + async getAllApiTokens(): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/project/api-tokens`, + "GET" + ); + + return response.data; + } + + async getDetails(): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/project/details`, + "GET" + ); + + return response.data; + } + + async updateMeta(meta: Record): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/project/update/meta`, + "POST", + { meta } + ); + + return response.data; + } +} diff --git a/src/classes/query.class.ts b/src/classes/query.class.ts new file mode 100644 index 0000000..f0aeb6b --- /dev/null +++ b/src/classes/query.class.ts @@ -0,0 +1,75 @@ +import { Api } from "./api.class"; + +export class Query { + constructor(private api: Api) {} + + async create( + projectToken: string, + source: string, + isTypescript: number = 0, + isPublic: number = 0 + ): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/query/create`, + "POST", + { + source, + isTypescript, + isPublic, + projectToken, + } + ); + + return response.data; + } + + async update( + id: string, + updateData: Partial<{ + source: string; + isTypescript?: number; + isPublic?: number; + }> + ): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/query/update/${id}`, + "POST", + updateData + ); + + return response.data; + } + + async run( + id: string, + query: Record, + headers: Record = {}, + isPublic: boolean = false + ): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/query/${isPublic ? "run-public" : "run"}/${id}`, + "POST", + query, + headers + ); + + return response.data; + } + + runPublic( + id: string, + query: Record, + headers: Record = {} + ): Promise { + return this.run(id, query, headers, true); + } + + async delete(id: string): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/query/delete/${id}`, + "DELETE" + ); + + return response.data; + } +} diff --git a/src/classes/redis.class.ts b/src/classes/redis.class.ts new file mode 100644 index 0000000..ed734f3 --- /dev/null +++ b/src/classes/redis.class.ts @@ -0,0 +1,25 @@ +import { Api } from "./api.class"; + +export class Redis { + constructor(private api: Api) {} + + async createNode( + host: string, + port: number, + user: string, + password: string + ): Promise { + const response = await this.api.createRequestWithAuthHeaders( + `${this.api.getUrl()}/redis/node/create`, + "POST", + { + host, + port, + user, + password, + } + ); + + return response.data; + } +} diff --git a/src/index.ts b/src/index.ts index 1bb9302..e4b4ebd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,398 +1,3 @@ -import axios from "axios"; -import { GenerateTokenResponse } from "./types/token.type"; +import { Api } from "./classes/api.class"; -export class FewLineSDK { - constructor(private url: string, private token: string) {} - - public getUrl(): string { - return this.url; - } - - private async createRequestWithAuthHeaders( - url: string, - method: string, - data?: any, - headers: any = {} - ): Promise { - return await axios.request({ - url, - method, - data, - headers: { - "x-api-token": this.token, - ...headers, - }, - }); - } - - /** - * API Tokens part - */ - async generateToken(projectId: string): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/api/token/generate`, - "POST", - { - id: projectId, - } - ); - - return response.data; - } - - async revokeToken(token: string): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/api/token/revoke/${token}`, - "DELETE" - ); - - return response.data; - } - - /** - * Projects management part - */ - async createProject(name: string, withDb: boolean = true): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/project/${withDb ? "create" : "create-without-db"}`, - "PUT", - { - name, - } - ); - - return response.data; - } - - async createSettings(setting: { key: string; value: string }): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/project/settings/create`, - "PUT", - setting - ); - - return response.data; - } - - async deleteSetting(key: string): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/project/settings/delete/${key}`, - "DELETE" - ); - - return response.data; - } - - async getAllSettings(): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/project/settings`, - "GET" - ); - - return response.data; - } - - async getAllApiTokens(): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/project/api-tokens`, - "GET" - ); - - return response.data; - } - - async getProjectDetails(): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/project/details`, - "GET" - ); - - return response.data; - } - - async updateProjectMeta(meta: Record): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/project/update/meta`, - "POST", - { meta } - ); - - return response.data; - } - - /** - * Database management part - */ - async createDatabase(projectId: string): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/database/create`, - "POST", - { projectId } - ); - - return response.data; - } - - async createNode( - host: string, - port: number, - username: string, - password: string - ): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/database/node/create`, - "POST", - { - host, - port, - username, - password, - } - ); - - return response.data; - } - - async getTables(databaseId: string): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/database/tables/${databaseId}`, - "GET" - ); - - return response.data; - } - - async getColumns(databaseId: string, tableName: string): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/database/columns/${databaseId}/${tableName}`, - "GET" - ); - - return response.data; - } - - async migrateUp(databaseId: string): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/database/migration/up/${databaseId}`, - "GET" - ); - - return response.data; - } - - async migrateDown(databaseId: string): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/database/migration/down/${databaseId}`, - "GET" - ); - - return response.data; - } - - async createMigration( - up: string, - down: string, - databaseId: string - ): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/database/migration/create`, - "POST", - { - up, - down, - databaseId, - } - ); - - return response.data; - } - - async runQuery(databaseId: string, query: string): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/database/query/${databaseId}`, - "POST", - { - query, - } - ); - - return response.data; - } - - /** - * Query and Commands part - */ - - async createQuery( - projectToken: string, - source: string, - isTypescript: number = 0, - isPublic: number = 0 - ): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/query/create`, - "POST", - { - source, - isTypescript, - isPublic, - projectToken, - } - ); - - return response.data; - } - - async updateQuery( - id: string, - updateData: Partial<{ - source: string; - isTypescript?: number; - isPublic?: number; - }> - ): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/query/update/${id}`, - "POST", - updateData - ); - - return response.data; - } - - async query( - id: string, - query: Record, - headers: Record = {}, - isPublic: boolean = false - ): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/query/${isPublic ? "run-public" : "run"}/${id}`, - "POST", - query, - headers - ); - - return response.data; - } - - async deleteQuery(id: string): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/query/delete/${id}`, - "DELETE" - ); - - return response.data; - } - - async createCommand( - projectToken: string, - source: string, - isTypescript: number = 0, - isPublic: number = 0 - ): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/command/create`, - "POST", - { - source, - isTypescript, - isPublic, - projectToken, - } - ); - - return response.data; - } - - async updateCommand( - id: string, - updateData: Partial<{ - source: string; - isTypescript?: number; - isPublic?: number; - }> - ): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/command/update/${id}`, - "POST", - updateData - ); - - return response.data; - } - - async command( - id: string, - command: Record, - headers: Record = {}, - isPublic: boolean = false - ): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/command/${isPublic ? "run-public" : "run"}/${id}`, - "POST", - command, - headers - ); - - return response.data; - } - - async deleteCommand(id: string): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/command/delete/${id}`, - "DELETE" - ); - - return response.data; - } - - /** - * Redis management part - */ - async createRedisNode( - host: string, - port: number, - user: string, - password: string - ): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/redis/node/create`, - "POST", - { - host, - port, - user, - password, - } - ); - - return response.data; - } - - /** - * Functions management part - */ - - async createFunction(name: string, source: string): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/functions/create`, - "POST", - { - name, - source, - } - ); - - return response.data; - } - - async deleteFunction(name: string): Promise { - const response = await this.createRequestWithAuthHeaders( - `${this.url}/functions/delete/${name}`, - "DELETE" - ); - - return response.data; - } -} +export default Api;