Skip to content

Commit 710d739

Browse files
committed
refactor: move multipart body builder to helpers.js
1 parent 245268b commit 710d739

2 files changed

Lines changed: 40 additions & 29 deletions

File tree

‎src/targets/objc/helpers.js‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ module.exports = {
2323
* @return {string} A valid Objective-C declaration and initialization of an Objective-C object litteral.
2424
*
2525
* @example
26-
* nsDeclarationBuilder('NSDictionary', 'params', {a: 'b', c: 'd'}, true)
26+
* nsDeclaration('NSDictionary', 'params', {a: 'b', c: 'd'}, true)
2727
* // returns:
2828
* NSDictionary *params = @{ @"a": @"b",
2929
* @"c": @"d" };
3030
*
31-
* nsDeclarationBuilder('NSDictionary', 'params', {a: 'b', c: 'd'})
31+
* nsDeclaration('NSDictionary', 'params', {a: 'b', c: 'd'})
3232
* // returns:
3333
* NSDictionary *params = @{ @"a": @"b", @"c": @"d" };
3434
*/
35-
nsDeclarationBuilder: function (nsClass, name, parameters, indent) {
35+
nsDeclaration: function (nsClass, name, parameters, indent) {
3636
var opening = nsClass + ' *' + name + ' = '
3737
var literal = this.literalRepresentation(parameters, indent ? opening.length : undefined)
3838
return opening + literal + ';'
@@ -64,5 +64,39 @@ module.exports = {
6464
default:
6565
return '@"' + value.replace(/"/g, '\\"') + '"'
6666
}
67+
},
68+
69+
/**
70+
* By appending multipart parameters one by one in the resulting snippet,
71+
* we make it easier for the user to edit it according to his or her needs after pasting.
72+
* The user can just edit the parameters NSDictionary or put this part of a snippet in a multipart builder method.
73+
*/
74+
multipartBody: function (source, opts) {
75+
var code = []
76+
var indent = opts.indent
77+
78+
code.push(this.nsDeclaration('NSArray', 'parameters', source.postData.params, opts.pretty))
79+
code.push(util.format('NSString *boundary = @"%s";', source.postData.boundary))
80+
code.push(null)
81+
code.push('NSError *error;')
82+
code.push('NSMutableString *body = [NSMutableString string];')
83+
code.push('for (NSDictionary *param in parameters) {')
84+
code.push(indent + '[body appendFormat:@"--%@\\r\\n", boundary];')
85+
code.push(indent + 'if (param[@"fileName"]) {')
86+
code.push(indent + indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"; filename=\\"%@\\"\\r\\n", param[@"name"], param[@"fileName"]];')
87+
code.push(indent + indent + '[body appendFormat:@"Content-Type: %@\\r\\n\\r\\n", param[@"contentType"]];')
88+
code.push(indent + indent + '[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];')
89+
code.push(indent + indent + 'if (error) {')
90+
code.push(indent + indent + indent + 'NSLog(@"%@", error);')
91+
code.push(indent + indent + '}')
92+
code.push(indent + '} else {')
93+
code.push(indent + indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"\\r\\n\\r\\n", param[@"name"]];')
94+
code.push(indent + indent + '[body appendFormat:@"%@", param[@"value"]];')
95+
code.push(indent + '}')
96+
code.push('}')
97+
code.push('[body appendFormat:@"\\r\\n--%@--\\r\\n", boundary];')
98+
code.push('NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];')
99+
100+
return code.join('\n')
67101
}
68102
}

‎src/targets/objc/nsurlsession.js‎

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = function (source, options) {
3535
if (Object.keys(source.allHeaders).length) {
3636
req.hasHeaders = true
3737
code.push(null)
38-
code.push(objcHelpers.nsDeclarationBuilder('NSDictionary', 'headers', source.allHeaders, opts.pretty))
38+
code.push(objcHelpers.nsDeclaration('NSDictionary', 'headers', source.allHeaders, opts.pretty))
3939
}
4040

4141
if (source.postData.text || source.postData.jsonObj || source.postData.params) {
@@ -57,37 +57,14 @@ module.exports = function (source, options) {
5757

5858
case 'application/json':
5959
if (source.postData.jsonObj) {
60-
code.push(objcHelpers.nsDeclarationBuilder('NSDictionary', 'parameters', source.postData.jsonObj, opts.pretty))
60+
code.push(objcHelpers.nsDeclaration('NSDictionary', 'parameters', source.postData.jsonObj, opts.pretty))
6161
code.push(null)
6262
code.push('NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];')
6363
}
6464
break
6565

6666
case 'multipart/form-data':
67-
// By appending multipart parameters one by one in the resulting snippet,
68-
// we make it easier for the user to edit it according to his or her needs after pasting.
69-
// The user can just edit the parameters NSDictionary or put this part of a snippet in a multipart builder method.
70-
code.push(objcHelpers.nsDeclarationBuilder('NSArray', 'parameters', source.postData.params, opts.pretty))
71-
code.push(util.format('NSString *boundary = @"%s";', source.postData.boundary))
72-
code.push(null)
73-
code.push('NSError *error;')
74-
code.push('NSMutableString *body = [NSMutableString string];')
75-
code.push('for (NSDictionary *param in parameters) {')
76-
code.push(indent + '[body appendFormat:@"--%@\\r\\n", boundary];')
77-
code.push(indent + 'if (param[@"fileName"]) {')
78-
code.push(indent + indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"; filename=\\"%@\\"\\r\\n", param[@"name"], param[@"fileName"]];')
79-
code.push(indent + indent + '[body appendFormat:@"Content-Type: %@\\r\\n\\r\\n", param[@"contentType"]];')
80-
code.push(indent + indent + '[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];')
81-
code.push(indent + indent + 'if (error) {')
82-
code.push(indent + indent + indent + 'NSLog(@"%@", error);')
83-
code.push(indent + indent + '}')
84-
code.push(indent + '} else {')
85-
code.push(indent + indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"\\r\\n\\r\\n", param[@"name"]];')
86-
code.push(indent + indent + '[body appendFormat:@"%@", param[@"value"]];')
87-
code.push(indent + '}')
88-
code.push('}')
89-
code.push('[body appendFormat:@"\\r\\n--%@--\\r\\n", boundary];')
90-
code.push('NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];')
67+
code.push(objcHelpers.multipartBody(source, opts))
9168
break
9269

9370
default:

0 commit comments

Comments
 (0)