Skip to content

Commit

Permalink
last work
Browse files Browse the repository at this point in the history
  • Loading branch information
scaljeri committed Dec 20, 2023
1 parent decc896 commit c85353c
Showing 19 changed files with 110 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ export class MockDetailsComponent implements OnInit, OnChanges {

delete values.contentType; // is not part if IOhMyResponse

this.storeService.upsertResponse({
this.storeService.upsertResponse(this.response.id, {
...values,
headersMock: {
...this.response.headersMock,
Original file line number Diff line number Diff line change
@@ -122,7 +122,7 @@ export class RequestHeaderComponent implements OnInit, OnChanges {
this.request.presets[this.context.presetId].responseId,
update, this.request, this.context);
} else {
this.storeService.upsertResponse(update, this.request, this.context);
this.storeService.upsertResponse(this.request.id, update, this.request, this.context);
}
});
}
14 changes: 7 additions & 7 deletions src/app/components/request/request.component.ts
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ export class RequestComponent implements OnChanges, OnDestroy {
}));

this.responseCtrl.valueChanges.subscribe(val => {
this.storeService.upsertResponse({ responseMock: val, id: this.response.id }, this.request, this.context);
this.storeService.upsertResponse(this.response.id, { responseMock: val, id: this.response.id }, this.request, this.context);
});

