From faad7a3a69e7247af603bf798e45f7f62b70a70b Mon Sep 17 00:00:00 2001 From: Luca Lanziani Date: Sat, 10 Dec 2011 15:23:15 +0100 Subject: [PATCH] first commit --- .gitignore | 4 + .monitor | 0 .npmignore | 4 + History.md | 6 ++ Makefile | 5 ++ Readme.md | 51 +++++++++++++ docs/index.md | 7 ++ index.js | 2 + lib/rext-connect.js | 176 ++++++++++++++++++++++++++++++++++++++++++++ package.json | 13 ++++ test/index.html | 65 ++++++++++++++++ test/server.js | 39 ++++++++++ 12 files changed, 372 insertions(+) create mode 100644 .gitignore create mode 100644 .monitor create mode 100644 .npmignore create mode 100644 History.md create mode 100644 Makefile create mode 100644 Readme.md create mode 100644 docs/index.md create mode 100644 index.js create mode 100644 lib/rext-connect.js create mode 100644 package.json create mode 100644 test/index.html create mode 100644 test/server.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2f741b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +node_modules +*.sock +test.js diff --git a/.monitor b/.monitor new file mode 100644 index 0000000..e69de29 diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..f1250e5 --- /dev/null +++ b/.npmignore @@ -0,0 +1,4 @@ +support +test +examples +*.sock diff --git a/History.md b/History.md new file mode 100644 index 0000000..1026087 --- /dev/null +++ b/History.md @@ -0,0 +1,6 @@ + +0.0.1 / 2011-12-10 +================== + + * Initial commit + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2275937 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ + +SRC = lib/rest-connect.js + +test-server: + @node test/server diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..317caae --- /dev/null +++ b/Readme.md @@ -0,0 +1,51 @@ + +# Rext-connect + + A simpler rext's connect router middleware + +![image alt](link) + +## How to use + + var connect = require('connect') + , rext_connect = require('rext-connect'); + + app.use(connect.bodyParser()); + app.use(connect.router(rext_connect.routes({"prefix":'myapp',"rext":rext}))); + + +## Running node tests + + Install dependencies: + + $ npm install -d + + Run them! + + $ make server-test + + +## License + +(The MIT License) + +Copyright (c) 2011 Grapily <dev@grapily.com> + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 0000000..6e8d807 --- /dev/null +++ b/docs/index.md @@ -0,0 +1,7 @@ +# DOCS + +## sec one + +### sec one.one + +## sec two \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..4d20955 --- /dev/null +++ b/index.js @@ -0,0 +1,2 @@ + +module.exports = require('./lib/rext-connect'); \ No newline at end of file diff --git a/lib/rext-connect.js b/lib/rext-connect.js new file mode 100644 index 0000000..0f3fb2c --- /dev/null +++ b/lib/rext-connect.js @@ -0,0 +1,176 @@ +/*! + * NAME + * Copyright (c) 2011 Grapily + * MIT Licensed + */ + +var rext_connect = module.exports = {}; + +/** + * Library version + */ +rext_connect.version = '0.0.1'; + +function echo(options, callback) { + callback(null, JSON.stringify(options)); +}; + +/** + * Initialize the module. + * + * @param {Object} options + * @param {String} [options.prefix="rext"] set the prefix of routes + * @param {Object} [options.rext] set the functions attached to routes + * @param {function} [options.rext.list] + * @param {function} [options.rext.create] + * @param {function} [options.rext.destroy] + * @param {function} [options.rext.retrieve] + * @param {function} [options.rext.update] + * @api public + */ + +rext_connect.routes = function (options) { + var options = options || {} + , prefix = options.prefix || 'rext' + , version = 1 + , pre = '/' + prefix + '/' + version + , rext = options.rext || {} + , list = rext.list || echo + , create = rext.create || echo + , destroy = rext.destroy || echo + , retrieve = rext.retrieve || echo + , update = rext.update || echo + ; + + return function (app) { + + /** + * Define "list" route. + * Call the list function and pass a options object to it + * @params {Object} req + * @params {Object} req.params + * @params {String} [req.params.module] the module name + */ + app.get(pre + '/resources/:module?', + function (req, res, next) { + var options = {}; + + options['name']= req.params.module; + + list(options, function (err, reply) { + if (err) res.end(err); + + res.writeHead(200, { + 'Content-Length': reply.length, + 'Content-Type': 'text/plain' } + ); + + res.end(reply); + }) + } + ); + + /** + * Define "retrieve" route. + * + */ + app.get(pre + '/resources/:module/:version', + function (req, res, next) { + var options = {}; + + options['name'] = req.params.module; + options['version'] = req.params.version; + + retrieve(options, function (err, reply) { + if (err) res.send(err); + + res.writeHead(200, { + 'Content-Length': reply.length, + 'Content-Type': 'text/plain' } + ); + + res.end(reply); + }); + } + ); + + /** + * Define "destroy" route. + * + */ + app.get(pre + '/resources/:module/:version/destroy', + function (req, res, next) { + var options = {}; + + options['name'] = req.params.module; + options['version'] = req.params.version; + + destroy(options, function (err, reply) { + if (err) res.send(err); + + res.writeHead(200, { + 'Content-Length': reply.length, + 'Content-Type': 'text/plain' } + ); + + res.end(reply); + }); + } + ); + + /** + * Define "create" route. + * + */ + app.post(pre + '/resources/:module/:version/create', + function (req, res, next) { + var options = {}; + + options['name'] = req.params.module; + options['version'] = req.params.version; + options['data'] = req.body.data; + + create(options, function (err, reply) { + if (err) res.send(err); + + res.writeHead(200, { + 'Content-Length': reply.length, + 'Content-Type': 'text/plain' + } + ); + + res.end(reply); + + }); + } + ); + + /** + * Define "update" route. + * + */ + app.post(pre + '/resources/:module/:version/update', + function (req, res, next) { + var options = {}; + + options['name'] = req.params.module; + options['version'] = req.params.version; + options['data'] = req.body.data; + + update(options, function (err, reply) { + if (err) res.send(err); + + res.writeHead(200, { + 'Content-Length': reply.length, + 'Content-Type': 'text/plain' } + ); + + res.end(reply); + }); + } + ); + + } +} + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..3121d4c --- /dev/null +++ b/package.json @@ -0,0 +1,13 @@ +{ + "name": "rext-connect" + , "version": "0.0.1" + , "description": "Add rext route to connect" + , "keywords": "Grapily " + , "author": [""] + , "dependencies": {} + , "devDependencies": { + "connect": "1.8.2" + } + , "main": "./index.js" + , "engines": { "node": ">= 0.4.0 < 0.7.0" } +} diff --git a/test/index.html b/test/index.html new file mode 100644 index 0000000..4814c6d --- /dev/null +++ b/test/index.html @@ -0,0 +1,65 @@ + + Test by hand + +
+

