@react-native-hero/network 0.0.7 → 0.0.9
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.
package/README.md
CHANGED
|
@@ -43,6 +43,7 @@ import {
|
|
|
43
43
|
CODE,
|
|
44
44
|
download,
|
|
45
45
|
upload,
|
|
46
|
+
fetch,
|
|
46
47
|
} from '@react-native-hero/network'
|
|
47
48
|
|
|
48
49
|
download(
|
|
@@ -55,10 +56,10 @@ download(
|
|
|
55
56
|
function (progress) {
|
|
56
57
|
// [0, 1]
|
|
57
58
|
}
|
|
58
|
-
).then(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
).then(response => {
|
|
60
|
+
response.name
|
|
61
|
+
response.path
|
|
62
|
+
response.size
|
|
62
63
|
})
|
|
63
64
|
.catch(err => {
|
|
64
65
|
if (err.code === CODE.DOWNLOAD_FAILURE) {
|
|
@@ -91,9 +92,9 @@ upload(
|
|
|
91
92
|
function (progress) {
|
|
92
93
|
// [0, 1]
|
|
93
94
|
}
|
|
94
|
-
).then(
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
).then(response => {
|
|
96
|
+
response.status_code
|
|
97
|
+
response.body
|
|
97
98
|
})
|
|
98
99
|
.catch(err => {
|
|
99
100
|
if (err.code === CODE.UPLOAD_FAILURE) {
|
|
@@ -101,4 +102,26 @@ upload(
|
|
|
101
102
|
}
|
|
102
103
|
})
|
|
103
104
|
|
|
105
|
+
fetch({
|
|
106
|
+
url: '',
|
|
107
|
+
methods: 'post',
|
|
108
|
+
// optional, send request params
|
|
109
|
+
data: {
|
|
110
|
+
key1: 'value1',
|
|
111
|
+
key2: 'value2',
|
|
112
|
+
},
|
|
113
|
+
// optional, set request headers
|
|
114
|
+
headers: {
|
|
115
|
+
|
|
116
|
+
}
|
|
117
|
+
})
|
|
118
|
+
.then(response => {
|
|
119
|
+
response.status_code
|
|
120
|
+
response.body
|
|
121
|
+
})
|
|
122
|
+
.catch(err => {
|
|
123
|
+
if (err.code === CODE.FETCH_FAILURE) {
|
|
124
|
+
console.log('fetch error')
|
|
125
|
+
}
|
|
126
|
+
})
|
|
104
127
|
```
|
|
@@ -4,17 +4,24 @@ import com.facebook.react.bridge.*
|
|
|
4
4
|
import com.facebook.react.modules.core.DeviceEventManagerModule
|
|
5
5
|
import okhttp3.*
|
|
6
6
|
import okhttp3.Callback
|
|
7
|
+
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
|
8
|
+
import okhttp3.MediaType.Companion.toMediaType
|
|
7
9
|
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
|
10
|
+
import okhttp3.RequestBody.Companion.toRequestBody
|
|
8
11
|
import okio.*
|
|
12
|
+
import org.json.JSONObject
|
|
9
13
|
import java.io.File
|
|
10
14
|
import java.io.IOException
|
|
11
15
|
import java.util.*
|
|
12
16
|
|
|
17
|
+
|
|
13
18
|
class RNTNetworkModule(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
|
|
14
19
|
|
|
15
20
|
companion object {
|
|
16
21
|
private const val ERROR_CODE_DOWNLOAD_FAILURE = "1"
|
|
17
22
|
private const val ERROR_CODE_UPLOAD_FAILURE = "2"
|
|
23
|
+
private const val ERROR_CODE_FETCH_FAILURE = "3"
|
|
24
|
+
private val JSON = "application/json; charset=utf-8".toMediaType()
|
|
18
25
|
}
|
|
19
26
|
|
|
20
27
|
override fun getName(): String {
|
|
@@ -27,6 +34,7 @@ class RNTNetworkModule(private val reactContext: ReactApplicationContext) : Reac
|
|
|
27
34
|
|
|
28
35
|
constants["ERROR_CODE_DOWNLOAD_FAILURE"] = ERROR_CODE_DOWNLOAD_FAILURE
|
|
29
36
|
constants["ERROR_CODE_UPLOAD_FAILURE"] = ERROR_CODE_UPLOAD_FAILURE
|
|
37
|
+
constants["ERROR_CODE_FETCH_FAILURE"] = ERROR_CODE_FETCH_FAILURE
|
|
30
38
|
|
|
31
39
|
return constants
|
|
32
40
|
|
|
@@ -110,14 +118,14 @@ class RNTNetworkModule(private val reactContext: ReactApplicationContext) : Reac
|
|
|
110
118
|
val file = options.getMap("file") as ReadableMap
|
|
111
119
|
|
|
112
120
|
val data = if (options.hasKey("data")) {
|
|
113
|
-
options.getMap("data")
|
|
121
|
+
options.getMap("data")?.toHashMap()
|
|
114
122
|
}
|
|
115
123
|
else {
|
|
116
124
|
null
|
|
117
125
|
}
|
|
118
126
|
|
|
119
127
|
val headers = if (options.hasKey("headers")) {
|
|
120
|
-
options.getMap("headers")
|
|
128
|
+
options.getMap("headers")?.toHashMap()
|
|
121
129
|
}
|
|
122
130
|
else {
|
|
123
131
|
null
|
|
@@ -153,7 +161,7 @@ class RNTNetworkModule(private val reactContext: ReactApplicationContext) : Reac
|
|
|
153
161
|
)
|
|
154
162
|
|
|
155
163
|
data?.let {
|
|
156
|
-
for ((key,value) in it
|
|
164
|
+
for ((key,value) in it) {
|
|
157
165
|
formBuilder.addFormDataPart(key, value.toString())
|
|
158
166
|
}
|
|
159
167
|
}
|
|
@@ -162,7 +170,7 @@ class RNTNetworkModule(private val reactContext: ReactApplicationContext) : Reac
|
|
|
162
170
|
val requestBuilder = Request.Builder().url(url).post(formBuilder.build())
|
|
163
171
|
|
|
164
172
|
headers?.let {
|
|
165
|
-
for ((key, value) in it
|
|
173
|
+
for ((key, value) in it) {
|
|
166
174
|
requestBuilder.addHeader(key, value.toString())
|
|
167
175
|
}
|
|
168
176
|
}
|
|
@@ -175,7 +183,89 @@ class RNTNetworkModule(private val reactContext: ReactApplicationContext) : Reac
|
|
|
175
183
|
override fun onResponse(call: Call, response: Response) {
|
|
176
184
|
val map = Arguments.createMap()
|
|
177
185
|
map.putInt("status_code", response.code)
|
|
186
|
+
|
|
187
|
+
val headers = Arguments.createMap()
|
|
188
|
+
for ((key, value) in response.headers) {
|
|
189
|
+
headers.putString(key, value)
|
|
190
|
+
}
|
|
191
|
+
map.putMap("headers", headers)
|
|
192
|
+
map.putString("body", response.body?.string())
|
|
193
|
+
|
|
194
|
+
promise.resolve(map)
|
|
195
|
+
}
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
@ReactMethod
|
|
201
|
+
fun fetch(options: ReadableMap, promise: Promise) {
|
|
202
|
+
|
|
203
|
+
var url = options.getString("url") as String
|
|
204
|
+
val method = options.getString("method") as String
|
|
205
|
+
|
|
206
|
+
val data = if (options.hasKey("data")) {
|
|
207
|
+
options.getMap("data")?.toHashMap()
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
null
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
val headers = if (options.hasKey("headers")) {
|
|
214
|
+
options.getMap("headers")?.toHashMap()
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
null
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
val client = OkHttpClient()
|
|
221
|
+
val isPost = method.toUpperCase(Locale.ROOT) == "POST"
|
|
222
|
+
|
|
223
|
+
var requestBody: RequestBody? = null
|
|
224
|
+
|
|
225
|
+
if (isPost) {
|
|
226
|
+
data?.let {
|
|
227
|
+
requestBody = JSONObject(it).toString().toRequestBody(JSON)
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
val httpUrl = url.toHttpUrlOrNull() ?: return
|
|
232
|
+
val urlBuilder = httpUrl.newBuilder()
|
|
233
|
+
data?.let {
|
|
234
|
+
for ((key, value) in it) {
|
|
235
|
+
urlBuilder.addQueryParameter(key, value.toString())
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
url = urlBuilder.build().toString()
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
var requestBuilder = Request.Builder().url(url)
|
|
242
|
+
|
|
243
|
+
requestBody?.let {
|
|
244
|
+
requestBuilder = requestBuilder.post(it)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
headers?.let {
|
|
248
|
+
for ((key, value) in it) {
|
|
249
|
+
requestBuilder.addHeader(key, value.toString())
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
client.newCall(requestBuilder.build()).enqueue(object : Callback {
|
|
254
|
+
override fun onFailure(call: Call, e: IOException) {
|
|
255
|
+
promise.reject(ERROR_CODE_FETCH_FAILURE, e.localizedMessage)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
override fun onResponse(call: Call, response: Response) {
|
|
259
|
+
val map = Arguments.createMap()
|
|
260
|
+
map.putInt("status_code", response.code)
|
|
261
|
+
|
|
262
|
+
val headers = Arguments.createMap()
|
|
263
|
+
for ((key, value) in response.headers) {
|
|
264
|
+
headers.putString(key, value)
|
|
265
|
+
}
|
|
266
|
+
map.putMap("headers", headers)
|
|
178
267
|
map.putString("body", response.body?.string())
|
|
268
|
+
|
|
179
269
|
promise.resolve(map)
|
|
180
270
|
}
|
|
181
271
|
})
|
package/index.js
CHANGED
|
@@ -22,6 +22,7 @@ eventEmitter.addListener('upload_progress', handleProgress)
|
|
|
22
22
|
export const CODE = {
|
|
23
23
|
DOWNLOAD_FAILURE: RNTNetwork.ERROR_CODE_DOWNLOAD_FAILURE,
|
|
24
24
|
UPLOAD_FAILURE: RNTNetwork.ERROR_CODE_UPLOAD_FAILURE,
|
|
25
|
+
FETCH_FAILURE: RNTNetwork.ERROR_CODE_FETCH_FAILURE,
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
/**
|
|
@@ -55,3 +56,10 @@ export function upload(options, onProgress) {
|
|
|
55
56
|
}
|
|
56
57
|
})
|
|
57
58
|
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 发送请求
|
|
62
|
+
*/
|
|
63
|
+
export function fetch(options) {
|
|
64
|
+
return RNTNetwork.fetch(options)
|
|
65
|
+
}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
static NSString *ERROR_CODE_DOWNLOAD_FAILURE = @"1";
|
|
8
8
|
static NSString *ERROR_CODE_UPLOAD_FAILURE = @"2";
|
|
9
|
+
static NSString *ERROR_CODE_FETCH_FAILURE = @"3";
|
|
9
10
|
|
|
10
11
|
static NSDictionary* getFileInfo(NSURL *url) {
|
|
11
12
|
|
|
@@ -58,6 +59,7 @@ static NSString* dictionary2JsonString(NSDictionary *dict) {
|
|
|
58
59
|
return @{
|
|
59
60
|
@"ERROR_CODE_DOWNLOAD_FAILURE": ERROR_CODE_DOWNLOAD_FAILURE,
|
|
60
61
|
@"ERROR_CODE_UPLOAD_FAILURE": ERROR_CODE_UPLOAD_FAILURE,
|
|
62
|
+
@"ERROR_CODE_FETCH_FAILURE": ERROR_CODE_FETCH_FAILURE,
|
|
61
63
|
};
|
|
62
64
|
}
|
|
63
65
|
|
|
@@ -166,6 +168,7 @@ RCT_EXPORT_METHOD(upload:(NSDictionary*)options resolve:(RCTPromiseResolveBlock)
|
|
|
166
168
|
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
|
|
167
169
|
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
|
|
168
170
|
result[@"status_code"] = @(httpResponse.statusCode);
|
|
171
|
+
result[@"headers"] = httpResponse.allHeaderFields;
|
|
169
172
|
}
|
|
170
173
|
|
|
171
174
|
if ([responseObject isKindOfClass:[NSDictionary class]]) {
|
|
@@ -187,4 +190,83 @@ RCT_EXPORT_METHOD(upload:(NSDictionary*)options resolve:(RCTPromiseResolveBlock)
|
|
|
187
190
|
|
|
188
191
|
}
|
|
189
192
|
|
|
193
|
+
RCT_EXPORT_METHOD(fetch:(NSDictionary*)options resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
|
|
194
|
+
|
|
195
|
+
NSString *url = [RCTConvert NSString:options[@"url"]];
|
|
196
|
+
NSString *method = [RCTConvert NSString:options[@"method"]];
|
|
197
|
+
NSDictionary *data = [RCTConvert NSDictionary:options[@"data"]];
|
|
198
|
+
NSDictionary *headers = [RCTConvert NSDictionary:options[@"headers"]];
|
|
199
|
+
|
|
200
|
+
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
|
201
|
+
manager.responseSerializer = [AFJSONResponseSerializer serializer];
|
|
202
|
+
|
|
203
|
+
if ([method.uppercaseString isEqual: @"POST"]) {
|
|
204
|
+
manager.requestSerializer = [AFJSONRequestSerializer serializer];
|
|
205
|
+
[manager
|
|
206
|
+
POST:url
|
|
207
|
+
parameters:data
|
|
208
|
+
headers:headers
|
|
209
|
+
progress:nil
|
|
210
|
+
success:^(NSURLSessionDataTask *task, id responseObject) {
|
|
211
|
+
|
|
212
|
+
NSURLResponse *response = task.response;
|
|
213
|
+
|
|
214
|
+
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
|
|
215
|
+
|
|
216
|
+
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
|
|
217
|
+
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
|
|
218
|
+
result[@"status_code"] = @(httpResponse.statusCode);
|
|
219
|
+
result[@"headers"] = httpResponse.allHeaderFields;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if ([responseObject isKindOfClass:[NSDictionary class]]) {
|
|
223
|
+
NSDictionary *json = (NSDictionary*)responseObject;
|
|
224
|
+
// 安卓返回 map 比较麻烦,因此这里统一改成返回字符串
|
|
225
|
+
result[@"body"] = dictionary2JsonString(json);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
resolve(result);
|
|
229
|
+
|
|
230
|
+
}
|
|
231
|
+
failure:^(NSURLSessionDataTask *task, NSError *error) {
|
|
232
|
+
reject(ERROR_CODE_FETCH_FAILURE, error.localizedDescription, error);
|
|
233
|
+
}
|
|
234
|
+
];
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
[manager
|
|
238
|
+
GET:url
|
|
239
|
+
parameters:data
|
|
240
|
+
headers:headers
|
|
241
|
+
progress:nil
|
|
242
|
+
success:^(NSURLSessionDataTask *task, id responseObject) {
|
|
243
|
+
|
|
244
|
+
NSURLResponse *response = task.response;
|
|
245
|
+
|
|
246
|
+
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
|
|
247
|
+
|
|
248
|
+
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
|
|
249
|
+
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
|
|
250
|
+
result[@"status_code"] = @(httpResponse.statusCode);
|
|
251
|
+
result[@"headers"] = httpResponse.allHeaderFields;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if ([responseObject isKindOfClass:[NSDictionary class]]) {
|
|
255
|
+
NSDictionary *json = (NSDictionary*)responseObject;
|
|
256
|
+
// 安卓返回 map 比较麻烦,因此这里统一改成返回字符串
|
|
257
|
+
result[@"body"] = dictionary2JsonString(json);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
resolve(result);
|
|
261
|
+
|
|
262
|
+
}
|
|
263
|
+
failure:^(NSURLSessionDataTask *task, NSError *error) {
|
|
264
|
+
reject(ERROR_CODE_FETCH_FAILURE, error.localizedDescription, error);
|
|
265
|
+
}
|
|
266
|
+
];
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
}
|
|
271
|
+
|
|
190
272
|
@end
|