-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (138 loc) · 5.99 KB
/
index.js
File metadata and controls
157 lines (138 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* MIT License
*
* Copyright (c) 2016-present, CriticalBlue Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
* OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { NativeModules } from 'react-native'
const NativeApproovService = NativeModules.ApproovService
function requestMayHaveBody(request) {
if (!request || /^(GET|HEAD)$/i.test(request.method || 'GET')) {
return false
}
if ('bodyUsed' in request && request.bodyUsed) {
return true
}
if ('body' in request && request.body != null) {
return true
}
return false
}
async function extractRequestBody(request) {
if (!requestMayHaveBody(request)) {
return undefined
}
if (typeof request.text === 'function' && typeof request.clone === 'function') {
try {
const requestForBody = request.clone()
const bodyText = await requestForBody.text()
return bodyText
} catch (error) {
console.warn(
'ApproovService.fetchWithApproov(): unable to extract the Request body via Request.clone().text(). ' +
'Pass a string body in the init argument for non-text or already-consumed Request bodies.',
error
)
return undefined
}
}
console.warn(
'ApproovService.fetchWithApproov(): Request body extraction is unavailable in this environment. ' +
'Pass a string body in the init argument.'
)
return undefined
}
// Use a Proxy so all native module methods are accessible regardless of
// enumerability. Spreading NativeApproovService in New Architecture loses
// non-enumerable Proxy-trapped methods (e.g. setUseApproovStatusIfNoToken).
const ApproovService = new Proxy(NativeApproovService || {}, {
get(target, prop) {
if (prop === 'setProceedOnNetworkFail') {
return () => {
// No-op for backwards compatibility. This function no longer does anything.
console.warn('ApproovService.setProceedOnNetworkFail() is deprecated and has no effect.')
}
}
if (prop === 'fetchWithApproov') {
return async (input, init = {}) => {
let url;
let options = { ...init };
// Handle if the first argument is a Request object
if (typeof Request !== 'undefined' && typeof input === 'object' && input instanceof Request) {
url = input.url;
options.method = options.method || input.method;
// Extract headers from the Request object safely
const requestHeaders = {};
if (input.headers && typeof input.headers.forEach === 'function') {
input.headers.forEach((value, key) => {
requestHeaders[key] = value;
});
}
// Merge with any headers provided in the init object
const initHeaders = {};
if (init.headers) {
const h = new Headers(init.headers);
h.forEach((value, key) => {
initHeaders[key] = value;
});
}
options.headers = { ...requestHeaders, ...initHeaders };
if (options.body === undefined || options.body === null) {
const extractedBody = await extractRequestBody(input)
if (extractedBody !== undefined) {
options.body = extractedBody
}
}
} else {
url = input;
// Ensure headers are a plain object for the NativeBridge
if (options.headers) {
const plainHeaders = {};
const h = new Headers(options.headers);
h.forEach((value, key) => {
plainHeaders[key] = value;
});
options.headers = plainHeaders;
}
}
// Call the native implementation
const nativeResponse = await NativeApproovService.fetchWithApproov(url, options);
// Construct a WHATWG Response object to return
return new Response(nativeResponse.body, {
status: nativeResponse.status,
headers: new Headers(nativeResponse.headers || {})
});
}
}
if (prop === 'initialize') {
return (config, comment = null) => target.initialize(config, comment)
}
return target[prop]
}
})
// Add log levels
ApproovService.Log = {
EXTREME: 0,
DEBUG: 1,
INFO: 2,
WARN: 3,
ERROR: 4,
NONE: 5
}
import { ApproovProvider, useApproov } from './approov-provider'
import { ApproovMonitor } from './approov-monitor'
export { ApproovService, ApproovProvider, ApproovMonitor, useApproov }