react-native-nitro-fetch 1.3.2 → 1.4.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.
Files changed (39) hide show
  1. package/NitroFetch.podspec +1 -3
  2. package/README.md +39 -11
  3. package/android/src/main/java/com/margelo/nitro/nitrofetch/AutoPrefetcher.kt +6 -2
  4. package/android/src/main/java/com/margelo/nitro/nitrofetch/DevToolsReporterImpl.kt +27 -36
  5. package/android/src/main/java/com/margelo/nitro/nitrofetch/NitroFetchClient.kt +45 -0
  6. package/ios/NitroAutoPrefetcher.swift +4 -0
  7. package/ios/NitroDevToolsReporter.mm +37 -31
  8. package/ios/NitroFetchClient.swift +56 -0
  9. package/lib/module/CurlGenerator.js.map +1 -2
  10. package/lib/module/Headers.js.map +2 -1
  11. package/lib/module/HermesProfiler.js.map +2 -1
  12. package/lib/module/NetworkInspector.js +1 -5
  13. package/lib/module/NetworkInspector.js.map +2 -1
  14. package/lib/module/NitroCronet.nitro.js.map +2 -1
  15. package/lib/module/NitroFetch.nitro.js.map +2 -1
  16. package/lib/module/NitroInstances.js.map +2 -1
  17. package/lib/module/Request.js.map +1 -2
  18. package/lib/module/Response.js.map +2 -2
  19. package/lib/module/fetch.js +147 -1
  20. package/lib/module/fetch.js.map +1 -1
  21. package/lib/module/index.web.js +1 -0
  22. package/lib/module/index.web.js.map +1 -2
  23. package/lib/module/tokenRefresh.js.map +1 -2
  24. package/lib/module/utf8.js.map +1 -2
  25. package/lib/typescript/src/fetch.d.ts.map +1 -1
  26. package/package.json +1 -1
  27. package/src/CurlGenerator.js +26 -23
  28. package/src/Headers.js +116 -108
  29. package/src/HermesProfiler.js +18 -16
  30. package/src/NetworkInspector.js +179 -171
  31. package/src/NitroInstances.js +1 -2
  32. package/src/Request.js +164 -167
  33. package/src/Response.js +242 -244
  34. package/src/fetch.js +842 -706
  35. package/src/fetch.ts +170 -1
  36. package/src/index.js +2 -17
  37. package/src/index.web.js +67 -69
  38. package/src/tokenRefresh.js +77 -75
  39. package/src/utf8.js +27 -28
package/src/Request.js CHANGED
@@ -1,176 +1,173 @@
1
1
  import { NitroHeaders } from './Headers';
2
2
  import { stringToUTF8, utf8ToString } from './utf8';
