Skip to content

Commit 6eb8f0f

Browse files
committed
New API
* Add `before` and `after` middleware handlers * Move configuration to an `expose` method * Add tests * Rename to restful-keystone
1 parent c7a0e60 commit 6eb8f0f

32 files changed

+818
-229
lines changed

.jscsrc

+82-88
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,84 @@
11
{
2-
"disallowEmptyBlocks": true,
3-
"disallowKeywordsOnNewLine": ["else"],
4-
"disallowKeywords": ["with"],
5-
"disallowMixedSpacesAndTabs": true,
6-
"disallowMultipleLineBreaks": true,
7-
"disallowMultipleLineStrings": true,
8-
"disallowMultipleVarDecl": true,
9-
"disallowNewlineBeforeBlockStatements": true,
10-
"disallowOperatorBeforeLineBreak": [
11-
"+",
12-
"-"
13-
],
14-
"disallowPaddingNewlinesInBlocks": true,
15-
"disallowSpaceAfterPrefixUnaryOperators": true,
16-
"disallowSpaceBeforePostfixUnaryOperators": true,
17-
"disallowSpacesInAnonymousFunctionExpression": {
18-
"beforeOpeningRoundBrace": true,
19-
"beforeOpeningCurlyBrace": true
20-
},
21-
"disallowSpacesInCallExpression": true,
22-
"disallowSpacesInFunctionDeclaration": {
23-
"beforeOpeningRoundBrace": true,
24-
"beforeOpeningCurlyBrace": true
25-
},
26-
"disallowSpacesInFunctionExpression": {
27-
"beforeOpeningRoundBrace": true,
28-
"beforeOpeningCurlyBrace": true
29-
},
30-
"disallowSpacesInNamedFunctionExpression": {
31-
"beforeOpeningRoundBrace": true,
32-
"beforeOpeningCurlyBrace": true
33-
},
34-
"disallowTrailingComma": true,
35-
"disallowTrailingWhitespace": true,
36-
"maximumLineLength": {
37-
"value": 120,
38-
"allowComments": true,
39-
"allowUrlComments": true,
40-
"allowRegex": true
41-
},
42-
"requireAlignedObjectValues": "ignoreLineBreak",
43-
"requireBlocksOnNewline": true,
44-
"requireCamelCaseOrUpperCaseIdentifiers": true,
45-
"requireCapitalizedConstructors": true,
46-
"requireCurlyBraces": [
47-
"for",
48-
"while",
49-
"do",
50-
"try",
51-
"catch"
52-
],
53-
"requireDotNotation": true,
54-
"requireKeywordsOnNewLine": [],
55-
"requireLineFeedAtFileEnd": true,
56-
"requirePaddingNewLinesInObjects": true,
57-
"requireParenthesesAroundIIFE": true,
58-
"requireSpaceAfterBinaryOperators": true,
59-
"requireSpaceAfterKeywords": [
60-
"return",
61-
"case"
62-
],
63-
"requireSpaceAfterLineComment": true,
64-
"requireSpaceAfterObjectKeys": true,
65-
"requireSpaceBeforeBinaryOperators": true,
66-
"requireSpaceBeforeKeywords": true,
67-
"requireSpaceBeforeObjectValues": true,
68-
"requireSpaceBetweenArguments": true,
69-
"requireSpacesInConditionalExpression": true,
70-
"requireSpacesInsideArrayBrackets": "all",
71-
"requireSpacesInsideObjectBrackets": "all",
72-
"requireSpacesInsideParentheses": {
73-
"all": true
74-
},
75-
"requireYodaConditions": true,
76-
"validateIndentation": 2,
77-
"validateLineBreaks": "LF",
78-
"validateParameterSeparator": ", ",
79-
"validateQuoteMarks": {
80-
"mark": "\"",
81-
"escape": true
82-
},
83-
"fileExtensions": [
84-
".js",
85-
"jscs"
86-
],
87-
"excludeFiles": [
88-
"templates"
89-
]
2+
"disallowEmptyBlocks": true,
3+
"disallowKeywordsOnNewLine": ["else"],
4+
"disallowKeywords": ["with"],
5+
"disallowMixedSpacesAndTabs": true,
6+
"disallowMultipleLineBreaks": true,
7+
"disallowMultipleLineStrings": true,
8+
"disallowNewlineBeforeBlockStatements": true,
9+
"disallowOperatorBeforeLineBreak": [
10+
"?",
11+
":"
12+
],
13+
"disallowPaddingNewlinesInBlocks": true,
14+
"disallowSpaceAfterPrefixUnaryOperators": true,
15+
"disallowSpaceBeforePostfixUnaryOperators": true,
16+
"disallowSpacesInAnonymousFunctionExpression": {
17+
"beforeOpeningRoundBrace": true,
18+
"beforeOpeningCurlyBrace": true
19+
},
20+
"disallowSpacesInCallExpression": true,
21+
"disallowSpacesInFunctionDeclaration": {
22+
"beforeOpeningRoundBrace": true,
23+
"beforeOpeningCurlyBrace": true
24+
},
25+
"disallowSpacesInFunctionExpression": {
26+
"beforeOpeningRoundBrace": true,
27+
"beforeOpeningCurlyBrace": true
28+
},
29+
"disallowSpacesInNamedFunctionExpression": {
30+
"beforeOpeningRoundBrace": true,
31+
"beforeOpeningCurlyBrace": true
32+
},
33+
"disallowTrailingComma": true,
34+
"disallowTrailingWhitespace": true,
35+
"maximumLineLength": {
36+
"value": 120,
37+
"allowComments": true,
38+
"allowUrlComments": true,
39+
"allowRegex": true
40+
},
41+
"requireAlignedObjectValues": "ignoreLineBreak",
42+
"requireBlocksOnNewline": true,
43+
"requireCamelCaseOrUpperCaseIdentifiers": true,
44+
"requireCapitalizedConstructors": true,
45+
"requireCurlyBraces": [
46+
"for",
47+
"while",
48+
"do",
49+
"try",
50+
"catch"
51+
],
52+
"requireDotNotation": true,
53+
"requireKeywordsOnNewLine": [],
54+
"requireLineFeedAtFileEnd": true,
55+
"requirePaddingNewLinesInObjects": true,
56+
"requireParenthesesAroundIIFE": true,
57+
"requireSpaceAfterBinaryOperators": true,
58+
"requireSpaceAfterKeywords": [
59+
"return",
60+
"case"
61+
],
62+
"requireSpaceAfterObjectKeys": true,
63+
"requireSpaceBeforeBinaryOperators": true,
64+
"requireSpaceBeforeKeywords": true,
65+
"requireSpaceBeforeObjectValues": true,
66+
"requireSpaceBetweenArguments": true,
67+
"requireSpacesInConditionalExpression": true,
68+
"requireSpacesInsideArrayBrackets": "all",
69+
"requireSpacesInsideObjectBrackets": "all",
70+
"requireSpacesInsideParentheses": {
71+
"all": true
72+
},
73+
"requireYodaConditions": true,
74+
"validateIndentation": 2,
75+
"validateLineBreaks": "LF",
76+
"validateParameterSeparator": ", ",
77+
"fileExtensions": [
78+
".js",
79+
"jscs"
80+
],
81+
"excludeFiles": [
82+
"templates"
83+
]
9084
}

.jshintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"eqnull": true,
1212
"node": true,
1313
"laxbreak": true,
14-
"expr": true
14+
"expr": true,
15+
"laxcomma": true
1516
}

Gruntfile.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ module.exports = function( grunt ){
1111

1212
grunt.initConfig( configs );
1313

14-
grunt.registerTask( 'default', [ 'jshint', 'mochacli' ] );
14+
grunt.registerTask( 'lint', [ 'jshint', 'jscs' ] );
15+
grunt.registerTask( 'default', [ 'lint', 'mochacli' ] );
1516
};

README.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ It allows for very granular control of editable fields, population, filters and
1010
## Install
1111

1212
```sh
13-
$ npm install --save keystone-restful
13+
$ npm install --save restful-keystone
1414
```
1515

1616
## Usage
@@ -21,7 +21,7 @@ $ npm install --save keystone-restful
2121

2222
```js
2323
// file: routes/index.js
24-
var restful = require('keystone-restful');
24+
var restful = require('restful-keystone');
2525

2626
// ...
2727

@@ -72,7 +72,7 @@ Status: 200 OK
7272
"categories": [],
7373
"content": {
7474
"brief": "<p>This is a test</p>",
75-
"extended": "<p>To make sure keystone-restful is functioning correctly</p>"
75+
"extended": "<p>To make sure restful-keystone is functioning correctly</p>"
7676
},
7777
"state": "published"
7878
}
@@ -82,7 +82,7 @@ Status: 200 OK
8282

8383
### Configuration
8484

85-
A number of keystone-restful specific options can be passed to the module to configure how it functions:
85+
A number of restful-keystone specific options can be passed to the module to configure how it functions:
8686

8787
#### `root`
8888

@@ -160,7 +160,7 @@ Idem to `show`, except it configures which fields are editable. All fields passe
160160

161161
**{Boolean|String}** default: `"<%=name%>"`
162162

163-
Used for enveloping the results (which is definitely best practice, _especially_ when multiple resources are returned).
163+
Used for enveloping the results (which is definitely best practice, _especially_ when multiple resources are returned).
164164
**By default this will be the singular or plural version of the list name**, e.g. `"posts"` or `"post"`.
165165

166166
```sh
@@ -184,7 +184,7 @@ Status: 200 OK
184184
"categories": [],
185185
"content": {
186186
"brief": "<p>This is a test</p>",
187-
"extended": "<p>To make sure keystone-restful is functioning correctly</p>"
187+
"extended": "<p>To make sure restful-keystone is functioning correctly</p>"
188188
},
189189
"state": "published"
190190
}
@@ -234,7 +234,7 @@ Status: 200 OK
234234
"categories": [],
235235
"content": {
236236
"brief": "<p>This is a test</p>",
237-
"extended": "<p>To make sure keystone-restful is functioning correctly</p>"
237+
"extended": "<p>To make sure restful-keystone is functioning correctly</p>"
238238
},
239239
"state": "published"
240240
}
@@ -347,11 +347,11 @@ Status: 200 OK
347347

