-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_parameterization.js
57 lines (54 loc) · 1.81 KB
/
path_parameterization.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
var qs = require('qs');
var url = require('url');
module.exports = function(handle) {
handle('request', function(env, next) {
var _req = env.request;
var urlObj = url.parse(_req.url, true);
var urlParts = urlObj.pathname.split('/');
var [resource, convoId, subresource] = urlParts.splice(3);
if (resource == 'conversations' && convoId) {
if (subresource) resource = subresource;
urlParts.push(resource);
urlObj.pathname = urlParts.join('/');
if (convoId) {
var convoParam = { conversation_id: convoId };
console.log(_req.method);
switch (_req.method) {
case 'GET':
case 'HEAD':
var newQuery = Object.assign(urlObj.query, convoParam);
urlObj.search = qs.stringify(newQuery);
env.argo.currentUrl = urlObj.format();
console.log(env.argo.currentUrl);
next(env);
break;
default:
var body = [];
var _req = env.request;
_req.on('data', function(chunk) {
body.push(chunk);
}).on('end', function() {
if (body.length > 0) {
body = Buffer.concat(body).toString();
} else {
body = '{}';
};
body = JSON.parse(body);
body = Object.assign(body, convoParam);
body = JSON.stringify(body);
env.request.body = body;
env.request.headers['Content-Length'] = Buffer.byteLength(body);
env.argo.currentUrl = urlObj.format();
console.log(body);
console.log(env.argo.currentUrl);
next(env)
});
};
};
} else {
// POST /conversations
console.log(env.argo.currentUrl);
next(env);
};
});
};