Skip to content

Commit

Permalink
add minor network calls for fetch POST/GET to walk around CORs in WKW… (
Browse files Browse the repository at this point in the history
syrjs#158)

* add minor network calls for fetch POST/GET to walk around CORs in WKWebView

* add check for callback before call
  • Loading branch information
dmikey committed Feb 8, 2018
1 parent 045a238 commit 77c3f54
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
19 changes: 16 additions & 3 deletions ios/SyrNative/SyrNative/SyrNetworking.m
Expand Up @@ -12,7 +12,7 @@ @implementation SyrNetworking

SYR_EXPORT_MODULE()

SYR_EXPORT_METHOD(get: (NSDictionary*) requestDict) {
SYR_EXPORT_METHOD(request: (NSDictionary*) requestDict) {
// fetch on the async queue
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
dispatch_async(queue, ^{
Expand All @@ -21,6 +21,21 @@ @implementation SyrNetworking
NSString* guid = [requestDict valueForKey:@"guid"];
NSString* method = [requestDict valueForKey:@"method"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString* body = [requestDict valueForKey:@"body"];
id headers = [requestDict objectForKey:@"headers"];

if(body != nil) {
NSData* postData = [body dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
[request setHTTPBody: postData];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
}

// set more headers here
for (id key in headers) {
[request setValue:[headers valueForKey:key] forHTTPHeaderField:key];
}

[request setHTTPMethod:method];
[request setURL:[NSURL URLWithString:requestUrl]];

Expand All @@ -30,7 +45,6 @@ @implementation SyrNetworking
// get data
NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];


// log error if not success just for info
if([responseCode statusCode] != 200){
NSLog(@"Error getting %@, HTTP status code %li", requestUrl, (long)[responseCode statusCode]);
Expand All @@ -40,7 +54,6 @@ @implementation SyrNetworking
NSString* response = [[NSString alloc] initWithData:oResponseData encoding:NSASCIIStringEncoding];
NSNumber* statusCode = [NSNumber numberWithDouble:[responseCode statusCode]];


// send the response back
[self sendEventWithName:@"NetworkingCallback" body:@{@"data": response, @"responseCode": statusCode, @"guid": guid}];
});
Expand Down
25 changes: 16 additions & 9 deletions lib/networking.js
Expand Up @@ -31,20 +31,19 @@ class networking {
}
fetch(url, opts) {

if(opts && opts.mode.toLowerCase() === 'bypass') {
if(opts && opts.mode && opts.mode.toLowerCase() === 'bypass') {
const guid = Utils.guid();
const method = (opts.method && opts.method.toUpperCase()) || 'GET';
const headers = {};

console.log(method);
for (var header of opts.headers.entries()) {
headers[header[0]] = header[1];
}

const request = { method: method, guid: guid, url: url, body: opts.body, headers: headers };

const request = {
method: method,
guid: guid,
url: url
};

// call to native layer for fetch
NativeModules.SyrNetworking.get(request);
NativeModules.SyrNetworking.request(request);
return {
then: (cb)=>{
request.cb = cb;
Expand All @@ -59,6 +58,14 @@ class networking {
}
requestHandler(event) {
const body = event.body;

if(!this.requests[body.guid]) {
setTimeout(
()=>this.requestHandler(event)
, 20); // make sure we have cb ready before we fire it
return;
}

const req = this.requests[body.guid];
const response = {
status: body.responseCode,
Expand Down

0 comments on commit 77c3f54

Please sign in to comment.