Skip to content
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

Merged
merged 3 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm i
- name: Build types
run: npm run build-types
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
3 changes: 2 additions & 1 deletion .gitignore
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/
15 changes: 15 additions & 0 deletions lib/api-get-error.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
'use strict';

/**
* @typedef CodesError
* @property {Number} INVALID_REQUEST_DATA
* @property {Number} INVALID_ENTITY
* @property {Number} INTERNAL_ERROR
*/

class ApiGetError extends Error {

/**
* Get the error codes
* @returns {CodesError}
*/
static get codes() {

return {
Expand All @@ -12,6 +23,10 @@ class ApiGetError extends Error {

}

/**
* @param {Error} err The details of the error
* @param {Number} code The error code
*/
constructor(err, code) {

const message = err.message || err;
Expand Down
23 changes: 23 additions & 0 deletions lib/api-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

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

module.exports = class ApiGet extends API {


/**
* Validate the model, extracted from the parsed endpoint

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Creo que falta el

* @returns {void}

Que usaste en el process .

Y de pasó en todos los metodos que no hay un return también se podría agregar. De esa manera quedan todos parejos.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Como descripción le pondría:

"Realiza validaciones antes de procesar. Settea el Modelo a usar y parsea el Endpoint. Importante, no es aconsejable sobreescribirlo"

*/
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 = {
Expand Down Expand Up @@ -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
Expand All @@ -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));
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lib/helpers/endpoint-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

/**

Choose a reason for hiding this comment

The 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) {
Expand Down
5 changes: 5 additions & 0 deletions lib/utils/camelize.js
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;
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
"mocha": "^8.1.2",
"mock-require": "^3.0.3",
"nyc": "^15.1.0",
"sinon": "^9.0.3"
"sinon": "^9.0.3",
"typescript": "^4.5.2"
},
"files": [
"lib/"
"lib/",
"types/"
],
"types": "types/index.d.ts",
"directories": {
"test": "tests"
},
gastonpereyra marked this conversation as resolved.
Show resolved Hide resolved
Expand Down