11'use strict'
22
33var util = require ( 'util' )
4+ var objcHelpers = require ( './helpers' )
45
56module . exports = function ( source , options ) {
67 var opts = util . _extend ( {
7- timeout : '10'
8+ timeout : '10' ,
9+ indent : ' ' ,
10+ pretty : true
811 } , options )
912
1013 var code = [ ]
1114
12- // Dependencies
15+ var req = {
16+ hasHeaders : false ,
17+ hasBody : false
18+ }
19+
20+ var indent = opts . indent
21+
1322 code . push ( '#import <Foundation/Foundation.h>' )
23+
24+ if ( Object . keys ( source . allHeaders ) . length ) {
25+ req . hasHeaders = true
26+ code . push ( null )
27+ code . push ( objcHelpers . nsDictionaryBuilder ( 'headers' , source . allHeaders , opts . pretty ) )
28+ }
29+
30+ if ( source . postData . text || source . postData . jsonObj || source . postData . params ) {
31+ req . hasBody = true
32+
33+ switch ( source . postData . mimeType ) {
34+ case 'application/x-www-form-urlencoded' :
35+ code . push ( null )
36+ // Makes it easier to implement logice than just putting the entire body string
37+ code . push ( util . format ( 'NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];' , source . postData . params [ 0 ] . name , source . postData . params [ 0 ] . value ) )
38+ for ( var i = 1 , len = source . postData . params . length ; i < len ; i ++ ) {
39+ code . push ( util . format ( '[postData appendData:[@"&%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];' , source . postData . params [ i ] . name , source . postData . params [ i ] . value ) )
40+ }
41+ break
42+
43+ case 'application/json' :
44+ if ( source . postData . jsonObj ) {
45+ code . push ( objcHelpers . nsDictionaryBuilder ( 'parameters' , source . postData . jsonObj , opts . pretty ) )
46+ code . push ( null )
47+ code . push ( 'NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];' )
48+ }
49+ break
50+
51+ case 'multipart/form-data' :
52+ code . push ( objcHelpers . nsArrayBuilder ( 'parameters' , source . postData . params , opts . pretty ) )
53+ code . push ( util . format ( 'NSString *boundary = @"%s";' , source . postData . boundary ) )
54+ code . push ( null )
55+ code . push ( 'NSError *error;' )
56+ code . push ( 'NSMutableString *body = [NSMutableString string];' )
57+ code . push ( 'for (NSDictionary *param in parameters) {' )
58+ code . push ( indent + '[body appendFormat:@"--%@\\r\\n", boundary];' )
59+ code . push ( indent + 'if (param[@"fileName"]) {' )
60+ code . push ( indent + indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"; filename=\\"%@\\"\\r\\n", param[@"name"], param[@"fileName"]];' )
61+ code . push ( indent + indent + '[body appendFormat:@"Content-Type: %@\\r\\n\\r\\n", param[@"contentType"]];' )
62+ code . push ( indent + indent + '[body appendFormat:@"%@", [[NSString alloc] initWithContentsOfFile:param[@"fileName"]' )
63+ code . push ( indent + indent + ' encoding:NSUTF8StringEncoding error:&error]];' )
64+ code . push ( indent + indent + 'if (error) {' )
65+ code . push ( indent + indent + indent + 'NSLog(@"%@", error);' )
66+ code . push ( indent + indent + '}' )
67+ code . push ( indent + '} else {' )
68+ code . push ( indent + indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"\\r\\n\\r\\n", param[@"name"]];' )
69+ code . push ( indent + indent + '[body appendFormat:@"%@", param[@"value"]];' )
70+ code . push ( indent + '}' )
71+ code . push ( '}' )
72+ code . push ( '[body appendFormat:@"\\r\\n--%@--\\r\\n", boundary];' )
73+ code . push ( 'NSData *postData = [[NSData alloc] initWithData:[body dataUsingEncoding:NSUTF8StringEncoding]];' )
74+ break
75+
76+ default :
77+ code . push ( null )
78+ code . push ( 'NSData *postData = [[NSData alloc] initWithData:[@"' + source . postData . text + '" dataUsingEncoding:NSUTF8StringEncoding]];' )
79+ }
80+ }
81+
1482 code . push ( null )
15- code . push ( 'NSURLSession *session = [NSURLSession sharedSession];' )
16- code . push ( null )
17- // Create request object
1883 code . push ( 'NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"' + source . fullUrl + '"]' )
1984 code . push ( ' cachePolicy:NSURLRequestUseProtocolCachePolicy' )
2085 code . push ( ' timeoutInterval:' + parseInt ( opts . timeout , 10 ) . toFixed ( 1 ) + '];' )
2186 code . push ( '[request setHTTPMethod:@"' + source . method + '"];' )
2287
23- // Set headers
24- Object . keys ( source . allHeaders ) . sort ( ) . map ( function ( key ) {
25- code . push ( '[request setValue:@"' + source . allHeaders [ key ] + '" forHTTPHeaderField:@"' + key + '"];' )
26- } )
27-
28- // Set request body
29- if ( source . postData && ( source . postData . params || source . postData . text ) ) {
30- code . push ( null )
31-
32- if ( source . postData . mimeType === 'application/x-www-form-urlencoded' && source . postData . params ) {
33- var params = source . postData . params
34- code . push ( 'NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"' + params [ 0 ] . name + '=' + params [ 0 ] . value + '" dataUsingEncoding:NSUTF8StringEncoding]];' )
35- for ( var i = 1 , len = params . length ; i < len ; i ++ ) {
36- code . push ( '[postData appendData:[@"&' + params [ i ] . name + '=' + params [ i ] . value + '" dataUsingEncoding:NSUTF8StringEncoding]];' )
37- }
38- } else if ( source . postData . mimeType === 'application/json' && source . postData . text ) {
39- code . push ( 'NSData *postData = [[NSData alloc] initWithData:[@' + JSON . stringify ( source . postData . text ) + ' dataUsingEncoding:NSUTF8StringEncoding]];' )
40- } else if ( source . postData . text ) {
41- code . push ( 'NSData *postData = [[NSData alloc] initWithData:[@"' + source . postData . text + '" dataUsingEncoding:NSUTF8StringEncoding]];' )
42- }
88+ if ( req . hasHeaders ) {
89+ code . push ( '[request setAllHTTPHeaderFields:headers];' )
90+ }
4391
92+ if ( req . hasBody ) {
4493 code . push ( '[request setHTTPBody:postData];' )
4594 }
4695
47- // Set completion block
4896 code . push ( null )
97+ code . push ( 'NSURLSession *session = [NSURLSession sharedSession];' ) // Retrieve shared session for simplicity
4998 code . push ( 'NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request' )
5099 code . push ( ' completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {' )
51- code . push ( null )
52- code . push ( '}];' )
53-
54- // Start request
55- code . push ( null )
100+ code . push ( ' ' + indent + 'if (error) {' )
101+ code . push ( ' ' + indent + indent + 'NSLog(@"%@", error);' )
102+ code . push ( ' ' + indent + '} else {' )
103+ code . push ( ' ' + indent + indent + 'NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;' )
104+ code . push ( ' ' + indent + indent + 'NSLog(@"%@", httpResponse);' )
105+ code . push ( ' ' + indent + '}' )
106+ code . push ( ' }];' )
56107 code . push ( '[dataTask resume];' )
57108
58109 return code . join ( '\n' )
@@ -62,5 +113,5 @@ module.exports.info = {
62113 key : 'native' ,
63114 title : 'NSURLSession' ,
64115 link : 'https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html' ,
65- description : " Foundation's NSURLSession request"
116+ description : ' Foundation\ 's NSURLSession request'
66117}
0 commit comments