diff --git a/api/schema-client.ts b/api/schema-client.ts index aaef855..e48a3ce 100644 --- a/api/schema-client.ts +++ b/api/schema-client.ts @@ -19,14 +19,25 @@ export interface SchemaInterface { contentType: string | null } +export class Context { + debouncing: number | null = null +} + export class SchemaClient { - baseUrl: string + baseUrl: string | null = null + context: Context | null = null - constructor({baseUrl}: ClientOptions) { - this.baseUrl = baseUrl + debouncing(time: number) + { + let context = this.grabContext() + context.debouncing = time + this.context = context + return this } async send(schema: SchemaInterface, data: any) { + let context = this.context + this.context = null let url = `${this.baseUrl}${schema.url}` let {url: preparedUrl, data: preparedData} = this.processAttributes(url, data) @@ -46,6 +57,7 @@ export class SchemaClient { let responseData = response.headers.get('Content-Type')?.toString().includes('application/json') ? await response.json() : await response.text() + return {responseData: responseData, headers: response.headers} } @@ -60,4 +72,9 @@ export class SchemaClient { } return {url, data: preparedData } } + + private grabContext(): Context + { + return this.context || new Context() + } } \ No newline at end of file diff --git a/api/sm/sm-client.ts b/api/sm/sm-client.ts index 2d6b667..0ba4f32 100644 --- a/api/sm/sm-client.ts +++ b/api/sm/sm-client.ts @@ -17,17 +17,11 @@ import stopProcessSchema from "./schemas/stop-process"; import killProcessSchema from "./schemas/kill-process"; import commandSchema from "./schemas/command"; -export class SMClient { - schemaClient: SchemaClient - - constructor() { - this.schemaClient = new SchemaClient({ - baseUrl: 'http://fmw.sipachev.sv' - }) - } +export class SMClient extends SchemaClient { + baseUrl = 'http://fmw.sipachev.sv' async getCommands(): Promise> { - let { responseData, headers } = await this.schemaClient.send(commandsSchema, {}) + let { responseData, headers } = await this.send(commandsSchema, {}) return { data: responseData, headers: headers @@ -35,7 +29,7 @@ export class SMClient { } async getCommand(name: string): Promise> { - let { responseData, headers } = await this.schemaClient.send(commandSchema, { + let { responseData, headers } = await this.send(commandSchema, { commandName: name }) return { @@ -45,7 +39,7 @@ export class SMClient { } async runCommand(data: any): Promise { - let { headers } = await this.schemaClient.send(runCommandsSchema, data) + let { headers } = await this.send(runCommandsSchema, data) return { data: null, headers: headers @@ -53,7 +47,7 @@ export class SMClient { } async getProcesses(data: GetProcessesRequest | PaginationRequest): Promise> { - let { responseData, headers } = await this.schemaClient.send(processesSchema, data) + let { responseData, headers } = await this.send(processesSchema, data) return { data: responseData, headers: headers @@ -61,7 +55,7 @@ export class SMClient { } async repeatProcess(data: RepeatProcessRequest): Promise { - let { responseData, headers } = await this.schemaClient.send(repeatProcessSchema, data) + let { responseData, headers } = await this.send(repeatProcessSchema, data) return { data: responseData as ProcessesResponseInterface[], headers: headers @@ -69,7 +63,7 @@ export class SMClient { } async getProcessOutput(data: ProcessOutputRequest): Promise { - let { responseData, headers } = await this.schemaClient.send(processOutputSchema, data) + let { responseData, headers } = await this.send(processOutputSchema, data) return { data: responseData, headers: headers @@ -77,7 +71,7 @@ export class SMClient { } async playProcess(id: string): Promise { - let { headers } = await this.schemaClient.send(playProcessSchema, {id}) + let { headers } = await this.send(playProcessSchema, {id}) return { data: null, headers: headers @@ -85,7 +79,7 @@ export class SMClient { } async pauseProcess(id: string): Promise { - let { headers } = await this.schemaClient.send(pauseProcessSchema, {id}) + let { headers } = await this.send(pauseProcessSchema, {id}) return { data: null, headers: headers @@ -93,7 +87,7 @@ export class SMClient { } async stopProcess(id: string): Promise { - let { headers } = await this.schemaClient.send(stopProcessSchema, {id}) + let { headers } = await this.send(stopProcessSchema, {id}) return { data: null, headers: headers @@ -101,7 +95,7 @@ export class SMClient { } async killProcess(id: string): Promise { - let { headers } = await this.schemaClient.send(killProcessSchema, {id}) + let { headers } = await this.send(killProcessSchema, {id}) return { data: null, headers: headers