insomnia/app/actions/requests.js

90 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-03-20 04:00:40 +00:00
import * as types from '../constants/actionTypes'
import * as methods from '../constants/global'
import {loadStart} from "./global";
import {loadStop} from "./global";
function defaultRequest () {
return {
id: null,
_mode: 'json',
created: 0,
modified: 0,
2016-03-20 23:20:00 +00:00
url: '',
2016-03-20 04:00:40 +00:00
name: '',
method: methods.METHOD_GET,
body: '',
params: [],
headers: [],
authentication: {}
}
}
/**
* Build a new request from a subset of fields
* @param request values to override defaults with
* @returns {*}
*/
function buildRequest (request) {
// Build the required fields
2016-03-20 20:42:27 +00:00
const id = request.id || `rq_${Date.now()}`;
2016-03-20 04:00:40 +00:00
const created = request.created || Date.now();
const modified = request.modified || Date.now();
// Create the request
2016-03-20 23:20:00 +00:00
return Object.assign(defaultRequest(), request, {
2016-03-20 04:00:40 +00:00
id, created, modified
});
}
export function addRequest (name = 'My Request') {
2016-03-22 05:01:58 +00:00
return (dispatch) => {
2016-03-20 04:00:40 +00:00
dispatch(loadStart());
const request = buildRequest({name});
2016-03-20 20:42:27 +00:00
dispatch({type: types.REQUEST_ADD, request});
2016-03-20 04:00:40 +00:00
2016-03-20 20:42:27 +00:00
return new Promise((resolve) => {
setTimeout(() => {
dispatch(loadStop());
resolve();
}, 500);
});
2016-03-20 04:00:40 +00:00
};
}
export function updateRequest (requestPatch) {
2016-03-20 23:20:00 +00:00
if (!requestPatch.id) {
throw new Error('Cannot update request without id');
}
2016-03-20 04:00:40 +00:00
return (dispatch) => {
dispatch(loadStart());
2016-03-20 23:20:00 +00:00
const modified = Date.now();
const patch = Object.assign({}, requestPatch, {modified});
dispatch({type: types.REQUEST_UPDATE, patch});
2016-03-20 20:42:27 +00:00
return new Promise((resolve) => {
setTimeout(() => {
dispatch(loadStop());
resolve();
2016-03-20 23:20:00 +00:00
}, 800);
2016-03-20 20:42:27 +00:00
});
2016-03-20 04:00:40 +00:00
};
}
2016-03-20 04:47:43 +00:00
2016-03-20 23:20:00 +00:00
export function updateRequestUrl (id, url) {
return updateRequest({id, url});
}
export function updateRequestBody (id, body) {
return updateRequest({id, body});
}
2016-03-20 23:27:46 +00:00
export function updateRequestMethod (id, method) {
return updateRequest({id, method});
}
2016-03-20 23:20:00 +00:00
export function activateRequest (id) {
return {type: types.REQUEST_ACTIVATE, id};
2016-03-20 04:47:43 +00:00
}