/
patch.js
82 lines (54 loc) · 2.13 KB
/
patch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
module.exports = {
friendlyName: 'PATCH',
description: 'Send a PATCH request and receive the response.',
extendedDescription: require('../constants/http-shortcuts.extended-description'),
inputs: {
url: require('../constants/url.input'),
data: require('../constants/data.input'),
headers: require('../constants/headers.input'),
baseUrl: require('../constants/base-url.input')
},
exits: {
success: require('../constants/http-shortcuts-success.exit'),
non200Response: require('../constants/non-200-response.exit'),
requestFailed: require('../constants/request-failed.exit')
},
fn: function (inputs,exits) {
// Require this pack
var Http = require('../');
// Send the HTTP request.
Http.sendHttpRequest({
method: 'PATCH',
url: inputs.url,
baseUrl: inputs.baseUrl,
body: inputs.data,
headers: inputs.headers
}).switch({
error: function (err) { return exits.error(err); },
requestFailed: function (err) { return exits.requestFailed(err); },
non200Response: function (serverRes) { return exits.non200Response(serverRes); },
success: function (serverRes) {
// Now that the server responded successfully, we need to check out
// the response body and, if relevant, attempt to parse data from it.
// If there is no response body (i.e. `body` is `""`),
// then we'll interpret that as `null` and return that as
// our response data.
if (serverRes.body === '') {
return exits.success(null);
}
// --•
// Otherwise, attempt to parse the response body as JSON.
var resData;
try {
resData = JSON.parse(serverRes.body);
} catch (unusedErr) {
// If the raw response body string cannot be parsed as JSON,
// then interpret it as a string and just use the raw body.
resData = serverRes.body;
}
// And finally, return the response data through the `success` exit.
return exits.success(resData);
}//</on success :: Http.sendHttpRequest()>
});//</Http.sendHttpRequest()>
}
};