Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit bd6a71b

Browse files
committed
Rename restified to createrest
1 parent 73bc033 commit bd6a71b

5 files changed

Lines changed: 154 additions & 12 deletions

File tree

.eslintrc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"extends": "airbnb-base",
3+
"env": {
4+
"browser": true,
5+
"node": true,
6+
"es6": true
7+
},
8+
"parserOptions": {
9+
"ecmaVersion": 7,
10+
"ecmaFeatures": {
11+
"experimentalObjectRestSpread": true
12+
},
13+
"sourceType": "module"
14+
},
15+
"rules": {
16+
"quotes": [0, "single", {"avoidEscape": true}],
17+
"semi": [2, "never"],
18+
"no-unused-vars": ["error", { "vars": "all", "args": "after-used", "varsIgnorePattern": "PropTypes" }],
19+
"import/no-unresolved": [0, { "commonjs": true, "amd": true }],
20+
"global-require": 0,
21+
"quote-props": [2, "as-needed"],
22+
"max-len": [1, 120],
23+
"no-unused-expressions": [2, { "allowShortCircuit": true, "allowTernary": true }],
24+
"no-confusing-arrow": 0,
25+
"no-use-before-define": [2, { "functions": true }],
26+
"no-param-reassign": ["error", { "props": false }],
27+
"consistent-return": 0,
28+
"import/no-extraneous-dependencies": 0,
29+
"import/prefer-default-export": 0,
30+
"no-restricted-syntax": [2, "WithStatement"],
31+
"no-continue": 0,
32+
"babel/arrow-parens": 0,
33+
"generator-star-spacing": 0,
34+
"no-multiple-empty-lines": [1, { "max": 1 }],
35+
"no-mixed-operators": 0,
36+
"react/forbid-prop-types": 0,
37+
"class-methods-use-this": 0
38+
},
39+
"parser": "babel-eslint",
40+
"settings": {
41+
"import/resolve": {
42+
"moduleDirectory": ["node_modules", "src"]
43+
}
44+
}
45+
}

README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Restified
1+
# createrest
22

33
Declare your routes
44

@@ -11,7 +11,8 @@ Declare your routes
1111
## Usage example
1212

1313
```js
14-
import Restified from 'restified'
14+
import { createRest } from 'createrest'
15+
import createExpressMiddleware from 'createrest-express'
1516

1617
import {
1718
BooksController,
@@ -22,7 +23,7 @@ import {
2223
BookmarksController,
2324
} from './controllers'
2425

25-
const router = Restified()
26+
const router = createRest()
2627

2728

2829
router.root(r => {
@@ -78,9 +79,15 @@ router.root(r => {
7879
// post /profile/bookmarks/:id/share_link -> BookmarksController.ShareLinkController.#create
7980
// patch /profile/bookmarks/:id/share_link -> BookmarksController.ShareLinkController.#update
8081
// delete /profile/bookmarks/:id/share_link -> BookmarksController.ShareLinkController.#destroy
82+
83+
r.scope('admin', r => {
84+
85+
})
8186
})
8287

83-
export default router.expressMiddleware()
88+
89+
const middleware = createExpressMiddleware(router)
90+
export default middleware
8491

8592
// or for Koa
8693
// export default router.koaMiddleware()
@@ -91,7 +98,9 @@ export default router.expressMiddleware()
9198
## CLI
9299

93100
```bash
94-
restified # Show man page
95-
restified init # Create main files
96-
restified routes # Show routes
101+
rest # Show man page
102+
rest init # Create main files
103+
rest routes # Show routes
97104
```
105+
106+
or with alias: `createrest`

lib/index.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
class BaseRouter {
3+
constructor(parent) {
4+
this.parent = parent
5+
this.routes = {}
6+
}
7+
8+
createSimpleRoute(method, route, originOptions) {
9+
const options = Object.assign({}, {
10+
methodName: route,
11+
}, originOptions)
12+
this.router[method + ':' + router] = options
13+
}
14+
15+
get(route, options) {
16+
this.createSimpleRoute('get', route, options)
17+
}
18+
19+
post(route, options) {
20+
this.createSimpleRoute('post', route, options)
21+
}
22+
23+
put(route, options) {
24+
this.createSimpleRoute('put', route, options)
25+
26+
if (!options.noPatch) {
27+
this.patch(route, options)
28+
}
29+
}
30+
31+
patch(route, options) {
32+
this.createSimpleRoute('patch', route, options)
33+
}
34+
35+
delete(route, options) {
36+
this.createSimpleRoute('delete', route, options)
37+
}
38+
}
39+
40+
class RestifiedRouter {
41+
constructor() {
42+
this.router = new BaseRouter(this)
43+
}
44+
45+
root(callback) {
46+
callback(this.router)
47+
}
48+
49+
controller(controller) {
50+
this.controller = controller
51+
}
52+
53+
expressMidleware() {
54+
}
55+
56+
printRoutes(from, root = '/') {
57+
const origRouter = from || this.router
58+
Object.keys(origRouter.routes)
59+
.forEach(router => {
60+
const [method, route] = router.split(':')
61+
const options = origRouter.routes[router]
62+
63+
console.log(method.toUpperCase(), root + route, options)
64+
})
65+
}
66+
}
67+
68+
module.exports = function restified() {
69+
return new RestifiedRouter()
70+
}

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
{
2-
"name": "restified",
2+
"name": "createrest",
33
"version": "0.0.1-dev.0",
44
"description": "REST routes constructor for express and koa",
55
"main": "dist/index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"lint": "eslint ./lib"
89
},
910
"repository": {
1011
"type": "git",
11-
"url": "git+https://github.com/atomixinteractions/restified.git"
12+
"url": "git+https://github.com/atomixinteractions/createrest.git"
1213
},
1314
"author": "Sergey Sova <i.am@lestad.net> (https://lestad.top)",
1415
"license": "MIT",
1516
"bugs": {
16-
"url": "https://github.com/atomixinteractions/restified/issues"
17+
"url": "https://github.com/atomixinteractions/createrest/issues"
1718
},
18-
"homepage": "https://github.com/atomixinteractions/restified#readme",
19+
"homepage": "https://github.com/atomixinteractions/createrest#readme",
1920
"dependencies": {
2021
"pluralize": "^3.1.0"
22+
},
23+
"devDependencies": {
24+
"babel-eslint": "^7.1.1",
25+
"eslint": "^3.17.0",
26+
"eslint-config-airbnb-base": "^11.1.1",
27+
"eslint-plugin-import": "^2.2.0",
28+
"express": "^4.14.1"
2129
}
2230
}

test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const Restified = require('./lib')
2+
3+
const router = Restified()
4+
5+
6+
router.root(r => {
7+
8+
})
9+
10+
router.printRoutes()

0 commit comments

Comments
 (0)