Skip to content

Commit

Permalink
fix uniapp auto promisify api HuolalaTech/page-spy-web#306
Browse files Browse the repository at this point in the history
  • Loading branch information
qkang07 committed Jan 22, 2025
1 parent 7d12a13 commit b8b9624
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 23 deletions.
18 changes: 13 additions & 5 deletions packages/page-spy-base/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,14 @@ interface PrimitiveResult {
value: any;
}

const stringify = (value: any) => `${value}`;
const stringify = (value: any) => {
// symbol in string template will throw error
// just defensive code
if (typeof value === 'symbol') {
return value.toString();
}
return `${value}`;
};
const primitive = (value: any) => ({
ok: true,
value,
Expand All @@ -158,16 +165,17 @@ export function makePrimitiveValue(value: unknown): PrimitiveResult {
if (value === null) {
return primitive(value);
}
// check symbol first
if (typeof value === 'symbol' || typeof value === 'function') {
return primitive(stringify(value.toString()));
}
if (isNumber(value)) {
if (value === -Infinity || value === Infinity || Number.isNaN(value)) {
return primitive(stringify(value));
}
}
if (isBigInt(value)) {
return primitive(`${value}n`);
}
if (typeof value === 'symbol' || typeof value === 'function') {
return primitive(stringify(value.toString()));
return primitive(stringify(value) + 'n');
}
if (value instanceof Error) {
return primitive(stringify(value.stack!));
Expand Down
9 changes: 9 additions & 0 deletions packages/page-spy-mp-base/src/helpers/mp-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ export const getMPSDK = () => {

export const getOriginMPSDK = () => originMPSDK as MPSDK;

// Some frameworks(uniapp) make the mp sdk wrapped by Proxy...
// this must be handled by different method.
// If the mp sdk is a Proxy, it will not have own property, so we check the object keys of
// origin sdk. The magic number here is because we may add some property to the sdk, so the
// length will not be 0.
export const isMPProxy = () => {
return Object.keys(originMPSDK).length < 20;
};

export const setMPSDK = (SDK: MPSDK) => {
originMPSDK = SDK;
if (typeof Proxy === 'undefined') {
Expand Down
4 changes: 2 additions & 2 deletions packages/page-spy-mp-base/src/plugins/network/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export default class NetworkPlugin implements PageSpyPlugin {

public static hasInitd = false;

public onInit({ config }: OnInitParams<SpyMP.MPInitConfig>) {
public onInit({ config, client }: OnInitParams<SpyMP.MPInitConfig>) {
if (NetworkPlugin.hasInitd) return;
NetworkPlugin.hasInitd = true;
NetworkProxyBase.dataProcessor = config.dataProcessor.network;

this.requestProxy = new RequestProxy();
this.requestProxy = new RequestProxy({ client });
}

public onReset() {
Expand Down
52 changes: 39 additions & 13 deletions packages/page-spy-mp-base/src/plugins/network/proxy/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,34 @@ import {
} from '@huolala-tech/page-spy-base/dist/network/common';
import MPNetworkProxyBase from './base';
import { MPNetworkAPI } from '../../../types';
import { getOriginMPSDK } from '../../../helpers/mp-api';
import { getOriginMPSDK, isMPProxy } from '../../../helpers/mp-api';
import type { Client } from '@huolala-tech/page-spy-base';

export default class MPWeixinRequestProxy extends MPNetworkProxyBase {
public request: MPNetworkAPI['request'] | null = null;

constructor() {
client: Client;
constructor({ client }: { client: Client }) {
super();
this.client = client;
this.initProxyHandler();
}

public reset() {
if (this.request) {
const mp = getOriginMPSDK();
Object.defineProperty(mp, 'request', {
value: this.request,
configurable: true,
writable: true,
enumerable: true,
});
// if the origin mp sdk is a proxy, just delete the property.
if (isMPProxy()) {
delete (mp as any).request;
} else {
Object.defineProperty(mp, 'request', {
value: this.request,
configurable: true,
writable: true,
enumerable: true,
});
}
this.request = null;
}
}

Expand Down Expand Up @@ -98,7 +107,9 @@ export default class MPWeixinRequestProxy extends MPNetworkProxyBase {
req.costTime = req.endTime - (req.startTime || req.endTime);
};

params.success = function (res) {
type SuccessRes = Parameters<Required<typeof params>['success']>[0];
type FailRes = Parameters<Required<typeof params>['fail']>[0];
const successHandler = (res: SuccessRes) => {
commonEnd();
req.status = res?.statusCode || 200;
req.statusText = 'Done';
Expand Down Expand Up @@ -148,17 +159,32 @@ export default class MPWeixinRequestProxy extends MPNetworkProxyBase {
}
originOnSuccess?.(res);
};

params.fail = function (err) {
const failHandler = (err: FailRes) => {
commonEnd();
originOnFailed?.(err);
};

params.complete = function (res: any) {
const completeHandler = (res: any) => {
req.readyState = ReqReadyState.DONE;
that.sendRequestItem(id, req);
originOnComplete?.(res);
};
// In uniapp, if no success / fail / complete passed in, the return value will be
// a promise, has to handle this logic...
if (
that.client.info.sdk === 'uniapp' &&
!params.success &&
!params.fail &&
!params.complete
) {
const resPromise = originRequest(params);
resPromise
.then(successHandler, failHandler)
.finally(completeHandler);
return resPromise;
}
params.success = successHandler;
params.fail = failHandler;
params.complete = completeHandler;

const requestInstance = originRequest(params);
return requestInstance;
Expand Down
31 changes: 28 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12054,7 +12054,16 @@ string-length@^4.0.1:
char-regex "^1.0.2"
strip-ansi "^6.0.0"

"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -12123,7 +12132,7 @@ string_decoder@~1.1.1:
dependencies:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand All @@ -12137,6 +12146,13 @@ strip-ansi@^5.0.0, strip-ansi@^5.2.0:
dependencies:
ansi-regex "^4.1.0"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^7.0.1, strip-ansi@^7.1.0:
version "7.1.0"
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
Expand Down Expand Up @@ -13027,7 +13043,7 @@ wordwrap@^1.0.0:
resolved "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
integrity sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
version "7.0.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand All @@ -13045,6 +13061,15 @@ wrap-ansi@^6.0.1, wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
Expand Down

0 comments on commit b8b9624

Please sign in to comment.