348348
All requests respond with a `200 OK` status if the request was succesful, except for `remove` requests which will return `204 No Content`.
349349

350-
When something goes wrong appropriate errors are thrown, however keystone-restful does not provide any error handling out of the box, i.e. you need to make sure you have some kind of error handling middleware in place.
350+
When something goes wrong appropriate errors are thrown, however restful-keystone does not provide any error handling out of the box, i.e. you need to make sure you have some kind of error handling middleware in place.
351351

352352
### Permissions
353353

354-
Keystone-restful does **NOT** provide any security checks, i.e. if you expose a resource it is available to anonymous requests !!
354+
restful-keystone does **NOT** provide any security checks, i.e. if you expose a resource it is available to anonymous requests !!
355355
You need to set up any restrictions you want to see applied to routes yourself. (Probably this will change in the near future)
356356

357357
## Roadmap
@@ -367,9 +367,9 @@ You need to set up any restrictions you want to see applied to routes yourself.
367367
MIT © [d-pac](http://www.d-pac.be)
368368

369369

370-
[npm-url]: https://npmjs.org/package/keystone-restful
371-
[npm-image]: https://badge.fury.io/js/keystone-restful.svg
372-
[travis-url]: https://travis-ci.org/d-pac/keystone-restful
373-
[travis-image]: https://travis-ci.org/d-pac/keystone-restful.svg?branch=master
374-
[daviddm-url]: https://david-dm.org/d-pac/keystone-restful.svg?theme=shields.io
375-
[daviddm-image]: https://david-dm.org/d-pac/keystone-restful
370+
[npm-url]: https://npmjs.org/package/restful-keystone
371+
[npm-image]: https://badge.fury.io/js/restful-keystone.svg
372+
[travis-url]: https://travis-ci.org/d-pac/restful-keystone
373+
[travis-image]: https://travis-ci.org/d-pac/restful-keystone.svg?branch=master
374+
[daviddm-url]: https://david-dm.org/d-pac/restful-keystone.svg?theme=shields.io
375+
[daviddm-image]: https://david-dm.org/d-pac/restful-keystone

index.js

+1-82
Original file line numberDiff line numberDiff line change
@@ -1,82 +1 @@
1-
'use strict';
2-
var debug = require( "debug" );
3-
var _ = require( "lodash" );
4-
var path = require( "path" );
5-
6-
function log( obj ){
7-
var fs = require( "fs" );
8-
var util = require( "util" );
9-
fs.writeFileSync( "keystone.txt", util.inspect( obj, {
10-
showHidden : true,
11-
depth : 1,
12-
colors : false
13-
} ) );
14-
}
15-
16-
function parseMixedValue( value,
17-
defaultValue ){
18-
if( _.isUndefined( value ) ){
19-
value = true;
20-
}
21-
22-
if( _.isArray( value ) ){
23-
value = value.join( " " );
24-
} else if( _.isBoolean( value ) ){
25-
value = (value)
26-
? defaultValue
27-
: "";
28-
}
29-
30-
if( !_.isString( value ) ){
31-
//todo: throw error?
32-
}
33-
return value
34-
}
35-
36-
function parseConfig( list,
37-
config ){
38-
39-
if(config){
40-
config = _.defaults( {}, config );
41-
var fields = _.keys( list.fields ).join( " " );
42-
config.methods = parseMixedValue( config.methods, "list retrieve remove create update" );
43-
config.show = parseMixedValue( config.show, fields );
44-
config.edit = parseMixedValue( config.edit, fields );
45-
config.populate = parseMixedValue( config.populate, "" );
46-
config.envelop = parseMixedValue( config.envelop, "<%=name%>" );
47-
48-
config.key = config.key || list.key;
49-
config.path = config.path || list.path;
50-
51-
return config;
52-
}
53-
54-
return {
55-
hidden: true
56-
};
57-
}
58-
59-
module.exports = function( keystone,
60-
config ){
61-
_.defaults( config, {
62-
root : "/api",
63-
resources : {}
64-
} );
65-
//todo: throw error if keystone not supplied or lists not available
66-
_.each( keystone.lists, function( list,
67-
key ){
68-
var restConfig = parseConfig( list, config.resources[ key ] );
69-
if( !restConfig.hidden ){
70-
var entry = path.join( config.root, restConfig.path );
71-
var methods = restConfig.methods.split( " " );
72-
_.each( methods, function( methodName ){
73-
var method = require( "./lib/methods/" + methodName );
74-
var route = method( list, restConfig, entry );
75-
keystone.app[ route.verb ]( route.url, route.handle );
76-
} );
77-
}
78-
} );
79-
return function(){
80-
81-
}
82-
};
1+
module.exports = require( './lib/restful' );

lib/constants.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
3+
var define = require( "node-constants" )( exports );
4+
5+
define( {
6+
METHODS_ALL : [ "retrieve", "list", "create", "update", "remove" ],
7+
SYMBOL_ALL : "*"
8+
} );

0 commit comments

Comments
 (0)