this.headersCtrl.valueChanges.subscribe(val => {
@@ -105,12 +105,12 @@ export class RequestComponent implements OnChanges, OnDestroy {
}

onRevertResponse(): void {
this.storeService.upsertResponse({ responseMock: this.response.response, id: this.response.id }, this.request, this.context);
this.storeService.upsertResponse(this.response.id, { responseMock: this.response.response, id: this.response.id }, this.request, this.context);
}

onHeadersChange(headersMock: string): void {
try {
this.storeService.upsertResponse({
this.storeService.upsertResponse(this.response.id, {
id: this.response.id,
headersMock: JSON.parse(headersMock)
}, this.request, this.context);
@@ -120,14 +120,14 @@ export class RequestComponent implements OnChanges, OnDestroy {
}

onRevertHeaders(): void {
this.storeService.upsertResponse({ headersMock: this.response.headers, id: this.response.id }, this.request, this.context);
this.storeService.upsertResponse(this.response.id, { headersMock: this.response.headers, id: this.response.id }, this.request, this.context);
}

openShowMockCode(): void {
const data = { code: this.response.jsCode, type: 'javascript', allowErrors: false };

this.openCodeDialog(data, (update: string) => {
this.storeService.upsertResponse({ jsCode: update, id: this.response.id }, this.request, this.context);
this.storeService.upsertResponse(this.response.id, { jsCode: update, id: this.response.id }, this.request, this.context);
});
}

@@ -138,7 +138,7 @@ export class RequestComponent implements OnChanges, OnDestroy {
};

this.openCodeDialog(data, (update: string) => {
this.storeService.upsertResponse({
this.storeService.upsertResponse(this.response.id, {
responseMock: update, id: this.response.id
}, this.request, this.context);
});
@@ -169,7 +169,7 @@ export class RequestComponent implements OnChanges, OnDestroy {
this.dialogIsOpen = false;

if (update) {
this.storeService.upsertResponse({
this.storeService.upsertResponse(this.response.id, {
id: this.response.id,
...(update.data && { responseMock: update.data }),
rules: update.rules
1 change: 1 addition & 0 deletions src/app/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// export const presetInfo = 'Enable a whole group of mocks by selecting a preset. To edit or delete presets click the edit icon';
export const presetInfo = 'Responses can be enabled or disabled per preset';
export const VERSION = '__OH_MY_VERSION__';
16 changes: 11 additions & 5 deletions src/app/services/content.service.ts
Original file line number Diff line number Diff line change
@@ -38,9 +38,9 @@ export class ContentService {
this.open(true);
});

this.listener = async ({ payload, source, domain }: IPacket, sender) => {
this.listener = async ({ payload, source }: IPacket, sender) => {
// Only accept messages from the content script
// const domain = payload.context?.domain;
const domain = payload.context?.key;
if (source !== appSources.CONTENT && source !== appSources.BACKGROUND || !domain) {
return;
}
@@ -79,7 +79,8 @@ export class ContentService {
payload: {
context: payload.context,
type: payloadType.API_RESPONSE_MOCKED,
data: output
data: output,
description: 'mocked sandboxed api response'
}
} as IPacket);

@@ -114,7 +115,10 @@ export class ContentService {
source: appSources.POPUP,
domain: this.appStateService.domain,
payload: {
type: payloadType.PING
type: payloadType.PING,
data: null,
context: null,
description: 'pingpong',
}
} as IPacket;

@@ -129,7 +133,9 @@ export class ContentService {
domain: this.appStateService.domain,
payload: {
type: isOpen ? payloadType.POPUP_OPEN : payloadType.POPUP_CLOSED,
data: { isOpen }
data: { isOpen },
description: '',
context: null
}
} as IPacket;

41 changes: 29 additions & 12 deletions src/app/services/oh-my-store.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { Injectable } from '@angular/core';
import { objectTypes, payloadType, STORAGE_KEY } from '@shared/constants';

import { IOhMyMock, IOhMyDomainId, IOhMyDomain, IOhMyResponseId, IOhMyRequest, IOhMyResponse, IOhMyContext, IOhMyRequestId, IOhMyAux, IOhMyPresetId, IOhMyShallowResponse, IOhMyDomainContext } from '@shared/types';
import {
IOhMyResponseUpdate,
IOhMyMock,
IOhMyDomainId,
IOhMyDomain,
IOhMyResponseId,
IOhMyRequest,
IOhMyResponse,
IOhMyContext,
IOhMyRequestId,
IOhMyAux,
IOhMyPresetId,
IOhMyShallowResponse,
IOhMyDomainContext,
IOhMyResponseUpsert
} from '@shared/types';
import { StateUtils } from '@shared/utils/state';
import { DataUtils } from '@shared/utils/data';
import { uniqueId } from '@shared/utils/unique-id';
import { url2regex } from '@shared/utils/urls';
import { StorageService } from './storage.service';
import { OhMySendToBg } from '@shared/utils/send-to-background';
import { IOhMyResponseUpdate } from '@shared/packet-type';

@Injectable({
providedIn: 'root'
@@ -90,7 +104,7 @@ export class OhMyState {

async cloneResponse(sourceId: IOhMyResponseId, update: Partial<IOhMyResponse>, request: Partial<IOhMyRequest>, context: IOhMyContext): Promise<IOhMyResponse> {
if (!sourceId) {
return this.upsertResponse(update, request, context);
return this.upsertResponse(update.id, update as IOhMyResponseUpdate, request, context);
}

const response = { ...await this.storageService.get<IOhMyResponse>(sourceId), ...update };
@@ -103,13 +117,14 @@ export class OhMyState {
delete response.modifiedOn;
}

return this.upsertResponse(response, request, context);
return this.upsertResponse(undefined, response, request, context);
}

async upsertResponse(response: Partial<IOhMyResponse>, request: Partial<IOhMyRequest>, context: IOhMyContext): Promise<IOhMyResponse> {
const retVal = await OhMySendToBg.full<IOhMyResponseUpdate, IOhMyResponse>({
request,
response
async upsertResponse(responseId: IOhMyResponseId, update: IOhMyResponseUpdate, request: Partial<IOhMyRequest> | IOhMyRequestId, context: IOhMyContext): Promise<IOhMyResponse> {
const retVal = await OhMySendToBg.full<IOhMyResponseUpsert, IOhMyResponse>({
response: update,
responseId: responseId,
request
}, payloadType.RESPONSE, context, 'popup;upsertResponse');

return retVal;
@@ -201,10 +216,12 @@ export class OhMyState {
}

async deleteResponse(responseId: IOhMyResponseId, requestId: IOhMyRequestId, context: IOhMyDomainContext): Promise<IOhMyDomain> {
const state = await OhMySendToBg.full<IOhMyResponseUpdate, IOhMyDomain>({
response: { id: responseId },
request: { id: requestId }
}, payloadType.RESPONSE, context, 'popup;deleteResponse');
const data: IOhMyResponseUpsert = {
responseId, request: { id: requestId }
};

const state = await OhMySendToBg.full<IOhMyResponseUpsert, IOhMyDomain>(
data, payloadType.RESPONSE, context, 'popup;deleteResponse');

return state;
}
4 changes: 2 additions & 2 deletions src/background/handlers/handler.ts
Original file line number Diff line number Diff line change
@@ -6,10 +6,10 @@ import { StorageUtils } from "../../shared/utils/storage";
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IOhMyHandler { }

export interface IOhMyHandlerConstructor<T = unknown, K = IOhMyMock | IOhMyResponse | IOhMyDomain> {
export interface IOhMyHandlerConstructor<T = unknown, K = IOhMyMock | IOhMyResponse | IOhMyDomain, X = IOhMyContext> {
new(): IOhMyHandler;
StorageUtils: StorageUtils;
update: (input: IPacketPayload<T, IOhMyContext>) => Promise<K | void>;
update: (input: IPacketPayload<T, X>) => Promise<K | void>;
queue?: OhMyQueue;
}

50 changes: 26 additions & 24 deletions src/background/handlers/response-upsert.handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IPacketPayload } from "../../shared/packet-type";
import { IOhMyResponseUpsert, IOhMyRequest, IOhMyResponse, IOhMyDomain, IOhMyContext } from "../../shared/types";
import { IOhMyResponseUpsert, IOhMyRequest, IOhMyResponse, IOhMyDomain, IOhMyContext, IOhMyRequestId, IOhMyDomainContext, IOhMyPropertyContext } from "../../shared/types";
import { MockUtils } from "../../shared/utils/mock";
import { update } from "../../shared/utils/partial-updater";
import { OhMyQueue } from "../../shared/utils/queue";
@@ -12,39 +12,41 @@ import { IOhMyHandlerConstructor, staticImplements } from "./handler";
import { uniqueId } from "../../shared/utils/unique-id";
// import { shallowSearch, splitIntoSearchTerms } from "../../shared/utils/search";

@staticImplements<IOhMyHandlerConstructor<IOhMyResponseUpsert, IOhMyResponse>>()
@staticImplements<IOhMyHandlerConstructor<IOhMyResponseUpsert, IOhMyResponse, IOhMyPropertyContext>>()
export class OhMyResponseHandler {
static StorageUtils = StorageUtils;
static queue: OhMyQueue;

static async update({ data, context }: IPacketPayload<IOhMyResponseUpsert, IOhMyContext>): Promise<IOhMyResponse | void> {
static async update({ data, context }: IPacketPayload<IOhMyResponseUpsert, IOhMyPropertyContext>): Promise<IOhMyResponse | void> {
try {
const responseId = data.responseId;
const requestId = data.requestId || uniqueId();
let response: IOhMyResponse;

const state = await OhMyResponseHandler.StorageUtils.get<IOhMyDomain>(context.key);
let request = OhMyResponseHandler.StorageUtils.get<IOhMyRequest>(data.requestId) ||
DataUtils.init({ id: requestId });
const requestId = (data.request as Partial<IOhMyRequest>).id || data.request as IOhMyRequestId;

if (context!.path) { // new/update
response = await OhMyResponseHandler.StorageUtils.get<IOhMyResponse>(response.id);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
response = update<IOhMyResponse>(context!.path, response as IOhMyResponse, context!.propertyName!, data.response[context!.propertyName!]);
response.modifiedOn = timestamp();
} else { // new, update or delete
const base = response.id ? await OhMyResponseHandler.StorageUtils.get<IOhMyResponse>(response.id) : undefined;
response = MockUtils.init(base, response);
let request = await OhMyResponseHandler.StorageUtils.get<IOhMyRequest>(requestId) || DataUtils.init({ id: requestId });

if (base) {
response.modifiedOn = timestamp();
}
}
request = DataUtils.addResponse(state.context, request, response, autoActivate);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (context!.path) { // new/update.
response = await OhMyResponseHandler.StorageUtils.get<IOhMyResponse>(
(update as Partial<IOhMyResponse>)?.id);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
response = update<IOhMyResponse>(context!.path, response as IOhMyResponse, context!.propertyName!, data.response![context!.propertyName!]);
response.modifiedOn = timestamp();
} else { // new, update or delete
const base = data.responseId ? await OhMyResponseHandler.StorageUtils.get<IOhMyResponse>(data.responseId) : undefined;
response = MockUtils.init(base, data.response as Partial<IOhMyResponse>);

// if (state.aux.filterKeywords) { // Update filter results
// const words = splitIntoSearchTerms(state.aux.filterKeywords);
// const out = shallowSearch({ [request.id]: request }, words, state.aux.filterOptions);
// }
if (base) {
response.modifiedOn = timestamp();
}
}
request = DataUtils.addResponse(state.context, request, response, false /*autoActivate*/);

// if (state.aux.filterKeywords) { // Update filter results
// const words = splitIntoSearchTerms(state.aux.filterKeywords);
// const out = shallowSearch({ [request.id]: request }, words, state.aux.filterOptions);
// }

OhMyResponseHandler.updateFiltering(state, request, response as IOhMyResponse);

6 changes: 3 additions & 3 deletions src/background/server-dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { connectWithLocalServer, dispatchRemote } from "./dispatch-remote";
import { appSources, OhMyResponseStatus, payloadType } from "../shared/constants";
import { IOhMessage } from "../shared/packet-type";
import { IOhMyAPIRequest, IOhMyUpsertRequest, IOhMyDomain, IOhMyMockResponse, IOhMyRequest, IOhMyDomainContext } from "../shared/types";
import { IOhMyAPIRequest, IOhMyDomain, IOhMyMockResponse, IOhMyRequest, IOhMyDomainContext, IOhMyRequestUpsert } from "../shared/types";
import { OhMyMessageBus } from "../shared/utils/message-bus";
import { StateUtils } from "../shared/utils/state";
import { StorageUtils } from "../shared/utils/storage";
@@ -11,7 +11,7 @@ import { DataUtils } from "../shared/utils/data";
const mb = new OhMyMessageBus().setTrigger(triggerRuntime);
mb.streamByType$<IOhMyAPIRequest>(payloadType.DISPATCH_TO_SERVER, appSources.CONTENT)
.subscribe(async ({ packet, callback }: IOhMessage<IOhMyAPIRequest>) => {
const request = { ...packet.payload.data } as IOhMyUpsertRequest;
const request = { ...packet.payload.data } as IOhMyAPIRequest;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const result = await dispatch2Server(request, (packet.payload.context as IOhMyDomainContext).key);

@@ -22,7 +22,7 @@ connectWithLocalServer();

// -- ******************************

export async function dispatch2Server(request: IOhMyUpsertRequest, domain: string): Promise<IOhMyMockResponse> {
export async function dispatch2Server(request: IOhMyAPIRequest, domain: string): Promise<IOhMyMockResponse> {
const state = await StorageUtils.get<IOhMyDomain>(domain);
let data; // = request;
let mock;
14 changes: 8 additions & 6 deletions src/injected/fetch/persist-response.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { STORAGE_KEY } from '../../shared/constants';
import { IOhMyResponseUpdate } from '../../shared/packet-type';
import { IOhMyAPIRequest } from '../../shared/types';
import { IOhMyAPIRequest, IOhMyResponseUpdate, IOhMyResponseUpsert } from '../../shared/types';
import { convertToB64 } from '../../shared/utils/binary';
import { extractMimeType, isMimeTypeText } from '../../shared/utils/mime-type';
import { dispatchApiResponse } from '../message/dispatch-api-response';
import { removeDomainFromUrl } from '../utils';

export async function persistResponse(response: Response, request: IOhMyAPIRequest): Promise<IOhMyResponseUpdate | undefined> {
export async function persistResponse(response: Response, request: IOhMyAPIRequest): Promise<IOhMyResponseUpsert | undefined> {
if (!window[STORAGE_KEY].state?.active) {
return undefined;
}
@@ -32,9 +31,12 @@ export async function persistResponse(response: Response, request: IOhMyAPIReque
}

const ohResult = {
request: { url: removeDomainFromUrl(response['ohUrl'] || response.url), requestMethod: response['ohMethod'] || request?.requestMethod, requestType: 'FETCH' },
response: { statusCode: clone['__status'], response: output, headers }
} as IOhMyResponseUpdate;
request: {
url: removeDomainFromUrl(response['ohUrl'] || response.url),
requestMethod: response['ohMethod'] || request?.requestMethod,
},
update: { statusCode: clone['__status'], response: output, headers }
} as IOhMyResponseUpsert;

dispatchApiResponse(ohResult);
response['ohResult'] = ohResult;
6 changes: 2 additions & 4 deletions src/injected/message/dispatch-api-response.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { payloadType, STORAGE_KEY } from '../../shared/constants';
import { IOhMyResponseUpdate } from '../../shared/packet-type';
import { IOhMyDomainContext } from '../../shared/types';
import { log } from '../utils';
import { IOhMyDomainContext, IOhMyResponseUpsert } from '../../shared/types';
import { send } from './send';

export function dispatchApiResponse(payload: IOhMyResponseUpdate) {
export function dispatchApiResponse(payload: IOhMyResponseUpsert) {
window[STORAGE_KEY].cache.unshift(payload);

send({
7 changes: 3 additions & 4 deletions src/injected/xhr/persist-response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { STORAGE_KEY } from '../../shared/constants';
import { IOhMyResponseUpdate } from '../../shared/packet-type';
import { IOhMyAPIRequest } from '../../shared/types';
import { IOhMyAPIRequest, IOhMyResponseUpdate, IOhMyResponseUpsert } from '../../shared/types';
import { convertToB64 } from '../../shared/utils/binary';
import { parse } from '../../shared/utils/xhr-headers';
import { dispatchApiResponse } from '../message/dispatch-api-response';
@@ -35,6 +34,6 @@ export async function persistResponse(xhr: XMLHttpRequest, request: IOhMyAPIRequ
method: xhr['ohMethod'] || request?.requestMethod,
requestType: 'XHR'
},
response: { statusCode: xhr['__status'], response: output, headers },
} as IOhMyResponseUpdate);
update: { statusCode: xhr['__status'], response: output, headers },
} as IOhMyResponseUpsert);
}
4 changes: 2 additions & 2 deletions src/shared/packet-type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { appSources, payloadType } from './constants';
import { IOhMyContext, IOhMyAPIRequest, IOhMyMockResponse, IOhMyDomainId, IOhMyRequest, IOhMyResponse, IOhMyUpsertRequest, IOhMyStatusCode, IOhMyDomainContext, IOhMyRequestId, IOhMyResponseId } from './types';
import { IOhMyContext, IOhMyAPIRequest, IOhMyMockResponse, IOhMyRequest, IOhMyStatusCode } from './types';
import { ImportResultEnum } from './utils/import-json';

export type ohMessage = <T = unknown>(message: IOhMessage) => void;
@@ -36,7 +36,7 @@ export interface IOhMyReadyResponse<T = string> {
}

export interface IOhMyDispatchServerRequest {
request: IOhMyRequest | IOhMyUpsertRequest,
request: IOhMyRequest,
context: IOhMyContext,
mock?: {
response: unknown,
1 change: 1 addition & 0 deletions src/shared/types/index.ts
Original file line number Diff line number Diff line change
@@ -8,5 +8,6 @@ export * from './preset';
export * from './request';
export * from './response';
export * from './context';
export * from './modifiers';

export type ResetStateOptions = resetStateOptions; // ??
Loading

0 comments on commit c85353c

Please sign in to comment.