insomnia/app/actions/requests.js

72 lines
1.6 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,
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
return Object.assign({}, defaultRequest(), request, {
id, created, modified
});
}
export function addRequest (name = 'My Request') {
return (dispatch) => {
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) {
return (dispatch) => {
dispatch(loadStart());
2016-03-20 20:42:27 +00:00
const request = Object.assign({}, requestPatch, {modified: Date.now()});
dispatch({type: types.REQUEST_UPDATE, request});
return new Promise((resolve) => {
setTimeout(() => {
dispatch(loadStop());
resolve();
}, 500);
});
2016-03-20 04:00:40 +00:00
};
}
2016-03-20 04:47:43 +00:00
export function activateRequest (request) {
return {type: types.REQUEST_ACTIVATE, request: request};
}