From bf6ea0168ea1840e3fc8768e7a1ebe8dbd19f482 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 12 Mar 2020 02:30:42 +0200 Subject: [PATCH 1/6] Update nsurlsession.js --- src/targets/swift/nsurlsession.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/targets/swift/nsurlsession.js b/src/targets/swift/nsurlsession.js index 2c42a5a9d..ede1b2464 100644 --- a/src/targets/swift/nsurlsession.js +++ b/src/targets/swift/nsurlsession.js @@ -30,6 +30,7 @@ module.exports = function (source, options) { // We just want to make sure people understand that is the only dependency code.push('import Foundation') + code.push('//Just testing that custom npm package works') if (Object.keys(source.allHeaders).length) { req.hasHeaders = true From 464101afcab046ad2f3543f8cf7e047a05c8e5c0 Mon Sep 17 00:00:00 2001 From: Michalis Karagiorgos Date: Sat, 14 Mar 2020 16:24:07 +0200 Subject: [PATCH 2/6] add required custom swift template path --- src/targets/swift/index.js | 3 +- src/targets/swift/nsurlsession.js | 90 +++++++++++++++---------------- 2 files changed, 47 insertions(+), 46 deletions(-) diff --git a/src/targets/swift/index.js b/src/targets/swift/index.js index fba4dfea2..9e6733bf1 100644 --- a/src/targets/swift/index.js +++ b/src/targets/swift/index.js @@ -8,5 +8,6 @@ module.exports = { default: 'nsurlsession' }, - nsurlsession: require('./nsurlsession') + nsurlsession: require('./nsurlsession'), + custom: require('../../../../custom/custom_swift') } diff --git a/src/targets/swift/nsurlsession.js b/src/targets/swift/nsurlsession.js index ede1b2464..d2b01c039 100644 --- a/src/targets/swift/nsurlsession.js +++ b/src/targets/swift/nsurlsession.js @@ -35,7 +35,7 @@ module.exports = function (source, options) { if (Object.keys(source.allHeaders).length) { req.hasHeaders = true code.blank() - .push(helpers.literalDeclaration('headers', source.allHeaders, opts)) + .push(helpers.literalDeclaration('headers', source.allHeaders, opts)) } if (source.postData.text || source.postData.jsonObj || source.postData.params) { @@ -47,7 +47,7 @@ module.exports = function (source, options) { // we make it easier for the user to edit it according to his or her needs after pasting. // The user can just add/remove lines adding/removing body parameters. code.blank() - .push('let postData = NSMutableData(data: "%s=%s".data(using: String.Encoding.utf8)!)', source.postData.params[0].name, source.postData.params[0].value) + .push('let postData = NSMutableData(data: "%s=%s".data(using: String.Encoding.utf8)!)', source.postData.params[0].name, source.postData.params[0].value) for (var i = 1, len = source.postData.params.length; i < len; i++) { code.push('postData.append("&%s=%s".data(using: String.Encoding.utf8)!)', source.postData.params[i].name, source.postData.params[i].value) } @@ -56,8 +56,8 @@ module.exports = function (source, options) { case 'application/json': if (source.postData.jsonObj) { code.push(helpers.literalDeclaration('parameters', source.postData.jsonObj, opts), 'as [String : Any]') - .blank() - .push('let postData = JSONSerialization.data(withJSONObject: parameters, options: [])') + .blank() + .push('let postData = JSONSerialization.data(withJSONObject: parameters, options: [])') } break @@ -68,42 +68,42 @@ module.exports = function (source, options) { * The user can just edit the parameters NSDictionary or put this part of a snippet in a multipart builder method. */ code.push(helpers.literalDeclaration('parameters', source.postData.params, opts)) - .blank() - .push('let boundary = "%s"', source.postData.boundary) - .blank() - .push('var body = ""') - .push('var error: NSError? = nil') - .push('for param in parameters {') - .push(1, 'let paramName = param["name"]!') - .push(1, 'body += "--\\(boundary)\\r\\n"') - .push(1, 'body += "Content-Disposition:form-data; name=\\"\\(paramName)\\""') - .push(1, 'if let filename = param["fileName"] {') - .push(2, 'let contentType = param["content-type"]!') - .push(2, 'let fileContent = String(contentsOfFile: filename, encoding: String.Encoding.utf8)') - .push(2, 'if (error != nil) {') - .push(3, 'print(error)') - .push(2, '}') - .push(2, 'body += "; filename=\\"\\(filename)\\"\\r\\n"') - .push(2, 'body += "Content-Type: \\(contentType)\\r\\n\\r\\n"') - .push(2, 'body += fileContent') - .push(1, '} else if let paramValue = param["value"] {') - .push(2, 'body += "\\r\\n\\r\\n\\(paramValue)"') - .push(1, '}') - .push('}') + .blank() + .push('let boundary = "%s"', source.postData.boundary) + .blank() + .push('var body = ""') + .push('var error: NSError? = nil') + .push('for param in parameters {') + .push(1, 'let paramName = param["name"]!') + .push(1, 'body += "--\\(boundary)\\r\\n"') + .push(1, 'body += "Content-Disposition:form-data; name=\\"\\(paramName)\\""') + .push(1, 'if let filename = param["fileName"] {') + .push(2, 'let contentType = param["content-type"]!') + .push(2, 'let fileContent = String(contentsOfFile: filename, encoding: String.Encoding.utf8)') + .push(2, 'if (error != nil) {') + .push(3, 'print(error)') + .push(2, '}') + .push(2, 'body += "; filename=\\"\\(filename)\\"\\r\\n"') + .push(2, 'body += "Content-Type: \\(contentType)\\r\\n\\r\\n"') + .push(2, 'body += fileContent') + .push(1, '} else if let paramValue = param["value"] {') + .push(2, 'body += "\\r\\n\\r\\n\\(paramValue)"') + .push(1, '}') + .push('}') break default: code.blank() - .push('let postData = NSData(data: "%s".data(using: String.Encoding.utf8)!)', source.postData.text) + .push('let postData = NSData(data: "%s".data(using: String.Encoding.utf8)!)', source.postData.text) } } code.blank() - // NSURLRequestUseProtocolCachePolicy is the default policy, let's just always set it to avoid confusion. - .push('let request = NSMutableURLRequest(url: NSURL(string: "%s")! as URL,', source.fullUrl) - .push(' cachePolicy: .useProtocolCachePolicy,') - .push(' timeoutInterval: %s)', parseInt(opts.timeout, 10).toFixed(1)) - .push('request.httpMethod = "%s"', source.method) + // NSURLRequestUseProtocolCachePolicy is the default policy, let's just always set it to avoid confusion. + .push('let request = NSMutableURLRequest(url: NSURL(string: "%s")! as URL,', source.fullUrl) + .push(' cachePolicy: .useProtocolCachePolicy,') + .push(' timeoutInterval: %s)', parseInt(opts.timeout, 10).toFixed(1)) + .push('request.httpMethod = "%s"', source.method) if (req.hasHeaders) { code.push('request.allHTTPHeaderFields = headers') @@ -114,19 +114,19 @@ module.exports = function (source, options) { } code.blank() - // Retrieving the shared session will be less verbose than creating a new one. - .push('let session = URLSession.shared') - .push('let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in') - .push(1, 'if (error != nil) {') - .push(2, 'print(error)') - .push(1, '} else {') - // Casting the NSURLResponse to NSHTTPURLResponse so the user can see the status . - .push(2, 'let httpResponse = response as? HTTPURLResponse') - .push(2, 'print(httpResponse)') - .push(1, '}') - .push('})') - .blank() - .push('dataTask.resume()') + // Retrieving the shared session will be less verbose than creating a new one. + .push('let session = URLSession.shared') + .push('let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in') + .push(1, 'if (error != nil) {') + .push(2, 'print(error)') + .push(1, '} else {') + // Casting the NSURLResponse to NSHTTPURLResponse so the user can see the status . + .push(2, 'let httpResponse = response as? HTTPURLResponse') + .push(2, 'print(httpResponse)') + .push(1, '}') + .push('})') + .blank() + .push('dataTask.resume()') return code.join() } From 3e3a5aceb45794f02ef40a8c99d348b5b1a52208 Mon Sep 17 00:00:00 2001 From: Michalis Karagiorgos Date: Sat, 14 Mar 2020 16:37:22 +0200 Subject: [PATCH 3/6] fix custom_swift path --- src/targets/swift/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/targets/swift/index.js b/src/targets/swift/index.js index 9e6733bf1..98f960199 100644 --- a/src/targets/swift/index.js +++ b/src/targets/swift/index.js @@ -9,5 +9,5 @@ module.exports = { }, nsurlsession: require('./nsurlsession'), - custom: require('../../../../custom/custom_swift') + custom: require('../../../../../custom/custom_swift') } From e13d4ac4424a54fa3039fa298d6da06e186472f5 Mon Sep 17 00:00:00 2001 From: Michalis Karagiorgos Date: Sat, 14 Mar 2020 16:50:27 +0200 Subject: [PATCH 4/6] add comment property inside request for naming --- src/index.js | 5 +++-- src/targets/swift/nsurlsession.js | 1 - 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index c50e35347..37a0694f4 100644 --- a/src/index.js +++ b/src/index.js @@ -45,12 +45,12 @@ var HTTPSnippet = function (data) { throw err } - self.requests.push(self.prepare(entry.request)) + self.requests.push(self.prepare(entry.request, entry.comment)) }) }) } -HTTPSnippet.prototype.prepare = function (request) { +HTTPSnippet.prototype.prepare = function (request, comment) { // construct utility properties request.queryObj = {} request.headersObj = {} @@ -58,6 +58,7 @@ HTTPSnippet.prototype.prepare = function (request) { request.allHeaders = {} request.postData.jsonObj = false request.postData.paramsObj = false + request.comment = comment; // construct query objects if (request.queryString && request.queryString.length) { diff --git a/src/targets/swift/nsurlsession.js b/src/targets/swift/nsurlsession.js index d2b01c039..7fe3ef9f4 100644 --- a/src/targets/swift/nsurlsession.js +++ b/src/targets/swift/nsurlsession.js @@ -30,7 +30,6 @@ module.exports = function (source, options) { // We just want to make sure people understand that is the only dependency code.push('import Foundation') - code.push('//Just testing that custom npm package works') if (Object.keys(source.allHeaders).length) { req.hasHeaders = true From a098770a2711d4b8e1223ce904f84f6e66f41b73 Mon Sep 17 00:00:00 2001 From: Michalis Karagiorgos Date: Sat, 14 Mar 2020 17:29:15 +0200 Subject: [PATCH 5/6] add custom_kotlin require --- src/targets/index.js | 1 + src/targets/kotlin/index.js | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 src/targets/kotlin/index.js diff --git a/src/targets/index.js b/src/targets/index.js index 68f83a6b8..dd90b4874 100644 --- a/src/targets/index.js +++ b/src/targets/index.js @@ -8,6 +8,7 @@ module.exports = { http: require('./http'), java: require('./java'), javascript: require('./javascript'), + kotlin: require('./kotlin'), node: require('./node'), objc: require('./objc'), ocaml: require('./ocaml'), diff --git a/src/targets/kotlin/index.js b/src/targets/kotlin/index.js new file mode 100644 index 000000000..3d1c1128d --- /dev/null +++ b/src/targets/kotlin/index.js @@ -0,0 +1,10 @@ +module.exports = { + info: { + key: 'kotlin', + title: 'kotlin', + extname: '.kt', + default: 'custom' + }, + + custom: require('../../../../../custom/custom_kotlin') +} \ No newline at end of file From a71e9be17c6ff0dfac0a560beb2fb0f91a907c81 Mon Sep 17 00:00:00 2001 From: Michalis Karagiorgos Date: Sun, 15 Mar 2020 22:39:47 +0200 Subject: [PATCH 6/6] add response as property inside request --- src/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 37a0694f4..69df7df39 100644 --- a/src/index.js +++ b/src/index.js @@ -45,12 +45,12 @@ var HTTPSnippet = function (data) { throw err } - self.requests.push(self.prepare(entry.request, entry.comment)) + self.requests.push(self.prepare(entry.request, entry.comment, entry.response)) }) }) } -HTTPSnippet.prototype.prepare = function (request, comment) { +HTTPSnippet.prototype.prepare = function (request, comment, response) { // construct utility properties request.queryObj = {} request.headersObj = {} @@ -59,6 +59,7 @@ HTTPSnippet.prototype.prepare = function (request, comment) { request.postData.jsonObj = false request.postData.paramsObj = false request.comment = comment; + request.response = response; // construct query objects if (request.queryString && request.queryString.length) {