diff --git a/lib/rest-client-view.coffee b/lib/rest-client-view.coffee index c49ea9f..8ffc67a 100644 --- a/lib/rest-client-view.coffee +++ b/lib/rest-client-view.coffee @@ -1,6 +1,7 @@ {$, ScrollView} = require 'atom-space-pen-views' {Emitter} = require 'atom' querystring = require 'querystring' +mime = require 'mime-types' RestClientResponse = require './rest-client-response' RestClientEditor = require './rest-client-editor' @@ -125,17 +126,18 @@ class RestClientView extends ScrollView @a class: "#{rest_form.result_headers_link.split('.')[1]}", 'Headers' @span ' | ' @span class: "#{rest_form.status.split('.')[1]}" + @span class: "text-info lnk #{rest_form.open_in_editor.split('.')[1]}", 'Open in separate editor' @span class: "#{rest_form.loading.split('.')[1]} loading loading-spinner-small inline-block", style: 'display: none;' @pre class: "#{rest_form.result_headers.split('.')[1]}", "" @pre class: "#{rest_form.result.split('.')[1]}", "#{RestClientResponse.DEFAULT_RESPONSE}" - @div class: "text-info lnk #{rest_form.open_in_editor.split('.')[1]}", 'Open in separate editor' initialize: -> @COLLECTIONS_PATH = "#{PACKAGE_PATH}/collections.json" @RECENT_REQUESTS_PATH = "#{PACKAGE_PATH}/recent.json" @lastRequest = null + @lastResponse = null @recentRequests = new RestClientPersist(@RECENT_REQUESTS_PATH) @recentRequests.setRequestFileLimit(RECENT_REQUESTS_FILE_LIMIT) @@ -194,10 +196,13 @@ class RestClientView extends ScrollView openInEditor: -> textResult = $(rest_form.result).text() - file_name = "#{current_method} - #{$(rest_form.url).val()}" - editor = new RestClientEditor(textResult, file_name) + editor = new RestClientEditor(textResult, @constructFileName()) editor.open() + constructFileName: -> + extension = mime.extension(@lastResponse.headers['content-type']) + "#{@lastRequest.method} #{@lastRequest.url}#{'.' + extension if extension}" + encodePayload: -> $(rest_form.payload).val( RestClientHttp.encodePayload($(rest_form.payload).val()) @@ -267,6 +272,7 @@ class RestClientView extends ScrollView body: @getRequestBody() onResponse: (error, response, body) => + @setLastResponse(response) if !error statusMessage = response.statusCode + " " + response.statusMessage @@ -386,6 +392,9 @@ class RestClientView extends ScrollView setLastRequest: (request) => @lastRequest = request + setLastResponse: (response) => + @lastResponse = response + getHeadersAsString: (headers) -> output = '' diff --git a/package.json b/package.json index 79f54a3..d3a49d2 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "dependencies": { "atom-space-pen-views": "^2.0.3", + "mime-types": "^2.1.12", "request": "*" } } diff --git a/spec/rest-client-view-spec.coffee b/spec/rest-client-view-spec.coffee index 86ca5a7..b12976b 100644 --- a/spec/rest-client-view-spec.coffee +++ b/spec/rest-client-view-spec.coffee @@ -9,3 +9,9 @@ describe "RestClientView test", -> describe "View", -> it "the view is loaded", -> expect(restClient.find('.rest-client-send')).toExist() + + describe "constructFileName", -> + it "contains the url, the method and an extension inferred from the response content-type", -> + restClient.setLastRequest { url: "http://example.com", method: "GET" } + restClient.setLastResponse { headers: { "content-type": "application/json" } } + expect(restClient.constructFileName()).toEqual "GET http://example.com.json" diff --git a/styles/rest-client.less b/styles/rest-client.less index f7cb4db..b2e7799 100644 --- a/styles/rest-client.less +++ b/styles/rest-client.less @@ -30,6 +30,10 @@ float: right; width: 50%; + .rest-client-open-in-editor { + float: right; + } + @media only screen and (min-width: 800px) { padding-top: 0; }