sketch-fetch-complete
Advanced tools
Comparing version 0.0.2 to 0.0.4
156
lib/index.js
@@ -6,3 +6,3 @@ /* globals NSJSONSerialization NSJSONWritingPrettyPrinted NSDictionary NSHTTPURLResponse NSString NSASCIIStringEncoding NSUTF8StringEncoding coscript NSURL NSMutableURLRequest NSMutableData NSURLConnection */ | ||
function response (httpResponse, data) { | ||
function response(httpResponse, data) { | ||
var keys = [] | ||
@@ -14,3 +14,6 @@ var all = [] | ||
for (var i = 0; i < httpResponse.allHeaderFields().allKeys().length; i++) { | ||
var key = httpResponse.allHeaderFields().allKeys()[i].toLowerCase() | ||
var key = httpResponse | ||
.allHeaderFields() | ||
.allKeys() | ||
[i].toLowerCase() | ||
var value = String(httpResponse.allHeaderFields()[key]) | ||
@@ -20,7 +23,7 @@ keys.push(key) | ||
header = headers[key] | ||
headers[key] = header ? (header + ',' + value) : value | ||
headers[key] = header ? header + ',' + value : value | ||
} | ||
return { | ||
ok: (httpResponse.statusCode() / 200 | 0) == 1, // 200-399 | ||
ok: ((httpResponse.statusCode() / 200) | 0) == 1, // 200-399 | ||
status: httpResponse.statusCode(), | ||
@@ -30,4 +33,4 @@ statusText: NSHTTPURLResponse.localizedStringForStatusCode(httpResponse.statusCode()), | ||
clone: response.bind(this, httpResponse, data), | ||
text: function () { | ||
return new Promise(function (resolve, reject) { | ||
text: function() { | ||
return new Promise(function(resolve, reject) { | ||
const str = NSString.alloc().initWithData_encoding(data, NSASCIIStringEncoding) | ||
@@ -41,4 +44,4 @@ if (str) { | ||
}, | ||
json: function () { | ||
return new Promise(function (resolve, reject) { | ||
json: function() { | ||
return new Promise(function(resolve, reject) { | ||
var str = NSString.alloc().initWithData_encoding(data, NSUTF8StringEncoding) | ||
@@ -54,18 +57,32 @@ if (str) { | ||
}, | ||
blob: function () { | ||
blob: function() { | ||
return Promise.resolve(data) | ||
}, | ||
headers: { | ||
keys: function () { return keys }, | ||
entries: function () { return all }, | ||
get: function (n) { return headers[n.toLowerCase()] }, | ||
has: function (n) { return n.toLowerCase() in headers } | ||
} | ||
keys: function() { | ||
return keys | ||
}, | ||
entries: function() { | ||
return all | ||
}, | ||
get: function(n) { | ||
return headers[n.toLowerCase()] | ||
}, | ||
has: function(n) { | ||
return n.toLowerCase() in headers | ||
}, | ||
}, | ||
} | ||
} | ||
function toUTF8(data) { | ||
return NSString.alloc() | ||
.initWithString(data) | ||
.dataUsingEncoding(NSUTF8StringEncoding) | ||
} | ||
// We create one ObjC class for ourselves here | ||
var DelegateClass | ||
function fetch (urlString, options) { | ||
function fetch(urlString, options) { | ||
options = options || {} | ||
@@ -78,3 +95,3 @@ var fiber | ||
} | ||
return new Promise(function (resolve, reject) { | ||
return new Promise(function(resolve, reject) { | ||
var url = NSURL.alloc().initWithString(urlString) | ||
@@ -84,42 +101,57 @@ var request = NSMutableURLRequest.requestWithURL(url) | ||
var boundary = '' | ||
Object.keys(options.headers || {}).forEach(function(i) { | ||
request.setValue_forHTTPHeaderField(options.headers[i], i) | ||
}) | ||
if (options.formdata) { | ||
boundary = '--WebKitFormBoundaryDAolWtOraDBpelWB' | ||
request.setValue_forHTTPHeaderField(`multipart/form-data; boundary=${boundary}`, 'Content-Type') | ||
} else { | ||
Object.keys(options.headers || {}).forEach(function (i) { | ||
request.setValue_forHTTPHeaderField(options.headers[i], i) | ||
var boundary = '--WebKitFormBoundary' + String(NSUUID.UUID().UUIDString()) | ||
var endBoundary = boundary + '--' | ||
request.setValue_forHTTPHeaderField( | ||
'multipart/form-data; boundary=' + boundary, | ||
'Content-Type', | ||
) | ||
var body = NSMutableData.data() | ||
var newLine = toUTF8('\r\n') | ||
Object.keys(options.formdata).forEach(key => { | ||
if (key !== 'files') { | ||
body.appendData(toUTF8('--' + boundary)) | ||
body.appendData(newLine) | ||
body.appendData(toUTF8('Content-Disposition: form-data; name="' + key + '"')) | ||
body.appendData(newLine) | ||
body.appendData(newLine) | ||
body.appendData(toUTF8(options.formdata[key])) | ||
body.appendData(newLine) | ||
} | ||
}) | ||
} | ||
if (options.formdata) { | ||
/* | ||
{name: [value, filename]} | ||
*/ | ||
const data = NSData.dataWithContentsOfFile(options.formdata) | ||
const filename = options.formdata.split('/').pop() | ||
const newLine = '\r\n' | ||
function toUTF8(data) { | ||
const str = NSString.alloc().initWithString(data) | ||
return str.dataUsingEncoding(NSUTF8StringEncoding) | ||
} | ||
const body = NSMutableData.data() | ||
if (Array.isArray(options.formdata.files)) { | ||
options.formdata.files.forEach(file => { | ||
var fileData = NSData.dataWithContentsOfFile(file) | ||
body.appendData(toUTF8(`--${boundary}`)) | ||
body.appendData(toUTF8('--' + boundary)) | ||
body.appendData(newLine) | ||
body.appendData(toUTF8(newLine)) | ||
body.appendData(toUTF8(`Content-Disposition: form-data;name="file";filename="${filename}"`)) | ||
body.appendData( | ||
toUTF8( | ||
'Content-Disposition: form-data; name="file"; filename="' + | ||
file.split('/').pop() + | ||
'"', | ||
), | ||
) | ||
body.appendData(newLine) | ||
body.appendData(toUTF8(newLine)) | ||
body.appendData(toUTF8(`Content-Type: application/octet-stream`)) | ||
body.appendData(toUTF8('Content-Type: application/octet-stream')) | ||
body.appendData(newLine) | ||
body.appendData(newLine) | ||
body.appendData(toUTF8(newLine)) | ||
body.appendData(toUTF8(newLine)) | ||
body.appendData(data) | ||
body.appendData(toUTF8(newLine)) | ||
body.appendData(fileData) | ||
body.appendData(newLine) | ||
}) | ||
} | ||
body.appendData(toUTF8(`--${boundary}--`)) | ||
body.appendData(toUTF8(newLine)) | ||
body.appendData(toUTF8('--' + endBoundary)) | ||
request.setValue_forHTTPHeaderField('' + body.length(), 'Content-Length') | ||
@@ -134,3 +166,7 @@ request.setHTTPBody(body) | ||
var error | ||
data = NSJSONSerialization.dataWithJSONObject_options_error(options.body, NSJSONWritingPrettyPrinted, error) | ||
data = NSJSONSerialization.dataWithJSONObject_options_error( | ||
options.body, | ||
NSJSONWritingPrettyPrinted, | ||
error, | ||
) | ||
if (error != null) { | ||
@@ -154,3 +190,3 @@ return reject(error) | ||
'connectionDidFinishLoading:': function (connection) { | ||
'connectionDidFinishLoading:': function(connection) { | ||
finished = true | ||
@@ -164,7 +200,7 @@ this.callbacks.resolve(response(this.httpResponse, this.data)) | ||
}, | ||
'connection:didReceiveResponse:': function (connection, httpResponse) { | ||
'connection:didReceiveResponse:': function(connection, httpResponse) { | ||
this.httpResponse = httpResponse | ||
this.data = NSMutableData.alloc().init() | ||
}, | ||
'connection:didFailWithError:': function (connection, error) { | ||
'connection:didFailWithError:': function(connection, error) { | ||
finished = true | ||
@@ -178,5 +214,5 @@ this.callbacks.reject(error) | ||
}, | ||
'connection:didReceiveData:': function (connection, data) { | ||
'connection:didReceiveData:': function(connection, data) { | ||
this.data.appendData(data) | ||
} | ||
}, | ||
}) | ||
@@ -188,13 +224,10 @@ } | ||
resolve: resolve, | ||
reject: reject | ||
reject: reject, | ||
}) | ||
connectionDelegate.fiber = fiber; | ||
connectionDelegate.fiber = fiber | ||
var connection = NSURLConnection.alloc().initWithRequest_delegate( | ||
request, | ||
connectionDelegate | ||
) | ||
var connection = NSURLConnection.alloc().initWithRequest_delegate(request, connectionDelegate) | ||
if (fiber) { | ||
fiber.onCleanup(function () { | ||
fiber.onCleanup(function() { | ||
if (!finished) { | ||
@@ -205,3 +238,2 @@ connection.cancel() | ||
} | ||
}) | ||
@@ -208,0 +240,0 @@ } |
{ | ||
"name": "sketch-fetch-complete", | ||
"version": "0.0.2", | ||
"version": "0.0.4", | ||
"description": "A fetch for sketch", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
9418
219