diff --git a/src/classes/api.class.ts b/src/classes/api.class.ts index d80c318..8b02219 100644 --- a/src/classes/api.class.ts +++ b/src/classes/api.class.ts @@ -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 { + return await axios.request({ + url, + method, + data, + headers, + }); + } + async generateToken(projectId: string): Promise { - 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 { - const response = await this.createRequestWithAuthHeaders( + const response = await this.requestWithAuthHeaders( `${this.url}/api/token/revoke/${token}`, "DELETE" ); diff --git a/src/classes/command.class.ts b/src/classes/command.class.ts index 16afd54..fee9bcc 100644 --- a/src/classes/command.class.ts +++ b/src/classes/command.class.ts @@ -9,7 +9,7 @@ export class Command { isTypescript: number = 0, isPublic: number = 0 ): Promise { - 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 { - 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, - headers: Record = {}, - isPublic: boolean = false + headers: Record = {} ): Promise { - 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, headers: Record = {} ): Promise { - 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 { - const response = await this.api.createRequestWithAuthHeaders( + const response = await this.api.requestWithAuthHeaders( `${this.api.getUrl()}/command/delete/${id}`, "DELETE" ); diff --git a/src/classes/database.class.ts b/src/classes/database.class.ts index 3dc6340..810b43d 100644 --- a/src/classes/database.class.ts +++ b/src/classes/database.class.ts @@ -4,7 +4,7 @@ export class Database { constructor(private api: Api) {} async create(projectId: string): Promise { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 { - const response = await this.api.createRequestWithAuthHeaders( + const response = await this.api.requestWithAuthHeaders( `${this.api.getUrl()}/database/query/${databaseId}`, "POST", { diff --git a/src/classes/functions.class.ts b/src/classes/functions.class.ts index 9dd9350..0e814cc 100644 --- a/src/classes/functions.class.ts +++ b/src/classes/functions.class.ts @@ -4,7 +4,7 @@ export class Functions { constructor(private api: Api) {} async create(name: string, source: string): Promise { - 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 { - const response = await this.api.createRequestWithAuthHeaders( + const response = await this.api.requestWithAuthHeaders( `${this.api.getUrl()}/functions/delete/${name}`, "DELETE" ); diff --git a/src/classes/project.class..ts b/src/classes/project.class..ts index a39d747..84e2ba8 100644 --- a/src/classes/project.class..ts +++ b/src/classes/project.class..ts @@ -4,7 +4,7 @@ export class Project { constructor(private api: Api) {} async create(name: string, withDb: boolean = true): Promise { - 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 { - 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 { - 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 { - 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 { - 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 { - 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): Promise { - const response = await this.api.createRequestWithAuthHeaders( + const response = await this.api.requestWithAuthHeaders( `${this.api.getUrl()}/project/update/meta`, "POST", { meta } diff --git a/src/classes/query.class.ts b/src/classes/query.class.ts index f0aeb6b..1166024 100644 --- a/src/classes/query.class.ts +++ b/src/classes/query.class.ts @@ -9,7 +9,7 @@ export class Query { isTypescript: number = 0, isPublic: number = 0 ): Promise { - 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 { - 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, - headers: Record = {}, - isPublic: boolean = false + headers: Record = {} ): Promise { - 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, headers: Record = {} ): Promise { - 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 { - const response = await this.api.createRequestWithAuthHeaders( + const response = await this.api.requestWithAuthHeaders( `${this.api.getUrl()}/query/delete/${id}`, "DELETE" ); diff --git a/src/classes/redis.class.ts b/src/classes/redis.class.ts index ed734f3..c98448e 100644 --- a/src/classes/redis.class.ts +++ b/src/classes/redis.class.ts @@ -9,7 +9,7 @@ export class Redis { user: string, password: string ): Promise { - const response = await this.api.createRequestWithAuthHeaders( + const response = await this.api.requestWithAuthHeaders( `${this.api.getUrl()}/redis/node/create`, "POST", { diff --git a/src/classes/session.class.ts b/src/classes/session.class.ts new file mode 100644 index 0000000..4085904 --- /dev/null +++ b/src/classes/session.class.ts @@ -0,0 +1,45 @@ +import { Api } from "./api.class"; + +export class Session { + constructor(private api: Api) {} + + async create(ttl: number = 3600): Promise { + const response = await this.api.requestWithAuthHeaders( + `${this.api.getUrl()}/session/create`, + "POST", + { + ttl, + } + ); + + return response.data; + } + + async get(sessionId: string): Promise { + const response = await this.api.requestWithAuthHeaders( + `${this.api.getUrl()}/session/${sessionId}`, + "GET" + ); + + return response.data; + } + + async set(sessionId: string, data: any): Promise { + const response = await this.api.requestWithAuthHeaders( + `${this.api.getUrl()}/session/${sessionId}`, + "POST", + data + ); + + return response.data; + } + + async delete(sessionId: string): Promise { + const response = await this.api.requestWithAuthHeaders( + `${this.api.getUrl()}/session/${sessionId}`, + "DELETE" + ); + + return response.data; + } +}