feat: add session management class and refactor API request methods

This commit is contained in:
Boris D
2025-10-29 17:11:23 +02:00
parent 1cfc625442
commit 55f263debf
8 changed files with 113 additions and 39 deletions

View File

@ -5,6 +5,7 @@ import { Database } from "./database.class";
import { Query } from "./query.class"; import { Query } from "./query.class";
import { Command } from "./command.class"; import { Command } from "./command.class";
import { Redis } from "./redis.class"; import { Redis } from "./redis.class";
import { Session } from "./session.class";
export class Api { export class Api {
public project: Project; public project: Project;
@ -12,6 +13,7 @@ export class Api {
public query: Query; public query: Query;
public command: Command; public command: Command;
public redis: Redis; public redis: Redis;
public session: Session;
constructor(private url: string, private token: string) { constructor(private url: string, private token: string) {
this.project = new Project(this); this.project = new Project(this);
@ -19,13 +21,14 @@ export class Api {
this.query = new Query(this); this.query = new Query(this);
this.redis = new Redis(this); this.redis = new Redis(this);
this.command = new Command(this); this.command = new Command(this);
this.session = new Session(this);
} }
public getUrl(): string { public getUrl(): string {
return this.url; return this.url;
} }
public async createRequestWithAuthHeaders( public async requestWithAuthHeaders(
url: string, url: string,
method: string, method: string,
data?: any, data?: any,
@ -42,8 +45,22 @@ export class Api {
}); });
} }
public async request(
url: string,
method: string,
data?: any,
headers: any = {}
): Promise<any> {
return await axios.request({
url,
method,
data,
headers,
});
}
async generateToken(projectId: string): Promise<GenerateTokenResponse> { async generateToken(projectId: string): Promise<GenerateTokenResponse> {
const response = await this.createRequestWithAuthHeaders( const response = await this.requestWithAuthHeaders(
`${this.url}/api/token/generate`, `${this.url}/api/token/generate`,
"POST", "POST",
{ {
@ -55,7 +72,7 @@ export class Api {
} }
async revokeToken(token: string): Promise<GenerateTokenResponse> { async revokeToken(token: string): Promise<GenerateTokenResponse> {
const response = await this.createRequestWithAuthHeaders( const response = await this.requestWithAuthHeaders(
`${this.url}/api/token/revoke/${token}`, `${this.url}/api/token/revoke/${token}`,
"DELETE" "DELETE"
); );

View File

@ -9,7 +9,7 @@ export class Command {
isTypescript: number = 0, isTypescript: number = 0,
isPublic: number = 0 isPublic: number = 0
): Promise<any> { ): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/command/create`, `${this.api.getUrl()}/command/create`,
"POST", "POST",
{ {
@ -31,7 +31,7 @@ export class Command {
isPublic?: number; isPublic?: number;
}> }>
): Promise<any> { ): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/command/update/${id}`, `${this.api.getUrl()}/command/update/${id}`,
"POST", "POST",
updateData updateData
@ -43,11 +43,10 @@ export class Command {
async run( async run(
id: string, id: string,
query: Record<string, any>, query: Record<string, any>,
headers: Record<string, any> = {}, headers: Record<string, any> = {}
isPublic: boolean = false
): Promise<any> { ): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/command/${isPublic ? "run-public" : "run"}/${id}`, `${this.api.getUrl()}/command/run/${id}`,
"POST", "POST",
query, query,
headers headers
@ -56,16 +55,23 @@ export class Command {
return response.data; return response.data;
} }
runPublic( async runPublic(
id: string, id: string,
query: Record<string, any>, query: Record<string, any>,
headers: Record<string, any> = {} headers: Record<string, any> = {}
): Promise<any> { ): Promise<any> {
return this.run(id, query, headers, true); const response = await this.api.request(
`${this.api.getUrl()}/command/run-public/${id}`,
"POST",
query,
headers
);
return response.data;
} }
async delete(id: string): Promise<any> { async delete(id: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/command/delete/${id}`, `${this.api.getUrl()}/command/delete/${id}`,
"DELETE" "DELETE"
); );

View File

@ -4,7 +4,7 @@ export class Database {
constructor(private api: Api) {} constructor(private api: Api) {}
async create(projectId: string): Promise<any> { async create(projectId: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/database/create`, `${this.api.getUrl()}/database/create`,
"POST", "POST",
{ projectId } { projectId }
@ -19,7 +19,7 @@ export class Database {
username: string, username: string,
password: string password: string
): Promise<any> { ): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/database/node/create`, `${this.api.getUrl()}/database/node/create`,
"POST", "POST",
{ {
@ -34,7 +34,7 @@ export class Database {
} }
async getTables(databaseId: string): Promise<any> { async getTables(databaseId: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/database/tables/${databaseId}`, `${this.api.getUrl()}/database/tables/${databaseId}`,
"GET" "GET"
); );
@ -43,7 +43,7 @@ export class Database {
} }
async getColumns(databaseId: string, tableName: string): Promise<any> { async getColumns(databaseId: string, tableName: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/database/columns/${databaseId}/${tableName}`, `${this.api.getUrl()}/database/columns/${databaseId}/${tableName}`,
"GET" "GET"
); );
@ -52,7 +52,7 @@ export class Database {
} }
async migrateUp(databaseId: string): Promise<any> { async migrateUp(databaseId: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/database/migration/up/${databaseId}`, `${this.api.getUrl()}/database/migration/up/${databaseId}`,
"GET" "GET"
); );
@ -61,7 +61,7 @@ export class Database {
} }
async migrateDown(databaseId: string): Promise<any> { async migrateDown(databaseId: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/database/migration/down/${databaseId}`, `${this.api.getUrl()}/database/migration/down/${databaseId}`,
"GET" "GET"
); );
@ -74,7 +74,7 @@ export class Database {
down: string, down: string,
databaseId: string databaseId: string
): Promise<any> { ): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/database/migration/create`, `${this.api.getUrl()}/database/migration/create`,
"POST", "POST",
{ {
@ -88,7 +88,7 @@ export class Database {
} }
async query(databaseId: string, query: string): Promise<any> { async query(databaseId: string, query: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/database/query/${databaseId}`, `${this.api.getUrl()}/database/query/${databaseId}`,
"POST", "POST",
{ {

View File

@ -4,7 +4,7 @@ export class Functions {
constructor(private api: Api) {} constructor(private api: Api) {}
async create(name: string, source: string): Promise<any> { async create(name: string, source: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/functions/create`, `${this.api.getUrl()}/functions/create`,
"POST", "POST",
{ {
@ -17,7 +17,7 @@ export class Functions {
} }
async delete(name: string): Promise<any> { async delete(name: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/functions/delete/${name}`, `${this.api.getUrl()}/functions/delete/${name}`,
"DELETE" "DELETE"
); );

View File

@ -4,7 +4,7 @@ export class Project {
constructor(private api: Api) {} constructor(private api: Api) {}
async create(name: string, withDb: boolean = true): Promise<any> { async create(name: string, withDb: boolean = true): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/project/${withDb ? "create" : "create-without-db"}`, `${this.api.getUrl()}/project/${withDb ? "create" : "create-without-db"}`,
"PUT", "PUT",
{ {
@ -16,7 +16,7 @@ export class Project {
} }
async createSettings(setting: { key: string; value: string }): Promise<any> { async createSettings(setting: { key: string; value: string }): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/project/settings/create`, `${this.api.getUrl()}/project/settings/create`,
"PUT", "PUT",
setting setting
@ -26,7 +26,7 @@ export class Project {
} }
async deleteSetting(key: string): Promise<any> { async deleteSetting(key: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/project/settings/delete/${key}`, `${this.api.getUrl()}/project/settings/delete/${key}`,
"DELETE" "DELETE"
); );
@ -35,7 +35,7 @@ export class Project {
} }
async getAllSettings(): Promise<any> { async getAllSettings(): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/project/settings`, `${this.api.getUrl()}/project/settings`,
"GET" "GET"
); );
@ -44,7 +44,7 @@ export class Project {
} }
async getAllApiTokens(): Promise<any> { async getAllApiTokens(): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/project/api-tokens`, `${this.api.getUrl()}/project/api-tokens`,
"GET" "GET"
); );
@ -53,7 +53,7 @@ export class Project {
} }
async getDetails(): Promise<any> { async getDetails(): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/project/details`, `${this.api.getUrl()}/project/details`,
"GET" "GET"
); );
@ -62,7 +62,7 @@ export class Project {
} }
async updateMeta(meta: Record<string, any>): Promise<any> { async updateMeta(meta: Record<string, any>): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/project/update/meta`, `${this.api.getUrl()}/project/update/meta`,
"POST", "POST",
{ meta } { meta }

View File

@ -9,7 +9,7 @@ export class Query {
isTypescript: number = 0, isTypescript: number = 0,
isPublic: number = 0 isPublic: number = 0
): Promise<any> { ): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/query/create`, `${this.api.getUrl()}/query/create`,
"POST", "POST",
{ {
@ -31,7 +31,7 @@ export class Query {
isPublic?: number; isPublic?: number;
}> }>
): Promise<any> { ): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/query/update/${id}`, `${this.api.getUrl()}/query/update/${id}`,
"POST", "POST",
updateData updateData
@ -43,11 +43,10 @@ export class Query {
async run( async run(
id: string, id: string,
query: Record<string, any>, query: Record<string, any>,
headers: Record<string, any> = {}, headers: Record<string, any> = {}
isPublic: boolean = false
): Promise<any> { ): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/query/${isPublic ? "run-public" : "run"}/${id}`, `${this.api.getUrl()}/query/run/${id}`,
"POST", "POST",
query, query,
headers headers
@ -56,16 +55,23 @@ export class Query {
return response.data; return response.data;
} }
runPublic( async runPublic(
id: string, id: string,
query: Record<string, any>, query: Record<string, any>,
headers: Record<string, any> = {} headers: Record<string, any> = {}
): Promise<any> { ): Promise<any> {
return this.run(id, query, headers, true); const response = await this.api.request(
`${this.api.getUrl()}/query/run-public/${id}`,
"POST",
query,
headers
);
return response.data;
} }
async delete(id: string): Promise<any> { async delete(id: string): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/query/delete/${id}`, `${this.api.getUrl()}/query/delete/${id}`,
"DELETE" "DELETE"
); );

View File

@ -9,7 +9,7 @@ export class Redis {
user: string, user: string,
password: string password: string
): Promise<any> { ): Promise<any> {
const response = await this.api.createRequestWithAuthHeaders( const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/redis/node/create`, `${this.api.getUrl()}/redis/node/create`,
"POST", "POST",
{ {

View File

@ -0,0 +1,45 @@
import { Api } from "./api.class";
export class Session {
constructor(private api: Api) {}
async create(ttl: number = 3600): Promise<any> {
const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/session/create`,
"POST",
{
ttl,
}
);
return response.data;
}
async get(sessionId: string): Promise<any> {
const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/session/${sessionId}`,
"GET"
);
return response.data;
}
async set(sessionId: string, data: any): Promise<void> {
const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/session/${sessionId}`,
"POST",
data
);
return response.data;
}
async delete(sessionId: string): Promise<void> {
const response = await this.api.requestWithAuthHeaders(
`${this.api.getUrl()}/session/${sessionId}`,
"DELETE"
);
return response.data;
}
}