dreaction-react-native 1.8.4 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import type { DReactionCore } from 'dreaction-client-core';
1
+ import { type DReactionCore } from 'dreaction-client-core';
2
2
  export interface NetworkingOptions {
3
3
  ignoreContentTypes?: RegExp;
4
4
  ignoreUrls?: RegExp;
@@ -1 +1 @@
1
- {"version":3,"file":"networking.d.ts","sourceRoot":"","sources":["../../src/plugins/networking.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAU,MAAM,uBAAuB,CAAC;AAOnE,MAAM,WAAW,iBAAiB;IAChC,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAMD,QAAA,MAAM,UAAU,kBACC,iBAAiB,iBACpB,aAAa;;CAwJxB,CAAC;AACJ,eAAe,UAAU,CAAC"}
1
+ {"version":3,"file":"networking.d.ts","sourceRoot":"","sources":["../../src/plugins/networking.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,aAAa,EAGnB,MAAM,uBAAuB,CAAC;AAO/B,MAAM,WAAW,iBAAiB;IAChC,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAMD,QAAA,MAAM,UAAU,kBACC,iBAAiB,iBACpB,aAAa;;CA+JxB,CAAC;AACJ,eAAe,UAAU,CAAC"}
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  // @ts-ignore
4
4
  const xhrInterceptor_1 = require("./xhrInterceptor");
5
+ const dreaction_client_core_1 = require("dreaction-client-core");
5
6
  /**
6
7
  * Don't include the response bodies for images by default.
7
8
  */
@@ -13,8 +14,6 @@ const networking = (pluginConfig = {}) => (dreaction) => {
13
14
  const options = Object.assign({}, DEFAULTS, pluginConfig);
14
15
  // a RegExp to suppress adding the body cuz it costs a lot to serialize
15
16
  const ignoreContentTypes = options.ignoreContentTypes || DEFAULT_CONTENT_TYPES_RX;
16
- // a XHR call tracker
17
- let dreactionCounter = 1000;
18
17
  // a temporary cache to hold requests so we can match up the data
19
18
  const requestCache = {};
20
19
  /**
@@ -28,16 +27,42 @@ const networking = (pluginConfig = {}) => (dreaction) => {
28
27
  xhr._skipDReaction = true;
29
28
  return;
30
29
  }
31
- // bump the counter
32
- dreactionCounter++;
30
+ // Generate unique request ID
31
+ const requestId = (0, dreaction_client_core_1.generateRequestId)();
33
32
  // tag
34
- xhr._trackingName = dreactionCounter;
33
+ xhr._trackingName = requestId;
34
+ // Parse URL and params
35
+ const url = xhr._url;
36
+ let params = null;
37
+ const queryParamIdx = url ? url.indexOf('?') : -1;
38
+ if (queryParamIdx > -1) {
39
+ params = {};
40
+ url
41
+ .substr(queryParamIdx + 1)
42
+ .split('&')
43
+ .forEach((pair) => {
44
+ const [key, value] = pair.split('=');
45
+ if (key && value !== undefined) {
46
+ params[key] = decodeURIComponent(value.replace(/\+/g, ' '));
47
+ }
48
+ });
49
+ }
50
+ const tronRequest = {
51
+ url: url || '',
52
+ method: xhr._method || 'GET',
53
+ data,
54
+ headers: xhr._headers || {},
55
+ params,
56
+ };
35
57
  // cache
36
- requestCache[dreactionCounter] = {
58
+ requestCache[requestId] = {
37
59
  data,
38
60
  xhr,
61
+ request: tronRequest,
39
62
  stopTimer: dreaction.startTimer(),
40
63
  };
64
+ // Send api.request event
65
+ dreaction.apiRequest(requestId, tronRequest);
41
66
  }
42
67
  /**
43
68
  * Fires when the server gives us a response.
@@ -53,33 +78,14 @@ const networking = (pluginConfig = {}) => (dreaction) => {
53
78
  if (xhr._skipDReaction) {
54
79
  return;
55
80
  }
56
- let params = null;
57
- const queryParamIdx = url ? url.indexOf('?') : -1;
58
- if (queryParamIdx > -1) {
59
- params = {};
60
- url
61
- .substr(queryParamIdx + 1)
62
- .split('&')
63
- .forEach((pair) => {
64
- const [key, value] = pair.split('=');
65
- if (key && value !== undefined) {
66
- params[key] = decodeURIComponent(value.replace(/\+/g, ' '));
67
- }
68
- });
69
- }
70
81
  // fetch and clear the request data from the cache
71
- const rid = xhr._trackingName;
72
- const cachedRequest = requestCache[rid] || { xhr };
73
- requestCache[rid] = null;
74
- // assemble the request object
75
- const { data, stopTimer } = cachedRequest;
76
- const tronRequest = {
77
- url: url || cachedRequest.xhr._url,
78
- method: xhr._method || null,
79
- data,
80
- headers: xhr._headers || {},
81
- params,
82
- };
82
+ const requestId = xhr._trackingName;
83
+ const cachedRequest = requestCache[requestId];
84
+ if (!cachedRequest) {
85
+ return;
86
+ }
87
+ delete requestCache[requestId];
88
+ const { request: tronRequest, stopTimer } = cachedRequest;
83
89
  // what type of content is this?
84
90
  const contentType = (xhr.responseHeaders && xhr.responseHeaders['content-type']) ||
85
91
  (xhr.responseHeaders && xhr.responseHeaders['Content-Type']) ||
@@ -98,9 +104,9 @@ const networking = (pluginConfig = {}) => (dreaction) => {
98
104
  const tronResponse = {
99
105
  body,
100
106
  status,
101
- headers: xhr.responseHeaders || null,
107
+ headers: xhr.responseHeaders || {},
102
108
  };
103
- dreaction.apiResponse(tronRequest, tronResponse, stopTimer ? stopTimer() : null); // TODO: Fix
109
+ dreaction.apiResponse(requestId, tronRequest, tronResponse, stopTimer ? stopTimer() : 0);
104
110
  };
105
111
  // can we use the real response?
106
112
  const useRealResponse = (typeof response === 'string' || typeof response === 'object') &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dreaction-react-native",
3
- "version": "1.8.4",
3
+ "version": "1.9.0",
4
4
  "private": false,
5
5
  "description": "DReaction client for React Native applications with draggable debug ball and powerful debugging tools",
6
6
  "main": "lib/index.js",
@@ -32,8 +32,8 @@
32
32
  "homepage": "https://github.com/moonrailgun/dreaction#readme",
33
33
  "dependencies": {
34
34
  "mitt": "^3.0.1",
35
- "dreaction-client-core": "1.3.0",
36
- "dreaction-protocol": "1.0.10"
35
+ "dreaction-client-core": "1.4.0",
36
+ "dreaction-protocol": "1.0.11"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@react-native-async-storage/async-storage": "^2.1.0",
@@ -1,6 +1,10 @@
1
1
  // @ts-ignore
2
2
  import { XHRInterceptor } from './xhrInterceptor';
3
- import type { DReactionCore, Plugin } from 'dreaction-client-core';
3
+ import {
4
+ type DReactionCore,
5
+ type Plugin,
6
+ generateRequestId,
7
+ } from 'dreaction-client-core';
4
8
 
5
9
  /**
6
10
  * Don't include the response bodies for images by default.
@@ -25,11 +29,8 @@ const networking =
25
29
  const ignoreContentTypes =
26
30
  options.ignoreContentTypes || DEFAULT_CONTENT_TYPES_RX;
27
31
 
28
- // a XHR call tracker
29
- let dreactionCounter = 1000;
30
-
31
32
  // a temporary cache to hold requests so we can match up the data
32
- const requestCache = {};
33
+ const requestCache: Record<string, any> = {};
33
34
 
34
35
  /**
35
36
  * Fires when we talk to the server.
@@ -43,18 +44,47 @@ const networking =
43
44
  return;
44
45
  }
45
46
 
46
- // bump the counter
47
- dreactionCounter++;
47
+ // Generate unique request ID
48
+ const requestId = generateRequestId();
48
49
 
49
50
  // tag
50
- xhr._trackingName = dreactionCounter;
51
+ xhr._trackingName = requestId;
52
+
53
+ // Parse URL and params
54
+ const url = xhr._url;
55
+ let params: Record<string, string> | null = null;
56
+ const queryParamIdx = url ? url.indexOf('?') : -1;
57
+ if (queryParamIdx > -1) {
58
+ params = {};
59
+ url
60
+ .substr(queryParamIdx + 1)
61
+ .split('&')
62
+ .forEach((pair: string) => {
63
+ const [key, value] = pair.split('=');
64
+ if (key && value !== undefined) {
65
+ params![key] = decodeURIComponent(value.replace(/\+/g, ' '));
66
+ }
67
+ });
68
+ }
69
+
70
+ const tronRequest = {
71
+ url: url || '',
72
+ method: xhr._method || 'GET',
73
+ data,
74
+ headers: xhr._headers || {},
75
+ params,
76
+ };
51
77
 
52
78
  // cache
53
- (requestCache as any)[dreactionCounter] = {
79
+ requestCache[requestId] = {
54
80
  data,
55
81
  xhr,
82
+ request: tronRequest,
56
83
  stopTimer: dreaction.startTimer(),
57
84
  };
85
+
86
+ // Send api.request event
87
+ (dreaction as any).apiRequest(requestId, tronRequest);
58
88
  }
59
89
 
60
90
  /**
@@ -79,35 +109,15 @@ const networking =
79
109
  return;
80
110
  }
81
111
 
82
- let params = null;
83
- const queryParamIdx = url ? url.indexOf('?') : -1;
84
- if (queryParamIdx > -1) {
85
- params = {};
86
- url
87
- .substr(queryParamIdx + 1)
88
- .split('&')
89
- .forEach((pair: any) => {
90
- const [key, value] = pair.split('=');
91
- if (key && value !== undefined) {
92
- params[key] = decodeURIComponent(value.replace(/\+/g, ' '));
93
- }
94
- });
95
- }
96
-
97
112
  // fetch and clear the request data from the cache
98
- const rid = xhr._trackingName;
99
- const cachedRequest = (requestCache as any)[rid] || { xhr };
100
- (requestCache as any)[rid] = null;
113
+ const requestId = xhr._trackingName;
114
+ const cachedRequest = requestCache[requestId];
115
+ if (!cachedRequest) {
116
+ return;
117
+ }
118
+ delete requestCache[requestId];
101
119
 
102
- // assemble the request object
103
- const { data, stopTimer } = cachedRequest;
104
- const tronRequest = {
105
- url: url || cachedRequest.xhr._url,
106
- method: xhr._method || null,
107
- data,
108
- headers: xhr._headers || {},
109
- params,
110
- };
120
+ const { request: tronRequest, stopTimer } = cachedRequest;
111
121
 
112
122
  // what type of content is this?
113
123
  const contentType =
@@ -116,7 +126,7 @@ const networking =
116
126
  '';
117
127
 
118
128
  const sendResponse = (responseBodyText: string) => {
119
- let body = `~~~ skipped ~~~`;
129
+ let body: any = `~~~ skipped ~~~`;
120
130
  if (responseBodyText) {
121
131
  try {
122
132
  // all i am saying, is give JSON a chance...
@@ -128,13 +138,14 @@ const networking =
128
138
  const tronResponse = {
129
139
  body,
130
140
  status,
131
- headers: xhr.responseHeaders || null,
141
+ headers: xhr.responseHeaders || {},
132
142
  };
133
143
  (dreaction as any).apiResponse(
144
+ requestId,
134
145
  tronRequest,
135
146
  tronResponse,
136
- stopTimer ? stopTimer() : null
137
- ); // TODO: Fix
147
+ stopTimer ? stopTimer() : 0
148
+ );
138
149
  };
139
150
 
140
151
  // can we use the real response?