3
3
  export class NitroRequest {
4
- url;
5
- method;
6
- headers;
7
- redirect;
8
- signal;
9
- cache;
10
- credentials;
11
- mode;
12
- referrer;
13
- referrerPolicy;
14
- integrity;
15
- keepalive;
16
- destination;
17
- _body;
18
- _bodyUsed = false;
19
- constructor(input, init) {
20
- if (input instanceof NitroRequest) {
21
- // Clone from another NitroRequest
22
- this.url = input.url;
23
- this.method = (init?.method ?? input.method).toUpperCase();
24
- this.headers = new NitroHeaders(
25
- init?.headers
26
- ? init.headers instanceof NitroHeaders
27
- ? init.headers
28
- : init.headers
29
- : input.headers
30
- );
31
- this.redirect = init?.redirect ?? input.redirect;
32
- this.signal = init?.signal ?? input.signal;
33
- this.cache = init?.cache ?? input.cache;
34
- this.credentials = init?.credentials ?? input.credentials;
35
- this.mode = init?.mode ?? input.mode;
36
- this.referrer = init?.referrer ?? input.referrer;
37
- this.referrerPolicy = init?.referrerPolicy ?? input.referrerPolicy;
38
- this.integrity = init?.integrity ?? input.integrity;
39
- this.keepalive = init?.keepalive ?? input.keepalive;
40
- this._body = init?.body !== undefined ? (init.body ?? null) : input._body;
41
- } else if (
42
- typeof input === 'object' &&
43
- input !== null &&
44
- 'url' in input &&
45
- 'method' in input &&
46
- 'headers' in input &&
47
- !(input instanceof URL)
48
- ) {
49
- // Construct from a Request-like object (standard Request or duck-typed)
50
- this.url = input.url;
51
- this.method = (init?.method ?? input.method).toUpperCase();
52
- this.headers = new NitroHeaders(
53
- init?.headers
54
- ? init.headers instanceof NitroHeaders
55
- ? init.headers
56
- : init.headers
57
- : input.headers
58
- );
59
- this.redirect = init?.redirect ?? input.redirect ?? 'follow';
60
- this.signal = init?.signal ?? input.signal;
61
- this.cache = init?.cache ?? input.cache ?? 'default';
62
- this.credentials =
63
- init?.credentials ?? input.credentials ?? 'same-origin';
64
- this.mode = init?.mode ?? input.mode ?? 'cors';
65
- this.referrer = init?.referrer ?? input.referrer ?? 'about:client';
66
- this.referrerPolicy = init?.referrerPolicy ?? input.referrerPolicy ?? '';
67
- this.integrity = init?.integrity ?? input.integrity ?? '';
68
- this.keepalive = init?.keepalive ?? input.keepalive ?? false;
69
- this._body = init?.body ?? null;
70
- } else {
71
- this.url = String(input);
72
- this.method = (init?.method ?? 'GET').toUpperCase();
73
- this.headers = new NitroHeaders(
74
- init?.headers
75
- ? init.headers instanceof NitroHeaders
76
- ? init.headers
77
- : init.headers
78
- : undefined
79
- );
80
- this.redirect = init?.redirect ?? 'follow';
81
- this.signal = init?.signal ?? new AbortController().signal;
82
- this.cache = init?.cache ?? 'default';
83
- this.credentials = init?.credentials ?? 'same-origin';
84
- this.mode = init?.mode ?? 'cors';
85
- this.referrer = init?.referrer ?? 'about:client';
86
- this.referrerPolicy = init?.referrerPolicy ?? '';
87
- this.integrity = init?.integrity ?? '';
88
- this.keepalive = init?.keepalive ?? false;
89
- this._body = init?.body ?? null;
4
+ url;
5
+ method;
6
+ headers;
7
+ redirect;
8
+ signal;
9
+ cache;
10
+ credentials;
11
+ mode;
12
+ referrer;
13
+ referrerPolicy;
14
+ integrity;
15
+ keepalive;
16
+ destination;
17
+ _body;
18
+ _bodyUsed = false;
19
+ constructor(input, init) {
20
+ if (input instanceof NitroRequest) {
21
+ // Clone from another NitroRequest
22
+ this.url = input.url;
23
+ this.method = (init?.method ?? input.method).toUpperCase();
24
+ this.headers = new NitroHeaders(init?.headers
25
+ ? init.headers instanceof NitroHeaders
26
+ ? init.headers
27
+ : init.headers
28
+ : input.headers);
29
+ this.redirect = init?.redirect ?? input.redirect;
30
+ this.signal = init?.signal ?? input.signal;
31
+ this.cache = init?.cache ?? input.cache;
32
+ this.credentials = init?.credentials ?? input.credentials;
33
+ this.mode = init?.mode ?? input.mode;
34
+ this.referrer = init?.referrer ?? input.referrer;
35
+ this.referrerPolicy = init?.referrerPolicy ?? input.referrerPolicy;
36
+ this.integrity = init?.integrity ?? input.integrity;
37
+ this.keepalive = init?.keepalive ?? input.keepalive;
38
+ this._body = init?.body !== undefined ? (init.body ?? null) : input._body;
39
+ }
40
+ else if (typeof input === 'object' &&
41
+ input !== null &&
42
+ 'url' in input &&
43
+ 'method' in input &&
44
+ 'headers' in input &&
45
+ !(input instanceof URL)) {
46
+ // Construct from a Request-like object (standard Request or duck-typed)
47
+ this.url = input.url;
48
+ this.method = (init?.method ?? input.method).toUpperCase();
49
+ this.headers = new NitroHeaders(init?.headers
50
+ ? init.headers instanceof NitroHeaders
51
+ ? init.headers
52
+ : init.headers
53
+ : input.headers);
54
+ this.redirect =
55
+ init?.redirect ?? input.redirect ?? 'follow';
56
+ this.signal = init?.signal ?? input.signal;
57
+ this.cache = init?.cache ?? input.cache ?? 'default';
58
+ this.credentials =
59
+ init?.credentials ?? input.credentials ?? 'same-origin';
60
+ this.mode = init?.mode ?? input.mode ?? 'cors';
61
+ this.referrer = init?.referrer ?? input.referrer ?? 'about:client';
62
+ this.referrerPolicy =
63
+ init?.referrerPolicy ?? input.referrerPolicy ?? '';
64
+ this.integrity = init?.integrity ?? input.integrity ?? '';
65
+ this.keepalive = init?.keepalive ?? input.keepalive ?? false;
66
+ this._body = init?.body ?? null;
67
+ }
68
+ else {
69
+ this.url = String(input);
70
+ this.method = (init?.method ?? 'GET').toUpperCase();
71
+ this.headers = new NitroHeaders(init?.headers
72
+ ? init.headers instanceof NitroHeaders
73
+ ? init.headers
74
+ : init.headers
75
+ : undefined);
76
+ this.redirect = init?.redirect ?? 'follow';
77
+ this.signal = init?.signal ?? new AbortController().signal;
78
+ this.cache = init?.cache ?? 'default';
79
+ this.credentials = init?.credentials ?? 'same-origin';
80
+ this.mode = init?.mode ?? 'cors';
81
+ this.referrer = init?.referrer ?? 'about:client';
82
+ this.referrerPolicy = init?.referrerPolicy ?? '';
83
+ this.integrity = init?.integrity ?? '';
84
+ this.keepalive = init?.keepalive ?? false;
85
+ this._body = init?.body ?? null;
86
+ }
87
+ this.destination = '';
90
88
  }
91
- this.destination = '';
92
- }
93
- get bodyUsed() {
94
- return this._bodyUsed;
95
- }
96
- get body() {
97
- if (this._body == null) return null;
98
- const bodyBytes = this._getBodyBytes();
99
- if (!bodyBytes) return null;
100
- return new ReadableStream({
101
- start(controller) {
102
- controller.enqueue(new Uint8Array(bodyBytes));
103
- controller.close();
104
- },
105
- });
106
- }
107
- _throwIfBodyUsed() {
108
- if (this._bodyUsed) {
109
- throw new TypeError('Body has already been consumed.');
89
+ get bodyUsed() {
90
+ return this._bodyUsed;
110
91
  }
111
- }
112
- _getBodyBytes() {
113
- if (this._body == null) return undefined;
114
- if (typeof this._body === 'string') {
115
- const encoded = stringToUTF8(this._body);
116
- return encoded.buffer.slice(
117
- encoded.byteOffset,
118
- encoded.byteOffset + encoded.byteLength
119
- );
92
+ get body() {
93
+ if (this._body == null)
94
+ return null;
95
+ const bodyBytes = this._getBodyBytes();
96
+ if (!bodyBytes)
97
+ return null;
98
+ return new ReadableStream({
99
+ start(controller) {
100
+ controller.enqueue(new Uint8Array(bodyBytes));
101
+ controller.close();
102
+ },
103
+ });
120
104
  }
121
- if (this._body instanceof ArrayBuffer) return this._body;
122
- if (ArrayBuffer.isView(this._body)) {
123
- const view = this._body;
124
- return view.buffer.slice(
125
- view.byteOffset,
126
- view.byteOffset + view.byteLength
127
- );
105
+ _throwIfBodyUsed() {
106
+ if (this._bodyUsed) {
107
+ throw new TypeError('Body has already been consumed.');
108
+ }
128
109
  }
129
- return undefined;
130
- }
131
- _getBodyString() {
132
- if (this._body == null) return '';
133
- if (typeof this._body === 'string') return this._body;
134
- const bytes = this._getBodyBytes();
135
- if (bytes) return utf8ToString(new Uint8Array(bytes));
136
- return '';
137
- }
138
- async text() {
139
- this._throwIfBodyUsed();
140
- this._bodyUsed = true;
141
- return this._getBodyString();
142
- }
143
- async json() {
144
- this._throwIfBodyUsed();
145
- this._bodyUsed = true;
146
- const t = this._getBodyString();
147
- return JSON.parse(t || '{}');
148
- }
149
- async arrayBuffer() {
150
- this._throwIfBodyUsed();
151
- this._bodyUsed = true;
152
- return this._getBodyBytes() ?? new ArrayBuffer(0);
153
- }
154
- async blob() {
155
- this._throwIfBodyUsed();
156
- this._bodyUsed = true;
157
- const buffer = this._getBodyBytes() ?? new ArrayBuffer(0);
158
- const contentType = this.headers.get('content-type') ?? '';
159
- return new Blob([buffer], { type: contentType });
160
- }
161
- async bytes() {
162
- this._throwIfBodyUsed();
163
- this._bodyUsed = true;
164
- const buffer = this._getBodyBytes() ?? new ArrayBuffer(0);
165
- return new Uint8Array(buffer);
166
- }
167
- clone() {
168
- if (this._bodyUsed) {
169
- throw new TypeError('Cannot clone a Request whose body has been used.');
110
+ _getBodyBytes() {
111
+ if (this._body == null)
112
+ return undefined;
113
+ if (typeof this._body === 'string') {
114
+ const encoded = stringToUTF8(this._body);
115
+ return encoded.buffer.slice(encoded.byteOffset, encoded.byteOffset + encoded.byteLength);
116
+ }
117
+ if (this._body instanceof ArrayBuffer)
118
+ return this._body;
119
+ if (ArrayBuffer.isView(this._body)) {
120
+ const view = this._body;
121
+ return view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength);
122
+ }
123
+ return undefined;
124
+ }
125
+ _getBodyString() {
126
+ if (this._body == null)
127
+ return '';
128
+ if (typeof this._body === 'string')
129
+ return this._body;
130
+ const bytes = this._getBodyBytes();
131
+ if (bytes)
132
+ return utf8ToString(new Uint8Array(bytes));
133
+ return '';
134
+ }
135
+ async text() {
136
+ this._throwIfBodyUsed();
137
+ this._bodyUsed = true;
138
+ return this._getBodyString();
139
+ }
140
+ async json() {
141
+ this._throwIfBodyUsed();
142
+ this._bodyUsed = true;
143
+ const t = this._getBodyString();
144
+ return JSON.parse(t || '{}');
145
+ }
146
+ async arrayBuffer() {
147
+ this._throwIfBodyUsed();
148
+ this._bodyUsed = true;
149
+ return this._getBodyBytes() ?? new ArrayBuffer(0);
150
+ }
151
+ async blob() {
152
+ this._throwIfBodyUsed();
153
+ this._bodyUsed = true;
154
+ const buffer = this._getBodyBytes() ?? new ArrayBuffer(0);
155
+ const contentType = this.headers.get('content-type') ?? '';
156
+ return new Blob([buffer], { type: contentType });
157
+ }
158
+ async bytes() {
159
+ this._throwIfBodyUsed();
160
+ this._bodyUsed = true;
161
+ const buffer = this._getBodyBytes() ?? new ArrayBuffer(0);
162
+ return new Uint8Array(buffer);
163
+ }
164
+ clone() {
165
+ if (this._bodyUsed) {
166
+ throw new TypeError('Cannot clone a Request whose body has been used.');
167
+ }
168
+ return new NitroRequest(this);
169
+ }
170
+ async formData() {
171
+ throw new TypeError('formData() is not supported in NitroRequest');
170
172
  }
171
- return new NitroRequest(this);
172
- }
173
- async formData() {
174
- throw new TypeError('formData() is not supported in NitroRequest');
175
- }
176
173
  }