feat: add session management class and refactor API request methods
This commit is contained in:
@ -5,6 +5,7 @@ import { Database } from "./database.class";
|
||||
import { Query } from "./query.class";
|
||||
import { Command } from "./command.class";
|
||||
import { Redis } from "./redis.class";
|
||||
import { Session } from "./session.class";
|
||||
|
||||
export class Api {
|
||||
public project: Project;
|
||||
@ -12,6 +13,7 @@ export class Api {
|
||||
public query: Query;
|
||||
public command: Command;
|
||||
public redis: Redis;
|
||||
public session: Session;
|
||||
|
||||
constructor(private url: string, private token: string) {
|
||||
this.project = new Project(this);
|
||||
@ -19,13 +21,14 @@ export class Api {
|
||||
this.query = new Query(this);
|
||||
this.redis = new Redis(this);
|
||||
this.command = new Command(this);
|
||||
this.session = new Session(this);
|
||||
}
|
||||
|
||||
public getUrl(): string {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
public async createRequestWithAuthHeaders(
|
||||
public async requestWithAuthHeaders(
|
||||
url: string,
|
||||
method: string,
|
||||
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> {
|
||||
const response = await this.createRequestWithAuthHeaders(
|
||||
const response = await this.requestWithAuthHeaders(
|
||||
`${this.url}/api/token/generate`,
|
||||
"POST",
|
||||
{
|
||||
@ -55,7 +72,7 @@ export class Api {
|
||||
}
|
||||
|
||||
async revokeToken(token: string): Promise<GenerateTokenResponse> {
|
||||
const response = await this.createRequestWithAuthHeaders(
|
||||
const response = await this.requestWithAuthHeaders(
|
||||
`${this.url}/api/token/revoke/${token}`,
|
||||
"DELETE"
|
||||
);
|
||||
|
||||
@ -9,7 +9,7 @@ export class Command {
|
||||
isTypescript: number = 0,
|
||||
isPublic: number = 0
|
||||
): Promise<any> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/command/create`,
|
||||
"POST",
|
||||
{
|
||||
@ -31,7 +31,7 @@ export class Command {
|
||||
isPublic?: number;
|
||||
}>
|
||||
): Promise<any> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/command/update/${id}`,
|
||||
"POST",
|
||||
updateData
|
||||
@ -43,11 +43,10 @@ export class Command {
|
||||
async run(
|
||||
id: string,
|
||||
query: Record<string, any>,
|
||||
headers: Record<string, any> = {},
|
||||
isPublic: boolean = false
|
||||
headers: Record<string, any> = {}
|
||||
): Promise<any> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/command/${isPublic ? "run-public" : "run"}/${id}`,
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/command/run/${id}`,
|
||||
"POST",
|
||||
query,
|
||||
headers
|
||||
@ -56,16 +55,23 @@ export class Command {
|
||||
return response.data;
|
||||
}
|
||||
|
||||
runPublic(
|
||||
async runPublic(
|
||||
id: string,
|
||||
query: Record<string, any>,
|
||||
headers: Record<string, 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> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/command/delete/${id}`,
|
||||
"DELETE"
|
||||
);
|
||||
|
||||
@ -4,7 +4,7 @@ export class Database {
|
||||
constructor(private api: Api) {}
|
||||
|
||||
async create(projectId: string): Promise<any> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/database/create`,
|
||||
"POST",
|
||||
{ projectId }
|
||||
@ -19,7 +19,7 @@ export class Database {
|
||||
username: string,
|
||||
password: string
|
||||
): Promise<any> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/database/node/create`,
|
||||
"POST",
|
||||
{
|
||||
@ -34,7 +34,7 @@ export class Database {
|
||||
}
|
||||
|
||||
async getTables(databaseId: string): Promise<any> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/database/tables/${databaseId}`,
|
||||
"GET"
|
||||
);
|
||||
@ -43,7 +43,7 @@ export class Database {
|
||||
}
|
||||
|
||||
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}`,
|
||||
"GET"
|
||||
);
|
||||
@ -52,7 +52,7 @@ export class Database {
|
||||
}
|
||||
|
||||
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}`,
|
||||
"GET"
|
||||
);
|
||||
@ -61,7 +61,7 @@ export class Database {
|
||||
}
|
||||
|
||||
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}`,
|
||||
"GET"
|
||||
);
|
||||
@ -74,7 +74,7 @@ export class Database {
|
||||
down: string,
|
||||
databaseId: string
|
||||
): Promise<any> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/database/migration/create`,
|
||||
"POST",
|
||||
{
|
||||
@ -88,7 +88,7 @@ export class Database {
|
||||
}
|
||||
|
||||
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}`,
|
||||
"POST",
|
||||
{
|
||||
|
||||
@ -4,7 +4,7 @@ export class Functions {
|
||||
constructor(private api: Api) {}
|
||||
|
||||
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`,
|
||||
"POST",
|
||||
{
|
||||
@ -17,7 +17,7 @@ export class Functions {
|
||||
}
|
||||
|
||||
async delete(name: string): Promise<any> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/functions/delete/${name}`,
|
||||
"DELETE"
|
||||
);
|
||||
|
||||
@ -4,7 +4,7 @@ export class Project {
|
||||
constructor(private api: Api) {}
|
||||
|
||||
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"}`,
|
||||
"PUT",
|
||||
{
|
||||
@ -16,7 +16,7 @@ export class Project {
|
||||
}
|
||||
|
||||
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`,
|
||||
"PUT",
|
||||
setting
|
||||
@ -26,7 +26,7 @@ export class Project {
|
||||
}
|
||||
|
||||
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}`,
|
||||
"DELETE"
|
||||
);
|
||||
@ -35,7 +35,7 @@ export class Project {
|
||||
}
|
||||
|
||||
async getAllSettings(): Promise<any> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/project/settings`,
|
||||
"GET"
|
||||
);
|
||||
@ -44,7 +44,7 @@ export class Project {
|
||||
}
|
||||
|
||||
async getAllApiTokens(): Promise<any> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/project/api-tokens`,
|
||||
"GET"
|
||||
);
|
||||
@ -53,7 +53,7 @@ export class Project {
|
||||
}
|
||||
|
||||
async getDetails(): Promise<any> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/project/details`,
|
||||
"GET"
|
||||
);
|
||||
@ -62,7 +62,7 @@ export class Project {
|
||||
}
|
||||
|
||||
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`,
|
||||
"POST",
|
||||
{ meta }
|
||||
|
||||
@ -9,7 +9,7 @@ export class Query {
|
||||
isTypescript: number = 0,
|
||||
isPublic: number = 0
|
||||
): Promise<any> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/query/create`,
|
||||
"POST",
|
||||
{
|
||||
@ -31,7 +31,7 @@ export class Query {
|
||||
isPublic?: number;
|
||||
}>
|
||||
): Promise<any> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/query/update/${id}`,
|
||||
"POST",
|
||||
updateData
|
||||
@ -43,11 +43,10 @@ export class Query {
|
||||
async run(
|
||||
id: string,
|
||||
query: Record<string, any>,
|
||||
headers: Record<string, any> = {},
|
||||
isPublic: boolean = false
|
||||
headers: Record<string, any> = {}
|
||||
): Promise<any> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/query/${isPublic ? "run-public" : "run"}/${id}`,
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/query/run/${id}`,
|
||||
"POST",
|
||||
query,
|
||||
headers
|
||||
@ -56,16 +55,23 @@ export class Query {
|
||||
return response.data;
|
||||
}
|
||||
|
||||
runPublic(
|
||||
async runPublic(
|
||||
id: string,
|
||||
query: Record<string, any>,
|
||||
headers: Record<string, 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> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/query/delete/${id}`,
|
||||
"DELETE"
|
||||
);
|
||||
|
||||
@ -9,7 +9,7 @@ export class Redis {
|
||||
user: string,
|
||||
password: string
|
||||
): Promise<any> {
|
||||
const response = await this.api.createRequestWithAuthHeaders(
|
||||
const response = await this.api.requestWithAuthHeaders(
|
||||
`${this.api.getUrl()}/redis/node/create`,
|
||||
"POST",
|
||||
{
|
||||
|
||||
45
src/classes/session.class.ts
Normal file
45
src/classes/session.class.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user