parent
5b0960da0c
commit
43339e6a92
@ -0,0 +1,92 @@ |
|||||||
|
/* eslint-disable @typescript-eslint/no-empty-interface */ |
||||||
|
import { SchemaInterface } from './SchemaInterface' |
||||||
|
import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios' |
||||||
|
import stubResolver from '@/stubs/StubResolver' |
||||||
|
|
||||||
|
export abstract class Handler { |
||||||
|
abstract handle: any |
||||||
|
} |
||||||
|
|
||||||
|
export interface RequestInterface { |
||||||
|
method: Method; |
||||||
|
url: string; |
||||||
|
data: Record<string, any> | null; |
||||||
|
headers: Record<string, any> | null; |
||||||
|
} |
||||||
|
|
||||||
|
interface RequestHandleFunction { |
||||||
|
(request: RequestInterface): void; |
||||||
|
} |
||||||
|
|
||||||
|
export abstract class RequestHandler extends Handler { |
||||||
|
abstract handle: RequestHandleFunction |
||||||
|
} |
||||||
|
|
||||||
|
export interface ResponseInterface extends AxiosResponse { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
interface ResponseHandleFunction { |
||||||
|
(response: ResponseInterface): void; |
||||||
|
} |
||||||
|
|
||||||
|
export abstract class ResponseHandler extends Handler { |
||||||
|
abstract handle: ResponseHandleFunction |
||||||
|
} |
||||||
|
|
||||||
|
export class HttpClient { |
||||||
|
private baseUrl: string |
||||||
|
private handlers: Handler[] = [] |
||||||
|
|
||||||
|
constructor (baseUrl: string) { |
||||||
|
this.baseUrl = baseUrl |
||||||
|
} |
||||||
|
|
||||||
|
send (schema: SchemaInterface, data: any | null = null): Promise<AxiosResponse> { |
||||||
|
const requestData = { |
||||||
|
method: schema.method as Method, |
||||||
|
url: schema.url, |
||||||
|
data: data, |
||||||
|
headers: { |
||||||
|
'Content-Type': 'application/json;charset=utf-8' |
||||||
|
} |
||||||
|
} as RequestInterface |
||||||
|
|
||||||
|
/** |
||||||
|
* todo перевести на промисы + |
||||||
|
*/ |
||||||
|
this.handlers |
||||||
|
.filter((handler: Handler) => { |
||||||
|
return handler instanceof RequestHandler |
||||||
|
}) |
||||||
|
.map((handler: Handler) => { |
||||||
|
handler.handle(requestData) |
||||||
|
return handler |
||||||
|
}) |
||||||
|
|
||||||
|
const stub = stubResolver.handle(schema.code) |
||||||
|
const response = stub || axios(requestData as AxiosRequestConfig) |
||||||
|
|
||||||
|
return response |
||||||
|
.then((response: any) => { |
||||||
|
/** |
||||||
|
* todo перевести на промисы + |
||||||
|
*/ |
||||||
|
this.handlers |
||||||
|
.filter((handler: Handler) => { |
||||||
|
return handler instanceof ResponseHandler |
||||||
|
}) |
||||||
|
.map((handler: Handler) => { |
||||||
|
handler.handle(response) |
||||||
|
return handler |
||||||
|
}) |
||||||
|
|
||||||
|
return response |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
addHandler (handler: Handler) { |
||||||
|
this.handlers.push(handler) |
||||||
|
return this |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,11 @@ |
|||||||
|
import { HttpClient as BaseClient } from '../../api/HttpClient' |
||||||
|
import MocksSchema, { MockConfigurationsRequest } from '../../api/Main/Schemas/MocksSchema' |
||||||
|
import { SchemaInterface } from '@/api/SchemaInterface' |
||||||
|
|
||||||
|
export class HttpClient extends BaseClient { |
||||||
|
mockConfigurations (data: MockConfigurationsRequest): any { |
||||||
|
return this.send(MocksSchema as SchemaInterface, data) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export default new HttpClient('http://laen.sipachev.ru') |
@ -0,0 +1,9 @@ |
|||||||
|
export class MockConfigurationsRequest { |
||||||
|
personalId!: string |
||||||
|
} |
||||||
|
|
||||||
|
export default { |
||||||
|
code: 'api_plugin_get_configurations', |
||||||
|
method: 'POST', |
||||||
|
url: '/plugin/mock-configurations' |
||||||
|
} |
@ -0,0 +1,6 @@ |
|||||||
|
export interface SchemaInterface { |
||||||
|
code: string; |
||||||
|
method: string; |
||||||
|
url: string; |
||||||
|
stub: any; |
||||||
|
} |
@ -0,0 +1,3 @@ |
|||||||
|
export enum FlowEnum { |
||||||
|
base |
||||||
|
} |
@ -0,0 +1,11 @@ |
|||||||
|
import { FlowEnum } from '@/stubs/FlowEnum' |
||||||
|
import MocksSchema from '@/api/Main/Schemas/MocksSchema' |
||||||
|
import MocksSchemaSuccess from '@/stubs/stubs/Main/MocksSchema/MocksSchemaSuccess' |
||||||
|
|
||||||
|
const map: Record<string, any> = {} |
||||||
|
|
||||||
|
// Регистрируем базовые успешные заглуши
|
||||||
|
map[FlowEnum.base] = {} |
||||||
|
map[FlowEnum.base][MocksSchema.code] = MocksSchemaSuccess |
||||||
|
|
||||||
|
export default map |
@ -0,0 +1,24 @@ |
|||||||
|
import { flow, useStub } from '@/stubs/config' |
||||||
|
import map from '@/stubs/Stub2Flow' |
||||||
|
|
||||||
|
export class StubResolver { |
||||||
|
handle (routeCode: string): Promise<any> | null { |
||||||
|
if (!useStub) { |
||||||
|
return null |
||||||
|
} |
||||||
|
const hasFlow = flow in map || false |
||||||
|
if (!hasFlow) { |
||||||
|
throw new Error('Flow not found!!! Change flow in @/stubs/config.ts') |
||||||
|
} |
||||||
|
const availableStubs = map[flow] |
||||||
|
const hasRoute = routeCode in availableStubs || false |
||||||
|
if (!hasRoute) { |
||||||
|
throw new Error('Route`s stub not found!!! Add stub or change flow to check application') |
||||||
|
} |
||||||
|
|
||||||
|
return availableStubs[routeCode] |
||||||
|
// return new Promise(() => {})
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export default new StubResolver() |
@ -0,0 +1,4 @@ |
|||||||
|
import { FlowEnum } from '@/stubs/FlowEnum' |
||||||
|
|
||||||
|
export const useStub = true |
||||||
|
export const flow = FlowEnum.base |
@ -0,0 +1,12 @@ |
|||||||
|
import { AxiosResponse } from 'axios' |
||||||
|
|
||||||
|
export default new Promise((resolve) => { |
||||||
|
const response = { |
||||||
|
data: {}, |
||||||
|
status: 200, |
||||||
|
statusText: 'OK', |
||||||
|
headers: {}, |
||||||
|
config: {} |
||||||
|
} as AxiosResponse |
||||||
|
resolve(response) |
||||||
|
}) |
Loading…
Reference in new issue