-
-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge major with latest changes (#80)
- Loading branch information
Showing
6 changed files
with
90 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,6 @@ node_modules | |
|
||
# Users Environment Variables | ||
.lock-wscript | ||
|
||
# IDEs | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,41 @@ | ||
const paramCounts = { | ||
find: 1, | ||
get: 2, | ||
create: 2, | ||
update: 3, | ||
patch: 3, | ||
remove: 2 | ||
}; | ||
|
||
function isObjectOrArray (value) { | ||
return typeof value === 'object' && value !== null; | ||
} | ||
|
||
exports.validateArguments = function validateArguments (method, args) { | ||
exports.validateArguments = function validateArguments (argsOrders, method, args) { | ||
// Check if the last argument is a callback which are no longer supported | ||
if (typeof args[args.length - 1] === 'function') { | ||
throw new Error('Callbacks are no longer supported. Use Promises or async/await instead.'); | ||
} | ||
|
||
const methodParamCount = paramCounts[method]; | ||
const methodArgs = argsOrders[method] || ['params']; | ||
const methodParamCount = methodArgs.length; | ||
|
||
// Check the number of arguments and throw an error if too many are provided | ||
if (methodParamCount && args.length > methodParamCount) { | ||
throw new Error(`Too many arguments for '${method}' method`); | ||
} | ||
|
||
// `params` is always the last argument | ||
const params = args[methodParamCount - 1]; | ||
|
||
// Check if `params` is an object (can be undefined though) | ||
if (params !== undefined && !isObjectOrArray(params)) { | ||
throw new Error(`Params for '${method}' method must be an object`); | ||
} | ||
|
||
// Validate other arguments for each method | ||
switch (method) { | ||
case 'create': | ||
if (!isObjectOrArray(args[0])) { | ||
throw new Error(`A data object must be provided to the 'create' method`); | ||
} | ||
break; | ||
case 'get': | ||
case 'remove': | ||
case 'update': | ||
case 'patch': | ||
if (args[0] === undefined) { | ||
throw new Error(`An id must be provided to the '${method}' method`); | ||
} | ||
|
||
if ((method === 'update' || method === 'patch') && !isObjectOrArray(args[1])) { | ||
throw new Error(`A data object must be provided to the '${method}' method`); | ||
} | ||
} | ||
|
||
return true; | ||
return methodArgs.every((argName, index) => { | ||
switch (argName) { | ||
case 'id': | ||
if (args[index] === undefined) { | ||
throw new Error(`An id must be provided to the '${method}' method`); | ||
} | ||
break; | ||
case 'data': | ||
if (!isObjectOrArray(args[index])) { | ||
throw new Error(`A data object must be provided to the '${method}' method`); | ||
} | ||
break; | ||
case 'params': | ||
if (args[index] !== undefined && !isObjectOrArray(args[index])) { | ||
throw new Error(`Params for '${method}' method must be an object`); | ||
} | ||
break; | ||
} | ||
|
||
return true; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.