Default

+

app.use(connect.router(rext_connect.routes()))

+ +
+
+

Myapp

+

app.use(connect.router(rext_connect.routes({"prefix":prefix,"rext":rext})))

+ +
+ + \ No newline at end of file diff --git a/test/server.js b/test/server.js new file mode 100644 index 0000000..164ec15 --- /dev/null +++ b/test/server.js @@ -0,0 +1,39 @@ +var connect = require('../node_modules/connect/') + , rext_connect = require('../lib/rext-connect') + , app = connect() + , port = 3000 + , prefix = "myapp" + ; + + +var rext = {}; +rext.list = function (options, callback) { + callback(null, "myapp LIST "+JSON.stringify(options)); +}; + +rext.create = function (options, callback) { + callback(null, "myapp CREATE "+JSON.stringify(options)); +}; + +rext.update = function (options, callback) { + callback(null, "myapp UPDATE "+JSON.stringify(options)); +}; + +rext.destroy = function (options, callback) { + callback(null, "myapp DESTROY "+JSON.stringify(options)); +}; + +rext.retrieve = function (options, callback) { + callback(null, "myapp RETRIEVE "+JSON.stringify(options)); +}; + +app.use(connect.bodyParser()); +app.use(connect(connect.static(__dirname))); +app.use(connect.router(rext_connect.routes({"prefix":prefix,"rext":rext}))); +app.use(connect.router(rext_connect.routes())).listen(port); + +console.log('Server running at http://127.0.0.1:'+port+'/'); + + + +