Skip to content

Commit ef97853

Browse files
authored
Merge pull request #13 from thien-magenest/refactor/experimental-decorator
refactor: add `Frozen` decorator
2 parents 71c0187 + 45f5684 commit ef97853

File tree

9 files changed

+33
-6
lines changed

9 files changed

+33
-6
lines changed

babel.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
22
presets: [['module:react-native-builder-bob/babel-preset', { modules: 'commonjs' }]],
3+
plugins: [['@babel/plugin-proposal-decorators', { version: 'legacy' }]],
34
};

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"registry": "https://registry.npmjs.org/"
7878
},
7979
"devDependencies": {
80+
"@babel/plugin-proposal-decorators": "^7.25.9",
8081
"@commitlint/config-conventional": "^17.0.2",
8182
"@evilmartians/lefthook": "^1.5.0",
8283
"@react-native/eslint-config": "^0.73.1",
@@ -188,20 +189,23 @@
188189
[
189190
"commonjs",
190191
{
191-
"esm": true
192+
"esm": true,
193+
"configFile": true
192194
}
193195
],
194196
[
195197
"module",
196198
{
197-
"esm": true
199+
"esm": true,
200+
"configFile": true
198201
}
199202
],
200203
[
201204
"typescript",
202205
{
203206
"project": "tsconfig.build.json",
204-
"esm": true
207+
"esm": true,
208+
"configFile": true
205209
}
206210
]
207211
]

src/interceptors/ConsoleInterceptor.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable no-console */
22
import type { ConsoleHandlers } from '../types';
3+
import { Frozen } from '../utils';
34
import Interceptor from './Interceptor';
45

56
const originalConsoleError = console.error;
@@ -24,6 +25,7 @@ export default class ConsoleInterceptor extends Interceptor<ConsoleHandlers> {
2425
super();
2526
}
2627

28+
@Frozen()
2729
enableInterception(): void {
2830
if (this.isInterceptorEnabled) return;
2931

@@ -92,6 +94,7 @@ export default class ConsoleInterceptor extends Interceptor<ConsoleHandlers> {
9294
this.isInterceptorEnabled = true;
9395
}
9496

97+
@Frozen()
9598
disableInterception(): void {
9699
if (!this.isInterceptorEnabled) return;
97100

src/interceptors/FetchInterceptor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NETWORK_REQUEST_HEADER } from '../constants';
22
import { NetworkType } from '../types';
3-
import { formatRequestMethod, getHttpInterceptorId, keyValueToString } from '../utils';
3+
import { formatRequestMethod, Frozen, getHttpInterceptorId, keyValueToString } from '../utils';
44
import HttpInterceptor from './HttpInterceptor';
55

66
const originalFetch = global.fetch;
@@ -12,6 +12,7 @@ export default class FetchInterceptor extends HttpInterceptor {
1212
super();
1313
}
1414

15+
@Frozen()
1516
enableInterception() {
1617
if (this.isInterceptorEnabled) return;
1718

@@ -123,6 +124,7 @@ export default class FetchInterceptor extends HttpInterceptor {
123124
this.isInterceptorEnabled = true;
124125
}
125126

127+
@Frozen()
126128
disableInterception() {
127129
if (!this.isInterceptorEnabled) return;
128130

src/interceptors/Interceptor.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Frozen } from '../utils';
2+
13
export default abstract class Interceptor<T extends Object> {
24
#isInterceptorEnabled = false;
35

@@ -11,6 +13,7 @@ export default abstract class Interceptor<T extends Object> {
1113

1214
protected abstract handlers: T;
1315

16+
@Frozen()
1417
set<K extends keyof T>(key: K, handler: T[K]) {
1518
this.handlers[key] = handler;
1619
return this;

src/interceptors/WebSocketInterceptor.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { NativeEventEmitter, type EmitterSubscription } from 'react-native';
22
import NativeWebSocketModule from 'react-native/Libraries/WebSocket/NativeWebSocketModule';
33
import type { WebSocketHandlers } from '../types';
44
import { NetworkInterceptor } from './NetworkInterceptor';
5+
import { Frozen } from '../utils';
56

67
const originalWebSocketConnect = NativeWebSocketModule.connect;
78
const originalWebSocketSend = NativeWebSocketModule.send;
@@ -95,6 +96,7 @@ export default class WebSocketInterceptor extends NetworkInterceptor<WebSocketHa
9596
this.eventEmitter = null;
9697
}
9798

99+
@Frozen()
98100
enableInterception(): void {
99101
if (this.isInterceptorEnabled) return;
100102

@@ -134,6 +136,7 @@ export default class WebSocketInterceptor extends NetworkInterceptor<WebSocketHa
134136
this.isInterceptorEnabled = true;
135137
}
136138

139+
@Frozen()
137140
disableInterception(): void {
138141
if (!this.isInterceptorEnabled) return;
139142

src/interceptors/XHRInterceptor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NetworkType } from '../types';
2-
import { getHttpInterceptorId } from '../utils';
2+
import { Frozen, getHttpInterceptorId } from '../utils';
33
import HttpInterceptor from './HttpInterceptor';
44

55
const originalXHROpen = XMLHttpRequest.prototype.open;
@@ -13,6 +13,7 @@ export default class XHRInterceptor extends HttpInterceptor {
1313
super();
1414
}
1515

16+
@Frozen()
1617
enableInterception() {
1718
if (this.isInterceptorEnabled) return;
1819

@@ -88,6 +89,7 @@ export default class XHRInterceptor extends HttpInterceptor {
8889
this.isInterceptorEnabled = true;
8990
}
9091

92+
@Frozen()
9193
disableInterception() {
9294
if (!this.isInterceptorEnabled) return;
9395

src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,11 @@ export const convertToCurl = (
5555

5656
return curlCommand;
5757
};
58+
59+
export function Frozen() {
60+
return function (_target: Object) {
61+
const [_, __, descriptor] = arguments as unknown as [Object, string, PropertyDescriptor];
62+
descriptor.configurable = false;
63+
descriptor.writable = false;
64+
};
65+
}

yarn.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ __metadata:
412412
languageName: node
413413
linkType: hard
414414

415-
"@babel/plugin-proposal-decorators@npm:^7.12.9":
415+
"@babel/plugin-proposal-decorators@npm:^7.12.9, @babel/plugin-proposal-decorators@npm:^7.25.9":
416416
version: 7.25.9
417417
resolution: "@babel/plugin-proposal-decorators@npm:7.25.9"
418418
dependencies:
@@ -11734,6 +11734,7 @@ __metadata:
1173411734
version: 0.0.0-use.local
1173511735
resolution: "react-native-xenon@workspace:."
1173611736
dependencies:
11737+
"@babel/plugin-proposal-decorators": "npm:^7.25.9"
1173711738
"@commitlint/config-conventional": "npm:^17.0.2"
1173811739
"@evilmartians/lefthook": "npm:^1.5.0"
1173911740
"@react-native/eslint-config": "npm:^0.73.1"

0 commit comments

Comments
 (0)