Permalink
Browse files

Switch to hapi 0.6 route config format

  • Loading branch information...
hueniverse committed Aug 30, 2012
1 parent 4b8e590 commit 50f3088e61236293c2a9c57de32214f7545eff71
Showing with 2,260 additions and 2,065 deletions.
  1. +108 −108 api/batch.js
  2. +98 −86 api/details.js
  3. +19 −9 api/email.js
  4. +107 −99 api/invite.js
  5. +86 −74 api/last.js
  6. +486 −455 api/project.js
  7. +57 −56 api/routes.js
  8. +19 −13 api/session.js
  9. +121 −115 api/storage.js
  10. +63 −57 api/stream.js
  11. +62 −59 api/suggestions.js
  12. +238 −211 api/task.js
  13. +794 −716 api/user.js
  14. +2 −7 web/views/console.jade
View
@@ -16,200 +16,200 @@ var Config = require('./config');
var internals = {};
-// Type definition
-
-exports.type = {
+// Batch processing
- get: { type: 'string', array: true, required: true }
-};
+exports.post = {
+ schema: {
-// Batch processing
+ get: { type: 'string', array: true, required: true }
+ },
+
+ handler: function (request) {
-exports.post = function (request) {
+ var requests = [];
+ var results = [];
+ var resultsMap = {};
- var requests = [];
- var results = [];
- var resultsMap = {};
+ function entry() {
- function entry() {
+ var requestRegex = /(?:\/)(?:\$(\d)+\.)?([\w:\.]+)/g; // /project/$1.project/tasks, does not allow using array responses
- var requestRegex = /(?:\/)(?:\$(\d)+\.)?([\w:\.]+)/g; // /project/$1.project/tasks, does not allow using array responses
+ // Validate requests
- // Validate requests
+ var error = null;
+ var parseRequest = function ($0, $1, $2) {
- var error = null;
- var parseRequest = function ($0, $1, $2) {
+ if ($1) {
- if ($1) {
+ if ($1 < i) {
- if ($1 < i) {
+ if ($1.indexOf(':') === -1) {
- if ($1.indexOf(':') === -1) {
+ parts.push({ type: 'ref', index: $1, value: $2 });
+ return '';
+ }
+ else {
- parts.push({ type: 'ref', index: $1, value: $2 });
- return '';
+ error = 'Request reference includes invalid ":" character (' + i + ')';
+ return $0;
+ }
}
else {
- error = 'Request reference includes invalid ":" character (' + i + ')';
+ error = 'Request reference is beyond array size (' + i + ')';
return $0;
}
}
else {
- error = 'Request reference is beyond array size (' + i + ')';
- return $0;
+ parts.push({ type: 'text', value: $2 });
+ return '';
}
- }
- else {
+ };
- parts.push({ type: 'text', value: $2 });
- return '';
- }
- };
+ for (var i = 0, il = request.payload.get.length; i < il; ++i) {
+
+ // Break into parts
- for (var i = 0, il = request.payload.get.length; i < il; ++i) {
+ var parts = [];
+ var result = request.payload.get[i].replace(requestRegex, parseRequest);
- // Break into parts
+ // Make sure entire string was processed (empty)
- var parts = [];
- var result = request.payload.get[i].replace(requestRegex, parseRequest);
+ if (result === '') {
+
+ requests.push(parts);
+ }
+ else {
- // Make sure entire string was processed (empty)
+ error = error || 'Invalid request format (' + i + ')';
+ break;
+ }
+ }
- if (result === '') {
+ if (error === null) {
- requests.push(parts);
+ process();
}
else {
- error = error || 'Invalid request format (' + i + ')';
- break;
+ request.reply(Hapi.Error.badRequest(error));
}
}
- if (error === null) {
-
- process();
- }
- else {
+ function process() {
- request.reply(Hapi.Error.badRequest(error));
- }
- }
+ batch(0, function () {
- function process() {
+ // Return results
- batch(0, function () {
+ request.reply(results);
+ });
+ }
- // Return results
+ function batch(pos, callback) {
- request.reply(results);
- });
- }
+ if (pos >= requests.length) {
- function batch(pos, callback) {
+ callback();
+ }
+ else {
- if (pos >= requests.length) {
+ // Prepare request
- callback();
- }
- else {
-
- // Prepare request
+ var parts = requests[pos];
+ var path = '';
+ var error = null;
- var parts = requests[pos];
- var path = '';
- var error = null;
+ for (var i = 0, il = parts.length; i < il; ++i) {
- for (var i = 0, il = parts.length; i < il; ++i) {
+ path += '/';
- path += '/';
+ if (parts[i].type === 'ref') {
- if (parts[i].type === 'ref') {
+ var ref = resultsMap[parts[i].index];
+ if (ref) {
- var ref = resultsMap[parts[i].index];
- if (ref) {
+ var value = null;
- var value = null;
+ try {
- try {
+ eval('value = ref.' + parts[i].value + ';');
+ }
+ catch (e) {
- eval('value = ref.' + parts[i].value + ';');
- }
- catch (e) {
+ error = e.message;
+ }
- error = e.message;
- }
+ if (value) {
- if (value) {
+ if (value.match(/^[\w:]+$/)) {
- if (value.match(/^[\w:]+$/)) {
+ path += value;
+ }
+ else {
- path += value;
+ error = 'Reference value includes illegal characters';
+ break;
+ }
}
else {
- error = 'Reference value includes illegal characters';
+ error = error || 'Reference not found';
break;
}
}
else {
- error = error || 'Reference not found';
+ error = 'Missing reference response';
break;
}
}
else {
- error = 'Missing reference response';
- break;
+ path += parts[i].value;
}
}
- else {
- path += parts[i].value;
- }
- }
-
- if (error === null) {
+ if (error === null) {
- // Make request
+ // Make request
- internals.call('GET', path, null, request.session, function (data, err) {
+ internals.call('GET', path, null, request.session, function (data, err) {
- if (err === null) {
+ if (err === null) {
- // Process response
+ // Process response
- results.push(data);
- resultsMap[pos] = data;
- }
- else {
+ results.push(data);
+ resultsMap[pos] = data;
+ }
+ else {
- results.push(err);
- }
+ results.push(err);
+ }
- // Call next
+ // Call next
- batch(pos + 1, callback);
- });
- }
- else {
+ batch(pos + 1, callback);
+ });
+ }
+ else {
- // Set error response (as string)
+ // Set error response (as string)
- results.push(error);
+ results.push(error);
- // Call next
+ // Call next
- batch(pos + 1, callback);
+ batch(pos + 1, callback);
+ }
}
}
- }
- entry();
+ entry();
+ }
};
Oops, something went wrong.

0 comments on commit 50f3088

Please sign in to comment.