Добавил механизм работы с апи

master
Sipachev Igor 3 years ago
parent 5b0960da0c
commit 43339e6a92
  1. 1
      popup/package.json
  2. 92
      popup/src/api/HttpClient.ts
  3. 11
      popup/src/api/Main/HttpClient.ts
  4. 9
      popup/src/api/Main/Schemas/MocksSchema.ts
  5. 6
      popup/src/api/SchemaInterface.ts
  6. 3
      popup/src/stubs/FlowEnum.ts
  7. 11
      popup/src/stubs/Stub2Flow.ts
  8. 24
      popup/src/stubs/StubResolver.ts
  9. 4
      popup/src/stubs/config.ts
  10. 12
      popup/src/stubs/stubs/Main/MocksSchema/MocksSchemaSuccess.ts
  11. 12
      popup/yarn.lock

@ -12,6 +12,7 @@
"dependencies": {
"@types/chrome": "^0.0.133",
"@types/clipboard": "^2.0.1",
"axios": "^0.26.1",
"clipboard": "^2.0.6",
"core-js": "^3.6.5",
"register-service-worker": "^1.7.1",

@ -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)
})

@ -2322,6 +2322,13 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.11.0.tgz#d61f46d83b2519250e2784daf5b09479a8b41c59"
integrity sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==
axios@^0.26.1:
version "0.26.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.26.1.tgz#1ede41c51fcf51bbbd6fd43669caaa4f0495aaa9"
integrity sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==
dependencies:
follow-redirects "^1.14.8"
babel-code-frame@^6.22.0, babel-code-frame@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b"
@ -5088,6 +5095,11 @@ follow-redirects@^1.0.0:
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.2.tgz#dd73c8effc12728ba5cf4259d760ea5fb83e3147"
integrity sha512-6mPTgLxYm3r6Bkkg0vNM0HTjfGrOEtsfbhagQvbxDEsEkpNhw582upBaoRZylzen6krEmxXJgt9Ju6HiI4O7BA==
follow-redirects@^1.14.8:
version "1.14.9"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7"
integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==
for-each@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/for-each/-/for-each-0.3.3.tgz#69b447e88a0a5d32c3e7084f3f1710034b21376e"

Loading…
Cancel
Save