├── .gitignore ├── LICENSE ├── README.md ├── exampleApi.raml ├── package.json └── ramlServer.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | 29 | .DS_Store 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Franco Arolfo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RAML Server [![](https://badge.fury.io/js/raml-server.svg)](http://badge.fury.io/js/raml-server) 2 | Get a full fake REST API with **zero coding** by writing a [RAML](http://raml.org/) spec. 3 | 4 | **Why building a new RAML based server?** Cause this one works with [JSON-Server](https://github.com/typicode/json-server), so no much code to do (maintain). 5 | 6 | This is an open source project, so issues, PRs and suggestions are wellcome. 7 | 8 | ### Install 9 | 10 | RAML Server is tested under node v0.12. If you don't have node installed yet, doing it via [nvm](https://github.com/creationix/nvm) is recommended. 11 | 12 | If you have node installed, run 13 | 14 | ``` 15 | $ npm install -g raml-server 16 | ``` 17 | 18 | ### Usage 19 | 20 | Start a server based on the ```exampleApi.raml``` file 21 | 22 | ``` 23 | $ raml-server exampleApi.raml 24 | ``` 25 | 26 | And hit ```localhost:3000/songs```. Also, later POSTs to /songs on the server will be saved! 27 | 28 | ### How it works? 29 | 30 | This is done by generating responses' data reading the JSON schemas with the [RAML Mocker](https://github.com/RePoChO/raml-mocker), and run with [JSON-Server](https://github.com/typicode/json-server). 31 | 32 | ### Next release fixes 33 | 34 | * Support _imports_ in RAML. 35 | * Support nested resources (today it only routes the JSON schema of the top level resources, like /songs). 36 | * RAML 1.0. 37 | * Start a server based on the ```api.raml``` file that is on the current directory if no argument is provided. 38 | 39 | ### As seen at 40 | 41 | * http://www.programmableweb.com/news/standalone-raml-api-mocking-tools-surface/2015/08/13 42 | -------------------------------------------------------------------------------- /exampleApi.raml: -------------------------------------------------------------------------------- 1 | #%RAML 0.8 2 | 3 | title: World Music API 4 | baseUri: http://example.api.com/ 5 | version: v1 6 | 7 | /songs: 8 | get: 9 | queryParameters: 10 | genre: 11 | description: filter the song by genre 12 | responses: 13 | 200: 14 | body: 15 | application/json: 16 | schema: | 17 | { "$schema": "http://json-schema.org/schema", 18 | "type": "array", 19 | "items" : { 20 | "type" : "object", 21 | "description": "A canonical song", 22 | "properties": { 23 | "title": { "type": "string" }, 24 | "artist": { "type": "string"} 25 | }, 26 | "required": [ "title", "artist" ] 27 | } 28 | } 29 | post: 30 | /{songId}: 31 | get: 32 | responses: 33 | 200: 34 | body: 35 | application/json: 36 | schema: | 37 | { "$schema": "http://json-schema.org/schema", 38 | "type": "object", 39 | "description": "A canonical song", 40 | "properties": { 41 | "title": { "type": "string" }, 42 | "artist": { "type": "string"} 43 | }, 44 | "required": [ "title", "artist" ] 45 | } 46 | delete: 47 | description: | 48 | This method will *delete* an **individual song** 49 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "raml-server", 3 | "description": "Run a server based on RAML files .. zero coding", 4 | "version": "0.1.6", 5 | "author": { 6 | "name": "Franco Arolfo", 7 | "email": "francoarolfo@hotmail.com" 8 | }, 9 | "bugs": { 10 | "url": "https://github.com/farolfo/raml-server/issues" 11 | }, 12 | "dependencies": { 13 | "json-server": "^0.7.11", 14 | "raml-mocker": "^0.2.6", 15 | "underscore": "^1.8.3", 16 | "yargs" : "^4.7.1" 17 | }, 18 | "devDependencies": {}, 19 | "homepage": "https://github.com/farolfo/raml-server", 20 | "license": "MIT", 21 | "bin": { 22 | "raml-server": "ramlServer.js" 23 | }, 24 | "repository": { 25 | "type": "git", 26 | "url": "git://github.com/farolfo/raml-server.git" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /ramlServer.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | // RAML Server 0.1.1 3 | 4 | // (c) 2015 Franco Arolfo 5 | // RAML Server may be freely distributed under the MIT license. 6 | // For all details and documentation: 7 | // https://github.com/farolfo/raml-server 8 | 9 | var ramlMocker = require('raml-mocker'), 10 | _ = require('underscore'), 11 | jsonServer = require('json-server'), 12 | argv = require('yargs').argv;; 13 | 14 | var options = { 15 | path: argv.path || '.' 16 | }, 17 | mocksAmount = 10, 18 | port = process.env.PORT || 3000, 19 | server, 20 | router; 21 | 22 | function reqToDBEntry(request) { 23 | var resource = request.uri.substr(1), 24 | obj = {}; 25 | 26 | var response = {}; 27 | if ( request.example != undefined && request.example() != undefined ) { 28 | response = JSON.parse( request.example() ); 29 | } else if ( request.mock != undefined ) { 30 | response = request.mock(); 31 | } else { 32 | response = {}; 33 | } 34 | 35 | obj[resource] = response; 36 | return obj; 37 | } 38 | 39 | function asList(func, n) { 40 | if (n <= 0) { 41 | return []; 42 | } 43 | 44 | var ans = []; 45 | ans.push(func()); 46 | return ans.concat(asList(func, n-1)); 47 | } 48 | 49 | function generateServer(db) { 50 | server = jsonServer.create(); 51 | router = jsonServer.router(db); 52 | 53 | server.use(jsonServer.defaults); 54 | server.use(router); 55 | 56 | console.log('Running RAML server on localhost:' + port + '...'); 57 | server.listen(port); 58 | } 59 | 60 | ramlMocker.generate(options, function (requestsToMock) { 61 | generateServer(_.reduce(_.map(requestsToMock, reqToDBEntry), _.extend, {})); 62 | }); 63 | 64 | process.on('SIGTERM', function() { 65 | process.exit(); 66 | }); 67 | --------------------------------------------------------------------------------