Skip to content

Commit d14899d

Browse files
authored
Merge pull request #230 from digitalsadhu/code_formatting_and_linting
Code formatting and linting
2 parents 12f513c + 2696414 commit d14899d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3233
-2242
lines changed

lib/deserialize.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ module.exports = function (app, options) {
1616
return next()
1717
}
1818

19-
var regexs = [
20-
/^create$/,
21-
/^updateAttributes$/,
22-
/^patchAttributes$/
23-
]
19+
var regexs = [/^create$/, /^updateAttributes$/, /^patchAttributes$/]
2420

2521
var matches = regexs.filter(function (regex) {
2622
return regex.test(ctx.method.name)
@@ -30,7 +26,7 @@ module.exports = function (app, options) {
3026
* Handle include relationship requests (aka sideloading)
3127
*/
3228
if (RelUtils.isRequestingIncludes(ctx)) {
33-
ctx.res.set({'Content-Type': 'application/vnd.api+json'})
29+
ctx.res.set({ 'Content-Type': 'application/vnd.api+json' })
3430

3531
ctx.req.isSideloadingRelationships = true
3632

@@ -55,7 +51,7 @@ module.exports = function (app, options) {
5551
options.debug('Set Content-Type to `application/vnd.api+json`')
5652

5753
// set the JSON API Content-Type response header
58-
ctx.res.set({'Content-Type': 'application/vnd.api+json'})
54+
ctx.res.set({ 'Content-Type': 'application/vnd.api+json' })
5955

6056
options.model = utils.getModelFromContext(ctx, app)
6157
options.method = ctx.method.name
@@ -98,7 +94,10 @@ module.exports = function (app, options) {
9894
options.debug('======================')
9995
options.debug('Deserializer output ')
10096
options.debug('======================')
101-
options.debug('DESERIALIZED RESULT |:', JSON.stringify(deserializerOptions.result))
97+
options.debug(
98+
'DESERIALIZED RESULT |:',
99+
JSON.stringify(deserializerOptions.result)
100+
)
102101
next()
103102
})
104103
} else {

lib/deserializer.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@ function defaultAfterDeserialize (options, cb) {
2424
module.exports = function deserializer (options, cb) {
2525
var model = options.model
2626

27-
var beforeDeserialize = (typeof model.beforeJsonApiDeserialize === 'function')
28-
? model.beforeJsonApiDeserialize : defaultBeforeDeserialize
27+
var beforeDeserialize = typeof model.beforeJsonApiDeserialize === 'function'
28+
? model.beforeJsonApiDeserialize
29+
: defaultBeforeDeserialize
2930

30-
var deserialize = (typeof model.jsonApiDeserialize === 'function')
31-
? model.jsonApiDeserialize : defaultDeserialize
31+
var deserialize = typeof model.jsonApiDeserialize === 'function'
32+
? model.jsonApiDeserialize
33+
: defaultDeserialize
3234

33-
var afterDeserialize = (typeof model.afterJsonApiDeserialize === 'function')
34-
? model.afterJsonApiDeserialize : defaultAfterDeserialize
35+
var afterDeserialize = typeof model.afterJsonApiDeserialize === 'function'
36+
? model.afterJsonApiDeserialize
37+
: defaultAfterDeserialize
3538

3639
var deserializeOptions = _.cloneDeep(options)
3740

lib/errors.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ module.exports = function (app, options) {
55
debug = options.debug
66

77
if (options.handleErrors !== false) {
8-
debug('Register custom error handler to transform errors to meet jsonapi spec')
8+
debug(
9+
'Register custom error handler to transform errors to meet jsonapi spec'
10+
)
911
var remotes = app.remotes()
1012
remotes.options.rest = remotes.options.rest || {}
1113
remotes.options.rest.handleErrors = false
@@ -29,7 +31,9 @@ function JSONAPIErrorHandler (err, req, res, next) {
2931
res.set('Content-Type', 'application/vnd.api+json')
3032

3133
var errors = []
32-
var statusCode = err.statusCode || err.status || statusCodes.INTERNAL_SERVER_ERROR
34+
var statusCode = err.statusCode ||
35+
err.status ||
36+
statusCodes.INTERNAL_SERVER_ERROR
3337
debug('Raw error object:', err)
3438

3539
if (err.details && err.details.messages) {
@@ -73,18 +77,28 @@ function JSONAPIErrorHandler (err, req, res, next) {
7377
err.name = 'BadRequest'
7478
}
7579

76-
errors.push(buildErrorResponse(statusCode, err.message, err.code, err.name))
80+
errors.push(
81+
buildErrorResponse(statusCode, err.message, err.code, err.name)
82+
)
7783
} else {
78-
debug('Unable to determin error type. Treating error as a general 500 server error.')
84+
debug(
85+
'Unable to determin error type. Treating error as a general 500 server error.'
86+
)
7987
// catch all server 500 error if we were unable to understand the error.
80-
errors.push(buildErrorResponse(statusCodes.INTERNAL_SERVER_ERROR, 'Internal Server error', 'GENERAL_SERVER_ERROR'))
88+
errors.push(
89+
buildErrorResponse(
90+
statusCodes.INTERNAL_SERVER_ERROR,
91+
'Internal Server error',
92+
'GENERAL_SERVER_ERROR'
93+
)
94+
)
8195
}
8296

8397
// send the errors and close out the response.
8498
debug('Sending error response')
8599
debug('Response Code:', statusCode)
86-
debug('Response Object:', {errors: errors})
87-
res.status(statusCode).send({errors: errors}).end()
100+
debug('Response Object:', { errors: errors })
101+
res.status(statusCode).send({ errors: errors }).end()
88102
}
89103

90104
/**
@@ -98,10 +112,16 @@ function JSONAPIErrorHandler (err, req, res, next) {
98112
* @param {String} propertyName for validation errors, name of property validation refers to
99113
* @return {Object}
100114
*/
101-
function buildErrorResponse (httpStatusCode, errorDetail, errorCode, errorName, propertyName) {
115+
function buildErrorResponse (
116+
httpStatusCode,
117+
errorDetail,
118+
errorCode,
119+
errorName,
120+
propertyName
121+
) {
102122
return {
103123
status: httpStatusCode || statusCodes.INTERNAL_SERVER_ERROR,
104-
source: (propertyName) ? { pointer: 'data/attributes/' + propertyName } : '',
124+
source: propertyName ? { pointer: 'data/attributes/' + propertyName } : '',
105125
title: errorName || '',
106126
code: errorCode || '',
107127
detail: errorDetail || ''

lib/headers.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ module.exports = function (app, options) {
3737
strict: false,
3838
type: function (req) {
3939
// if Content-Type is any of the following, then parse otherwise don't
40-
return !!is(req, ['json', 'application/json', 'application/vnd.api+json'])
40+
return !!is(req, [
41+
'json',
42+
'application/json',
43+
'application/vnd.api+json'
44+
])
4145
}
4246
})
4347
}

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var update = require('./update')
1111
var del = require('./delete')
1212
var errors = require('./errors')
1313
var relationships = require('./relationships')
14-
var querystring = require('./querystring');
14+
var querystring = require('./querystring')
1515
var debug = require('debug')('loopback-component-jsonapi')
1616

1717
module.exports = function (app, options) {

lib/patch.js

Lines changed: 94 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ module.exports = function (app, options) {
1818
//
1919
// It would be good to have a discussion with the strongloop guys and see
2020
// if there is a better way this can be done.
21-
options.debug('Replace relationship remoting functions with custom implementations')
21+
options.debug(
22+
'Replace relationship remoting functions with custom implementations'
23+
)
2224
app.models().forEach(function (ctor) {
2325
ctor.belongsToRemoting = belongsToRemoting
2426
ctor.hasOneRemoting = hasOneRemoting
2527
ctor.hasManyRemoting = hasManyRemoting
2628
ctor.scopeRemoting = scopeRemoting
2729
})
28-
options.debug('`belongsToRemoting`, `hasOneRemoting`, `hasManyRemoting`, `scopeRemoting` replaced')
30+
options.debug(
31+
'`belongsToRemoting`, `hasOneRemoting`, `hasManyRemoting`, `scopeRemoting` replaced'
32+
)
2933

3034
// iterate through all remote methods and swap PUTs to PATCHs
3135
// as PUT is not supported by JSON API.
@@ -65,49 +69,59 @@ function convertNullToNotFoundError (toModelName, ctx, cb) {
6569

6670
function belongsToRemoting (relationName, relation, define) {
6771
var fn = this.prototype[relationName]
68-
var modelName = (relation.modelTo && relation.modelTo.modelName) || 'PersistedModel'
69-
var pathName = (relation.options.http && relation.options.http.path) || relationName
72+
var modelName = (relation.modelTo && relation.modelTo.modelName) ||
73+
'PersistedModel'
74+
var pathName = (relation.options.http && relation.options.http.path) ||
75+
relationName
7076

71-
define('__get__' + relationName, {
72-
isStatic: false,
73-
accessType: 'READ',
74-
description: 'Fetches belongsTo relation ' + relationName + '.',
75-
http: {
76-
verb: 'get',
77-
path: '/' + pathName
78-
},
79-
accepts: {
80-
arg: 'refresh',
81-
type: 'boolean',
77+
define(
78+
'__get__' + relationName,
79+
{
80+
isStatic: false,
81+
accessType: 'READ',
82+
description: 'Fetches belongsTo relation ' + relationName + '.',
8283
http: {
83-
source: 'query'
84+
verb: 'get',
85+
path: '/' + pathName
86+
},
87+
accepts: {
88+
arg: 'refresh',
89+
type: 'boolean',
90+
http: {
91+
source: 'query'
92+
}
93+
},
94+
returns: {
95+
arg: relationName,
96+
type: modelName,
97+
root: true
8498
}
8599
},
86-
returns: {
87-
arg: relationName,
88-
type: modelName,
89-
root: true
90-
}
91-
}, fn)
100+
fn
101+
)
92102

93103
var findBelongsToRelationshipsFunc = function (cb) {
94104
this['__get__' + pathName](cb)
95105
}
96106

97-
define('__findRelationships__' + relationName, {
98-
isStatic: false,
99-
accessType: 'READ',
100-
description: 'Find relations for ' + relationName + '.',
101-
http: {
102-
verb: 'get',
103-
path: '/relationships/' + pathName
107+
define(
108+
'__findRelationships__' + relationName,
109+
{
110+
isStatic: false,
111+
accessType: 'READ',
112+
description: 'Find relations for ' + relationName + '.',
113+
http: {
114+
verb: 'get',
115+
path: '/relationships/' + pathName
116+
},
117+
returns: {
118+
arg: 'result',
119+
type: modelName,
120+
root: true
121+
}
104122
},
105-
returns: {
106-
arg: 'result',
107-
type: modelName,
108-
root: true
109-
}
110-
}, findBelongsToRelationshipsFunc)
123+
findBelongsToRelationshipsFunc
124+
)
111125
}
112126

113127
/**
@@ -120,7 +134,8 @@ function belongsToRemoting (relationName, relation, define) {
120134
* @return {undefined}
121135
*/
122136
function hasOneRemoting (relationName, relation, define) {
123-
var pathName = (relation.options.http && relation.options.http.path) || relationName
137+
var pathName = (relation.options.http && relation.options.http.path) ||
138+
relationName
124139
var toModelName = relation.modelTo.modelName
125140

126141
define('__get__' + relationName, {
@@ -149,20 +164,24 @@ function hasOneRemoting (relationName, relation, define) {
149164
this['__get__' + pathName](cb)
150165
}
151166

152-
define('__findRelationships__' + relationName, {
153-
isStatic: false,
154-
accessType: 'READ',
155-
description: 'Find relations for ' + relationName + '.',
156-
http: {
157-
verb: 'get',
158-
path: '/relationships/' + pathName
167+
define(
168+
'__findRelationships__' + relationName,
169+
{
170+
isStatic: false,
171+
accessType: 'READ',
172+
description: 'Find relations for ' + relationName + '.',
173+
http: {
174+
verb: 'get',
175+
path: '/relationships/' + pathName
176+
},
177+
returns: {
178+
arg: 'result',
179+
type: toModelName,
180+
root: true
181+
}
159182
},
160-
returns: {
161-
arg: 'result',
162-
type: toModelName,
163-
root: true
164-
}
165-
}, findHasOneRelationshipsFunc)
183+
findHasOneRelationshipsFunc
184+
)
166185
}
167186

168187
/**
@@ -175,30 +194,35 @@ function hasOneRemoting (relationName, relation, define) {
175194
* @return {undefined}
176195
*/
177196
function hasManyRemoting (relationName, relation, define) {
178-
var pathName = (relation.options.http && relation.options.http.path) || relationName
197+
var pathName = (relation.options.http && relation.options.http.path) ||
198+
relationName
179199
var toModelName = relation.modelTo.modelName
180200

181201
var findHasManyRelationshipsFunc = function (cb) {
182202
this['__get__' + pathName](cb)
183203
}
184204

185-
define('__findRelationships__' + relationName, {
186-
isStatic: false,
187-
accessType: 'READ',
188-
description: 'Find relations for ' + relationName + '.',
189-
http: {
190-
verb: 'get',
191-
path: '/relationships/' + pathName
192-
},
193-
returns: {
194-
arg: 'result',
195-
type: toModelName,
196-
root: true
205+
define(
206+
'__findRelationships__' + relationName,
207+
{
208+
isStatic: false,
209+
accessType: 'READ',
210+
description: 'Find relations for ' + relationName + '.',
211+
http: {
212+
verb: 'get',
213+
path: '/relationships/' + pathName
214+
},
215+
returns: {
216+
arg: 'result',
217+
type: toModelName,
218+
root: true
219+
},
220+
rest: {
221+
after: convertNullToNotFoundError.bind(null, toModelName)
222+
}
197223
},
198-
rest: {
199-
after: convertNullToNotFoundError.bind(null, toModelName)
200-
}
201-
}, findHasManyRelationshipsFunc)
224+
findHasManyRelationshipsFunc
225+
)
202226

203227
// var createRelationshipFunc = function (cb) {
204228
// TODO: implement this
@@ -302,7 +326,10 @@ function hasManyRemoting (relationName, relation, define) {
302326
* @return {undefined}
303327
*/
304328
function scopeRemoting (scopeName, scope, define) {
305-
var pathName = (scope.options && scope.options.http && scope.options.http.path) || scopeName
329+
var pathName = (scope.options &&
330+
scope.options.http &&
331+
scope.options.http.path) ||
332+
scopeName
306333
var isStatic = scope.isStatic
307334
var toModelName = scope.modelTo.modelName
308335

0 commit comments

Comments
 (0)