feat: implement core API classes for project, database, query, command, functions, and redis management

This commit is contained in:
lborv
2025-10-28 20:59:44 +02:00
parent 814ed81b34
commit 1cfc625442
8 changed files with 443 additions and 397 deletions

65
src/classes/api.class.ts Normal file
View File

@ -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<any> {
return await axios.request({
url,
method,
data,
headers: {
"x-api-token": this.token,
...headers,
},
});
}
async generateToken(projectId: string): Promise<GenerateTokenResponse> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/api/token/generate`,
"POST",
{
id: projectId,
}
);
return response.data;
}
async revokeToken(token: string): Promise<GenerateTokenResponse> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/api/token/revoke/${token}`,
"DELETE"
);
return response.data;
}
}

View File

@ -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<any> {
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<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/command/update/${id}`,
"POST",
updateData
);
return response.data;
}
async run(
id: string,
query: Record<string, any>,
headers: Record<string, any> = {},
isPublic: boolean = false
): Promise<any> {
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<string, any>,
headers: Record<string, any> = {}
): Promise<any> {
return this.run(id, query, headers, true);
}
async delete(id: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/command/delete/${id}`,
"DELETE"
);
return response.data;
}
}

View File

@ -0,0 +1,101 @@
import { Api } from "./api.class";
export class Database {
constructor(private api: Api) {}
async create(projectId: string): Promise<any> {
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<any> {
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<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/database/tables/${databaseId}`,
"GET"
);
return response.data;
}
async getColumns(databaseId: string, tableName: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/database/columns/${databaseId}/${tableName}`,
"GET"
);
return response.data;
}
async migrateUp(databaseId: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/database/migration/up/${databaseId}`,
"GET"
);
return response.data;
}
async migrateDown(databaseId: string): Promise<any> {
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<any> {
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<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/database/query/${databaseId}`,
"POST",
{
query,
}
);
return response.data;
}
}

View File

@ -0,0 +1,27 @@
import { Api } from "./api.class";
export class Functions {
constructor(private api: Api) {}
async create(name: string, source: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/functions/create`,
"POST",
{
name,
source,
}
);
return response.data;
}
async delete(name: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/functions/delete/${name}`,
"DELETE"
);
return response.data;
}
}

View File

@ -0,0 +1,73 @@
import { Api } from "./api.class";
export class Project {
constructor(private api: Api) {}
async create(name: string, withDb: boolean = true): Promise<any> {
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<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/project/settings/create`,
"PUT",
setting
);
return response.data;
}
async deleteSetting(key: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/project/settings/delete/${key}`,
"DELETE"
);
return response.data;
}
async getAllSettings(): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/project/settings`,
"GET"
);
return response.data;
}
async getAllApiTokens(): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/project/api-tokens`,
"GET"
);
return response.data;
}
async getDetails(): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/project/details`,
"GET"
);
return response.data;
}
async updateMeta(meta: Record<string, any>): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/project/update/meta`,
"POST",
{ meta }
);
return response.data;
}
}

View File

@ -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<any> {
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<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/query/update/${id}`,
"POST",
updateData
);
return response.data;
}
async run(
id: string,
query: Record<string, any>,
headers: Record<string, any> = {},
isPublic: boolean = false
): Promise<any> {
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<string, any>,
headers: Record<string, any> = {}
): Promise<any> {
return this.run(id, query, headers, true);
}
async delete(id: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/query/delete/${id}`,
"DELETE"
);
return response.data;
}
}

View File

@ -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<any> {
const response = await this.api.createRequestWithAuthHeaders(
`${this.api.getUrl()}/redis/node/create`,
"POST",
{
host,
port,
user,
password,
}
);
return response.data;
}
}

View File

@ -1,398 +1,3 @@
import axios from "axios"; import { Api } from "./classes/api.class";
import { GenerateTokenResponse } from "./types/token.type";
export class FewLineSDK { export default Api;
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<any> {
return await axios.request({
url,
method,
data,
headers: {
"x-api-token": this.token,
...headers,
},
});
}
/**
* API Tokens part
*/
async generateToken(projectId: string): Promise<GenerateTokenResponse> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/api/token/generate`,
"POST",
{
id: projectId,
}
);
return response.data;
}
async revokeToken(token: string): Promise<GenerateTokenResponse> {
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<any> {
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<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/project/settings/create`,
"PUT",
setting
);
return response.data;
}
async deleteSetting(key: string): Promise<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/project/settings/delete/${key}`,
"DELETE"
);
return response.data;
}
async getAllSettings(): Promise<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/project/settings`,
"GET"
);
return response.data;
}
async getAllApiTokens(): Promise<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/project/api-tokens`,
"GET"
);
return response.data;
}
async getProjectDetails(): Promise<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/project/details`,
"GET"
);
return response.data;
}
async updateProjectMeta(meta: Record<string, any>): Promise<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/project/update/meta`,
"POST",
{ meta }
);
return response.data;
}
/**
* Database management part
*/
async createDatabase(projectId: string): Promise<any> {
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<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/database/node/create`,
"POST",
{
host,
port,
username,
password,
}
);
return response.data;
}
async getTables(databaseId: string): Promise<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/database/tables/${databaseId}`,
"GET"
);
return response.data;
}
async getColumns(databaseId: string, tableName: string): Promise<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/database/columns/${databaseId}/${tableName}`,
"GET"
);
return response.data;
}
async migrateUp(databaseId: string): Promise<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/database/migration/up/${databaseId}`,
"GET"
);
return response.data;
}
async migrateDown(databaseId: string): Promise<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/database/migration/down/${databaseId}`,
"GET"
);
return response.data;
}
async createMigration(
up: string,
down: string,
databaseId: string
): Promise<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/database/migration/create`,
"POST",
{
up,
down,
databaseId,
}
);
return response.data;
}
async runQuery(databaseId: string, query: string): Promise<any> {
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<any> {
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<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/query/update/${id}`,
"POST",
updateData
);
return response.data;
}
async query(
id: string,
query: Record<string, any>,
headers: Record<string, any> = {},
isPublic: boolean = false
): Promise<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/query/${isPublic ? "run-public" : "run"}/${id}`,
"POST",
query,
headers
);
return response.data;
}
async deleteQuery(id: string): Promise<any> {
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<any> {
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<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/command/update/${id}`,
"POST",
updateData
);
return response.data;
}
async command(
id: string,
command: Record<string, any>,
headers: Record<string, any> = {},
isPublic: boolean = false
): Promise<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/command/${isPublic ? "run-public" : "run"}/${id}`,
"POST",
command,
headers
);
return response.data;
}
async deleteCommand(id: string): Promise<any> {
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<any> {
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<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/functions/create`,
"POST",
{
name,
source,
}
);
return response.data;
}
async deleteFunction(name: string): Promise<any> {
const response = await this.createRequestWithAuthHeaders(
`${this.url}/functions/delete/${name}`,
"DELETE"
);
return response.data;
}
}