-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add type-definition #6
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules/ | ||
package-lock.json | ||
coverage/ | ||
.nyc_output/ | ||
.nyc_output/ | ||
types/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,15 +6,26 @@ const path = require('path'); | |
const ApiGetError = require('./api-get-error'); | ||
const EndpointParser = require('./helpers/endpoint-parser'); | ||
|
||
/** | ||
* @typedef {Object} ApiGetError A instance of APIGetError class | ||
*/ | ||
|
||
class ApiGet extends API { | ||
|
||
/** | ||
* Validate the model, extracted from the parsed endpoint | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 Creo que falta el
Que usaste en el Y de pasó en todos los metodos que no hay un return también se podría agregar. De esa manera quedan todos parejos. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Como descripción le pondría:
|
||
*/ | ||
async validate() { | ||
|
||
this._parseEndpoint(); | ||
|
||
this._validateModel(); | ||
gastonpereyra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* It makes the query to the DB with the filters and params obtained from the endpoint | ||
gastonpereyra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @returns {void} | ||
*/ | ||
async process() { | ||
gastonpereyra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const filters = { | ||
|
@@ -51,6 +62,9 @@ class ApiGet extends API { | |
this.setBody(response); | ||
} | ||
|
||
/** | ||
* Set the modelName, recordId and parents of API after parsing the endpoint | ||
*/ | ||
_parseEndpoint() { | ||
|
||
const { modelName, recordId, parents } = EndpointParser.parse(this.endpoint); | ||
gastonpereyra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -60,6 +74,10 @@ class ApiGet extends API { | |
this.parents = parents; | ||
} | ||
|
||
/** | ||
* Set the model of the API getting the model instance from its name | ||
* @throws {ApiGetError} if the model not exists | ||
*/ | ||
_validateModel() { | ||
try { | ||
this.model = this._getModelInstance(path.join(process.cwd(), process.env.MS_PATH || '', 'models', this.modelName)); | ||
|
@@ -68,6 +86,11 @@ class ApiGet extends API { | |
} | ||
} | ||
|
||
/** | ||
* Get the instance of the Model indicated by parameter | ||
* @param {String} modelPath The model path | ||
* @returns {Object} An instance injected with the session | ||
*/ | ||
_getModelInstance(modelPath) { | ||
|
||
// eslint-disable-next-line global-require, import/no-dynamic-require | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,18 @@ | |
const ApiGetError = require('../api-get-error'); | ||
const camelize = require('../utils/camelize'); | ||
|
||
/** | ||
* @typedef {Object} ParseEndpoint | ||
* @property {String} [modelName] The model name | ||
* @property {String} [recordId] The ID of the record | ||
* @property {String} [parents] The rest of the text string that does not have a specific function | ||
*/ | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Esto deberia estar en el metodo no en la definición de la clase |
||
* Parse the endpoint passed by parameter in different parts for use | ||
* @param {String} endpoint The endpoint to parse | ||
* @returns {ParseEndpoint} The parsed endpoint | ||
*/ | ||
class EndpointParser { | ||
|
||
static parse(endpoint) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Transform the string entered in camelCase format | ||
* @param {String} string | ||
* @returns {String} | ||
*/ | ||
const camelize = string => string.replace(/-+([^-])/g, (_, firstLetter) => firstLetter.toUpperCase()); | ||
|
||
module.exports = camelize; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Este package quedó viejito.. aca podriamos exportarlo de una