Skip to content

Commit

Permalink
Document-related endpoints in their own JSON config file
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinND committed Jun 10, 2014
1 parent 2c58e1e commit 66f387e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
1 change: 1 addition & 0 deletions config/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
defaultDescriptor: require('../config/json/default-descriptor.json'),
apiDescriptors: require('../config/json/api-descriptors.json'),
aliases: require('../config/json/aliases.json'),
documentRelatedEndpoints: require('../config/json/document-related-endpoints.json'),

apiHost: 'https://api.anyfetch.com'
};
6 changes: 6 additions & 0 deletions config/json/document-related-endpoints.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"getSimilar": { "endpoint": "/documents/{id}/similar" },
"getRelated": { "endpoint": "/documents/{id}/related" },
"getRaw": { "endpoint": "/documents/{id}/raw" },
"getFile": { "endpoint": "/documents/{id}/file" }
}
61 changes: 29 additions & 32 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,40 +157,43 @@ var generateAdvancedMappings = function(mappingFunctions) {

// Two-step call syntax
// getDocumentById(123).getRelated(cb)
return {
// TODO: do not hardcode these endpoints
getSimilar: generateMapping({ endpoint: '/documents/'+ id +'/similar' }, 'getSimilar'),
getRelated: generateMapping({ endpoint: '/documents/'+ id +'/related' }, 'getRelated'),
getRaw: generateMapping({ endpoint: '/documents/'+ id +'/raw' }, 'getRaw'),
getFile: generateMapping({ endpoint: '/documents/'+ id +'/file' }, 'getFile'),
var secondStep = {};
for (var name in configuration.documentRelatedEndpoints) {
var opt = {
endpoint: configuration.documentRelatedEndpoints[name].endpoint.replace('{id}', id)
};
secondStep[name] = generateMapping(opt, name);
}

/**
* Add a file to a previously uploaded document
*
* @param {Object} Must at least contain a `file` key, which can either be a stream (e.g. `fs.createReadStream`) or a Buffer object. Can also contains a `contentType` key (for MIME type), and a `filename`.
* @param {Function} cb Callback with error if any.
* @Warning: unfortunately, due to the variety of Stream, we can't type-check, so unexpected errors will occur if you specify weird file parameters.
*/
postFile: function(config, cb) {
if(typeof(config) === 'function') {
config = config();
}
if (!config.file) {
return cb(new Error('The config parameter must contain a `file` key'));
}

var endpoint = '/documents/' + id + '/file';
var options = {
filename: config.filename || '',
contentType: config.contentType || ''
};

anyfetchRequest.post(endpoint)
.set('Authorization', 'Bearer ' + accessToken)
.attach('file', config.file, options)
.expect(204)
.end(cb);
secondStep.postFile = function(config, cb) {
if(typeof(config) === 'function') {
config = config();
}
if (!config.file) {
return cb(new Error('The config parameter must contain a `file` key'));
}

var endpoint = '/documents/' + id + '/file';
var options = {
filename: config.filename || '',
contentType: config.contentType || ''
};

anyfetchRequest.post(endpoint)
.set('Authorization', 'Bearer ' + accessToken)
.attach('file', config.file, options)
.expect(204)
.end(cb);
};

return secondStep;
};

return mappingFunctions;
Expand All @@ -203,10 +206,4 @@ var generateAllMappings = function() {
return mappings;
};

var maps = generateAllMappings();

maps.getStatus(function(e, r) {
console.log(r.body);
});

module.exports = generateAllMappings();
module.exports = generateAllMappings();

0 comments on commit 66f387e

Please sign in to comment.