1 | "use strict";
|
2 |
|
3 | var lodash = require("lodash"),
|
4 | statePassthrough = require("./statePassthrough");
|
5 |
|
6 | function getData(item, method) {
|
7 | var data;
|
8 | if (method === "POST") {
|
9 | data = item.post;
|
10 | delete item.post;
|
11 | } else if (method === "PUT") {
|
12 | data = item.put;
|
13 | delete item.put;
|
14 | } else if (method === "PATCH") {
|
15 | data = item.patch;
|
16 | delete item.patch;
|
17 | } else if (method === "HEAD") {
|
18 | data = item.head;
|
19 | delete item.head;
|
20 | }
|
21 | return data || item.data;
|
22 | }
|
23 |
|
24 | function getMethod(item) {
|
25 | if (item.post) {
|
26 | return "POST";
|
27 | }
|
28 | if (item.put) {
|
29 | return "PUT";
|
30 | }
|
31 | if (item.patch) {
|
32 | return "PATCH";
|
33 | }
|
34 | if (item.head) {
|
35 | return "HEAD";
|
36 | }
|
37 | if (item.get) {
|
38 | return "GET";
|
39 | }
|
40 | return item.method ? item.method.toString().toUpperCase() : "GET";
|
41 | }
|
42 |
|
43 | function getPath(item) {
|
44 | var path = item.path;
|
45 | if (!path) {
|
46 | path = "";
|
47 | }
|
48 | if (item.get) {
|
49 | var queryIndex = path.indexOf("?");
|
50 | if (queryIndex !== -1) {
|
51 | path = path.substr(0, queryIndex);
|
52 | }
|
53 | path += "?" + item.get;
|
54 | }
|
55 | delete item.get;
|
56 | return path;
|
57 | }
|
58 |
|
59 | function prepareItem(item, defaults) {
|
60 | item = lodash.defaults(item, defaults);
|
61 | item.method = getMethod(item);
|
62 | item.data = getData(item, item.method);
|
63 | item.path = getPath(item);
|
64 | item.before = item.before || statePassthrough;
|
65 | item.after = item.after || statePassthrough;
|
66 | return item;
|
67 | }
|
68 |
|
69 | module.exports = prepareItem; |
\ | No newline at end of file |