перевел на наследование

Добавил контекст
jwt
Rinsvent 2 years ago
parent 2cd4d87d0a
commit 7ca897f7b0
  1. 23
      api/schema-client.ts
  2. 30
      api/sm/sm-client.ts

@ -19,14 +19,25 @@ export interface SchemaInterface {
contentType: string | null contentType: string | null
} }
export class Context {
debouncing: number | null = null
}
export class SchemaClient { export class SchemaClient {
baseUrl: string baseUrl: string | null = null
context: Context | null = null
constructor({baseUrl}: ClientOptions) { debouncing(time: number)
this.baseUrl = baseUrl {
let context = this.grabContext()
context.debouncing = time
this.context = context
return this
} }
async send(schema: SchemaInterface, data: any) { async send(schema: SchemaInterface, data: any) {
let context = this.context
this.context = null
let url = `${this.baseUrl}${schema.url}` let url = `${this.baseUrl}${schema.url}`
let {url: preparedUrl, data: preparedData} = this.processAttributes(url, data) 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') let responseData = response.headers.get('Content-Type')?.toString().includes('application/json')
? await response.json() ? await response.json()
: await response.text() : await response.text()
return {responseData: responseData, headers: response.headers} return {responseData: responseData, headers: response.headers}
} }
@ -60,4 +72,9 @@ export class SchemaClient {
} }
return {url, data: preparedData } return {url, data: preparedData }
} }
private grabContext(): Context
{
return this.context || new Context()
}
} }

@ -17,17 +17,11 @@ import stopProcessSchema from "./schemas/stop-process";
import killProcessSchema from "./schemas/kill-process"; import killProcessSchema from "./schemas/kill-process";
import commandSchema from "./schemas/command"; import commandSchema from "./schemas/command";
export class SMClient { export class SMClient extends SchemaClient {
schemaClient: SchemaClient baseUrl = 'http://fmw.sipachev.sv'
constructor() {
this.schemaClient = new SchemaClient({
baseUrl: 'http://fmw.sipachev.sv'
})
}
async getCommands(): Promise<ResponseInterface<CommandResponseInterface[]>> { async getCommands(): Promise<ResponseInterface<CommandResponseInterface[]>> {
let { responseData, headers } = await this.schemaClient.send(commandsSchema, {}) let { responseData, headers } = await this.send(commandsSchema, {})
return { return {
data: responseData, data: responseData,
headers: headers headers: headers
@ -35,7 +29,7 @@ export class SMClient {
} }
async getCommand(name: string): Promise<ResponseInterface<CommandResponseInterface>> { async getCommand(name: string): Promise<ResponseInterface<CommandResponseInterface>> {
let { responseData, headers } = await this.schemaClient.send(commandSchema, { let { responseData, headers } = await this.send(commandSchema, {
commandName: name commandName: name
}) })
return { return {
@ -45,7 +39,7 @@ export class SMClient {
} }
async runCommand(data: any): Promise<ResponseInterface> { async runCommand(data: any): Promise<ResponseInterface> {
let { headers } = await this.schemaClient.send(runCommandsSchema, data) let { headers } = await this.send(runCommandsSchema, data)
return { return {
data: null, data: null,
headers: headers headers: headers
@ -53,7 +47,7 @@ export class SMClient {
} }
async getProcesses(data: GetProcessesRequest | PaginationRequest): Promise<ResponseInterface<ProcessesResponseInterface[]>> { async getProcesses(data: GetProcessesRequest | PaginationRequest): Promise<ResponseInterface<ProcessesResponseInterface[]>> {
let { responseData, headers } = await this.schemaClient.send(processesSchema, data) let { responseData, headers } = await this.send(processesSchema, data)
return { return {
data: responseData, data: responseData,
headers: headers headers: headers
@ -61,7 +55,7 @@ export class SMClient {
} }
async repeatProcess(data: RepeatProcessRequest): Promise<ResponseInterface> { async repeatProcess(data: RepeatProcessRequest): Promise<ResponseInterface> {
let { responseData, headers } = await this.schemaClient.send(repeatProcessSchema, data) let { responseData, headers } = await this.send(repeatProcessSchema, data)
return { return {
data: responseData as ProcessesResponseInterface[], data: responseData as ProcessesResponseInterface[],
headers: headers headers: headers
@ -69,7 +63,7 @@ export class SMClient {
} }
async getProcessOutput(data: ProcessOutputRequest): Promise<ResponseInterface> { async getProcessOutput(data: ProcessOutputRequest): Promise<ResponseInterface> {
let { responseData, headers } = await this.schemaClient.send(processOutputSchema, data) let { responseData, headers } = await this.send(processOutputSchema, data)
return { return {
data: responseData, data: responseData,
headers: headers headers: headers
@ -77,7 +71,7 @@ export class SMClient {
} }
async playProcess(id: string): Promise<ResponseInterface> { async playProcess(id: string): Promise<ResponseInterface> {
let { headers } = await this.schemaClient.send(playProcessSchema, {id}) let { headers } = await this.send(playProcessSchema, {id})
return { return {
data: null, data: null,
headers: headers headers: headers
@ -85,7 +79,7 @@ export class SMClient {
} }
async pauseProcess(id: string): Promise<ResponseInterface> { async pauseProcess(id: string): Promise<ResponseInterface> {
let { headers } = await this.schemaClient.send(pauseProcessSchema, {id}) let { headers } = await this.send(pauseProcessSchema, {id})
return { return {
data: null, data: null,
headers: headers headers: headers
@ -93,7 +87,7 @@ export class SMClient {
} }
async stopProcess(id: string): Promise<ResponseInterface> { async stopProcess(id: string): Promise<ResponseInterface> {
let { headers } = await this.schemaClient.send(stopProcessSchema, {id}) let { headers } = await this.send(stopProcessSchema, {id})
return { return {
data: null, data: null,
headers: headers headers: headers
@ -101,7 +95,7 @@ export class SMClient {
} }
async killProcess(id: string): Promise<ResponseInterface> { async killProcess(id: string): Promise<ResponseInterface> {
let { headers } = await this.schemaClient.send(killProcessSchema, {id}) let { headers } = await this.send(killProcessSchema, {id})
return { return {
data: null, data: null,
headers: headers headers: headers

Loading…
Cancel
Save