diff --git a/README.md b/README.md index bcaa396..ab78e65 100644 --- a/README.md +++ b/README.md @@ -83,28 +83,44 @@ Then, any request that reaches the server and matches one of the directories and Dafuq translates any parameter on the request to command line flags with double dash `--`. The request fields searched for parmeters and its override order is as follows (upper means it will prevail in case of name clashing): * Multipart files * Multipart form fields -* Body fields +* Body fields: Can be URL encoded or JSON. Multilevel object structures will be flattened full path key * URL params: (parts of the url that start with `:`) * Query Params * Headers: (the ones starting with `X-Arg-`) ``` -POST /hello/john?age=12&male HTTP/1.1 -Content-Type: application/x-www-form-urlencoded +POST /hello/john?age=25&male HTTP/1.1 +Content-Type: application/json Content-Length: 100 X-Arg-username: jhon78 -surname=wick&profession=killer +{ + "surname": "wick", + "profession": "killer", + "confirmedKills": [ + "target1", + "target2" + ], + "ammo": { + "usp": 12 + "ump": 25 + } +} + ``` would be equivalent to the following command ``` ./hello/\:name/get.sh \ - --name john \ - --surname wick \ - --username username \ - --age 12 \ - --male \ - --profession killer + --name "john" \ + --surname "wick" \ + --username "jhon78" \ + --age "25" \ + --male \ + --profession "killer" \ + --confirmedKills.0 "target1" \ + --confirmedKills.1 "target2" \ + --ammo.usp "12" \ + --ammo.ump "25" ``` ### Recieving Files diff --git a/package.json b/package.json index 8ce7d95..2b47c43 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "express": "^4.14.0", "glob": "^7.0.5", "multer": "^1.2.0", + "traverse": "^0.6.6", "yargs": "^5.0.0" }, "devDependencies": { diff --git a/src/index.js b/src/index.js index f82b3b9..df5eb3c 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,7 @@ const fs = require('fs') , child_process = require('child_process') , debug = require('debug') , glob = require('glob') + , traverse = require('traverse') , express = require('express') , multer = require('multer') , bodyParser = require('body-parser') @@ -131,12 +132,19 @@ function buildCommandFlags(req) { return files }, {}) - const flags = Object.assign({}, headerFlags, req.query, req.params, req.body, uploadFlags) + // Flatten the body + const body = traverse(req.body).reduce(function(b, val) { + if (this.notRoot && this.isLeaf) + b[this.path.join('.')] = val + return b + }, {}) + + const flags = Object.assign({}, headerFlags, req.query, req.params, body, uploadFlags) Object.keys(flags).forEach(function(flagName) { let flagValue = flags[flagName] cmdFlags += ` --${flagName}` - if (flagValue) { - flagValue = flagValue + if (flagValue !== undefined) { + flagValue = String(flagValue) .replace(/\\/g, '\\\\') .replace(/"/g, '\\"') cmdFlags += ` "${flagValue}"` diff --git a/test/commands/json/post.js b/test/commands/json/post.js new file mode 100755 index 0000000..86babd6 --- /dev/null +++ b/test/commands/json/post.js @@ -0,0 +1,10 @@ +#!/usr/bin/env node +var traverse = require('traverse') +var argv = require('yargs') + .argv; + +traverse(argv).forEach(function(x) { + if (this.notRoot && this.isLeaf && [ '_', '$0' ].indexOf(this.path[0]) < 0) { + console.log(this.path.join('.') + ':' + x) + } +}) diff --git a/test/test.js b/test/test.js index f1fd5c1..735c0d2 100644 --- a/test/test.js +++ b/test/test.js @@ -618,4 +618,35 @@ describe('Arguments', () => { .expect(res => res.body.result.should.be.equal('Hello Sarah \\"Connor\\"')) .end(done) }) + + it('should pass complex json as flattened paths', (done) => { + const body = { + names: [ + 'Sarah', + 'Arnold' + ], + surnames: [ + 'Connor', + 'Swacheneger' + ], + films: { + 'terminator': 9 + } + } + const expectedBody = [ + 'names.0:Sarah', + 'names.1:Arnold', + 'surnames.0:Connor', + 'surnames.1:Swacheneger', + 'films.terminator:9' + ].join('\n') + + request(app) + .post('/json') + .send(body) + .expect(200) + .expect('Content-Type', /json/) + .expect(res => res.body.result.should.be.equal(expectedBody)) + .end(done) + }) })