From 43339e6a929af1e29b9df931146936e634127e8b Mon Sep 17 00:00:00 2001 From: Sipachev Igor Date: Fri, 11 Mar 2022 13:34:23 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BC=D0=B5=D1=85=D0=B0=D0=BD=D0=B8=D0=B7=D0=BC=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D1=8B=20=D1=81=20=D0=B0=D0=BF=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- popup/package.json | 1 + popup/src/api/HttpClient.ts | 92 +++++++++++++++++++ popup/src/api/Main/HttpClient.ts | 11 +++ popup/src/api/Main/Schemas/MocksSchema.ts | 9 ++ popup/src/api/SchemaInterface.ts | 6 ++ popup/src/stubs/FlowEnum.ts | 3 + popup/src/stubs/Stub2Flow.ts | 11 +++ popup/src/stubs/StubResolver.ts | 24 +++++ popup/src/stubs/config.ts | 4 + .../Main/MocksSchema/MocksSchemaSuccess.ts | 12 +++ popup/yarn.lock | 12 +++ 11 files changed, 185 insertions(+) create mode 100644 popup/src/api/HttpClient.ts create mode 100644 popup/src/api/Main/HttpClient.ts create mode 100644 popup/src/api/Main/Schemas/MocksSchema.ts create mode 100644 popup/src/api/SchemaInterface.ts create mode 100644 popup/src/stubs/FlowEnum.ts create mode 100644 popup/src/stubs/Stub2Flow.ts create mode 100644 popup/src/stubs/StubResolver.ts create mode 100644 popup/src/stubs/config.ts create mode 100644 popup/src/stubs/stubs/Main/MocksSchema/MocksSchemaSuccess.ts diff --git a/popup/package.json b/popup/package.json index e274ccd..4dec9bd 100644 --- a/popup/package.json +++ b/popup/package.json @@ -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", diff --git a/popup/src/api/HttpClient.ts b/popup/src/api/HttpClient.ts new file mode 100644 index 0000000..e1656d7 --- /dev/null +++ b/popup/src/api/HttpClient.ts @@ -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 | null; + headers: Record | 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 { + 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 + } +} diff --git a/popup/src/api/Main/HttpClient.ts b/popup/src/api/Main/HttpClient.ts new file mode 100644 index 0000000..2529d73 --- /dev/null +++ b/popup/src/api/Main/HttpClient.ts @@ -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') diff --git a/popup/src/api/Main/Schemas/MocksSchema.ts b/popup/src/api/Main/Schemas/MocksSchema.ts new file mode 100644 index 0000000..24b9036 --- /dev/null +++ b/popup/src/api/Main/Schemas/MocksSchema.ts @@ -0,0 +1,9 @@ +export class MockConfigurationsRequest { + personalId!: string +} + +export default { + code: 'api_plugin_get_configurations', + method: 'POST', + url: '/plugin/mock-configurations' +} diff --git a/popup/src/api/SchemaInterface.ts b/popup/src/api/SchemaInterface.ts new file mode 100644 index 0000000..4564aec --- /dev/null +++ b/popup/src/api/SchemaInterface.ts @@ -0,0 +1,6 @@ +export interface SchemaInterface { + code: string; + method: string; + url: string; + stub: any; +} diff --git a/popup/src/stubs/FlowEnum.ts b/popup/src/stubs/FlowEnum.ts new file mode 100644 index 0000000..7ab8549 --- /dev/null +++ b/popup/src/stubs/FlowEnum.ts @@ -0,0 +1,3 @@ +export enum FlowEnum { + base +} diff --git a/popup/src/stubs/Stub2Flow.ts b/popup/src/stubs/Stub2Flow.ts new file mode 100644 index 0000000..d3f42be --- /dev/null +++ b/popup/src/stubs/Stub2Flow.ts @@ -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 = {} + +// Регистрируем базовые успешные заглуши +map[FlowEnum.base] = {} +map[FlowEnum.base][MocksSchema.code] = MocksSchemaSuccess + +export default map diff --git a/popup/src/stubs/StubResolver.ts b/popup/src/stubs/StubResolver.ts new file mode 100644 index 0000000..4b410ab --- /dev/null +++ b/popup/src/stubs/StubResolver.ts @@ -0,0 +1,24 @@ +import { flow, useStub } from '@/stubs/config' +import map from '@/stubs/Stub2Flow' + +export class StubResolver { + handle (routeCode: string): Promise | 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() diff --git a/popup/src/stubs/config.ts b/popup/src/stubs/config.ts new file mode 100644 index 0000000..63990cc --- /dev/null +++ b/popup/src/stubs/config.ts @@ -0,0 +1,4 @@ +import { FlowEnum } from '@/stubs/FlowEnum' + +export const useStub = true +export const flow = FlowEnum.base diff --git a/popup/src/stubs/stubs/Main/MocksSchema/MocksSchemaSuccess.ts b/popup/src/stubs/stubs/Main/MocksSchema/MocksSchemaSuccess.ts new file mode 100644 index 0000000..0785da1 --- /dev/null +++ b/popup/src/stubs/stubs/Main/MocksSchema/MocksSchemaSuccess.ts @@ -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) +}) diff --git a/popup/yarn.lock b/popup/yarn.lock index 567fe4c..6217be7 100644 --- a/popup/yarn.lock +++ b/popup/yarn.lock @@ -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"