Skip to content

Commit

Permalink
feat: reorganize the network plugin structure (#3)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The headers and getData structure have been changed to [string, string][]. The use of postData is now deprecated. Please use requestPayload instead, with a structure of [string, string][] | string.
  • Loading branch information
wqcstrong committed May 31, 2023
1 parent 2e26cd1 commit aa199fb
Show file tree
Hide file tree
Showing 39 changed files with 1,782 additions and 1,487 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coveralls.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: yarn install, yarn test
run: |
yarn install
yarn test
yarn test --silent
- name: Coveralls
uses: coverallsapp/github-action@v2
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
"build": "rollup -c",
"postbuild": "cp -R ./types ./dist",
"build:watch": "rollup -c -w",
"test": "jest --silent",
"test": "jest",
"lint": "eslint --ext .js,.ts ./src",
"lint:fix": "eslint --ext .js,.ts --fix ./src",
"prettier": "prettier --write '**/*.{js,jsx,tsx,ts,md,json}'",
"commit": "git-cz",
"prepublishOnly": "yarn test && yarn build",
"prepublishOnly": "yarn test --silent && yarn build",
"release:major": "standard-version --release-as major",
"release:minor": "standard-version --release-as minor",
"release:patch": "standard-version --release-as patch"
Expand Down
8 changes: 4 additions & 4 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { psLog } from 'src/utils';
import { combineName, parseUserAgent } from 'src/utils/ua';

interface TResponse<T> {
Expand All @@ -14,7 +15,7 @@ interface TCreateRoom {
group: string;
tags: Record<string, any>;
}
/* c8 ignore start */

const resolvedProtocol = (() => {
try {
const { protocol } = new URL(document.currentScript?.getAttribute('src')!);
Expand All @@ -23,14 +24,13 @@ const resolvedProtocol = (() => {
}
} catch (e) {
console.error(e);
console.error(
'[PageSpy] Failed to resolve the protocol and fallback to [http://, ws://]',
psLog.error(
'Failed to resolve the protocol and fallback to [http://, ws://]',
);
}
return ['http://', 'ws://'];
})();

/* c8 ignore stop */
export default class Request {
constructor(public base: string = '') {
/* c8 ignore next 3 */
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { StoragePlugin } from './plugins/storage';

import socketStore from './utils/socket';
import Request from './api';
import { getRandomId } from './utils';
import { getRandomId, psLog } from './utils';
import pkg from '../package.json';

import type { UElement } from './utils/moveable';
Expand Down Expand Up @@ -229,7 +229,7 @@ export default class PageSpy {
moveable(logo);
this.handleDeviceDPR();

console.log('[PageSpy] init success.');
psLog.log('Init success.');
}

handleDeviceDPR() {
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import atom from 'src/utils/atom';
import type PageSpyPlugin from './index';

export default class ConsolePlugin implements PageSpyPlugin {
name: string = 'ConsolePlugin';
public name: string = 'ConsolePlugin';

console: Record<string, any> = {};
private console: Record<string, any> = {};

onCreated() {
public onCreated() {
const type: SpyConsole.ProxyType[] = ['log', 'info', 'error', 'warn'];
type.forEach((item) => {
this.console[item] = window.console[item];
Expand All @@ -23,7 +23,7 @@ export default class ConsolePlugin implements PageSpyPlugin {
});
}

printLog(data: SpyConsole.DataItem) {
private printLog(data: SpyConsole.DataItem) {
if (data.logs && data.logs.length) {
this.console[data.logType](...data.logs);
// eslint-disable-next-line no-param-reassign
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import socketStore from 'src/utils/socket';
import type PageSpyPlugin from './index';

export default class ErrorPlugin implements PageSpyPlugin {
name = 'ErrorPlugin';
public name = 'ErrorPlugin';

error: OnErrorEventHandler = null;
private error: OnErrorEventHandler = null;

onCreated() {
public onCreated() {
const that = this;
this.error = window.onerror;

Expand All @@ -35,7 +35,7 @@ export default class ErrorPlugin implements PageSpyPlugin {
true,
);

// Promise unhandleRejection Error
// Promise unhandledRejection Error
window.addEventListener(
'unhandledrejection',
(evt: PromiseRejectionEvent) => {
Expand All @@ -44,7 +44,7 @@ export default class ErrorPlugin implements PageSpyPlugin {
);
}

static sendMessage(data: any) {
public static sendMessage(data: any) {
// Treat `error` data as `console`
const message = makeMessage(DEBUG_MESSAGE_TYPE.CONSOLE, {
logType: 'error',
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default abstract class PageSpyPlugin {
constructor(public name: string) {}
public constructor(public name: string) {}
// 加载后立即生效
abstract onCreated?(): void;
public abstract onCreated?(): void;
// 用户主动触发的回调
abstract onLoaded?(): void;
public abstract onLoaded?(): void;
}
Loading

0 comments on commit aa199fb

Please sign in to comment.