Skip to content

Commit

Permalink
Merge pull request #35 from smollweide/develop
Browse files Browse the repository at this point in the history
v0.15.0
  • Loading branch information
smollweide authored Jun 10, 2017
2 parents d69e781 + 1f7f4ef commit c546a13
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 3 deletions.
56 changes: 56 additions & 0 deletions lib/controller/ResponseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var Utils = require('../Utils');
var util = require('util');
var extend = util._extend;
var open = require('open');
var log = require('chip')();
var AppControllerSingleton = require('./AppController');
var appController = AppControllerSingleton.getInstance();
Expand Down Expand Up @@ -37,6 +38,8 @@ ResponseController.prototype = extend(ResponseController.prototype, {
appController.app.post('/service/expected-response', this._serviceWriteExpectedResponse.bind(this));
// add get also to make it linkable
appController.app.get('/service/expected-response', this._serviceWriteExpectedResponseLinkable.bind(this));
appController.app.post('/service/response/:path/:method', this._serviceWriteNewResponse.bind(this));
appController.app.delete('/service/response/:path/:method', this._serviceDeleteResponse.bind(this));
appController.app.get('/view/response', this._viewResponse.bind(this));
},

Expand Down Expand Up @@ -75,6 +78,59 @@ ResponseController.prototype = extend(ResponseController.prototype, {
res.end();
},

/**
* @method _serviceDeleteResponse
* @param {object} req
* @param {object} res
* @private
*/
_serviceDeleteResponse: function (req, res) {

var path = decodeURIComponent(req.params.path);
var method = decodeURIComponent(req.params.method).toUpperCase();
var name = req.body.name;
var filePath = path + '/' + method + '/mock/' + name + '.json';

res.send({});

if (this.existFile(filePath)) {
this.removeFile(filePath);
if (process.env.NODE_ENV !== 'test') {
log.info('Deleted response file: ' + filePath);
}
}

res.end();
},

/**
* @method _serviceWriteNewResponse
* @param {object} req
* @param {object} res
* @private
*/
_serviceWriteNewResponse: function (req, res) {

var path = decodeURIComponent(req.params.path);
var method = decodeURIComponent(req.params.method).toUpperCase();
var name = req.body.name;
var filePath = path + '/' + method + '/mock/' + name + '.json';

res.send({});

if (!this.existFile(filePath)) {
this.writeFile(filePath, '{}');
if (process.env.NODE_ENV !== 'test') {
log.info('Created new response file: ' + filePath);
}

}

open(filePath);

res.end();
},

/**
* @method _serviceWriteExpectedResponse
* @param {object} req
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-mock-server",
"version": "0.14.0",
"version": "0.15.0",
"description": "File based Node REST API mock server",
"email": "[email protected]",
"author": "Simon Mollweide <[email protected]>",
Expand Down
76 changes: 75 additions & 1 deletion src/app/js/script.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@

/* global window, document, hljs, JSONFormatter, jQuery */

/* eslint no-alert: 0*/
((($) => {
$(document).ready(function () {

function init() {
var $modal = $(window.location.hash);
if ($modal.length < 1) {
return;
}
$modal.eq(0).modal('show');
}

init();

$('.modal')
.on('shown.bs.modal', function (event) {
var $modal = $(event.currentTarget);
window.location.hash = $modal.attr('id');
})
.on('hide.bs.modal', function () {
window.location.hash = '';
});

$('.page-header pre code, .top-resource-description pre code').each(function (i, block) {
hljs.highlightBlock(block);
});
Expand Down Expand Up @@ -285,5 +304,60 @@
});
});

$('.js-add-response').on('click', function (event) {
event.preventDefault();

var $btn = $(event.currentTarget);
var name = window.prompt('Enter response name');
var path = encodeURIComponent($btn.data('path'));
var method = encodeURIComponent($btn.data('method'));

if (name === null || name === '') {
return;
}

$.ajax({
url: '/service/response/' + path + '/' + method,
type: 'post',
data: {
name,
},
success: function () {
window.location.reload();
},
error: function () {
window.alert('Error: please try again later!');
},
});
});

$('.js-delete-response').on('click', function (event) {
event.preventDefault();

var $btn = $(event.currentTarget);
var path = encodeURIComponent($btn.data('path'));
var method = encodeURIComponent($btn.data('method'));
var name = $btn.data('name');
var accepted = window.confirm('Are you sure?');

if (accepted !== true) {
return;
}

$.ajax({
url: '/service/response/' + path + '/' + method,
type: 'delete',
data: {
name,
},
success: function () {
window.location.reload();
},
error: function () {
window.alert('Error: please try again later!');
},
});
});

});
})(jQuery));
23 changes: 22 additions & 1 deletion views/tab-pane-mock.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,20 @@
<%=mock.name%>
</label>
<% if (!isMiddleware) { %>
<a class="spacer-left btn btn-default pull-right btn-xs js-open-ide" data-path="<%=method.path%>/mock/<%=mock.name%>.json">Open file</a>
<a
class="spacer-left btn btn-danger pull-right btn-xs js-delete-response"
data-path="<%=method.basePath%>"
data-method="<%=method.id%>"
data-name="<%=mock.name%>"
>
delete
</a>
<a
class="spacer-left btn btn-default pull-right btn-xs js-open-ide"
data-path="<%=method.path%>/mock/<%=mock.name%>.json"
>
open
</a>
<% } %>
<% if (!isError && !isMiddleware) { %>
&nbsp;<a target="_blank" href="<%=urlBase%>/view/response?path=<%=encodeURIComponent(method.basePath);%>&method=<%=method.id%>&expected=<%=mock.name%>">preview</a>
Expand All @@ -56,5 +69,13 @@
</div>
<% } %>
<button
class="btn btn-primary btn-xs js-add-response"
href="#"
data-path="<%=method.basePath%>"
data-method="<%=method.id%>"
>
+ add
</button>
</div>
</div>

0 comments on commit c546a13

Please sign in to comment.